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

Prometheus Receiver metric type fixes to match Prometheus functionality #4865

Merged
merged 5 commits into from
Sep 30, 2021
Merged
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
11 changes: 10 additions & 1 deletion receiver/prometheusreceiver/internal/metricfamily.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,18 @@ func newMetricFamily(metricName string, mc MetadataCache, logger *zap.Logger, in
}
} else if !ok && isInternalMetric(metricName) {
metadata = defineInternalMetric(metricName, metadata, logger)
} else if !ok {
// Prometheus sends metrics without a type hint as gauges.
// MetricTypeUnknown is converted to a gauge in convToOCAMetricType()
metadata.Type = textparse.MetricTypeUnknown
}
ocaMetricType := convToOCAMetricType(metadata.Type)
if ocaMetricType == metricspb.MetricDescriptor_UNSPECIFIED {

// If a counter has a _total suffix but metadata is stored without it, keep _total suffix as the name otherwise
// the metric sent won't have the suffix
if ocaMetricType == metricspb.MetricDescriptor_CUMULATIVE_DOUBLE && strings.HasSuffix(metricName, metricSuffixTotal) {
familyName = metricName
} else if ocaMetricType == metricspb.MetricDescriptor_UNSPECIFIED {
logger.Debug(fmt.Sprintf("Invalid metric : %s %+v", metricName, metadata))
}

Expand Down
35 changes: 33 additions & 2 deletions receiver/prometheusreceiver/internal/metricsbuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,37 @@ func Test_metricBuilder_counters(t *testing.T) {
},
},
},
// Some counters such as "python_gc_collections_total" have metadata key as "python_gc_collections" but still need
// to be converted using full metric name as "python_gc_collections_total" to match Prometheus functionality
{
name: "counter-with-metadata-without-total-suffix",
inputs: []*testScrapedPage{
{
pts: []*testDataPoint{
createDataPoint("counter_test_total", 100, "foo", "bar"),
},
},
},
wants: [][]*metricspb.Metric{
{
{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: "counter_test_total",
Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE,
LabelKeys: []*metricspb.LabelKey{{Key: "foo"}}},
Timeseries: []*metricspb.TimeSeries{
{
StartTimestamp: timestampFromMs(startTs),
LabelValues: []*metricspb.LabelValue{{Value: "bar", HasValue: true}},
Points: []*metricspb.Point{
{Timestamp: timestampFromMs(startTs), Value: &metricspb.Point_DoubleValue{DoubleValue: 100.0}},
},
},
},
},
},
},
},
{
name: "two-items",
inputs: []*testScrapedPage{
Expand Down Expand Up @@ -544,7 +575,7 @@ func Test_metricBuilder_untype(t *testing.T) {
{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: "something_not_exists",
Type: metricspb.MetricDescriptor_UNSPECIFIED,
Type: metricspb.MetricDescriptor_GAUGE_DOUBLE,
LabelKeys: []*metricspb.LabelKey{{Key: "foo"}}},
Timeseries: []*metricspb.TimeSeries{
{
Expand All @@ -558,7 +589,7 @@ func Test_metricBuilder_untype(t *testing.T) {
{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: "theother_not_exists",
Type: metricspb.MetricDescriptor_UNSPECIFIED,
Type: metricspb.MetricDescriptor_GAUGE_DOUBLE,
LabelKeys: []*metricspb.LabelKey{{Key: "bar"}, {Key: "foo"}}},
Timeseries: []*metricspb.TimeSeries{
{
Expand Down
13 changes: 12 additions & 1 deletion receiver/prometheusreceiver/internal/otlp_metricfamily.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,21 @@ func newMetricFamilyPdata(metricName string, mc MetadataCache, intervalStartTime
metadata.Metric = familyName
metadata.Type = textparse.MetricTypeUnknown
}
} else if !ok {
// Prometheus sends metrics without a type hint as gauges.
// MetricTypeUnknown is converted to a gauge in convToOCAMetricType()
metadata.Type = textparse.MetricTypeUnknown
}
mtype := convToPdataMetricType(metadata.Type)

// If a counter has a _total suffix but metadata is stored without it, keep _total suffix as the name otherwise
// the metric sent won't have the suffix
if mtype == pdata.MetricDataTypeSum && strings.HasSuffix(metricName, metricSuffixTotal) {
familyName = metricName
}

return &metricFamilyPdata{
mtype: convToPdataMetricType(metadata.Type),
mtype: mtype,
groups: make(map[string]*metricGroupPdata),
metricFamily: metricFamily{
name: familyName,
Expand Down