diff --git a/receiver/prometheusreceiver/internal/metricfamily.go b/receiver/prometheusreceiver/internal/metricfamily.go index f091f0bded03..274c6a456974 100644 --- a/receiver/prometheusreceiver/internal/metricfamily.go +++ b/receiver/prometheusreceiver/internal/metricfamily.go @@ -37,9 +37,7 @@ const ( ) type metricFamily struct { - mtype pmetric.MetricType - // isMonotonic only applies to sums - isMonotonic bool + mtype pmetric.MetricType groups map[uint64]*metricGroup name string metadata *scrape.MetricMetadata @@ -50,7 +48,6 @@ type metricFamily struct { // a couple data complexValue (buckets and count/sum), a group of a metric family always share a same set of tags. for // simple types like counter and gauge, each data point is a group of itself type metricGroup struct { - mtype pmetric.MetricType ts int64 ls labels.Labels count float64 @@ -64,17 +61,16 @@ type metricGroup struct { func newMetricFamily(metricName string, mc scrape.MetricMetadataStore, logger *zap.Logger) *metricFamily { metadata, familyName := metadataForMetric(metricName, mc) - mtype, isMonotonic := convToMetricType(metadata.Type) + mtype := convToMetricType(metadata.Type) if mtype == pmetric.MetricTypeEmpty { logger.Debug(fmt.Sprintf("Unknown-typed metric : %s %+v", metricName, metadata)) } return &metricFamily{ - mtype: mtype, - isMonotonic: isMonotonic, - groups: make(map[uint64]*metricGroup), - name: familyName, - metadata: metadata, + mtype: mtype, + groups: make(map[uint64]*metricGroup), + name: familyName, + metadata: metadata, } } @@ -200,11 +196,11 @@ func (mg *metricGroup) toSummaryPoint(dest pmetric.SummaryDataPointSlice) { populateAttributes(pmetric.MetricTypeSummary, mg.ls, point.Attributes()) } -func (mg *metricGroup) toNumberDataPoint(dest pmetric.NumberDataPointSlice) { +func (mg *metricGroup) toNumberDataPoint(dest pmetric.NumberDataPointSlice, mType pmetric.MetricType) { tsNanos := timestampFromMs(mg.ts) point := dest.AppendEmpty() // gauge/undefined types have no start time. - if mg.mtype == pmetric.MetricTypeSum { + if mType == pmetric.MetricTypeSum { point.SetStartTimestamp(tsNanos) // metrics_adjuster adjusts the startTimestamp to the initial scrape timestamp } point.SetTimestamp(tsNanos) @@ -240,7 +236,6 @@ func (mf *metricFamily) loadMetricGroupOrCreate(groupKey uint64, ls labels.Label mg, ok := mf.groups[groupKey] if !ok { mg = &metricGroup{ - mtype: mf.mtype, ts: ts, ls: ls, exemplars: pmetric.NewExemplarSlice(), @@ -312,10 +307,11 @@ func (mf *metricFamily) appendMetric(metrics pmetric.MetricSlice, normalizer *pr case pmetric.MetricTypeSum: sum := metric.SetEmptySum() sum.SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) - sum.SetIsMonotonic(mf.isMonotonic) + // is_monotonic MUST be equal to true + sum.SetIsMonotonic(true) sdpL := sum.DataPoints() for _, mg := range mf.groupOrders { - mg.toNumberDataPoint(sdpL) + mg.toNumberDataPoint(sdpL, pmetric.MetricTypeSum) } pointCount = sdpL.Len() @@ -323,7 +319,7 @@ func (mf *metricFamily) appendMetric(metrics pmetric.MetricSlice, normalizer *pr gauge := metric.SetEmptyGauge() gdpL := gauge.DataPoints() for _, mg := range mf.groupOrders { - mg.toNumberDataPoint(gdpL) + mg.toNumberDataPoint(gdpL, pmetric.MetricTypeGauge) } pointCount = gdpL.Len() } diff --git a/receiver/prometheusreceiver/internal/util.go b/receiver/prometheusreceiver/internal/util.go index 3f8633a7b31f..f8f16e9ea7ad 100644 --- a/receiver/prometheusreceiver/internal/util.go +++ b/receiver/prometheusreceiver/internal/util.go @@ -102,27 +102,27 @@ func getBoundary(metricType pmetric.MetricType, labels labels.Labels) (float64, } // convToMetricType returns the data type and if it is monotonic -func convToMetricType(metricType textparse.MetricType) (pmetric.MetricType, bool) { +func convToMetricType(metricType textparse.MetricType) pmetric.MetricType { switch metricType { case textparse.MetricTypeCounter: // always use float64, as it's the internal data type used in prometheus - return pmetric.MetricTypeSum, true + return pmetric.MetricTypeSum // textparse.MetricTypeUnknown is converted to gauge by default to prevent Prometheus untyped metrics from being dropped case textparse.MetricTypeGauge, textparse.MetricTypeUnknown: - return pmetric.MetricTypeGauge, false + return pmetric.MetricTypeGauge case textparse.MetricTypeHistogram: - return pmetric.MetricTypeHistogram, true + return pmetric.MetricTypeHistogram // dropping support for gaugehistogram for now until we have an official spec of its implementation // a draft can be found in: https://docs.google.com/document/d/1KwV0mAXwwbvvifBvDKH_LU1YjyXE_wxCkHNoCGq1GX0/edit#heading=h.1cvzqd4ksd23 // case textparse.MetricTypeGaugeHistogram: // return case textparse.MetricTypeSummary: - return pmetric.MetricTypeSummary, true + return pmetric.MetricTypeSummary case textparse.MetricTypeInfo, textparse.MetricTypeStateset: - return pmetric.MetricTypeSum, false + return pmetric.MetricTypeSum default: // including: textparse.MetricTypeGaugeHistogram - return pmetric.MetricTypeEmpty, false + return pmetric.MetricTypeEmpty } } diff --git a/receiver/prometheusreceiver/internal/util_test.go b/receiver/prometheusreceiver/internal/util_test.go index 2c83d80bc198..7db58268af62 100644 --- a/receiver/prometheusreceiver/internal/util_test.go +++ b/receiver/prometheusreceiver/internal/util_test.go @@ -63,67 +63,57 @@ func TestTimestampFromFloat64(t *testing.T) { func TestConvToMetricType(t *testing.T) { tests := []struct { - name string - mtype textparse.MetricType - want pmetric.MetricType - wantMonotonic bool + name string + mtype textparse.MetricType + want pmetric.MetricType }{ { - name: "textparse.counter", - mtype: textparse.MetricTypeCounter, - want: pmetric.MetricTypeSum, - wantMonotonic: true, + name: "textparse.counter", + mtype: textparse.MetricTypeCounter, + want: pmetric.MetricTypeSum, }, { - name: "textparse.gauge", - mtype: textparse.MetricTypeGauge, - want: pmetric.MetricTypeGauge, - wantMonotonic: false, + name: "textparse.gauge", + mtype: textparse.MetricTypeGauge, + want: pmetric.MetricTypeGauge, }, { - name: "textparse.unknown", - mtype: textparse.MetricTypeUnknown, - want: pmetric.MetricTypeGauge, - wantMonotonic: false, + name: "textparse.unknown", + mtype: textparse.MetricTypeUnknown, + want: pmetric.MetricTypeGauge, }, { - name: "textparse.histogram", - mtype: textparse.MetricTypeHistogram, - want: pmetric.MetricTypeHistogram, - wantMonotonic: true, + name: "textparse.histogram", + mtype: textparse.MetricTypeHistogram, + want: pmetric.MetricTypeHistogram, }, { - name: "textparse.summary", - mtype: textparse.MetricTypeSummary, - want: pmetric.MetricTypeSummary, - wantMonotonic: true, + name: "textparse.summary", + mtype: textparse.MetricTypeSummary, + want: pmetric.MetricTypeSummary, }, { - name: "textparse.metric_type_info", - mtype: textparse.MetricTypeInfo, - want: pmetric.MetricTypeSum, - wantMonotonic: false, + name: "textparse.metric_type_info", + mtype: textparse.MetricTypeInfo, + want: pmetric.MetricTypeSum, }, { - name: "textparse.metric_state_set", - mtype: textparse.MetricTypeStateset, - want: pmetric.MetricTypeSum, - wantMonotonic: false, + name: "textparse.metric_state_set", + mtype: textparse.MetricTypeStateset, + want: pmetric.MetricTypeSum, }, { - name: "textparse.metric_gauge_hostogram", - mtype: textparse.MetricTypeGaugeHistogram, - want: pmetric.MetricTypeEmpty, - wantMonotonic: false, + name: "textparse.metric_gauge_hostogram", + mtype: textparse.MetricTypeGaugeHistogram, + want: pmetric.MetricTypeEmpty, }, } for _, tt := range tests { tt := tt t.Run(tt.name, func(t *testing.T) { - got, monotonic := convToMetricType(tt.mtype) + got := convToMetricType(tt.mtype) require.Equal(t, got.String(), tt.want.String()) - require.Equal(t, monotonic, tt.wantMonotonic) }) } }