fix to support all metric types #53

Merged
vtolstov merged 1 commits from fixup into v3 2022-03-11 01:39:40 +03:00

View File

@ -52,21 +52,6 @@ func newFloat64(v float64) *float64 {
return &nv 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 { func newString(v string) *string {
nv := v nv := v
return &nv 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)), Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { for _, c := range metrics.cs {
m := &dto.Metric{ m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)}
Label: make([]*dto.LabelPair, 0, len(c.labels)/2), c.c.Write(m)
Gauge: &dto.Gauge{
Value: newFloat64(float64(c.Get())),
},
}
for idx := 0; idx < len(c.labels); idx++ { for idx := 0; idx < len(c.labels); idx++ {
m.Label = append(m.Label, &dto.LabelPair{ m.Label = append(m.Label, &dto.LabelPair{
Name: &c.labels[idx], 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)), Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { for _, c := range metrics.cs {
m := &dto.Metric{ m := &dto.Metric{Label: make([]*dto.LabelPair, 0, len(c.labels)/2)}
Label: make([]*dto.LabelPair, 0, len(c.labels)/2), c.c.Write(m)
Gauge: &dto.Gauge{ for idx := 0; idx < len(c.labels); idx++ {
Value: newFloat64(float64(c.Get())), 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++ { for idx := 0; idx < len(c.labels); idx++ {
m.Label = append(m.Label, &dto.LabelPair{ m.Label = append(m.Label, &dto.LabelPair{
Name: &c.labels[idx], Name: &c.labels[idx],