zsais / go-gin-prometheus

Gin Web Framework Prometheus metrics exporter
MIT License
450 stars 133 forks source link

change slice metrics to map metrics #52

Closed snowlyg closed 2 years ago

snowlyg commented 2 years ago

I change MetricsList []*Metric to MetricsList map[string]*Metric, when i use the custom metrics. Make a easy way to get custom metric.

customMetrics := []*ginprometheus.Metric{
    &ginprometheus.Metric{
        ID: "1234",             // optional string
        Name:   "test_metric",          // required string
        Description:    "Counter test metric",  // required string
        Type:   "counter",          // required string
    },
    &ginprometheus.Metric{
        ID: "1235",             // Identifier
        Name:   "test_metric_2",        // Metric Name
        Description:    "Summary test metric",  // Help Description
        Type:   "summary", // type associated with prometheus collector
    },
    // Type Options:
    //  counter, counter_vec, gauge, gauge_vec,
    //  histogram, histogram_vec, summary, summary_vec
}
p := ginprometheus.NewPrometheus("gin", customMetrics)
// add data to custom metrics
m := p.MetricsList["1234"].MetricCollector.(prometheus.Counter)
m.Inc()

25