Skip to content

Commit

Permalink
[connector/servicegraph] Remove use of host.GetExporters
Browse files Browse the repository at this point in the history
Fixes open-telemetry#31628

Signed-off-by: Juraci Paixão Kröhling <[email protected]>
  • Loading branch information
jpkrohling committed May 8, 2024
1 parent 7a248f5 commit eb21938
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 63 deletions.
28 changes: 3 additions & 25 deletions connector/servicegraphconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
Expand Down Expand Up @@ -155,33 +154,12 @@ func newConnector(set component.TelemetrySettings, config component.Config) *ser
}
}

type getExporters interface {
GetExporters() map[component.DataType]map[component.ID]component.Component
}

func (p *serviceGraphConnector) Start(_ context.Context, host component.Host) error {
func (p *serviceGraphConnector) Start(_ context.Context, _ component.Host) error {
p.store = store.NewStore(p.config.Store.TTL, p.config.Store.MaxItems, p.onComplete, p.onExpire)

if p.metricsConsumer == nil {
ge, ok := host.(getExporters)
if !ok {
return fmt.Errorf("unable to get exporters")
}
exporters := ge.GetExporters()

// The available list of exporters come from any configured metrics pipelines' exporters.
for k, exp := range exporters[component.DataTypeMetrics] {
metricsExp, ok := exp.(exporter.Metrics)
if k.String() == p.config.MetricsExporter && ok {
p.metricsConsumer = metricsExp
break
}
}

if p.metricsConsumer == nil {
return fmt.Errorf("failed to find metrics exporter: %s",
p.config.MetricsExporter)
}
return fmt.Errorf("failed to find metrics exporter: %s",
p.config.MetricsExporter)
}

go p.metricFlushLoop(p.config.MetricsFlushInterval)
Expand Down
44 changes: 6 additions & 38 deletions connector/servicegraphconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,22 +211,6 @@ func buildSampleTrace(t *testing.T, attrValue string) ptrace.Traces {
return traces
}

type mockHost struct {
component.Host
exps map[component.DataType]map[component.ID]component.Component
}

func newMockHost(exps map[component.DataType]map[component.ID]component.Component) component.Host {
return &mockHost{
Host: componenttest.NewNopHost(),
exps: exps,
}
}

func (m *mockHost) GetExporters() map[component.DataType]map[component.ID]component.Component {
return m.exps
}

var _ exporter.Metrics = (*mockMetricsExporter)(nil)

func newMockMetricsExporter() *mockMetricsExporter {
Expand Down Expand Up @@ -310,14 +294,8 @@ func TestStaleSeriesCleanup(t *testing.T) {
set := componenttest.NewNopTelemetrySettings()
set.Logger = zaptest.NewLogger(t)
p := newConnector(set, cfg)

mHost := newMockHost(map[component.DataType]map[component.ID]component.Component{
component.DataTypeMetrics: {
component.MustNewID("mock"): mockMetricsExporter,
},
})

assert.NoError(t, p.Start(context.Background(), mHost))
p.metricsConsumer = mockMetricsExporter
assert.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))

// ConsumeTraces
td := buildSampleTrace(t, "first")
Expand Down Expand Up @@ -355,14 +333,9 @@ func TestMapsAreConsistentDuringCleanup(t *testing.T) {
set := componenttest.NewNopTelemetrySettings()
set.Logger = zaptest.NewLogger(t)
p := newConnector(set, cfg)
p.metricsConsumer = mockMetricsExporter

mHost := newMockHost(map[component.DataType]map[component.ID]component.Component{
component.DataTypeMetrics: {
component.MustNewID("mock"): mockMetricsExporter,
},
})

assert.NoError(t, p.Start(context.Background(), mHost))
assert.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))

// ConsumeTraces
td := buildSampleTrace(t, "first")
Expand Down Expand Up @@ -429,14 +402,9 @@ func TestValidateOwnTelemetry(t *testing.T) {
reader := sdkmetric.NewManualReader()
set := setupTelemetry(reader)
p := newConnector(set, cfg)
p.metricsConsumer = mockMetricsExporter

mHost := newMockHost(map[component.DataType]map[component.ID]component.Component{
component.DataTypeMetrics: {
component.MustNewID("mock"): mockMetricsExporter,
},
})

assert.NoError(t, p.Start(context.Background(), mHost))
assert.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))

// ConsumeTraces
td := buildSampleTrace(t, "first")
Expand Down

0 comments on commit eb21938

Please sign in to comment.