[#29] metrics: Use map for constant labels

Signed-off-by: Denis Kirillov <d.kirillov@yadro.com>
This commit is contained in:
Denis Kirillov 2023-04-19 15:18:15 +03:00
parent 385f336a17
commit 15b65b521b

View file

@ -74,22 +74,17 @@ type Description struct {
Subsystem string Subsystem string
Name string Name string
Help string Help string
ConstantLabels []KeyValue ConstantLabels prometheus.Labels
VariableLabels []string VariableLabels []string
} }
type KeyValue struct {
Key string `json:"key"`
Value string `json:"value"`
}
func (d *Description) MarshalJSON() ([]byte, error) { func (d *Description) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct { return json.Marshal(&struct {
Type string `json:"type"` Type string `json:"type"`
FQName string `json:"name"` FQName string `json:"name"`
Help string `json:"help"` Help string `json:"help"`
ConstantLabels []KeyValue `json:"constant_labels"` ConstantLabels prometheus.Labels `json:"constant_labels,omitempty"`
VariableLabels []string `json:"variable_labels"` VariableLabels []string `json:"variable_labels,omitempty"`
}{ }{
Type: d.Type.String(), Type: d.Type.String(),
FQName: d.BuildFQName(), FQName: d.BuildFQName(),
@ -103,14 +98,6 @@ func (d *Description) BuildFQName() string {
return prometheus.BuildFQName(d.Namespace, d.Subsystem, d.Name) 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. // DescribeAll returns descriptions for metrics.
func DescribeAll() []Description { func DescribeAll() []Description {
var list []Description var list []Description
@ -129,7 +116,7 @@ func newOpts(description Description) prometheus.Opts {
Subsystem: description.Subsystem, Subsystem: description.Subsystem,
Name: description.Name, Name: description.Name,
Help: description.Help, Help: description.Help,
ConstLabels: description.ConstLabelsMap(), ConstLabels: description.ConstantLabels,
} }
} }