Skip to content

Commit

Permalink
Add tracing support in metadatatest (#12106)
Browse files Browse the repository at this point in the history
#### Description
Add tracing support in metadatatest

cc @bogdandrutu  
#### Documentation
Added
  • Loading branch information
sincejune authored Jan 17, 2025
1 parent cd5092b commit c14d4f9
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 54 deletions.
25 changes: 25 additions & 0 deletions .chloggen/add-tracing-support-in-metadatatest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: mdatagen

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add tracing support in metadatatest

# One or more tracking issues or pull requests related to the change
issues: [12106]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
4 changes: 2 additions & 2 deletions cmd/mdatagen/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ require (
go.opentelemetry.io/collector/scraper/scrapertest v0.117.0
go.opentelemetry.io/collector/semconv v0.117.0
go.opentelemetry.io/otel/metric v1.32.0
go.opentelemetry.io/otel/sdk v1.32.0
go.opentelemetry.io/otel/sdk/metric v1.32.0
go.opentelemetry.io/otel/trace v1.32.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
golang.org/x/text v0.21.0
)
Expand Down Expand Up @@ -57,8 +59,6 @@ require (
go.opentelemetry.io/collector/processor/xprocessor v0.117.0 // indirect
go.opentelemetry.io/collector/receiver/xreceiver v0.117.0 // indirect
go.opentelemetry.io/otel v1.32.0 // indirect
go.opentelemetry.io/otel/sdk v1.32.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241104194629-dd2ea8efbc28 // indirect
Expand Down

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

26 changes: 20 additions & 6 deletions cmd/mdatagen/internal/templates/telemetrytest.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (
"testing"

"github.com/stretchr/testify/require"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/metric/metricdata"
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
sdkmetric "go.opentelemetry.io/otel/sdk/metric"

sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
"go.uber.org/multierr"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config/configtelemetry"
Expand All @@ -21,15 +24,22 @@ import (
)

type Telemetry struct {
reader *sdkmetric.ManualReader
Reader *sdkmetric.ManualReader
SpanRecorder *tracetest.SpanRecorder

meterProvider *sdkmetric.MeterProvider
traceProvider *sdktrace.TracerProvider
}

func SetupTelemetry() Telemetry {
reader := sdkmetric.NewManualReader()
spanRecorder := new(tracetest.SpanRecorder)
return Telemetry{
reader: reader,
Reader: reader,
SpanRecorder: spanRecorder,

meterProvider: sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)),
traceProvider: sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(spanRecorder)),
}
}

Expand All @@ -46,12 +56,13 @@ func (tt *Telemetry) NewTelemetrySettings() component.TelemetrySettings {
set := componenttest.NewNopTelemetrySettings()
set.MeterProvider = tt.meterProvider
set.MetricsLevel = configtelemetry.LevelDetailed
set.TracerProvider = tt.traceProvider
return set
}

func (tt *Telemetry) AssertMetrics(t *testing.T, expected []metricdata.Metrics, opts ...metricdatatest.Option) {
var md metricdata.ResourceMetrics
require.NoError(t, tt.reader.Collect(context.Background(), &md))
require.NoError(t, tt.Reader.Collect(context.Background(), &md))
// ensure all required metrics are present
for _, want := range expected {
got := getMetric(want.Name, md)
Expand All @@ -63,7 +74,10 @@ func (tt *Telemetry) AssertMetrics(t *testing.T, expected []metricdata.Metrics,
}

func (tt *Telemetry) Shutdown(ctx context.Context) error {
return tt.meterProvider.Shutdown(ctx)
return multierr.Combine(
tt.meterProvider.Shutdown(ctx),
tt.traceProvider.Shutdown(ctx),
)
}

func getMetric(name string, got metricdata.ResourceMetrics) metricdata.Metrics {
Expand Down

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

4 changes: 2 additions & 2 deletions processor/batchprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ require (
go.opentelemetry.io/collector/processor/processortest v0.117.0
go.opentelemetry.io/otel v1.32.0
go.opentelemetry.io/otel/metric v1.32.0
go.opentelemetry.io/otel/sdk v1.32.0
go.opentelemetry.io/otel/sdk/metric v1.32.0
go.opentelemetry.io/otel/trace v1.32.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
)

Expand All @@ -45,8 +47,6 @@ require (
go.opentelemetry.io/collector/pdata/pprofile v0.117.0 // indirect
go.opentelemetry.io/collector/pipeline v0.117.0 // indirect
go.opentelemetry.io/collector/processor/xprocessor v0.117.0 // indirect
go.opentelemetry.io/otel/sdk v1.32.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
Expand Down

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

4 changes: 2 additions & 2 deletions processor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ require (
go.opentelemetry.io/collector/processor/processortest v0.117.0
go.opentelemetry.io/otel v1.32.0
go.opentelemetry.io/otel/metric v1.32.0
go.opentelemetry.io/otel/sdk v1.32.0
go.opentelemetry.io/otel/sdk/metric v1.32.0
go.opentelemetry.io/otel/trace v1.32.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
)

require (
Expand All @@ -34,8 +36,6 @@ require (
go.opentelemetry.io/collector/pdata/pprofile v0.117.0 // indirect
go.opentelemetry.io/collector/pdata/testdata v0.117.0 // indirect
go.opentelemetry.io/collector/processor/xprocessor v0.117.0 // indirect
go.opentelemetry.io/otel/sdk v1.32.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions processor/memorylimiterprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ require (
go.opentelemetry.io/collector/processor/processortest v0.117.0
go.opentelemetry.io/otel v1.32.0
go.opentelemetry.io/otel/metric v1.32.0
go.opentelemetry.io/otel/sdk v1.32.0
go.opentelemetry.io/otel/sdk/metric v1.32.0
go.opentelemetry.io/otel/trace v1.32.0
go.uber.org/goleak v1.3.0
go.uber.org/multierr v1.11.0
)

require (
Expand Down Expand Up @@ -53,8 +55,6 @@ require (
go.opentelemetry.io/collector/pdata/pprofile v0.117.0 // indirect
go.opentelemetry.io/collector/pdata/testdata v0.117.0 // indirect
go.opentelemetry.io/collector/processor/xprocessor v0.117.0 // indirect
go.opentelemetry.io/otel/sdk v1.32.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
Expand Down
Loading

0 comments on commit c14d4f9

Please sign in to comment.