Skip to content

Commit

Permalink
[chore] Replace usage of metadatatest.SetupTelemetry with new compone…
Browse files Browse the repository at this point in the history
…ntest

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Jan 31, 2025
1 parent 0c69e17 commit 9d7ba73
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 323 deletions.
21 changes: 7 additions & 14 deletions connector/servicegraphconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,8 @@ func TestValidateOwnTelemetry(t *testing.T) {
}

mockMetricsExporter := newMockMetricsExporter()
set := metadatatest.SetupTelemetry()
p, err := newConnector(set.NewTelemetrySettings(), cfg, mockMetricsExporter)
tel := componenttest.NewTelemetry()
p, err := newConnector(tel.NewTelemetrySettings(), cfg, mockMetricsExporter)
require.NoError(t, err)
assert.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))

Expand All @@ -594,18 +594,11 @@ func TestValidateOwnTelemetry(t *testing.T) {

// Shutdown the connector
assert.NoError(t, p.Shutdown(context.Background()))
set.AssertMetrics(t, []metricdata.Metrics{
{
Name: "otelcol_connector_servicegraph_total_edges",
Description: "Total number of unique edges",
Unit: "1",
Data: metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{Value: 2},
},
},
metadatatest.AssertEqualConnectorServicegraphTotalEdges(t, tel, metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{Value: 2},
},
}, metricdatatest.IgnoreTimestamp())
require.NoError(t, set.Shutdown(context.Background()))
Expand Down
10 changes: 4 additions & 6 deletions exporter/loadbalancingexporter/telemetry_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ package loadbalancingexporter // import "github.com/open-telemetry/opentelemetry
import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exportertest"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/loadbalancingexporter/internal/metadata"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/loadbalancingexporter/internal/metadatatest"
)

func getTelemetryAssets(t require.TestingT) (exporter.Settings, *metadata.TelemetryBuilder) {
s := metadatatest.SetupTelemetry()
st := s.NewSettings()
ts := st.TelemetrySettings
tb, err := metadata.NewTelemetryBuilder(ts)
s := exportertest.NewNopSettings()
tb, err := metadata.NewTelemetryBuilder(s.TelemetrySettings)
require.NoError(t, err)
return st, tb
return s, tb
}
28 changes: 8 additions & 20 deletions processor/filterprocessor/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
Expand Down Expand Up @@ -768,35 +769,22 @@ func TestFilterLogProcessorWithOTTL(t *testing.T) {
}

func TestFilterLogProcessorTelemetry(t *testing.T) {
tel := metadatatest.SetupTelemetry()
processor, err := newFilterLogsProcessor(tel.NewSettings(), &Config{
tel := componenttest.NewTelemetry()
t.Cleanup(func() { require.NoError(t, tel.Shutdown(context.Background())) })
processor, err := newFilterLogsProcessor(metadatatest.NewSettings(tel), &Config{
Logs: LogFilters{LogConditions: []string{`IsMatch(body, "operationA")`}},
})
assert.NoError(t, err)

_, err = processor.processLogs(context.Background(), constructLogs())
assert.NoError(t, err)

want := []metricdata.Metrics{
metadatatest.AssertEqualProcessorFilterDatapointsFiltered(t, tel, []metricdata.DataPoint[int64]{
{
Name: "otelcol_processor_filter_logs.filtered",
Description: "Number of logs dropped by the filter processor",
Unit: "1",
Data: metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{
Value: 2,
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
},
},
},
Value: 2,
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
},
}

tel.AssertMetrics(t, want, metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreTimestamp())
require.NoError(t, tel.Shutdown(context.Background()))
}, metricdatatest.IgnoreTimestamp())
}

