Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore][prometheusreceiver] Simplify metric family and metric group data structures. #16532

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions receiver/prometheusreceiver/internal/metricfamily.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -312,18 +307,19 @@ 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()

default: // Everything else should be set to a Gauge.
gauge := metric.SetEmptyGauge()
gdpL := gauge.DataPoints()
for _, mg := range mf.groupOrders {
mg.toNumberDataPoint(gdpL)
mg.toNumberDataPoint(gdpL, pmetric.MetricTypeGauge)
}
pointCount = gdpL.Len()
}
Expand Down
14 changes: 7 additions & 7 deletions receiver/prometheusreceiver/internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <pdata gauge histogram type>
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
}
}

Expand Down
66 changes: 28 additions & 38 deletions receiver/prometheusreceiver/internal/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down