Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[connector/servicegraph] Remove use of host.GetExporters #32902

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions connector/servicegraphconnector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
type Config struct {

// MetricsExporter is the name of the metrics exporter to use to ship metrics.
//
// Deprecated: The exporter is defined as part of the pipeline and this option is currently noop.
codeboten marked this conversation as resolved.
Show resolved Hide resolved
MetricsExporter string `mapstructure:"metrics_exporter"`

// LatencyHistogramBuckets is the list of durations representing latency histogram buckets.
Expand Down
41 changes: 10 additions & 31 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 @@ -89,9 +88,13 @@ func customMetricName(name string) string {
return "connector/" + metadata.Type.String() + "/" + name
}

func newConnector(set component.TelemetrySettings, config component.Config) *serviceGraphConnector {
func newConnector(set component.TelemetrySettings, config component.Config, next consumer.Metrics) *serviceGraphConnector {
pConfig := config.(*Config)

if pConfig.MetricsExporter != "" {
set.Logger.Warn("'metrics_exporter' is deprecated and will be removed in a future release. Please remove it from the configuration.")
}

bounds := defaultLatencyHistogramBuckets
if legacyLatencyUnitMsFeatureGate.IsEnabled() {
bounds = legacyDefaultLatencyHistogramBuckets
Expand Down Expand Up @@ -135,8 +138,10 @@ func newConnector(set component.TelemetrySettings, config component.Config) *ser
)

return &serviceGraphConnector{
config: pConfig,
logger: set.Logger,
config: pConfig,
logger: set.Logger,
metricsConsumer: next,

startTime: time.Now(),
reqTotal: make(map[string]int64),
reqFailedTotal: make(map[string]int64),
Expand All @@ -155,35 +160,9 @@ 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)
}
}

go p.metricFlushLoop(p.config.MetricsFlushInterval)

go p.cacheLoop(p.config.CacheLoop)
Expand Down
65 changes: 11 additions & 54 deletions connector/servicegraphconnector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ func TestConnectorShutdown(t *testing.T) {
next := new(consumertest.MetricsSink)
set := componenttest.NewNopTelemetrySettings()
set.Logger = zaptest.NewLogger(t)
p := newConnector(set, cfg)
p.metricsConsumer = next
p := newConnector(set, cfg, next)
err := p.Shutdown(context.Background())

// Verify
Expand All @@ -73,9 +72,7 @@ func TestConnectorConsume(t *testing.T) {

set := componenttest.NewNopTelemetrySettings()
set.Logger = zaptest.NewLogger(t)
conn := newConnector(set, cfg)
conn.metricsConsumer = newMockMetricsExporter()

conn := newConnector(set, cfg, newMockMetricsExporter())
assert.NoError(t, conn.Start(context.Background(), componenttest.NewNopHost()))

// Test & verify
Expand Down Expand Up @@ -211,22 +208,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 @@ -297,8 +278,7 @@ func TestUpdateDurationMetrics(t *testing.T) {
func TestStaleSeriesCleanup(t *testing.T) {
// Prepare
cfg := &Config{
MetricsExporter: "mock",
Dimensions: []string{"some-attribute", "non-existing-attribute"},
Dimensions: []string{"some-attribute", "non-existing-attribute"},
Store: StoreConfig{
MaxItems: 10,
TTL: time.Second,
Expand All @@ -309,15 +289,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 := newConnector(set, cfg, mockMetricsExporter)
assert.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))

// ConsumeTraces
td := buildSampleTrace(t, "first")
Expand All @@ -342,8 +315,7 @@ func TestStaleSeriesCleanup(t *testing.T) {
func TestMapsAreConsistentDuringCleanup(t *testing.T) {
// Prepare
cfg := &Config{
MetricsExporter: "mock",
Dimensions: []string{"some-attribute", "non-existing-attribute"},
Dimensions: []string{"some-attribute", "non-existing-attribute"},
Store: StoreConfig{
MaxItems: 10,
TTL: time.Second,
Expand All @@ -354,15 +326,8 @@ func TestMapsAreConsistentDuringCleanup(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 := newConnector(set, cfg, mockMetricsExporter)
assert.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))

// ConsumeTraces
td := buildSampleTrace(t, "first")
Expand Down Expand Up @@ -416,8 +381,7 @@ func setupTelemetry(reader *sdkmetric.ManualReader) component.TelemetrySettings

func TestValidateOwnTelemetry(t *testing.T) {
cfg := &Config{
MetricsExporter: "mock",
Dimensions: []string{"some-attribute", "non-existing-attribute"},
Dimensions: []string{"some-attribute", "non-existing-attribute"},
Store: StoreConfig{
MaxItems: 10,
TTL: time.Second,
Expand All @@ -428,15 +392,8 @@ func TestValidateOwnTelemetry(t *testing.T) {

reader := sdkmetric.NewManualReader()
set := setupTelemetry(reader)
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 := newConnector(set, cfg, mockMetricsExporter)
assert.NoError(t, p.Start(context.Background(), componenttest.NewNopHost()))

// ConsumeTraces
td := buildSampleTrace(t, "first")
Expand Down
3 changes: 1 addition & 2 deletions connector/servicegraphconnector/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func createDefaultConfig() component.Config {
}

func createTracesToMetricsConnector(_ context.Context, params connector.CreateSettings, cfg component.Config, nextConsumer consumer.Metrics) (connector.Traces, error) {
c := newConnector(params.TelemetrySettings, cfg)
c.metricsConsumer = nextConsumer
c := newConnector(params.TelemetrySettings, cfg, nextConsumer)
return c, nil
}