func constructLogs() plog.Logs {
Expand Down
28 changes: 7 additions & 21 deletions processor/filterprocessor/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
Expand Down Expand Up @@ -370,18 +371,15 @@ func TestFilterMetricProcessor(t *testing.T) {
}

func TestFilterMetricProcessorTelemetry(t *testing.T) {
tel := metadatatest.SetupTelemetry()
tel := componenttest.NewTelemetry()
cfg := &Config{
Metrics: MetricFilters{
MetricConditions: []string{
"name==\"metric1\"",
},
},
}
fmp, err := newFilterMetricProcessor(
tel.NewSettings(),
cfg,
)
fmp, err := newFilterMetricProcessor(metadatatest.NewSettings(tel), cfg)
assert.NotNil(t, fmp)
assert.NoError(t, err)

Expand All @@ -395,24 +393,12 @@ func TestFilterMetricProcessorTelemetry(t *testing.T) {
}))
assert.NoError(t, err)

want := []metricdata.Metrics{
metadatatest.AssertEqualProcessorFilterDatapointsFiltered(t, tel, []metricdata.DataPoint[int64]{
{
Name: "otelcol_processor_filter_datapoints.filtered",
Description: "Number of metric data points dropped by the filter processor",
Unit: "1",
Data: metricdata.Sum[int64]{
Temporality: metricdata.CumulativeTemporality,
IsMonotonic: true,
DataPoints: []metricdata.DataPoint[int64]{
{
Value: 1,
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
},
},
},
Value: 1,
Attributes: attribute.NewSet(attribute.String("filter", "filter")),
},
}
tel.AssertMetrics(t, want, metricdatatest.IgnoreTimestamp())
}, metricdatatest.IgnoreTimestamp())
require.NoError(t, tel.Shutdown(context.Background()))
}

Expand Down
34 changes: 9 additions & 25 deletions processor/tailsamplingprocessor/processor_decisions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/collector/processor/processortest"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor/internal/cache"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor/internal/metadatatest"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor/internal/sampling"
)

Expand All @@ -25,7 +25,6 @@ func TestSamplingPolicyTypicalPath(t *testing.T) {
NumTraces: defaultNumTraces,
}
nextConsumer := new(consumertest.TracesSink)
tel := metadatatest.SetupTelemetry()
idb := newSyncIDBatcher()

mpe1 := &mockPolicyEvaluator{}
Expand All @@ -34,7 +33,7 @@ func TestSamplingPolicyTypicalPath(t *testing.T) {
{name: "mock-policy-1", evaluator: mpe1, attribute: metric.WithAttributes(attribute.String("policy", "mock-policy-1"))},
}

p, err := newTracesProcessor(context.Background(), tel.NewSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
p, err := newTracesProcessor(context.Background(), processortest.NewNopSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
require.NoError(t, err)

require.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))
Expand All @@ -61,7 +60,6 @@ func TestSamplingPolicyTypicalPath(t *testing.T) {

// The final decision SHOULD be Sampled.
require.EqualValues(t, 1, nextConsumer.SpanCount())
require.NoError(t, tel.Shutdown(context.Background()))
}

