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] update promrw exporter to use mdatagen for internal metrics #33240

Merged
merged 2 commits into from
May 31, 2024
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
23 changes: 23 additions & 0 deletions exporter/prometheusremotewriteexporter/documentation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[comment]: <> (Code generated by mdatagen. DO NOT EDIT.)

# prometheusremotewrite

## Internal Telemetry

The following telemetry is emitted by this component.

### exporter_prometheusremotewrite_failed_translations

Number of translation operations that failed to translate metrics from Otel to Prometheus

| Unit | Metric Type | Value Type | Monotonic |
| ---- | ----------- | ---------- | --------- |
| 1 | Sum | Int | true |

### exporter_prometheusremotewrite_translated_time_series

Number of Prometheus time series that were translated from OTel metrics

| Unit | Metric Type | Value Type | Monotonic |
| ---- | ----------- | ---------- | --------- |
| 1 | Sum | Int | true |
31 changes: 10 additions & 21 deletions exporter/prometheusremotewriteexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@ type prwTelemetry interface {
}

type prwTelemetryOtel struct {
failedTranslations metric.Int64Counter
translatedTimeSeries metric.Int64Counter
otelAttrs []attribute.KeyValue
telemetryBuilder *metadata.TelemetryBuilder
otelAttrs []attribute.KeyValue
}

func (p *prwTelemetryOtel) recordTranslationFailure(ctx context.Context) {
p.failedTranslations.Add(ctx, 1, metric.WithAttributes(p.otelAttrs...))
p.telemetryBuilder.ExporterPrometheusremotewriteFailedTranslations.Add(ctx, 1, metric.WithAttributes(p.otelAttrs...))
}

func (p *prwTelemetryOtel) recordTranslatedTimeSeries(ctx context.Context, numTS int) {
p.translatedTimeSeries.Add(ctx, int64(numTS), metric.WithAttributes(p.otelAttrs...))
p.telemetryBuilder.ExporterPrometheusremotewriteTranslatedTimeSeries.Add(ctx, int64(numTS), metric.WithAttributes(p.otelAttrs...))
}

// prwExporter converts OTLP metrics to Prometheus remote write TimeSeries and sends them to a remote endpoint.
Expand All @@ -73,27 +72,17 @@ type prwExporter struct {
}

func newPRWTelemetry(set exporter.CreateSettings) (prwTelemetry, error) {

meter := metadata.Meter(set.TelemetrySettings)
// TODO: create helper functions similar to the processor helper: BuildCustomMetricName
prefix := "exporter/" + metadata.Type.String() + "/"
failedTranslations, errFailedTranslation := meter.Int64Counter(prefix+"failed_translations",
metric.WithDescription("Number of translation operations that failed to translate metrics from Otel to Prometheus"),
metric.WithUnit("1"),
)

translatedTimeSeries, errTranslatedMetrics := meter.Int64Counter(prefix+"translated_time_series",
metric.WithDescription("Number of Prometheus time series that were translated from OTel metrics"),
metric.WithUnit("1"),
)
telemetryBuilder, err := metadata.NewTelemetryBuilder(set.TelemetrySettings)
if err != nil {
return nil, err
}

return &prwTelemetryOtel{
failedTranslations: failedTranslations,
translatedTimeSeries: translatedTimeSeries,
telemetryBuilder: telemetryBuilder,
otelAttrs: []attribute.KeyValue{
attribute.String("exporter", set.ID.String()),
},
}, errors.Join(errFailedTranslation, errTranslatedMetrics)
}, nil
}

// newPRWExporter initializes a new prwExporter instance and sets fields accordingly.
Expand Down
56 changes: 38 additions & 18 deletions exporter/prometheusremotewriteexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import (
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/metric/metricdata"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/testdata"
)
Expand Down Expand Up @@ -372,19 +374,6 @@ func runExportPipeline(ts *prompb.TimeSeries, endpoint *url.URL) error {
return prwe.handleExport(context.Background(), testmap, nil)
}

