From 85319f9bd0782e2bad05acb12b134d728f6d3116 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 13 Mar 2022 11:18:38 +0300 Subject: [PATCH 1/2] use newMetric helper in all places Signed-off-by: Vasiliy Tolstov --- prometheus.go | 72 ++++++++++++++++----------------------------------- 1 file changed, 23 insertions(+), 49 deletions(-) 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 +} -- 2.45.2 From eac6b15775ecba487455782e7f01437aac6b32d2 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Sun, 13 Mar 2022 11:24:11 +0300 Subject: [PATCH 2/2] fixup Signed-off-by: Vasiliy Tolstov --- prometheus.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/prometheus.go b/prometheus.go index a1a3841..aca3d7e 100644 --- a/prometheus.go +++ b/prometheus.go @@ -302,8 +302,9 @@ 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 := newMetric(c.labels) + m := &dto.Metric{} _ = c.c.Write(m) + fillMetric(m, c.labels) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -316,8 +317,9 @@ 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 := newMetric(c.labels) + m := &dto.Metric{} _ = c.c.Write(m) + fillMetric(m, c.labels) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -330,8 +332,9 @@ 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 := newMetric(c.labels) + m := &dto.Metric{} _ = c.c.Write(m) + fillMetric(m, c.labels) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -344,8 +347,9 @@ 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 := newMetric(c.labels) + m := &dto.Metric{} _ = c.c.Write(m) + fillMetric(m, c.labels) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -358,8 +362,9 @@ 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 := newMetric(c.labels) + m := &dto.Metric{} _ = c.c.Write(m) + fillMetric(m, c.labels) mf.Metric = append(mf.Metric, m) } mfs = append(mfs, mf) @@ -514,12 +519,12 @@ 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)} +func fillMetric(m *dto.Metric, labels []string) *dto.Metric { + m.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], + Name: newString(labels[idx]), + Value: newString(labels[idx+1]), }) } return m -- 2.45.2