use newMetric helper in all places

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2022-03-13 11:18:38 +03:00
parent e4f2c77510
commit 85319f9bd0

View File

@ -87,9 +87,8 @@ func (m *prometheusMeter) buildMetric(name string, labels ...string) string {
return meter.BuildName(name, nlabels...) 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] nlabels[idx] = m.opts.LabelPrefix + nlabels[idx]
idx++
} }
return meter.BuildName(name, nlabels...) return meter.BuildName(name, nlabels...)
} }
@ -109,10 +108,9 @@ func (m *prometheusMeter) buildLabels(labels ...string) []string {
nlabels := make([]string, 0, nl) 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, m.opts.LabelPrefix+labels[idx])
nlabels = append(nlabels, labels[idx+1]) nlabels = append(nlabels, labels[idx+1])
idx++
} }
return nlabels 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)), Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} m := newMetric(c.labels)
c.c.Write(m) _ = 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++
}
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} }
mfs = append(mfs, mf) 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)), Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} m := newMetric(c.labels)
c.c.Write(m) _ = 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++
}
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} }
mfs = append(mfs, mf) 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)), Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} m := newMetric(c.labels)
c.c.Write(m) _ = 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++
}
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} }
mfs = append(mfs, mf) 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)), Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} m := newMetric(c.labels)
c.c.Write(m) _ = 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++
}
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} }
mfs = append(mfs, mf) 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)), Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { for _, c := range metrics.cs {
m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)} m := newMetric(c.labels)
c.c.Write(m) _ = 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++
}
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} }
mfs = append(mfs, mf) mfs = append(mfs, mf)
@ -550,3 +513,14 @@ func newHash(labels []string) uint64 {
} }
return h.Sum64() 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
}