Additional Metrics in github.com/docker/go-metrics
:
The github.com/docker/go-metrics
library provides several options for additional metrics beyond the basic ones. Here are some of the possible options with examples:
- Histogram Metric: Histogram is a more complex metric type that can be used for any calculated value which is counted based on bucket values. A common example would be the time it takes to reply to a request, called latency. Here’s an example of creating a histogram metric:
h := metrics.NewHistogram(
metrics.NewOptions(
metrics.WithName("request_latency_seconds"),
metrics.WithStdevBucketBoundaries([]float64{.005, .01, .025, .05, .1, .25, .5, .75, 1, 2.5, 5, 10}),
),
)
h.Observe(time.Since(start).Seconds())
PromQL functions like max_over_time
, min_over_time
and avg_over_time
can be used on gauge metrics.
- Disabled Metrics: The library also provides a way to track the count of disabled metrics. Here’s an example of creating a disabled metric:
c := metrics.NewDisabledCounter(
metrics.NewOptions(
metrics.WithName("disabled_metric_total"),
),
)
c.Inc(1)
- Field Metrics: Field metrics can be used to track the response latency distribution in seconds for each field validation value and whether field validation is enabled or not. Here’s an example of creating a field metric:
f := metrics.NewSummary(
metrics.NewOptions(
metrics.WithName("field_validation_request_duration_seconds"),
),
)
f.Observe(time.Since(start).Seconds())
- Go Metrics: The library also provides several pre-defined metrics for the Go runtime, such as the count of calls made from Go to C by the current process, and the estimated total CPU time goroutines spent performing GC tasks to assist the GC and prevent it from falling behind the application. Here’s an example of creating a Go metric:
g := metrics.NewGauge(
metrics.NewOptions(
metrics.WithName("go_cgo_go_to_c_calls_calls_total"),
),
)
g.Set(123)
For more information, you can refer to the following links:
- HTTP Requests Latency: https://kyverno.io/docs/monitoring/http-requests-latency
- First stable release of OTel Go metric SDK: https://opentelemetry.io/blog/2023/otel-go-metrics-sdk-stable
- Antrea Prometheus Integration: https://antrea.io/docs/v1.14.0/docs/prometheus-integration
- Understanding metric types: https://prometheus.io/docs/tutorials/understanding_metric_types
- Registry: https://opentelemetry.io/ecosystem/registry
- Interview with JustWatch: https://prometheus.io/blog/2016/10/12/interview-with-justwatch
- Manual instrumentation of Go applications with OpenTelemetry Metrics SDK: https://grafana.com/docs/opentelemetry/instrumentation/go/manual-instrumentation
- Instrumenting a Go application: https://prometheus.io/docs/guides/go-application