import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_model/go" ) // Create a new MetricFamily object with the name "example_metric" metricFamily := &io_prometheus_client.MetricFamily{ Name: "example_metric", Help: "A helpful description of this metric", Type: io_prometheus_client.MetricType_GAUGE, } // Create a new Gauge metric within the MetricFamily gaugeMetric := &io_prometheus_client.Metric{ Gauge: &io_prometheus_client.Gauge{}, } // Add the Gauge metric to the MetricFamily metricFamily.Metric = append(metricFamily.Metric, gaugeMetric) // Register the MetricFamily with the Prometheus client prometheus.Register(metricFamily)
import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_model/go" ) // Get the MetricFamily object for an existing metric metricFamily, err := prometheus.DefaultGatherer.Gatherer.(*prometheus.Registry).GetMetricFamily("example_metric") if err != nil { panic(err) } // Iterate over the metrics within the MetricFamily for _, metric := range metricFamily.Metric { // Do something with the metric fmt.Printf("%v\n", metric) }This example demonstrates how to retrieve an existing MetricFamily object for a metric named "example_metric" using the Prometheus client registry. It then iterates over the metrics within the MetricFamily and performs some action with each metric.