func TestSamplingPolicyInvertSampled(t *testing.T) {
Expand All @@ -70,7 +68,6 @@ func TestSamplingPolicyInvertSampled(t *testing.T) {
NumTraces: defaultNumTraces,
}
nextConsumer := new(consumertest.TracesSink)
tel := metadatatest.SetupTelemetry()
idb := newSyncIDBatcher()

mpe1 := &mockPolicyEvaluator{}
Expand All @@ -79,7 +76,7 @@ func TestSamplingPolicyInvertSampled(t *testing.T) {
{name: "mock-policy-1", evaluator: mpe1, attribute: metric.WithAttributes(attribute.String("policy", "mock-policy-1"))},
}

p, err := newTracesProcessor(context.Background(), tel.NewSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
p, err := newTracesProcessor(context.Background(), processortest.NewNopSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
require.NoError(t, err)

require.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))
Expand All @@ -106,7 +103,6 @@ func TestSamplingPolicyInvertSampled(t *testing.T) {

// The final decision SHOULD be Sampled.
require.EqualValues(t, 1, nextConsumer.SpanCount())
require.NoError(t, tel.Shutdown(context.Background()))
}

func TestSamplingMultiplePolicies(t *testing.T) {
Expand All @@ -115,7 +111,6 @@ func TestSamplingMultiplePolicies(t *testing.T) {
NumTraces: defaultNumTraces,
}
nextConsumer := new(consumertest.TracesSink)
tel := metadatatest.SetupTelemetry()
idb := newSyncIDBatcher()

mpe1 := &mockPolicyEvaluator{}
Expand All @@ -126,7 +121,7 @@ func TestSamplingMultiplePolicies(t *testing.T) {
{name: "mock-policy-2", evaluator: mpe2, attribute: metric.WithAttributes(attribute.String("policy", "mock-policy-2"))},
}

p, err := newTracesProcessor(context.Background(), tel.NewSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
p, err := newTracesProcessor(context.Background(), processortest.NewNopSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
require.NoError(t, err)

require.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))
Expand Down Expand Up @@ -157,7 +152,6 @@ func TestSamplingMultiplePolicies(t *testing.T) {

// The final decision SHOULD be Sampled.
require.EqualValues(t, 1, nextConsumer.SpanCount())
require.NoError(t, tel.Shutdown(context.Background()))
}

func TestSamplingPolicyDecisionNotSampled(t *testing.T) {
Expand All @@ -166,7 +160,6 @@ func TestSamplingPolicyDecisionNotSampled(t *testing.T) {
NumTraces: defaultNumTraces,
}
nextConsumer := new(consumertest.TracesSink)
tel := metadatatest.SetupTelemetry()
idb := newSyncIDBatcher()

mpe1 := &mockPolicyEvaluator{}
Expand All @@ -175,7 +168,7 @@ func TestSamplingPolicyDecisionNotSampled(t *testing.T) {
{name: "mock-policy-1", evaluator: mpe1, attribute: metric.WithAttributes(attribute.String("policy", "mock-policy-1"))},
}

p, err := newTracesProcessor(context.Background(), tel.NewSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
p, err := newTracesProcessor(context.Background(), processortest.NewNopSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
require.NoError(t, err)

require.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))
Expand Down Expand Up @@ -203,7 +196,6 @@ func TestSamplingPolicyDecisionNotSampled(t *testing.T) {

// The final decision SHOULD be NotSampled.
require.EqualValues(t, 0, nextConsumer.SpanCount())
require.NoError(t, tel.Shutdown(context.Background()))
}

func TestSamplingPolicyDecisionInvertNotSampled(t *testing.T) {
Expand All @@ -212,7 +204,6 @@ func TestSamplingPolicyDecisionInvertNotSampled(t *testing.T) {
NumTraces: defaultNumTraces,
}
nextConsumer := new(consumertest.TracesSink)
tel := metadatatest.SetupTelemetry()
idb := newSyncIDBatcher()

mpe1 := &mockPolicyEvaluator{}
Expand All @@ -223,7 +214,7 @@ func TestSamplingPolicyDecisionInvertNotSampled(t *testing.T) {
{name: "mock-policy-2", evaluator: mpe2, attribute: metric.WithAttributes(attribute.String("policy", "mock-policy-2"))},
}

p, err := newTracesProcessor(context.Background(), tel.NewSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
p, err := newTracesProcessor(context.Background(), processortest.NewNopSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
require.NoError(t, err)

require.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))
Expand Down Expand Up @@ -254,7 +245,6 @@ func TestSamplingPolicyDecisionInvertNotSampled(t *testing.T) {

// The final decision SHOULD be NotSampled.
require.EqualValues(t, 0, nextConsumer.SpanCount())
require.NoError(t, tel.Shutdown(context.Background()))
}

func TestLateArrivingSpansAssignedOriginalDecision(t *testing.T) {
Expand All @@ -263,7 +253,6 @@ func TestLateArrivingSpansAssignedOriginalDecision(t *testing.T) {
NumTraces: defaultNumTraces,
}
nextConsumer := new(consumertest.TracesSink)
tel := metadatatest.SetupTelemetry()
idb := newSyncIDBatcher()

mpe1 := &mockPolicyEvaluator{}
Expand All @@ -274,7 +263,7 @@ func TestLateArrivingSpansAssignedOriginalDecision(t *testing.T) {
{name: "mock-policy-2", evaluator: mpe2, attribute: metric.WithAttributes(attribute.String("policy", "mock-policy-2"))},
}

p, err := newTracesProcessor(context.Background(), tel.NewSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
p, err := newTracesProcessor(context.Background(), processortest.NewNopSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies))
require.NoError(t, err)

require.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))
Expand Down Expand Up @@ -324,7 +313,6 @@ func TestLateArrivingSpansAssignedOriginalDecision(t *testing.T) {
require.EqualValues(t, 1, mpe1.EvaluationCount)
require.EqualValues(t, 1, mpe2.EvaluationCount)
require.EqualValues(t, 0, nextConsumer.SpanCount(), "original final decision not honored")
require.NoError(t, tel.Shutdown(context.Background()))
}

func TestLateArrivingSpanUsesDecisionCache(t *testing.T) {
Expand All @@ -333,7 +321,6 @@ func TestLateArrivingSpanUsesDecisionCache(t *testing.T) {
NumTraces: defaultNumTraces,
}
nextConsumer := new(consumertest.TracesSink)
tel := metadatatest.SetupTelemetry()
idb := newSyncIDBatcher()

mpe := &mockPolicyEvaluator{}
Expand All @@ -344,7 +331,7 @@ func TestLateArrivingSpanUsesDecisionCache(t *testing.T) {
// Use this instead of the default no-op cache
c, err := cache.NewLRUDecisionCache[bool](200)
require.NoError(t, err)
p, err := newTracesProcessor(context.Background(), tel.NewSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies), withSampledDecisionCache(c))
p, err := newTracesProcessor(context.Background(), processortest.NewNopSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies), withSampledDecisionCache(c))
require.NoError(t, err)

require.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))
Expand Down Expand Up @@ -397,7 +384,6 @@ func TestLateArrivingSpanUsesDecisionCache(t *testing.T) {
require.NoError(t, p.ConsumeTraces(context.Background(), spanIndexToTraces(2)))
require.EqualValues(t, 1, mpe.EvaluationCount)
require.EqualValues(t, 2, nextConsumer.SpanCount(), "original final decision not honored")
require.NoError(t, tel.Shutdown(context.Background()))
}

func TestLateSpanUsesNonSampledDecisionCache(t *testing.T) {
Expand All @@ -406,8 +392,6 @@ func TestLateSpanUsesNonSampledDecisionCache(t *testing.T) {
NumTraces: defaultNumTraces,
}
nextConsumer := new(consumertest.TracesSink)
s := metadatatest.SetupTelemetry()
ct := s.NewSettings()
idb := newSyncIDBatcher()

mpe := &mockPolicyEvaluator{}
Expand All @@ -418,7 +402,7 @@ func TestLateSpanUsesNonSampledDecisionCache(t *testing.T) {
// Use this instead of the default no-op cache
c, err := cache.NewLRUDecisionCache[bool](200)
require.NoError(t, err)
p, err := newTracesProcessor(context.Background(), ct, nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies), withNonSampledDecisionCache(c))
p, err := newTracesProcessor(context.Background(), processortest.NewNopSettings(), nextConsumer, cfg, withDecisionBatcher(idb), withPolicies(policies), withNonSampledDecisionCache(c))
require.NoError(t, err)

require.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))
Expand Down
Loading

0 comments on commit 9d7ba73

Please sign in to comment.