Skip to content

Use runtime/metrics package for metrics collection in Go 1.16+ #842

Closed
@mknyszek

Description

@mknyszek

Following up from https://groups.google.com/g/prometheus-developers/c/FyRu5C4Yqzo.

I would like to modify the Go Prometheus client to use the new runtime/metrics package introduced in Go 1.16. The new package:

  • Supersedes existing APIs like runtime.ReadMemStats and runtime.NumGoroutine, merging them into one API.
  • Provides human-readable descriptions of each metric.
  • Namespaces each metric name and includes a unit.
  • Does not suffer from the same latency and whole-program performance issues that runtime.ReadMemStats does.

Specifically, I would like to provide an alternative implementation of the goCollector structure found in prometheus/collector.go that's used for Go 1.16+, chosen via build tag. This new structure would export all metrics it finds in runtime/metrics.All and transform the names it finds via:

// translateMetricName translates a metric name from one provided by the runtime/metrics
// package to one usable by Prometheus.
func translateMetricName(name string) string {
    // Replace / and : with _.
    return strings.ReplaceAll(strings.ReplaceAll(name, "/", "_"), ":", "_")
}

It will also re-export metrics that are currently provided by the existing goCollector under the same names, since they all appear in the runtime/metrics package.

The set of available metrics will grow over time, and more metrics will magically appear in everyone's dashboards. Automatic improvements in telemetry! One concern here is reading more metrics increases the cost of reading metrics but I think that's OK because:

  1. Today the sampling latency is O(microseconds), even in the 99th percentile. It's unlikely to increase by an order of magnitude overnight, bugs notwithstanding.
  2. They're going to be added relatively slowly, so if it becomes a problem, it wouldn't be hard to just sample some of them at a lower rate. Part of the point of the package is you can choose what you want to sample.
  3. Particularly performance sensitive metrics will be marked as such in their Descriptions, so identifying them at runtime will be straightforward.

I'm happy to do the implementation if someone is willing to review!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions