|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/cabify/gotoprom" |
| 8 | + "github.com/cabify/gotoprom/prometheusvanilla" |
| 9 | + "github.com/prometheus/client_golang/prometheus" |
| 10 | +) |
| 11 | + |
| 12 | +var Metrics struct { |
| 13 | + HttpDuration func(HttpLabels) TimeHistogram `name:"feast_http_request_duration_seconds" help:"Time taken to serve HTTP requests" buckets:".005,.01,.025,.05,.1,.25,.5,1,2.5,5,10"` |
| 14 | + |
| 15 | + GrpcDuration func(GrpcLabels) TimeHistogram `name:"feast_grpc_request_duration_seconds" help:"Time taken to serve gRPC requests" buckets:".005,.01,.025,.05,.1,.25,.5,1,2.5,5,10"` |
| 16 | + |
| 17 | + HttpRequestsTotal func(HttpLabels) prometheus.Counter `name:"feast_http_requests_total" help:"Total number of HTTP requests"` |
| 18 | + |
| 19 | + GrpcRequestsTotal func(GrpcLabels) prometheus.Counter `name:"feast_grpc_requests_total" help:"Total number of gRPC requests"` |
| 20 | +} |
| 21 | + |
| 22 | +type HttpLabels struct { |
| 23 | + Method string `label:"method"` |
| 24 | + Status int `label:"status"` |
| 25 | + Path string `label:"path"` |
| 26 | +} |
| 27 | + |
| 28 | +type GrpcLabels struct { |
| 29 | + Service string `label:"service"` |
| 30 | + Method string `label:"method"` |
| 31 | + Code string `label:"code"` |
| 32 | +} |
| 33 | + |
| 34 | +func InitMetrics() { |
| 35 | + gotoprom.MustInit(&Metrics, "feast") |
| 36 | +} |
| 37 | + |
| 38 | +// TimeHistogram boilerplate from gotoprom README |
| 39 | + |
| 40 | +var ( |
| 41 | + // TimeHistogramType is the reflect.Type of the TimeHistogram interface |
| 42 | + TimeHistogramType = reflect.TypeOf((*TimeHistogram)(nil)).Elem() |
| 43 | +) |
| 44 | + |
| 45 | +func init() { |
| 46 | + gotoprom.MustAddBuilder(TimeHistogramType, RegisterTimeHistogram) |
| 47 | +} |
| 48 | + |
| 49 | +// RegisterTimeHistogram registers a TimeHistogram after registering the underlying prometheus.Histogram in the prometheus.Registerer provided |
| 50 | +// The function it returns returns a TimeHistogram type as an interface{} |
| 51 | +func RegisterTimeHistogram(name, help, namespace string, labelNames []string, tag reflect.StructTag) (func(prometheus.Labels) interface{}, prometheus.Collector, error) { |
| 52 | + f, collector, err := prometheusvanilla.BuildHistogram(name, help, namespace, labelNames, tag) |
| 53 | + if err != nil { |
| 54 | + return nil, nil, err |
| 55 | + } |
| 56 | + |
| 57 | + return func(labels prometheus.Labels) interface{} { |
| 58 | + return timeHistogramAdapter{Histogram: f(labels).(prometheus.Histogram)} |
| 59 | + }, collector, nil |
| 60 | +} |
| 61 | + |
| 62 | +// TimeHistogram offers the basic prometheus.Histogram functionality |
| 63 | +// with additional time-observing functions |
| 64 | +type TimeHistogram interface { |
| 65 | + prometheus.Histogram |
| 66 | + // Duration observes the duration in seconds |
| 67 | + Duration(duration time.Duration) |
| 68 | + // Since observes the duration in seconds since the time point provided |
| 69 | + Since(time.Time) |
| 70 | +} |
| 71 | + |
| 72 | +type timeHistogramAdapter struct { |
| 73 | + prometheus.Histogram |
| 74 | +} |
| 75 | + |
| 76 | +// Duration observes the duration in seconds |
| 77 | +func (to timeHistogramAdapter) Duration(duration time.Duration) { |
| 78 | + to.Observe(duration.Seconds()) |
| 79 | +} |
| 80 | + |
| 81 | +// Since observes the duration in seconds since the time point provided |
| 82 | +func (to timeHistogramAdapter) Since(duration time.Time) { |
| 83 | + to.Duration(time.Since(duration)) |
| 84 | +} |
0 commit comments