Skip to content
10 changes: 9 additions & 1 deletion comp/otelcol/converter/impl/autoconfigure.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package converterimpl

import (
"context"
"slices"
"strings"

Expand All @@ -28,7 +29,7 @@ type component struct {
}

// Applies selected feature changes
func (c *ddConverter) enhanceConfig(conf *confmap.Conf) {
func (c *ddConverter) enhanceConfig(ctx context.Context, conf *confmap.Conf) {
var enabledFeatures []string
// If not specified, assume all features are enabled (ocb tests will not have coreConfig)
if c.coreConfig != nil {
Expand All @@ -55,12 +56,19 @@ func (c *ddConverter) enhanceConfig(conf *confmap.Conf) {
if c.coreConfig.GetBool("otelcollector.gateway.mode") {
deploymentType = "gateway"
}
resolvedHostname := ""
if c.hostname != nil {
if hostname, err := c.hostname.Get(ctx); err == nil {
resolvedHostname = hostname
}
}
extension.Config = map[string]any{
"api": map[string]any{
"key": c.coreConfig.GetString("api_key"),
"site": site,
},
"deployment_type": deploymentType,
"hostname": resolvedHostname,
}
}
addComponentToConfig(conf, extension)
Expand Down
10 changes: 7 additions & 3 deletions comp/otelcol/converter/impl/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"go.opentelemetry.io/collector/confmap"

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/hostname/hostnameinterface"
converter "github.com/DataDog/datadog-agent/comp/otelcol/converter/def"
)

type ddConverter struct {
coreConfig config.Component
hostname hostnameinterface.Component
logger *zap.Logger
}

Expand All @@ -36,7 +38,8 @@ var (
// the core config component is not available and the converter will not
// attempt to enhance the configuration using agent data.
type Requires struct {
Conf config.Component
Conf config.Component
Hostname hostnameinterface.Component
}

// NewFactory returns a new converter factory.
Expand All @@ -54,11 +57,12 @@ func newConverter(set confmap.ConverterSettings) confmap.Converter {
func NewConverterForAgent(reqs Requires) (converter.Component, error) {
return &ddConverter{
coreConfig: reqs.Conf,
hostname: reqs.Hostname,
}, nil
}

// Convert autoconfigures conf and stores both the provided and enhanced conf.
func (c *ddConverter) Convert(_ context.Context, conf *confmap.Conf) error {
c.enhanceConfig(conf)
func (c *ddConverter) Convert(ctx context.Context, conf *confmap.Conf) error {
c.enhanceConfig(ctx, conf)
return nil
}
34 changes: 33 additions & 1 deletion comp/otelcol/converter/impl/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ package converterimpl

import (
"context"
"errors"
"os"
"path/filepath"
"testing"

"github.com/DataDog/datadog-agent/comp/core/config"
"github.com/DataDog/datadog-agent/comp/core/hostname/hostnameinterface"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/confmap"
Expand All @@ -25,6 +27,23 @@ import (
"go.uber.org/zap"
)

type mockHostname struct {
hostname string
err error
}

func (m *mockHostname) Get(_ context.Context) (string, error) {
return m.hostname, m.err
}

func (m *mockHostname) GetWithProvider(_ context.Context) (hostnameinterface.Data, error) {
return hostnameinterface.Data{Hostname: m.hostname, Provider: "mock"}, m.err
}

func (m *mockHostname) GetSafe(_ context.Context) string {
return m.hostname
}

func uriFromFile(filename string) []string {
return []string{filepath.Join("testdata", filename)}
}
Expand Down Expand Up @@ -57,6 +76,7 @@ func TestConvert(t *testing.T) {
provided string
expectedResult string
agentConfig string
hostnameErr bool
}{
{
name: "extensions/ddflare-and-dd/datadog",
Expand Down Expand Up @@ -396,6 +416,13 @@ func TestConvert(t *testing.T) {
expectedResult: "features/no-defined-features/config-result.yaml",
agentConfig: "features/no-defined-features/acfg.yaml",
},
{
name: "extensions/no-extensions/dd-no-hostname",
provided: "extensions/no-extensions/dd-no-hostname/config.yaml",
expectedResult: "extensions/no-extensions/dd-no-hostname/config-result.yaml",
agentConfig: "extensions/no-extensions/dd-no-hostname/acfg.yaml",
hostnameErr: true,
},
}

for _, tc := range tests {
Expand All @@ -406,6 +433,11 @@ func TestConvert(t *testing.T) {
require.NoError(t, err)
acfg := config.NewMockFromYAML(t, string(f))
r.Conf = acfg
if tc.hostnameErr {
r.Hostname = &mockHostname{hostname: "", err: errors.New("hostname resolution failed")}
} else {
r.Hostname = &mockHostname{hostname: "test-host"}
}
}
converter, err := NewConverterForAgent(r)
assert.NoError(t, err)
Expand Down Expand Up @@ -455,7 +487,7 @@ func TestConvert(t *testing.T) {
func TestConvert_APIKeyFromEnvVar(t *testing.T) {
t.Setenv("DD_API_KEY", "123456")
t.Setenv("DD_SITE", "")
converter, err := NewConverterForAgent(Requires{config.NewMock(t)})
converter, err := NewConverterForAgent(Requires{Conf: config.NewMock(t), Hostname: &mockHostname{hostname: "test-host"}})
assert.NoError(t, err)

resolver, err := newResolver(uriFromFile("dd-core-cfg/apikey/unset-number/config.yaml"))
Expand Down
1 change: 1 addition & 0 deletions comp/otelcol/converter/impl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.25.0

require (
github.com/DataDog/datadog-agent/comp/core/config v0.64.0-devel
github.com/DataDog/datadog-agent/comp/core/hostname/hostnameinterface v0.73.0-devel.0.20251030121902-cd89eab046d6
github.com/DataDog/datadog-agent/comp/otelcol/converter/def v0.56.0-rc.3
github.com/stretchr/testify v1.11.1
go.opentelemetry.io/collector/confmap v1.53.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extensions:
key: "ggggg77777"
site: "datadoghq.com"
deployment_type: daemonset
hostname: test-host
ddflare/dd-autoconfigured:

service:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extensions:
key: ggggg77777
site: "datadoghq.com"
deployment_type: daemonset
hostname: test-host

service:
extensions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extensions:
key: ggggg77777
site: "datadoghq.com"
deployment_type: gateway
hostname: test-host

service:
extensions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extensions:
key: ggggg77777
site: "datadoghq.com"
deployment_type: daemonset
hostname: test-host

service:
extensions:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
api_key: ggggg77777
otelcollector:
converter:
features: [pprof, zpages, health_check, datadog]
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
receivers:
otlp:

exporters:
nop:

extensions:
pprof/dd-autoconfigured:
health_check/dd-autoconfigured:
zpages/dd-autoconfigured:
endpoint: "localhost:55679"
datadog/dd-autoconfigured:
api:
key: ggggg77777
site: "datadoghq.com"
deployment_type: daemonset
hostname: ""

service:
extensions:
[
pprof/dd-autoconfigured,
zpages/dd-autoconfigured,
health_check/dd-autoconfigured,
datadog/dd-autoconfigured,
]
pipelines:
traces:
receivers: [nop]
exporters: [nop]
metrics:
receivers: [nop]
exporters: [nop]
logs:
receivers: [nop]
exporters: [nop]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
receivers:
otlp:

exporters:
nop:

service:
pipelines:
traces:
receivers: [nop]
exporters: [nop]
metrics:
receivers: [nop]
exporters: [nop]
logs:
receivers: [nop]
exporters: [nop]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extensions:
key: ggggg77777
site: "us5.datadoghq.com"
deployment_type: daemonset
hostname: test-host

service:
extensions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ extensions:
key: ggggg77777
site: "datadoghq.com"
deployment_type: daemonset
hostname: test-host

service:
extensions:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extensions:
key: "ggggg77777"
site: "datadoghq.eu"
deployment_type: daemonset
hostname: test-host
ddflare/dd-autoconfigured:
health_check/dd-autoconfigured:
pprof/dd-autoconfigured:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ extensions:
key: "ggggg77777"
site: "datadoghq.eu"
deployment_type: daemonset
hostname: test-host
ddflare/dd-autoconfigured:
health_check/dd-autoconfigured:
pprof/dd-autoconfigured:
Expand Down
Loading