From b77f70aeb0df9f5c110ce20889cbab1b7cf6f54f Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Fri, 11 Mar 2022 01:37:53 +0300 Subject: [PATCH] fix to support all metric types Signed-off-by: Vasiliy Tolstov --- prometheus.go | 92 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/prometheus.go b/prometheus.go index eaf34a0..64fc469 100644 --- a/prometheus.go +++ b/prometheus.go @@ -52,21 +52,6 @@ func newFloat64(v float64) *float64 { return &nv } -func newMt(v dto.MetricType) *dto.MetricType { - nv := v - return &nv -} - -func newInt(v int) *int { - nv := v - return &nv -} - -func newInt64(v int64) *int64 { - nv := v - return &nv -} - func newString(v string) *string { nv := v return &nv @@ -319,12 +304,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), - Gauge: &dto.Gauge{ - Value: newFloat64(float64(c.Get())), - }, - } + 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], @@ -344,12 +325,71 @@ 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), - Gauge: &dto.Gauge{ - Value: newFloat64(float64(c.Get())), - }, + 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++ } + mf.Metric = append(mf.Metric, m) + } + mfs = append(mfs, mf) + } + + for name, metrics := range m.floatCounter { + mf := &dto.MetricFamily{ + Name: newString(name), + Type: dto.MetricType_GAUGE.Enum(), + 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++ + } + mf.Metric = append(mf.Metric, m) + } + mfs = append(mfs, mf) + } + + for name, metrics := range m.histogram { + mf := &dto.MetricFamily{ + Name: newString(name), + Type: dto.MetricType_HISTOGRAM.Enum(), + 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++ + } + mf.Metric = append(mf.Metric, m) + } + mfs = append(mfs, mf) + } + + for name, metrics := range m.summary { + mf := &dto.MetricFamily{ + Name: newString(name), + Type: dto.MetricType_SUMMARY.Enum(), + 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], -- 2.45.2