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

Update all github.com/datadog packages #31398

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix breaking changes from removing statsd and timing singletons
  • Loading branch information
liustanley committed Feb 23, 2024
commit 7e379f0560fc3a39c83272257764a22b6eec2017
6 changes: 4 additions & 2 deletions connector/datadogconnector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
traceconfig "github.com/DataDog/datadog-agent/pkg/trace/config"
"github.com/DataDog/datadog-agent/pkg/trace/timing"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics"
"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -45,7 +47,7 @@ type traceToMetricConnector struct {
var _ component.Component = (*traceToMetricConnector)(nil) // testing that the connectorImp properly implements the type Component interface

// function to create a new connector
func newTraceToMetricConnector(set component.TelemetrySettings, cfg component.Config, metricsConsumer consumer.Metrics) (*traceToMetricConnector, error) {
func newTraceToMetricConnector(set component.TelemetrySettings, cfg component.Config, metricsConsumer consumer.Metrics, metricsClient statsd.ClientInterface, timingReporter timing.Reporter) (*traceToMetricConnector, error) {
set.Logger.Info("Building datadog connector for traces to metrics")
in := make(chan *pb.StatsPayload, 100)
set.MeterProvider = noop.NewMeterProvider() // disable metrics for the connector
Expand All @@ -61,7 +63,7 @@ func newTraceToMetricConnector(set component.TelemetrySettings, cfg component.Co
ctx := context.Background()
return &traceToMetricConnector{
logger: set.Logger,
agent: datadog.NewAgentWithConfig(ctx, getTraceAgentCfg(cfg.(*Config).Traces), in),
agent: datadog.NewAgentWithConfig(ctx, getTraceAgentCfg(cfg.(*Config).Traces), in, metricsClient, timingReporter),
translator: trans,
in: in,
metricsConsumer: metricsConsumer,
Expand Down
11 changes: 4 additions & 7 deletions connector/datadogconnector/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package datadogconnector // import "github.com/open-telemetry/opentelemetry-coll
import (
"context"

"github.com/DataDog/datadog-agent/pkg/trace/timing"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/connector"
"go.opentelemetry.io/collector/consumer"
Expand Down Expand Up @@ -38,19 +39,15 @@ func createDefaultConfig() component.Config {
// defines the consumer type of the connector
// we want to consume traces and export metrics therefore define nextConsumer as metrics, consumer is the next component in the pipeline
func createTracesToMetricsConnector(_ context.Context, params connector.CreateSettings, cfg component.Config, nextConsumer consumer.Metrics) (connector.Traces, error) {
initializeMetricsClient(params)
c, err := newTraceToMetricConnector(params.TelemetrySettings, cfg, nextConsumer)
metricsClient := datadog.InitializeMetricClient(params.MeterProvider)
timingReporter := timing.New(metricsClient)
c, err := newTraceToMetricConnector(params.TelemetrySettings, cfg, nextConsumer, metricsClient, timingReporter)
if err != nil {
return nil, err
}
return c, nil
}

func createTracesToTracesConnector(_ context.Context, params connector.CreateSettings, _ component.Config, nextConsumer consumer.Traces) (connector.Traces, error) {
initializeMetricsClient(params)
return newTraceToTraceConnector(params.Logger, nextConsumer), nil
}

func initializeMetricsClient(params connector.CreateSettings) {
datadog.InitializeMetricClient(params.MeterProvider)
}
2 changes: 1 addition & 1 deletion connector/datadogconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/DataDog/datadog-agent/pkg/proto v0.52.0-rc.2
github.com/DataDog/datadog-agent/pkg/trace v0.52.0-rc.2
github.com/DataDog/datadog-go/v5 v5.1.1
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.13.3
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics v0.13.3
github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter v0.95.0
Expand Down Expand Up @@ -38,7 +39,6 @@ require (
github.com/DataDog/datadog-agent/pkg/util/pointer v0.52.0-rc.2 // indirect
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.52.0-rc.2 // indirect
github.com/DataDog/datadog-api-client-go/v2 v2.22.0 // indirect
github.com/DataDog/datadog-go/v5 v5.1.1 // indirect
github.com/DataDog/go-sqllexer v0.0.9 // indirect
github.com/DataDog/go-tuf v1.0.2-0.5.2 // indirect
github.com/DataDog/gohai v0.0.0-20230524154621-4316413895ee // indirect
Expand Down
8 changes: 5 additions & 3 deletions exporter/datadogexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
"github.com/DataDog/datadog-agent/pkg/trace/agent"
"github.com/DataDog/datadog-agent/pkg/trace/telemetry"
"github.com/DataDog/datadog-agent/pkg/trace/timing"
"github.com/DataDog/datadog-agent/pkg/trace/writer"
"github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes"
Expand Down Expand Up @@ -141,8 +142,7 @@ func (f *factory) StopReporter() {
}

func (f *factory) TraceAgent(ctx context.Context, params exporter.CreateSettings, cfg *Config, sourceProvider source.Provider) (*agent.Agent, error) {
datadog.InitializeMetricClient(params.MeterProvider)
agnt, err := newTraceAgent(ctx, params, cfg, sourceProvider)
agnt, err := newTraceAgent(ctx, params, cfg, sourceProvider, datadog.InitializeMetricClient(params.MeterProvider))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -290,7 +290,9 @@ func (f *factory) createMetricsExporter(
return nil, err
}
statsToAgent := make(chan *pb.StatsPayload)
statsWriter := writer.NewStatsWriter(acfg, statsToAgent, telemetry.NewNoopCollector())
metricsClient := datadog.InitializeMetricClient(set.MeterProvider)
timingReporter := timing.New(metricsClient)
statsWriter := writer.NewStatsWriter(acfg, statsToAgent, telemetry.NewNoopCollector(), metricsClient, timingReporter)

set.Logger.Debug("Starting Datadog Trace-Agent StatsWriter")
go statsWriter.Run()
Expand Down
2 changes: 1 addition & 1 deletion exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/DataDog/datadog-agent/pkg/proto v0.52.0-rc.2
github.com/DataDog/datadog-agent/pkg/trace v0.52.0-rc.2
github.com/DataDog/datadog-api-client-go/v2 v2.22.0
github.com/DataDog/datadog-go/v5 v5.1.1
github.com/DataDog/gohai v0.0.0-20230524154621-4316413895ee
github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata v0.13.3
github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes v0.13.3
Expand Down Expand Up @@ -75,7 +76,6 @@ require (
github.com/DataDog/datadog-agent/pkg/util/log v0.52.0-rc.2 // indirect
github.com/DataDog/datadog-agent/pkg/util/pointer v0.52.0-rc.2 // indirect
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.52.0-rc.2 // indirect
github.com/DataDog/datadog-go/v5 v5.1.1 // indirect
github.com/DataDog/go-sqllexer v0.0.9 // indirect
github.com/DataDog/go-tuf v1.0.2-0.5.2 // indirect
github.com/DataDog/zstd v1.5.2 // indirect
Expand Down
5 changes: 3 additions & 2 deletions exporter/datadogexporter/traces_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
tracelog "github.com/DataDog/datadog-agent/pkg/trace/log"
"github.com/DataDog/datadog-agent/pkg/trace/telemetry"
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/DataDog/opentelemetry-mapping-go/pkg/inframetadata"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/attributes/source"
"go.opentelemetry.io/collector/consumer"
Expand Down Expand Up @@ -181,12 +182,12 @@ func (exp *traceExporter) exportUsageMetrics(ctx context.Context, hosts map[stri
}
}

func newTraceAgent(ctx context.Context, params exporter.CreateSettings, cfg *Config, sourceProvider source.Provider) (*agent.Agent, error) {
func newTraceAgent(ctx context.Context, params exporter.CreateSettings, cfg *Config, sourceProvider source.Provider, metricsClient statsd.ClientInterface) (*agent.Agent, error) {
acfg, err := newTraceAgentConfig(ctx, params, cfg, sourceProvider)
if err != nil {
return nil, err
}
return agent.NewAgent(ctx, acfg, telemetry.NewNoopCollector()), nil
return agent.NewAgent(ctx, acfg, telemetry.NewNoopCollector(), metricsClient), nil
}

func newTraceAgentConfig(ctx context.Context, params exporter.CreateSettings, cfg *Config, sourceProvider source.Provider) (*traceconfig.AgentConfig, error) {
Expand Down
16 changes: 9 additions & 7 deletions internal/datadog/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
traceconfig "github.com/DataDog/datadog-agent/pkg/trace/config"
"github.com/DataDog/datadog-agent/pkg/trace/stats"
"github.com/DataDog/datadog-agent/pkg/trace/telemetry"
"github.com/DataDog/datadog-agent/pkg/trace/timing"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/DataDog/opentelemetry-mapping-go/pkg/otlp/metrics"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pdata/ptrace"
Expand Down Expand Up @@ -45,12 +47,12 @@ var ConnectorPerformanceFeatureGate = featuregate.GlobalRegistry().MustRegister(

// newAgent creates a new unstarted traceagent using the given context. Call Start to start the traceagent.
// The out channel will receive outoing stats payloads resulting from spans ingested using the Ingest method.
func NewAgent(ctx context.Context, out chan *pb.StatsPayload) *TraceAgent {
return NewAgentWithConfig(ctx, traceconfig.New(), out)
func NewAgent(ctx context.Context, out chan *pb.StatsPayload, metricsClient statsd.ClientInterface, timingReporter timing.Reporter) *TraceAgent {
return NewAgentWithConfig(ctx, traceconfig.New(), out, metricsClient, timingReporter)
}

// newAgentWithConfig creates a new traceagent with the given config cfg. Used in tests; use newAgent instead.
func NewAgentWithConfig(ctx context.Context, cfg *traceconfig.AgentConfig, out chan *pb.StatsPayload) *TraceAgent {
func NewAgentWithConfig(ctx context.Context, cfg *traceconfig.AgentConfig, out chan *pb.StatsPayload, metricsClient statsd.ClientInterface, timingReporter timing.Reporter) *TraceAgent {
// disable the HTTP receiver
cfg.ReceiverPort = 0
// set the API key to succeed startup; it is never used nor needed
Expand All @@ -62,16 +64,16 @@ func NewAgentWithConfig(ctx context.Context, cfg *traceconfig.AgentConfig, out c
// Ingest). This gives a better user experience.
cfg.Hostname = metrics.UnsetHostnamePlaceholder
pchan := make(chan *api.Payload, 1000)
a := agent.NewAgent(ctx, cfg, telemetry.NewNoopCollector())
a := agent.NewAgent(ctx, cfg, telemetry.NewNoopCollector(), metricsClient)
// replace the Concentrator (the component which computes and flushes APM Stats from incoming
// traces) with our own, which uses the 'out' channel.
a.Concentrator = stats.NewConcentrator(cfg, out, time.Now())
a.Concentrator = stats.NewConcentrator(cfg, out, time.Now(), metricsClient)
// ...and the same for the ClientStatsAggregator; we don't use it here, but it is also a source
// of stats which should be available to us.
a.ClientStatsAggregator = stats.NewClientStatsAggregator(cfg, out)
a.ClientStatsAggregator = stats.NewClientStatsAggregator(cfg, out, metricsClient)
// lastly, start the OTLP receiver, which will be used to introduce ResourceSpans into the traceagent,
// so that we can transform them to Datadog spans and receive stats.
a.OTLPReceiver = api.NewOTLPReceiver(pchan, cfg)
a.OTLPReceiver = api.NewOTLPReceiver(pchan, cfg, metricsClient, timingReporter)
return &TraceAgent{
Agent: a,
exit: make(chan struct{}),
Expand Down
69 changes: 55 additions & 14 deletions internal/datadog/metrics_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"sync"
"time"

"github.com/DataDog/datadog-agent/pkg/trace/metrics"
"github.com/DataDog/datadog-go/v5/statsd"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
Expand All @@ -20,19 +20,12 @@ type metricsClient struct {
mutex sync.Mutex
}

var initializeOnce sync.Once

// InitializeMetricClient using a meter provider. If the client has already been initialized,
// this function just returns the previous one.
func InitializeMetricClient(mp metric.MeterProvider) metrics.StatsClient {
initializeOnce.Do(func() {
m := &metricsClient{
meter: mp.Meter("datadog"),
gauges: make(map[string]float64),
}
metrics.Client = m
})
return metrics.Client
// InitializeMetricClient using a meter provider.
func InitializeMetricClient(mp metric.MeterProvider) statsd.ClientInterface {
return &metricsClient{
meter: mp.Meter("datadog"),
gauges: make(map[string]float64),
}
}
liustanley marked this conversation as resolved.
Show resolved Hide resolved

func (m *metricsClient) Gauge(name string, value float64, tags []string, _ float64) error {
Expand Down Expand Up @@ -89,10 +82,58 @@ func (m *metricsClient) Histogram(name string, value float64, tags []string, _ f
return nil
}

func (m *metricsClient) Distribution(name string, value float64, tags []string, rate float64) error {
return m.Histogram(name, value, tags, rate)
}

func (m *metricsClient) Timing(name string, value time.Duration, tags []string, rate float64) error {
return m.Histogram(name, float64(value.Milliseconds()), tags, rate)
}

func (m *metricsClient) Flush() error {
return nil
}

func (m *metricsClient) Decr(string, []string, float64) error {
return nil
}

func (m *metricsClient) Incr(string, []string, float64) error {
return nil
}

func (m *metricsClient) Set(string, string, []string, float64) error {
return nil
}

func (m *metricsClient) TimeInMilliseconds(string, float64, []string, float64) error {
return nil
}

func (m *metricsClient) Event(*statsd.Event) error {
return nil
}

func (m *metricsClient) SimpleEvent(string, string) error {
return nil
}

func (m *metricsClient) ServiceCheck(*statsd.ServiceCheck) error {
return nil
}

func (m *metricsClient) SimpleServiceCheck(string, statsd.ServiceCheckStatus) error {
return nil
}
liustanley marked this conversation as resolved.
Show resolved Hide resolved

func (m *metricsClient) Close() error {
return nil
}

func (m *metricsClient) IsClosed() bool {
return false
}

func (m *metricsClient) GetTelemetry() statsd.Telemetry {
return statsd.Telemetry{}
}
17 changes: 2 additions & 15 deletions internal/datadog/metrics_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ package datadog

import (
"context"
"sync"
"testing"
"time"

"github.com/DataDog/datadog-agent/pkg/trace/metrics"
"github.com/DataDog/datadog-go/v5/statsd"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -17,25 +16,13 @@ import (
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
)

func setupMetricClient() (*metric.ManualReader, metrics.StatsClient, *metric.MeterProvider) {
initializeOnce = sync.Once{}
func setupMetricClient() (*metric.ManualReader, statsd.ClientInterface, *metric.MeterProvider) {
reader := metric.NewManualReader()
meterProvider := metric.NewMeterProvider(metric.WithReader(reader))
metricClient := InitializeMetricClient(meterProvider)
return reader, metricClient, meterProvider
}

func TestNewMetricClient(t *testing.T) {
_, metricClient, _ := setupMetricClient()
assert.Equal(t, metrics.Client, metricClient)
}

func TestNewMetricClientComplex(t *testing.T) {
_, _, meterProvider := setupMetricClient()
metricClient2 := InitializeMetricClient(meterProvider)
assert.Equal(t, metrics.Client, metricClient2)
}

func TestGauge(t *testing.T) {
reader, metricClient, _ := setupMetricClient()

Expand Down
Loading