Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend prometheus.Registry to implement Collector #1103

Merged
merged 3 commits into from Aug 23, 2022

Commits on Aug 3, 2022

  1. prometheus: implement Collector interface for Registry

    This change allows Registries to be used as Collectors.
    
    This enables new instances of Registry to be passed to ephemeral
    subroutines for collecting metrics from subroutines which are still
    running:
    
    ```go
    package main
    
    import (
      "fmt"
    
      "github.com/prometheus/client_golang/prometheus"
    )
    
    func main() {
      globalReg := prometheus.NewRegistry()
    
      for i := 0; i < 100; i++ {
        workerReg := prometheus.WrapRegistererWith(prometheus.Labels{
          // Add an ID label so registered metrics from workers don't
          // collide.
          "worker_id": fmt.Sprintf("%d", i),
        }, prometheus.NewRegistry()
    
        globalReg.MustRegister(workerReg)
    
        go func(i int) {
          runWorker(workerReg)
    
          // Unregister any metrics the worker may have created.
          globalReg.Unregister(workerReg)
        }(i)
      }
    }
    
    // runWorker runs a worker, registering worker-specific metrics.
    func runWorker(reg *prometheus.Registry) {
      // ... register metrics ...
      // ... do work ...
    }
    ```
    
    This change makes it easier to avoid leaking metrics from subroutines
    which do not consistently properly unregister metrics.
    
    Signed-off-by: Robert Fratto <robertfratto@gmail.com>
    rfratto committed Aug 3, 2022
    Copy the full SHA
    97a08dd View commit details
    Browse the repository at this point in the history
  2. fix grammar in doc comment

    Signed-off-by: Robert Fratto <robertfratto@gmail.com>
    rfratto committed Aug 3, 2022
    Copy the full SHA
    688d41e View commit details
    Browse the repository at this point in the history

Commits on Aug 8, 2022

  1. document why Registry implements Collector with example

    Signed-off-by: Robert Fratto <robertfratto@gmail.com>
    rfratto committed Aug 8, 2022
    Copy the full SHA
    74ea536 View commit details
    Browse the repository at this point in the history