Skip to content

Commit

Permalink
[metadatatest] Generate NewSettings that accepts componenttest.Teleme…
Browse files Browse the repository at this point in the history
…try (#12216)

This is the last functionality needed to replace
`metadatatest.Telemetry` with `componenttest.Telemetry`.

The `metadatatest.Telemetry` will be deprecated next release to give
time to change contrib since it is a large change.

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Jan 30, 2025
1 parent fb8bb93 commit d9b97ff
Show file tree
Hide file tree
Showing 24 changed files with 266 additions and 216 deletions.
25 changes: 25 additions & 0 deletions .chloggen/newsettings.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: metadatatest

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Generate NewSettings that accepts componenttest.Telemetry

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

# (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]

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

10 changes: 5 additions & 5 deletions cmd/mdatagen/internal/samplereceiver/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ func TestGeneratedMetrics(t *testing.T) {
}

func TestComponentTelemetry(t *testing.T) {
tt := metadatatest.SetupTelemetry()
tt := componenttest.NewTelemetry()
factory := NewFactory()
receiver, err := factory.CreateMetrics(context.Background(), tt.NewSettings(), componenttest.NewNopHost(), new(consumertest.MetricsSink))
receiver, err := factory.CreateMetrics(context.Background(), metadatatest.NewSettings(tt), componenttest.NewNopHost(), new(consumertest.MetricsSink))
require.NoError(t, err)
metadatatest.AssertEqualBatchSizeTriggerSend(t, tt.Telemetry,
metadatatest.AssertEqualBatchSizeTriggerSend(t, tt,
[]metricdata.DataPoint[int64]{
{
Value: 1,
},
}, metricdatatest.IgnoreTimestamp())
metadatatest.AssertEqualProcessRuntimeTotalAllocBytes(t, tt.Telemetry,
metadatatest.AssertEqualProcessRuntimeTotalAllocBytes(t, tt,
[]metricdata.DataPoint[int64]{
{
Value: 2,
Expand All @@ -45,7 +45,7 @@ func TestComponentTelemetry(t *testing.T) {
rcv, ok := receiver.(nopReceiver)
require.True(t, ok)
rcv.initOptionalMetric()
metadatatest.AssertEqualQueueLength(t, tt.Telemetry,
metadatatest.AssertEqualQueueLength(t, tt,
[]metricdata.DataPoint[int64]{
{
Value: 3,
Expand Down
13 changes: 11 additions & 2 deletions cmd/mdatagen/internal/templates/telemetrytest.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
)

type Telemetry struct {
componenttest.Telemetry
*componenttest.Telemetry
}

func SetupTelemetry(opts ...componenttest.TelemetryOption) Telemetry {
Expand Down Expand Up @@ -50,9 +50,18 @@ func (tt *Telemetry) AssertMetrics(t *testing.T, expected []metricdata.Metrics,
require.Equal(t, len(expected), lenMetrics(md))
}

{{ if or isConnector isExporter isExtension isProcessor isReceiver isScraper }}
func NewSettings(tt *componenttest.Telemetry) {{ .Status.Class }}.Settings {
set := {{ .Status.Class }}test.NewNopSettings()
set.ID = component.NewID(component.MustNewType("{{ .Type }}"))
set.TelemetrySettings = tt.NewTelemetrySettings()
return set
}
{{- end }}

{{ range $name, $metric := .Telemetry.Metrics }}

func AssertEqual{{ $name.Render }}(t *testing.T, tt componenttest.Telemetry, dps []metricdata.{{- if eq $metric.Data.Type "Histogram" }} {{$metric.Data.Type}} {{- end }}DataPoint[{{ $metric.Data.BasicType }}], opts ...metricdatatest.Option) {
func AssertEqual{{ $name.Render }}(t *testing.T, tt *componenttest.Telemetry, dps []metricdata.{{- if eq $metric.Data.Type "Histogram" }} {{$metric.Data.Type}} {{- end }}DataPoint[{{ $metric.Data.BasicType }}], opts ...metricdatatest.Option) {
want := metricdata.Metrics{
Name: "otelcol_{{ $name }}",
Description: "{{ $metric.Description }}{{ $metric.Stability }}",
Expand Down
22 changes: 11 additions & 11 deletions component/componenttest/obsreporttest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,62 +26,62 @@ const (
)

type TestTelemetry struct {
Telemetry
*Telemetry
id component.ID
}

// Deprecated: [v0.119.0] use the metadatatest.AssertEqualMetric series of functions instead.
// CheckExporterTraces checks that for the current exported values for trace exporter metrics match given values.
func (tts *TestTelemetry) CheckExporterTraces(sentSpans, sendFailedSpans int64) error {
return checkExporterTraces(&tts.Telemetry, tts.id, sentSpans, sendFailedSpans)
return checkExporter(tts.Telemetry, tts.id, "spans", sentSpans, sendFailedSpans)
}

// Deprecated: [v0.119.0] use the metadatatest.AssertEqualMetric series of functions instead.
// CheckExporterMetrics checks that for the current exported values for metrics exporter metrics match given values.
func (tts *TestTelemetry) CheckExporterMetrics(sentMetricsPoints, sendFailedMetricsPoints int64) error {
return checkExporterMetrics(&tts.Telemetry, tts.id, sentMetricsPoints, sendFailedMetricsPoints)
return checkExporter(tts.Telemetry, tts.id, "metric_points", sentMetricsPoints, sendFailedMetricsPoints)
}

// Deprecated: [v0.119.0] use the metadatatest.AssertEqualMetric series of functions instead.
func (tts *TestTelemetry) CheckExporterEnqueueFailedMetrics(enqueueFailed int64) error {
return checkExporterEnqueueFailed(&tts.Telemetry, tts.id, "metric_points", enqueueFailed)
return checkExporterEnqueueFailed(tts.Telemetry, tts.id, "metric_points", enqueueFailed)
}

// Deprecated: [v0.119.0] use the metadatatest.AssertEqualMetric series of functions instead.
func (tts *TestTelemetry) CheckExporterEnqueueFailedTraces(enqueueFailed int64) error {
return checkExporterEnqueueFailed(&tts.Telemetry, tts.id, "spans", enqueueFailed)
return checkExporterEnqueueFailed(tts.Telemetry, tts.id, "spans", enqueueFailed)
}

// Deprecated: [v0.119.0] use the metadatatest.AssertEqualMetric series of functions instead.
func (tts *TestTelemetry) CheckExporterEnqueueFailedLogs(enqueueFailed int64) error {
return checkExporterEnqueueFailed(&tts.Telemetry, tts.id, "log_records", enqueueFailed)
return checkExporterEnqueueFailed(tts.Telemetry, tts.id, "log_records", enqueueFailed)
}

// Deprecated: [v0.119.0] use the metadatatest.AssertEqualMetric series of functions instead.
// CheckExporterLogs checks that for the current exported values for logs exporter metrics match given values.
func (tts *TestTelemetry) CheckExporterLogs(sentLogRecords, sendFailedLogRecords int64) error {
return checkExporterLogs(&tts.Telemetry, tts.id, sentLogRecords, sendFailedLogRecords)
return checkExporter(tts.Telemetry, tts.id, "log_records", sentLogRecords, sendFailedLogRecords)
}

func (tts *TestTelemetry) CheckExporterMetricGauge(metric string, val int64, extraAttrs ...attribute.KeyValue) error {
attrs := attributesForExporterMetrics(tts.id, extraAttrs...)
return checkIntGauge(&tts.Telemetry, metric, val, attrs)
return checkIntGauge(tts.Telemetry, metric, val, attrs)
}

// CheckReceiverTraces checks that for the current exported values for trace receiver metrics match given values.
func (tts *TestTelemetry) CheckReceiverTraces(protocol string, acceptedSpans, droppedSpans int64) error {
return checkReceiverTraces(&tts.Telemetry, tts.id, protocol, acceptedSpans, droppedSpans)
return checkReceiver(tts.Telemetry, tts.id, "spans", protocol, acceptedSpans, droppedSpans)
}

// Deprecated: [v0.119.0] use the metadatatest.AssertEqualMetric series of functions instead.
// CheckReceiverLogs checks that for the current exported values for logs receiver metrics match given values.
func (tts *TestTelemetry) CheckReceiverLogs(protocol string, acceptedLogRecords, droppedLogRecords int64) error {
return checkReceiverLogs(&tts.Telemetry, tts.id, protocol, acceptedLogRecords, droppedLogRecords)
return checkReceiver(tts.Telemetry, tts.id, "log_records", protocol, acceptedLogRecords, droppedLogRecords)
}

// CheckReceiverMetrics checks that for the current exported values for metrics receiver metrics match given values.
func (tts *TestTelemetry) CheckReceiverMetrics(protocol string, acceptedMetricPoints, droppedMetricPoints int64) error {
return checkReceiverMetrics(&tts.Telemetry, tts.id, protocol, acceptedMetricPoints, droppedMetricPoints)
return checkReceiver(tts.Telemetry, tts.id, "metric_points", protocol, acceptedMetricPoints, droppedMetricPoints)
}

// TelemetrySettings returns the TestTelemetry's TelemetrySettings
Expand Down
24 changes: 0 additions & 24 deletions component/componenttest/otelchecker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,13 @@ import (
"go.opentelemetry.io/collector/component"
)

func checkReceiverTraces(tel *Telemetry, receiver component.ID, protocol string, accepted, dropped int64) error {
return checkReceiver(tel, receiver, "spans", protocol, accepted, dropped)
}

func checkReceiverLogs(tel *Telemetry, receiver component.ID, protocol string, accepted, dropped int64) error {
return checkReceiver(tel, receiver, "log_records", protocol, accepted, dropped)
}

func checkReceiverMetrics(tel *Telemetry, receiver component.ID, protocol string, accepted, dropped int64) error {
return checkReceiver(tel, receiver, "metric_points", protocol, accepted, dropped)
}

func checkReceiver(tel *Telemetry, receiver component.ID, datatype, protocol string, acceptedMetricPoints, droppedMetricPoints int64) error {
receiverAttrs := attributesForReceiverMetrics(receiver, protocol)
return multierr.Combine(
checkIntSum(tel, "otelcol_receiver_accepted_"+datatype, acceptedMetricPoints, receiverAttrs),
checkIntSum(tel, "otelcol_receiver_refused_"+datatype, droppedMetricPoints, receiverAttrs))
}

func checkExporterTraces(tel *Telemetry, exporter component.ID, sent, sendFailed int64) error {
return checkExporter(tel, exporter, "spans", sent, sendFailed)
}

func checkExporterLogs(tel *Telemetry, exporter component.ID, sent, sendFailed int64) error {
return checkExporter(tel, exporter, "log_records", sent, sendFailed)
}

func checkExporterMetrics(tel *Telemetry, exporter component.ID, sent, sendFailed int64) error {
return checkExporter(tel, exporter, "metric_points", sent, sendFailed)
}

func checkExporter(tel *Telemetry, exporter component.ID, datatype string, sent, sendFailed int64) error {
exporterAttrs := attributesForExporterMetrics(exporter)
errs := checkIntSum(tel, "otelcol_exporter_sent_"+datatype, sent, exporterAttrs)
Expand Down
4 changes: 2 additions & 2 deletions component/componenttest/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type Telemetry struct {
traceProvider *sdktrace.TracerProvider
}

func NewTelemetry(opts ...TelemetryOption) Telemetry {
func NewTelemetry(opts ...TelemetryOption) *Telemetry {
reader := sdkmetric.NewManualReader()
spanRecorder := new(tracetest.SpanRecorder)
tOpts := telemetryOption{
Expand All @@ -59,7 +59,7 @@ func NewTelemetry(opts ...TelemetryOption) Telemetry {
for _, opt := range opts {
opt.apply(&tOpts)
}
return Telemetry{
return &Telemetry{
Reader: reader,
SpanRecorder: spanRecorder,
meterProvider: sdkmetric.NewMeterProvider(tOpts.metricOpts...),
Expand Down
Loading

0 comments on commit d9b97ff

Please sign in to comment.