From 1cbc7f323f1612c8cd924eec232c443ae46bf128 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Wed, 19 Apr 2023 15:15:31 +0300 Subject: [PATCH] [#80] metrics: Use map for constant labels Signed-off-by: Denis Kirillov --- metrics/desc.go | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/metrics/desc.go b/metrics/desc.go index cc88730b..70eda008 100644 --- a/metrics/desc.go +++ b/metrics/desc.go @@ -140,22 +140,17 @@ type Description struct { Subsystem string Name string Help string - ConstantLabels []KeyValue + ConstantLabels prometheus.Labels VariableLabels []string } -type KeyValue struct { - Key string `json:"key"` - Value string `json:"value"` -} - func (d *Description) MarshalJSON() ([]byte, error) { return json.Marshal(&struct { - Type string `json:"type"` - FQName string `json:"name"` - Help string `json:"help"` - ConstantLabels []KeyValue `json:"constant_labels"` - VariableLabels []string `json:"variable_labels"` + Type string `json:"type"` + FQName string `json:"name"` + Help string `json:"help"` + ConstantLabels prometheus.Labels `json:"constant_labels,omitempty"` + VariableLabels []string `json:"variable_labels,omitempty"` }{ Type: d.Type.String(), FQName: d.BuildFQName(), @@ -169,14 +164,6 @@ func (d *Description) BuildFQName() string { return prometheus.BuildFQName(d.Namespace, d.Subsystem, d.Name) } -func (d *Description) ConstLabelsMap() map[string]string { - constsLabels := make(map[string]string, len(d.ConstantLabels)) - for _, kv := range d.ConstantLabels { - constsLabels[kv.Key] = kv.Value - } - return constsLabels -} - // DescribeAll returns descriptions for metrics. func DescribeAll() []Description { var list []Description @@ -195,7 +182,7 @@ func newOpts(description Description) prometheus.Opts { Subsystem: description.Subsystem, Name: description.Name, Help: description.Help, - ConstLabels: description.ConstLabelsMap(), + ConstLabels: description.ConstantLabels, } } @@ -204,7 +191,7 @@ func newDesc(description Description) *prometheus.Desc { description.BuildFQName(), description.Help, description.VariableLabels, - description.ConstLabelsMap()) + description.ConstantLabels) } func mustNewGauge(description Description) prometheus.Gauge { @@ -237,7 +224,7 @@ func mustNewHistogramVec(description Description, buckets []float64) *prometheus Subsystem: description.Subsystem, Name: description.Name, Help: description.Name, - ConstLabels: description.ConstLabelsMap(), + ConstLabels: description.ConstantLabels, Buckets: buckets, }, description.VariableLabels,