diff --git a/prometheus.go b/prometheus.go index 64fc469..a1a3841 100644 --- a/prometheus.go +++ b/prometheus.go @@ -87,9 +87,8 @@ func (m *prometheusMeter) buildMetric(name string, labels ...string) string { return meter.BuildName(name, nlabels...) } - for idx := 0; idx < nl; idx++ { + for idx := 0; idx < nl; idx += 2 { nlabels[idx] = m.opts.LabelPrefix + nlabels[idx] - idx++ } return meter.BuildName(name, nlabels...) } @@ -109,10 +108,9 @@ func (m *prometheusMeter) buildLabels(labels ...string) []string { nlabels := make([]string, 0, nl) - for idx := 0; idx < nl; idx++ { + for idx := 0; idx < nl; idx += 2 { nlabels = append(nlabels, m.opts.LabelPrefix+labels[idx]) nlabels = append(nlabels, labels[idx+1]) - idx++ } return nlabels } @@ -304,15 +302,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { Metric: make([]*dto.Metric, 0, len(metrics.cs)), } for _, c := range metrics.cs { - m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} - c.c.Write(m) - for idx := 0; idx < len(c.labels); idx++ { - m.Label = append(m.Label, &dto.LabelPair{ - Name: &c.labels[idx], - Value: &c.labels[idx+1], - }) - idx++ - } + m := newMetric(c.labels) + _ = c.c.Write(m) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -325,15 +316,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { Metric: make([]*dto.Metric, 0, len(metrics.cs)), } for _, c := range metrics.cs { - m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} - c.c.Write(m) - for idx := 0; idx < len(c.labels); idx++ { - m.Label = append(m.Label, &dto.LabelPair{ - Name: &c.labels[idx], - Value: &c.labels[idx+1], - }) - idx++ - } + m := newMetric(c.labels) + _ = c.c.Write(m) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -346,15 +330,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { Metric: make([]*dto.Metric, 0, len(metrics.cs)), } for _, c := range metrics.cs { - m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} - c.c.Write(m) - for idx := 0; idx < len(c.labels); idx++ { - m.Label = append(m.Label, &dto.LabelPair{ - Name: &c.labels[idx], - Value: &c.labels[idx+1], - }) - idx++ - } + m := newMetric(c.labels) + _ = c.c.Write(m) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -367,15 +344,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { Metric: make([]*dto.Metric, 0, len(metrics.cs)), } for _, c := range metrics.cs { - m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} - c.c.Write(m) - for idx := 0; idx < len(c.labels); idx++ { - m.Label = append(m.Label, &dto.LabelPair{ - Name: &c.labels[idx], - Value: &c.labels[idx+1], - }) - idx++ - } + m := newMetric(c.labels) + _ = c.c.Write(m) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -388,15 +358,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error { Metric: make([]*dto.Metric, 0, len(metrics.cs)), } for _, c := range metrics.cs { - m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} - c.c.Write(m) - for idx := 0; idx < len(c.labels); idx++ { - m.Label = append(m.Label, &dto.LabelPair{ - Name: &c.labels[idx], - Value: &c.labels[idx+1], - }) - idx++ - } + m := newMetric(c.labels) + _ = c.c.Write(m) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -550,3 +513,14 @@ func newHash(labels []string) uint64 { } return h.Sum64() } + +func newMetric(labels []string) *dto.Metric { + m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(labels)/2)} + for idx := 0; idx < len(labels); idx += 2 { + m.Label = append(m.Label, &dto.LabelPair{ + Name: &labels[idx], + Value: &labels[idx+1], + }) + } + return m +}