Skip to content

Commit

Permalink
Wrap double export in feature gate
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Jul 11, 2023
1 parent 17c9fb6 commit 678526b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions exporter/collector/go.mod
Expand Up @@ -15,6 +15,7 @@ require (
github.com/stretchr/testify v1.8.3
github.com/tidwall/wal v1.1.7
go.opencensus.io v0.24.0
go.opentelemetry.io/collector/featuregate v0.77.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0012
go.opentelemetry.io/collector/semconv v0.78.0
go.opentelemetry.io/otel v1.16.0
Expand Down
2 changes: 2 additions & 0 deletions exporter/collector/go.sum
Expand Up @@ -114,6 +114,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
go.opentelemetry.io/collector/featuregate v0.77.0 h1:m1/IzaXoQh6SgF6CM80vrBOCf5zSJ2GVISfA27fYzGU=
go.opentelemetry.io/collector/featuregate v0.77.0/go.mod h1:/kVAsGUCyJXIDSgHftCN63QiwAEVHRLX2Kh/S+dqgHY=
go.opentelemetry.io/collector/pdata v1.0.0-rcv0012 h1:R+cfEUMyLn9Q1QknyQ4QU77pbfc1aJKYEXFHtnwSbCg=
go.opentelemetry.io/collector/pdata v1.0.0-rcv0012/go.mod h1:rEAKFqc1L03lidKtra/2/dJtI0Hp+JsQxuPEIkj/2Vg=
go.opentelemetry.io/collector/semconv v0.78.0 h1:kiufXJhRiBkjGXPQPLklknbov9xr8N/HV4P/yoICnHI=
Expand Down
2 changes: 1 addition & 1 deletion exporter/collector/integrationtest/go.mod
Expand Up @@ -18,6 +18,7 @@ require (
go.opentelemetry.io/collector v0.78.0
go.opentelemetry.io/collector/component v0.78.0
go.opentelemetry.io/collector/exporter v0.78.0
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0012
go.opentelemetry.io/collector/pdata v1.0.0-rcv0012
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/sdk v1.16.0
Expand Down Expand Up @@ -87,7 +88,6 @@ require (
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opentelemetry.io/collector/confmap v0.78.0 // indirect
go.opentelemetry.io/collector/consumer v0.78.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0012 // indirect
go.opentelemetry.io/collector/receiver v0.78.0 // indirect
go.opentelemetry.io/collector/semconv v0.78.0 // indirect
go.opentelemetry.io/contrib/propagators/b3 v1.16.1 // indirect
Expand Down
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector/internal/integrationtest/testcases"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"

Expand Down
Expand Up @@ -19,6 +19,7 @@ import (
"strings"
"time"

"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
Expand Down Expand Up @@ -64,6 +65,10 @@ var MetricsTestCases = []TestCase{
OTLPInputFixturePath: "testdata/fixtures/metrics/untyped_prometheus_metrics.json",
ExpectFixturePath: "testdata/fixtures/metrics/untyped_prometheus_metrics_expect.json",
SkipForSDK: true,
ConfigureCollector: func(cfg *collector.Config) {
//nolint:errcheck
featuregate.GlobalRegistry().Set("gcp.untyped_double_export", true)
},
},
{
Name: "Modified prefix unknown domain",
Expand Down
54 changes: 32 additions & 22 deletions exporter/collector/metrics.go
Expand Up @@ -50,6 +50,7 @@ import (

"go.opencensus.io/plugin/ocgrpc"
"go.opencensus.io/stats/view"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"

Expand Down Expand Up @@ -140,6 +141,13 @@ const (
GCPOpsAgentUntypedMetricKey = "prometheus_untyped_metric"
)

var untypedDoubleExportFeatureGate = featuregate.GlobalRegistry().MustRegister(
"gcp.untyped_double_export",
featuregate.StageAlpha,
featuregate.WithRegisterFromVersion("v0.77.0"),
featuregate.WithRegisterDescription("Enable automatically exporting untyped Prometheus metrics as both gauge and cumulative to GCP."),
featuregate.WithRegisterReferenceURL("https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/pull/668"))

type labels map[string]string

func (me *MetricsExporter) Shutdown(ctx context.Context) error {
Expand Down Expand Up @@ -1236,29 +1244,31 @@ func (m *metricMapper) gaugePointToTimeSeries(

// if an untyped prometheus metric from ops agent, double-export as gauge and cumulative
// to match GMP exporter behavior. Only for internal use.
val, ok := point.Attributes().Get(GCPOpsAgentUntypedMetricKey)
if ok && val.AsString() == "true" {
metricKind := metricpb.MetricDescriptor_CUMULATIVE
value, valueType := m.numberDataPointToValue(point, metricKind, metric.Unit())
series = append(series, &monitoringpb.TimeSeries{
Resource: resource,
Unit: metric.Unit(),
MetricKind: metricKind,
ValueType: valueType,
Points: []*monitoringpb.Point{{
Interval: &monitoringpb.TimeInterval{
EndTime: timestamppb.New(point.Timestamp().AsTime()),
if untypedDoubleExportFeatureGate.IsEnabled() {
val, ok := point.Attributes().Get(GCPOpsAgentUntypedMetricKey)
if ok && val.AsString() == "true" {
metricKind := metricpb.MetricDescriptor_CUMULATIVE
value, valueType := m.numberDataPointToValue(point, metricKind, metric.Unit())
series = append(series, &monitoringpb.TimeSeries{
Resource: resource,
Unit: metric.Unit(),
MetricKind: metricKind,
ValueType: valueType,
Points: []*monitoringpb.Point{{
Interval: &monitoringpb.TimeInterval{
EndTime: timestamppb.New(point.Timestamp().AsTime()),
},
Value: value,
}},
Metric: &metricpb.Metric{
Type: t,
Labels: mergeLabels(
attributesToLabels(point.Attributes()),
extraLabels,
),
},
Value: value,
}},
Metric: &metricpb.Metric{
Type: t,
Labels: mergeLabels(
attributesToLabels(point.Attributes()),
extraLabels,
),
},
})
})
}
}

return series
Expand Down
3 changes: 3 additions & 0 deletions exporter/collector/metrics_test.go
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tidwall/wal"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/zap"
Expand Down Expand Up @@ -1178,6 +1179,8 @@ func TestGaugePointToTimeSeries(t *testing.T) {

// Add ops agent untyped prometheus metric attribute
// Should double-export as gauge+cumulative and drop untyped metric label
err := featuregate.GlobalRegistry().Set("gcp.untyped_double_export", true)
assert.NoError(t, err)
point.Attributes().PutStr(GCPOpsAgentUntypedMetricKey, "true")
tsl = mapper.gaugePointToTimeSeries(mr, extraLabels, metric, gauge, point)
assert.Len(t, tsl, 2)
Expand Down

0 comments on commit 678526b

Please sign in to comment.