From d2f01b4340ad49b6093c358e9f4849cdda1eb42b Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Thu, 25 Aug 2022 12:29:41 -0700 Subject: [PATCH] [chore] small improvements in prometheus metricfamily and builder (#13627) Signed-off-by: Bogdan Drutu Signed-off-by: Bogdan Drutu --- .../internal/otlp_metricfamily.go | 49 +++++++------------ .../internal/otlp_metricfamily_test.go | 6 +-- .../internal/otlp_metricsbuilder.go | 2 +- 3 files changed, 22 insertions(+), 35 deletions(-) diff --git a/receiver/prometheusreceiver/internal/otlp_metricfamily.go b/receiver/prometheusreceiver/internal/otlp_metricfamily.go index 3ba250d25678..c44e48ff5ddd 100644 --- a/receiver/prometheusreceiver/internal/otlp_metricfamily.go +++ b/receiver/prometheusreceiver/internal/otlp_metricfamily.go @@ -39,7 +39,7 @@ type metricFamily struct { labelKeys map[string]bool labelKeysOrdered []string metadata *scrape.MetricMetadata - groupOrders map[string]int + groupOrders []*metricGroup } // metricGroup, represents a single metric of a metric family. for example a histogram metric is usually represent by @@ -74,7 +74,6 @@ func newMetricFamily(metricName string, mc MetadataCache, logger *zap.Logger) *m labelKeys: make(map[string]bool), labelKeysOrdered: make([]string, 0), metadata: metadata, - groupOrders: make(map[string]int), } } @@ -91,7 +90,6 @@ func (mf *metricFamily) updateLabelKeys(ls labels.Labels) { mf.labelKeysOrdered = append(mf.labelKeysOrdered, "") copy(mf.labelKeysOrdered[i+1:], mf.labelKeysOrdered[i:]) mf.labelKeysOrdered[i] = l.Name - } } } @@ -120,7 +118,7 @@ func (mg *metricGroup) sortPoints() { }) } -func (mg *metricGroup) toDistributionPoint(orderedLabelKeys []string, dest *pmetric.HistogramDataPointSlice) bool { +func (mg *metricGroup) toDistributionPoint(orderedLabelKeys []string, dest pmetric.HistogramDataPointSlice) bool { if !mg.hasCount || len(mg.complexValue) == 0 { return false } @@ -179,7 +177,7 @@ func pdataTimestampFromMs(timeAtMs int64) pcommon.Timestamp { return pcommon.NewTimestampFromTime(time.Unix(secs, ns)) } -func (mg *metricGroup) toSummaryPoint(orderedLabelKeys []string, dest *pmetric.SummaryDataPointSlice) bool { +func (mg *metricGroup) toSummaryPoint(orderedLabelKeys []string, dest pmetric.SummaryDataPointSlice) bool { // expecting count to be provided, however, in the following two cases, they can be missed. // 1. data is corrupted // 2. ignored by startValue evaluation @@ -225,7 +223,7 @@ func (mg *metricGroup) toSummaryPoint(orderedLabelKeys []string, dest *pmetric.S return true } -func (mg *metricGroup) toNumberDataPoint(orderedLabelKeys []string, dest *pmetric.NumberDataPointSlice) bool { +func (mg *metricGroup) toNumberDataPoint(orderedLabelKeys []string, dest pmetric.NumberDataPointSlice) bool { var startTsNanos pcommon.Timestamp tsNanos := pdataTimestampFromMs(mg.ts) // gauge/undefined types have no start time. @@ -247,19 +245,17 @@ func (mg *metricGroup) toNumberDataPoint(orderedLabelKeys []string, dest *pmetri } func populateAttributes(orderedKeys []string, ls labels.Labels, dest pcommon.Map) { - src := ls.Map() + dest.EnsureCapacity(len(orderedKeys)) for _, key := range orderedKeys { - if src[key] == "" { + val := ls.Get(key) + if val == "" { // empty label values should be omitted continue } - dest.InsertString(key, src[key]) + dest.InsertString(key, val) } } -// Purposefully being referenced to avoid lint warnings about being "unused". -var _ = (*metricFamily)(nil).updateLabelKeys - func (mf *metricFamily) isCumulativeType() bool { return mf.mtype == pmetric.MetricDataTypeSum || mf.mtype == pmetric.MetricDataTypeHistogram || @@ -277,7 +273,7 @@ func (mf *metricFamily) loadMetricGroupOrCreate(groupKey string, ls labels.Label } mf.groups[groupKey] = mg // maintaining data insertion order is helpful to generate stable/reproducible metric output - mf.groupOrders[groupKey] = len(mf.groupOrders) + mf.groupOrders = append(mf.groupOrders, mg) } return mg } @@ -316,16 +312,7 @@ func (mf *metricFamily) Add(metricName string, ls labels.Labels, t int64, v floa return nil } -// getGroups to return groups in insertion order -func (mf *metricFamily) getGroups() []*metricGroup { - groups := make([]*metricGroup, len(mf.groupOrders)) - for k, v := range mf.groupOrders { - groups[v] = mf.groups[k] - } - return groups -} - -func (mf *metricFamily) ToMetric(metrics *pmetric.MetricSlice) (int, int) { +func (mf *metricFamily) toMetric(metrics pmetric.MetricSlice) (int, int) { metric := pmetric.NewMetric() metric.SetDataType(mf.mtype) metric.SetName(mf.name) @@ -339,8 +326,8 @@ func (mf *metricFamily) ToMetric(metrics *pmetric.MetricSlice) (int, int) { histogram := metric.Histogram() histogram.SetAggregationTemporality(pmetric.MetricAggregationTemporalityCumulative) hdpL := histogram.DataPoints() - for _, mg := range mf.getGroups() { - if !mg.toDistributionPoint(mf.labelKeysOrdered, &hdpL) { + for _, mg := range mf.groupOrders { + if !mg.toDistributionPoint(mf.labelKeysOrdered, hdpL) { mf.droppedTimeseries++ } } @@ -349,8 +336,8 @@ func (mf *metricFamily) ToMetric(metrics *pmetric.MetricSlice) (int, int) { case pmetric.MetricDataTypeSummary: summary := metric.Summary() sdpL := summary.DataPoints() - for _, mg := range mf.getGroups() { - if !mg.toSummaryPoint(mf.labelKeysOrdered, &sdpL) { + for _, mg := range mf.groupOrders { + if !mg.toSummaryPoint(mf.labelKeysOrdered, sdpL) { mf.droppedTimeseries++ } } @@ -361,8 +348,8 @@ func (mf *metricFamily) ToMetric(metrics *pmetric.MetricSlice) (int, int) { sum.SetAggregationTemporality(pmetric.MetricAggregationTemporalityCumulative) sum.SetIsMonotonic(mf.isMonotonic) sdpL := sum.DataPoints() - for _, mg := range mf.getGroups() { - if !mg.toNumberDataPoint(mf.labelKeysOrdered, &sdpL) { + for _, mg := range mf.groupOrders { + if !mg.toNumberDataPoint(mf.labelKeysOrdered, sdpL) { mf.droppedTimeseries++ } } @@ -372,8 +359,8 @@ func (mf *metricFamily) ToMetric(metrics *pmetric.MetricSlice) (int, int) { metric.SetDataType(pmetric.MetricDataTypeGauge) gauge := metric.Gauge() gdpL := gauge.DataPoints() - for _, mg := range mf.getGroups() { - if !mg.toNumberDataPoint(mf.labelKeysOrdered, &gdpL) { + for _, mg := range mf.groupOrders { + if !mg.toNumberDataPoint(mf.labelKeysOrdered, gdpL) { mf.droppedTimeseries++ } } diff --git a/receiver/prometheusreceiver/internal/otlp_metricfamily_test.go b/receiver/prometheusreceiver/internal/otlp_metricfamily_test.go index 913cb5c96348..8f66ed7bc50e 100644 --- a/receiver/prometheusreceiver/internal/otlp_metricfamily_test.go +++ b/receiver/prometheusreceiver/internal/otlp_metricfamily_test.go @@ -202,7 +202,7 @@ func TestMetricGroupData_toDistributionUnitTest(t *testing.T) { require.NotNil(t, mp.groups[groupKey], "Expecting the groupKey to have a value given key:: "+groupKey) sl := pmetric.NewMetricSlice() - mp.ToMetric(&sl) + mp.toMetric(sl) require.Equal(t, 1, sl.Len(), "Exactly one metric expected") metric := sl.At(0) @@ -445,7 +445,7 @@ func TestMetricGroupData_toSummaryUnitTest(t *testing.T) { require.NotNil(t, mp.groups[groupKey], "Expecting the groupKey to have a value given key:: "+groupKey) sl := pmetric.NewMetricSlice() - mp.ToMetric(&sl) + mp.toMetric(sl) require.Equal(t, 1, sl.Len(), "Exactly one metric expected") metric := sl.At(0) @@ -528,7 +528,7 @@ func TestMetricGroupData_toNumberDataUnitTest(t *testing.T) { require.NotNil(t, mp.groups[groupKey], "Expecting the groupKey to have a value given key:: "+groupKey) sl := pmetric.NewMetricSlice() - mp.ToMetric(&sl) + mp.toMetric(sl) require.Equal(t, 1, sl.Len(), "Exactly one metric expected") metric := sl.At(0) diff --git a/receiver/prometheusreceiver/internal/otlp_metricsbuilder.go b/receiver/prometheusreceiver/internal/otlp_metricsbuilder.go index f3207d3c02da..6249ba0e2370 100644 --- a/receiver/prometheusreceiver/internal/otlp_metricsbuilder.go +++ b/receiver/prometheusreceiver/internal/otlp_metricsbuilder.go @@ -204,7 +204,7 @@ func (b *metricBuilder) Build() (*pmetric.MetricSlice, int, int, error) { } for _, mf := range b.families { - ts, dts := mf.ToMetric(&b.metrics) + ts, dts := mf.toMetric(b.metrics) b.numTimeseries += ts b.droppedTimeseries += dts }