Metrics Collection Best Practices for “github.com/docker/go-metrics” project:
Design Philosophy:
The “github.com/docker/go-metrics” project follows the design philosophy of providing simple and easy-to-use metrics collection for Go applications. It is built on top of the Go Standard Library and Protocol Buffers.
Programming Languages:
The project is written in Go, and it is compatible with other programming languages through Protocol Buffers.
Prometheus Go Client:
The project integrates with the Prometheus Go client library, which provides features such as exposing different metrics types (gauges, histograms), non-global registries, pushing metrics to Prometheus PushGateways, and bridging Prometheus and Graphite.
Metrics Types:
The project supports different metrics types, including:
- Gauges: A gauge is a single numerical value that can arbitrarily go up and down.
- Histograms: A histogram is a distribution of values, which can be used to see the distribution of request latencies, response sizes, etc.
Namespaces and Labels:
Metrics should be organized in a hierarchical namespace, and labels should be used to provide additional context to the metrics. For example, a metric for measuring the response time of an HTTP handler can be organized under the namespace “http_handler” and labeled with the HTTP method and the path.
Labeled Timer Metric:
The project provides a labeled timer metric for measuring time in Go applications. The labeled timer metric can be used to measure the response time of HTTP handlers, database queries, or any other operation that takes a measurable amount of time.
Labeled Counter Metric:
The project provides a labeled counter metric for counting events in Go applications. The labeled counter metric can be used to count the number of successful or failed requests, the number of database queries, or any other event that can be counted.
Instrumenting HTTP Handlers:
The project provides a function for instrumenting HTTP handlers with metrics. The function can be used to measure the response time and the number of requests for each HTTP handler.
Telemetry Metrics:
The project supports the collection of telemetry metrics, such as the number of active goroutines, the number of open TCP connections, and the number of bytes sent and received over the network.
Aggregation:
Prometheus provides aggregation functions that can be used to aggregate metrics from multiple sources. For example, the sum function can be used to calculate the total number of requests, and the average function can be used to calculate the average response time.
Best Practices:
- Use descriptive and meaningful names for metrics.
- Use labels to provide additional context to the metrics.
- Avoid using labels with high cardinality, as it can increase the storage and querying costs.
- Use the appropriate metric type for each use case.
- Use Prometheus aggregation functions to aggregate metrics from multiple sources.
- Use Prometheus alerting rules to alert on anomalies in the metrics.
Sources:
- Instrumenting a Go application | Prometheus: https://prometheus.io/docs/guides/go-application
- Writing exporters | Prometheus: https://prometheus.io/docs/instrumenting/writing_exporters
- Agents - Enable Telemetry Metrics | Consul | HashiCorp Developer: https://developer.hashicorp.com/consul/docs/agent/telemetry
- Instrumentation | Prometheus: https://prometheus.io/docs/practices/instrumentation
- Manual instrumentation of Go applications with OpenTelemetry Metrics SDK | Opentelemetry documentation: https://grafana.com/docs/opentelemetry/instrumentation/go/manual-instrumentation
- Understanding metric types | Prometheus: https://prometheus.io/docs/tutorials/understanding_metric_types
- Registry | OpenTelemetry: https://opentelemetry.io/ecosystem/registry
- Interview with JustWatch | Prometheus: https://prometheus.io/blog/2016/10/12/interview-with-justwatch
- What metrics matter: A guide for open source projects | Opensource.com: https://opensource.com/article/19/1/metrics-guide-open-source-projects