type mockPRWTelemetry struct {
failedTranslations int
translatedTimeSeries int
}

func (m *mockPRWTelemetry) recordTranslationFailure(_ context.Context) {
m.failedTranslations++
}

func (m *mockPRWTelemetry) recordTranslatedTimeSeries(_ context.Context, numTs int) {
m.translatedTimeSeries += numTs
}

// Test_PushMetrics checks the number of TimeSeries received by server and the number of metrics dropped is the same as
// expected
func Test_PushMetrics(t *testing.T) {
Expand Down Expand Up @@ -697,7 +686,6 @@ func Test_PushMetrics(t *testing.T) {
}
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
mockTelemetry := &mockPRWTelemetry{}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tt.reqTestFunc != nil {
tt.reqTestFunc(t, r, tt.expectedTimeSeries, tt.isStaleMarker)
Expand Down Expand Up @@ -744,11 +732,11 @@ func Test_PushMetrics(t *testing.T) {
Description: "OpenTelemetry Collector",
Version: "1.0",
}
set := exportertest.NewNopCreateSettings()
tel := setupTestTelemetry()
set := tel.NewCreateSettings()
set.BuildInfo = buildInfo

prwe, nErr := newPRWExporter(cfg, set)
prwe.telemetry = mockTelemetry

require.NoError(t, nErr)
ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -762,9 +750,41 @@ func Test_PushMetrics(t *testing.T) {
assert.Error(t, err)
return
}
expectedMetrics := []metricdata.Metrics{}
if tt.expectedFailedTranslations > 0 {
expectedMetrics = append(expectedMetrics, metricdata.Metrics{
Name: "exporter_prometheusremotewrite_failed_translations",
Description: "Number of translation operations that failed to translate metrics from Otel to Prometheus",
Unit: "1",
Data: metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{
Value: int64(tt.expectedFailedTranslations),
Attributes: attribute.NewSet(attribute.String("exporter", "prometheusremotewrite")),
},
},
},
})
}

assert.Equal(t, tt.expectedFailedTranslations, mockTelemetry.failedTranslations)
assert.Equal(t, tt.expectedTimeSeries, mockTelemetry.translatedTimeSeries)
expectedMetrics = append(expectedMetrics, metricdata.Metrics{
Name: "exporter_prometheusremotewrite_translated_time_series",
Description: "Number of Prometheus time series that were translated from OTel metrics",
Unit: "1",
Data: metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{
Value: int64(tt.expectedTimeSeries),
Attributes: attribute.NewSet(attribute.String("exporter", "prometheusremotewrite")),
},
},
},
})
tel.assertMetrics(t, expectedMetrics)
assert.NoError(t, err)
})
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion exporter/prometheusremotewriteexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
go.opentelemetry.io/collector/pdata v1.8.1-0.20240527192838-af4fdd4e342a
go.opentelemetry.io/otel v1.27.0
go.opentelemetry.io/otel/metric v1.27.0
go.opentelemetry.io/otel/sdk/metric v1.27.0
go.opentelemetry.io/otel/trace v1.27.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
Expand Down Expand Up @@ -73,7 +74,6 @@ require (
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.49.0 // indirect
go.opentelemetry.io/otel/sdk v1.27.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.27.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion exporter/prometheusremotewriteexporter/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,21 @@ status:
active: [Aneurysm9, rapphil]

tests:
expect_consumer_error: true
expect_consumer_error: true

telemetry:
metrics:
exporter_prometheusremotewrite_failed_translations:
enabled: true
description: Number of translation operations that failed to translate metrics from Otel to Prometheus
unit: 1
sum:
value_type: int
monotonic: true
exporter_prometheusremotewrite_translated_time_series:
enabled: true
description: Number of Prometheus time series that were translated from OTel metrics
unit: 1
sum:
value_type: int
monotonic: true
Loading