forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mdatagen] generate utility code to test telemetry (open-telemetry#10212
) This will allow components to more easily test the telemetry they should be emitting. --------- Signed-off-by: Alex Boten <[email protected]>
- Loading branch information
Showing
22 changed files
with
641 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: auto-generate utilities to test component telemetry | ||
|
||
# One or more tracking issues or pull requests related to the change | ||
issues: [19783] | ||
|
||
# (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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
cmd/mdatagen/internal/samplereceiver/generated_component_telemetry_test.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Code generated by mdatagen. DO NOT EDIT. | ||
|
||
package {{ if isCommand -}}main{{ else }}{{ .Package }}{{- end }} | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" | ||
sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/{{ .Status.Class }}" | ||
"go.opentelemetry.io/collector/{{ .Status.Class }}/{{ .Status.Class }}test" | ||
) | ||
|
||
type componentTestTelemetry struct { | ||
reader *sdkmetric.ManualReader | ||
meterProvider *sdkmetric.MeterProvider | ||
} | ||
|
||
func (tt *componentTestTelemetry) NewCreateSettings() {{ .Status.Class }}.CreateSettings { | ||
settings := {{ .Status.Class }}test.NewNopCreateSettings() | ||
settings.MeterProvider = tt.meterProvider | ||
settings.ID = component.NewID(component.MustNewType("{{ .Type }}")) | ||
|
||
return settings | ||
} | ||
|
||
func setupTestTelemetry() componentTestTelemetry { | ||
reader := sdkmetric.NewManualReader() | ||
return componentTestTelemetry{ | ||
reader: reader, | ||
meterProvider: sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)), | ||
} | ||
} | ||
|
||
func (tt *componentTestTelemetry) assertMetrics(t *testing.T, expected []metricdata.Metrics) { | ||
var md metricdata.ResourceMetrics | ||
require.NoError(t, tt.reader.Collect(context.Background(), &md)) | ||
// ensure all required metrics are present | ||
for _, want := range expected { | ||
got := tt.getMetric(want.Name, md) | ||
metricdatatest.AssertEqual(t, want, got, metricdatatest.IgnoreTimestamp()) | ||
} | ||
|
||
// ensure no additional metrics are emitted | ||
require.Equal(t, len(expected), tt.len(md)) | ||
} | ||
|
||
func (tt *componentTestTelemetry) getMetric(name string, got metricdata.ResourceMetrics) metricdata.Metrics { | ||
for _, sm := range got.ScopeMetrics { | ||
for _, m := range sm.Metrics { | ||
if m.Name == name { | ||
return m | ||
} | ||
} | ||
} | ||
|
||
return metricdata.Metrics{} | ||
} | ||
|
||
func (tt *componentTestTelemetry) len(got metricdata.ResourceMetrics) int { | ||
metricsCount := 0 | ||
for _, sm := range got.ScopeMetrics { | ||
metricsCount += len(sm.Metrics) | ||
} | ||
|
||
return metricsCount | ||
} | ||
|
||
func (tt *componentTestTelemetry) Shutdown(ctx context.Context) error { | ||
return tt.meterProvider.Shutdown(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.