github.com/sequix/cortex@v1.1.6/pkg/chunk/cassandra/instrumentation.go (about) 1 package cassandra 2 3 import ( 4 "context" 5 "strings" 6 7 "github.com/gocql/gocql" 8 "github.com/prometheus/client_golang/prometheus" 9 ) 10 11 var requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ 12 Namespace: "cortex", 13 Name: "cassandra_request_duration_seconds", 14 Help: "Time spent doing Cassandra requests.", 15 Buckets: prometheus.ExponentialBuckets(0.001, 4, 6), 16 }, []string{"operation", "status_code"}) 17 18 func init() { 19 prometheus.MustRegister(requestDuration) 20 } 21 22 type observer struct{} 23 24 func err(err error) string { 25 if err != nil { 26 return "500" 27 } 28 return "200" 29 } 30 31 func (observer) ObserveBatch(ctx context.Context, b gocql.ObservedBatch) { 32 requestDuration.WithLabelValues("BATCH", err(b.Err)).Observe(b.End.Sub(b.Start).Seconds()) 33 } 34 35 func (observer) ObserveQuery(cts context.Context, q gocql.ObservedQuery) { 36 parts := strings.SplitN(q.Statement, " ", 2) 37 requestDuration.WithLabelValues(parts[0], err(q.Err)).Observe(q.End.Sub(q.Start).Seconds()) 38 }