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

[exporter/datadog] Deprecate config.Config.OnceMetadata method #8359

Merged
merged 6 commits into from
Mar 21, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### 🚩 Deprecations 🚩

- `datadogexporter`: Deprecate `OnlyMetadata` method from `Config` struct (#8359)

## v0.47.0

### 💡 Enhancements 💡
Expand Down
3 changes: 3 additions & 0 deletions exporter/datadogexporter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ type Config struct {
warnings []error
}

// OnceMetadata gets a sync.Once instance used for initializing the host metadata.
// Deprecated: [v0.48.0] do not use, will be removed on v0.49.0.
// TODO (#8373): Remove this method.
func (c *Config) OnceMetadata() *sync.Once {
return &c.onceMetadata
}
Expand Down
36 changes: 24 additions & 12 deletions exporter/datadogexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package datadogexporter // import "github.com/open-telemetry/opentelemetry-colle
import (
"context"
"os"
"sync"
"time"

"go.opentelemetry.io/collector/component"
Expand All @@ -36,13 +37,18 @@ const (
typeStr = "datadog"
)

type factory struct {
onceMetadata sync.Once
}

// NewFactory creates a Datadog exporter factory
func NewFactory() component.ExporterFactory {
f := &factory{}
return component.NewExporterFactory(
typeStr,
createDefaultConfig,
component.WithMetricsExporter(createMetricsExporter),
component.WithTracesExporter(createTracesExporter),
f.createDefaultConfig,
component.WithMetricsExporter(f.createMetricsExporter),
component.WithTracesExporter(f.createTracesExporter),
)
}

Expand All @@ -54,7 +60,7 @@ func defaulttimeoutSettings() exporterhelper.TimeoutSettings {

// createDefaultConfig creates the default exporter configuration
// TODO (#8396): Remove `os.Getenv` everywhere.
func createDefaultConfig() config.Exporter {
func (*factory) createDefaultConfig() config.Exporter {
return &ddconfig.Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
TimeoutSettings: defaulttimeoutSettings(),
Expand Down Expand Up @@ -105,7 +111,7 @@ func createDefaultConfig() config.Exporter {
}

// createMetricsExporter creates a metrics exporter based on this config.
func createMetricsExporter(
func (f *factory) createMetricsExporter(
ctx context.Context,
set component.ExporterCreateSettings,
c config.Exporter,
Expand All @@ -124,18 +130,21 @@ func createMetricsExporter(
if cfg.OnlyMetadata {
pushMetricsFn = func(_ context.Context, md pdata.Metrics) error {
// only sending metadata use only metrics
once := cfg.OnceMetadata()
once.Do(func() {
f.onceMetadata.Do(func() {
attrs := pdata.NewAttributeMap()
if md.ResourceMetrics().Len() > 0 {
attrs = md.ResourceMetrics().At(0).Resource().Attributes()
}
go metadata.Pusher(ctx, set, cfg, attrs)
})

// Consume configuration sync.Once to preserve behavior.
// TODO (#8373): Remove this call.
cfg.OnceMetadata().Do(func() {})
return nil
}
} else {
exp, err := newMetricsExporter(ctx, set, cfg)
exp, err := newMetricsExporter(ctx, set, cfg, &f.onceMetadata)
if err != nil {
cancel()
return nil, err
Expand Down Expand Up @@ -165,7 +174,7 @@ func createMetricsExporter(
}

// createTracesExporter creates a trace exporter based on this config.
func createTracesExporter(
func (f *factory) createTracesExporter(
ctx context.Context,
set component.ExporterCreateSettings,
c config.Exporter,
Expand All @@ -184,18 +193,21 @@ func createTracesExporter(
if cfg.OnlyMetadata {
pushTracesFn = func(_ context.Context, td pdata.Traces) error {
// only sending metadata, use only attributes
once := cfg.OnceMetadata()
once.Do(func() {
f.onceMetadata.Do(func() {
attrs := pdata.NewAttributeMap()
if td.ResourceSpans().Len() > 0 {
attrs = td.ResourceSpans().At(0).Resource().Attributes()
}
go metadata.Pusher(ctx, set, cfg, attrs)
})

// Use configuration sync.Once to do nothing to preserve behavior.
// TODO (#8373): Remove this call.
cfg.OnceMetadata().Do(func() {})
return nil
}
} else {
pushTracesFn = newTracesExporter(ctx, set, cfg).pushTraceDataScrubbed
pushTracesFn = newTracesExporter(ctx, set, cfg, &f.onceMetadata).pushTraceDataScrubbed
}

return exporterhelper.NewTracesExporter(
Expand Down
40 changes: 23 additions & 17 deletions exporter/datadogexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"fmt"
"net/http"
"sync"
"time"

"go.opentelemetry.io/collector/component"
Expand All @@ -37,13 +38,14 @@ import (
)

type metricsExporter struct {
params component.ExporterCreateSettings
cfg *config.Config
ctx context.Context
client *datadog.Client
tr *translator.Translator
scrubber scrub.Scrubber
retrier *utils.Retrier
params component.ExporterCreateSettings
cfg *config.Config
ctx context.Context
client *datadog.Client
tr *translator.Translator
scrubber scrub.Scrubber
retrier *utils.Retrier
onceMetadata *sync.Once
}

// assert `hostProvider` implements HostnameProvider interface
Expand Down Expand Up @@ -94,7 +96,7 @@ func translatorFromConfig(logger *zap.Logger, cfg *config.Config) (*translator.T
return translator.New(logger, options...)
}

func newMetricsExporter(ctx context.Context, params component.ExporterCreateSettings, cfg *config.Config) (*metricsExporter, error) {
func newMetricsExporter(ctx context.Context, params component.ExporterCreateSettings, cfg *config.Config, onceMetadata *sync.Once) (*metricsExporter, error) {
client := utils.CreateClient(cfg.API.Key, cfg.Metrics.TCPAddr.Endpoint)
client.ExtraHeader["User-Agent"] = utils.UserAgent(params.BuildInfo)
client.HttpClient = utils.NewHTTPClient(cfg.TimeoutSettings, cfg.LimitedHTTPClientSettings)
Expand All @@ -108,13 +110,14 @@ func newMetricsExporter(ctx context.Context, params component.ExporterCreateSett

scrubber := scrub.NewScrubber()
return &metricsExporter{
params: params,
cfg: cfg,
ctx: ctx,
client: client,
tr: tr,
scrubber: scrubber,
retrier: utils.NewRetrier(params.Logger, cfg.RetrySettings, scrubber),
params: params,
cfg: cfg,
ctx: ctx,
client: client,
tr: tr,
scrubber: scrubber,
retrier: utils.NewRetrier(params.Logger, cfg.RetrySettings, scrubber),
onceMetadata: onceMetadata,
}, nil
}

Expand Down Expand Up @@ -157,14 +160,17 @@ func (exp *metricsExporter) PushMetricsData(ctx context.Context, md pdata.Metric
// Start host metadata with resource attributes from
// the first payload.
if exp.cfg.SendMetadata {
once := exp.cfg.OnceMetadata()
once.Do(func() {
exp.onceMetadata.Do(func() {
attrs := pdata.NewAttributeMap()
if md.ResourceMetrics().Len() > 0 {
attrs = md.ResourceMetrics().At(0).Resource().Attributes()
}
go metadata.Pusher(exp.ctx, exp.params, exp.cfg, attrs)
})

// Consume configuration's sync.Once to preserve behavior.
// TODO (#8373): Remove this function call.
exp.cfg.OnceMetadata().Do(func() {})
}

consumer := metrics.NewConsumer()
Expand Down
9 changes: 6 additions & 3 deletions exporter/datadogexporter/metrics_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@ func TestNewExporter(t *testing.T) {
},
}
params := componenttest.NewNopExporterCreateSettings()
f := NewFactory()

// The client should have been created correctly
exp, err := newMetricsExporter(context.Background(), params, cfg)
exp, err := f.CreateMetricsExporter(context.Background(), params, cfg)
require.NoError(t, err)
assert.NotNil(t, exp)
_ = exp.PushMetricsData(context.Background(), testutils.TestMetrics.Clone())
err = exp.ConsumeMetrics(context.Background(), testutils.TestMetrics.Clone())
require.NoError(t, err)
assert.Equal(t, len(server.MetadataChan), 0)

cfg.SendMetadata = true
cfg.UseResourceMetadata = true
_ = exp.PushMetricsData(context.Background(), testutils.TestMetrics.Clone())
err = exp.ConsumeMetrics(context.Background(), testutils.TestMetrics.Clone())
require.NoError(t, err)
body := <-server.MetadataChan
var recvMetadata metadata.HostMetadata
err = json.Unmarshal(body, &recvMetadata)
Expand Down
12 changes: 9 additions & 3 deletions exporter/datadogexporter/traces_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package datadogexporter // import "github.com/open-telemetry/opentelemetry-colle

import (
"context"
"sync"
"time"

"github.com/DataDog/datadog-agent/pkg/trace/exportable/config/configdefs"
Expand All @@ -42,6 +43,7 @@ type traceExporter struct {
client *datadog.Client
denylister *denylister
scrubber scrub.Scrubber
onceMetadata *sync.Once
}

var (
Expand All @@ -62,7 +64,7 @@ var (
}
)

func newTracesExporter(ctx context.Context, params component.ExporterCreateSettings, cfg *config.Config) *traceExporter {
func newTracesExporter(ctx context.Context, params component.ExporterCreateSettings, cfg *config.Config, onceMetadata *sync.Once) *traceExporter {
// client to send running metric to the backend & perform API key validation
client := utils.CreateClient(cfg.API.Key, cfg.Metrics.TCPAddr.Endpoint)
utils.ValidateAPIKey(params.Logger, client)
Expand All @@ -83,6 +85,7 @@ func newTracesExporter(ctx context.Context, params component.ExporterCreateSetti
client: client,
denylister: denylister,
scrubber: scrub.NewScrubber(),
onceMetadata: onceMetadata,
}

return exporter
Expand Down Expand Up @@ -114,14 +117,17 @@ func (exp *traceExporter) pushTraceData(
// Start host metadata with resource attributes from
// the first payload.
if exp.cfg.SendMetadata {
once := exp.cfg.OnceMetadata()
once.Do(func() {
exp.onceMetadata.Do(func() {
attrs := pdata.NewAttributeMap()
if td.ResourceSpans().Len() > 0 {
attrs = td.ResourceSpans().At(0).Resource().Attributes()
}
go metadata.Pusher(exp.ctx, exp.params, exp.cfg, attrs)
})

// Consume configuration's sync.Once to preserve behavior.
// TODO (#8373): Remove this function call.
exp.cfg.OnceMetadata().Do(func() {})
}

// convert traces to datadog traces and group trace payloads by env
Expand Down
16 changes: 10 additions & 6 deletions exporter/datadogexporter/traces_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func testTracesExporterHelper(td pdata.Traces, t *testing.T) []string {
}

params := componenttest.NewNopExporterCreateSettings()

exporter, err := createTracesExporter(context.Background(), params, &cfg)
f := NewFactory()
exporter, err := f.CreateTracesExporter(context.Background(), params, &cfg)

assert.NoError(t, err)

Expand Down Expand Up @@ -163,7 +163,9 @@ func TestNewTracesExporter(t *testing.T) {
params := componenttest.NewNopExporterCreateSettings()

// The client should have been created correctly
exp := newTracesExporter(context.Background(), params, cfg)
f := NewFactory()
exp, err := f.CreateTracesExporter(context.Background(), params, cfg)
assert.NoError(t, err)
assert.NotNil(t, exp)
}

Expand All @@ -175,7 +177,7 @@ func TestPushTraceData(t *testing.T) {
Key: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
TagsConfig: config.TagsConfig{
Hostname: "test_host",
Hostname: "test-host",
Env: "test_env",
Tags: []string{"key:val"},
},
Expand All @@ -191,9 +193,11 @@ func TestPushTraceData(t *testing.T) {
}

params := componenttest.NewNopExporterCreateSettings()
exp := newTracesExporter(context.Background(), params, cfg)
f := NewFactory()
exp, err := f.CreateTracesExporter(context.Background(), params, cfg)
assert.NoError(t, err)

err := exp.pushTraceData(context.Background(), testutils.TestTraces.Clone())
err = exp.ConsumeTraces(context.Background(), testutils.TestTraces.Clone())
assert.NoError(t, err)

body := <-server.MetadataChan
Expand Down