Skip to content

Commit bdb3b03

Browse files
authored
[OTAGENT-206] Add otel collector configs to Agent's config cmd output (#33766)
1 parent 3fb51b5 commit bdb3b03

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

pkg/cli/subcommands/config/command.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
package config
88

99
import (
10+
"encoding/json"
1011
"fmt"
12+
"net/http"
13+
"strings"
1114

1215
"go.uber.org/fx"
1316

1417
"github.com/DataDog/datadog-agent/comp/core"
1518
"github.com/DataDog/datadog-agent/comp/core/config"
1619
log "github.com/DataDog/datadog-agent/comp/core/log/def"
20+
ddflareextension "github.com/DataDog/datadog-agent/comp/otelcol/ddflareextension/def"
1721
"github.com/DataDog/datadog-agent/pkg/api/util"
1822
"github.com/DataDog/datadog-agent/pkg/config/settings"
1923
"github.com/DataDog/datadog-agent/pkg/util/fxutil"
@@ -117,11 +121,48 @@ func showRuntimeConfiguration(_ log.Component, config config.Component, cliParam
117121
return err
118122
}
119123

124+
if config.GetBool("otelcollector.enabled") && config.GetBool("otelcollector.converter.enabled") {
125+
runtimeConfig, err = insertOTelCollectorConfig(config.GetString("otelcollector.extension_url"), runtimeConfig, c.HTTPClient())
126+
if err != nil {
127+
return err
128+
}
129+
}
130+
120131
fmt.Println(runtimeConfig)
121132

122133
return nil
123134
}
124135

136+
func insertOTelCollectorConfig(extensionURL string, runtimeConfig string, httpClient *http.Client) (string, error) {
137+
resp, err := util.DoGet(httpClient, extensionURL, util.CloseConnection)
138+
if err != nil {
139+
return runtimeConfig, err
140+
}
141+
var extensionResp ddflareextension.Response
142+
if err = json.Unmarshal(resp, &extensionResp); err != nil {
143+
return runtimeConfig, err
144+
}
145+
otelRuntimeCfg := extensionResp.RuntimeConfig
146+
var sb strings.Builder
147+
for _, cfg := range strings.Split(runtimeConfig, "\n") {
148+
sb.WriteString(cfg)
149+
sb.WriteString("\n")
150+
if cfg == "otelcollector:" {
151+
sb.WriteString(" config:")
152+
sb.WriteString("\n")
153+
for _, otcfg := range strings.Split(otelRuntimeCfg, "\n") {
154+
if otcfg == "" {
155+
continue
156+
}
157+
sb.WriteString(" ")
158+
sb.WriteString(otcfg)
159+
sb.WriteString("\n")
160+
}
161+
}
162+
}
163+
return sb.String(), nil
164+
}
165+
125166
func listRuntimeConfigurableValue(_ log.Component, config config.Component, cliParams *cliParams) error {
126167
err := util.SetAuthToken(config)
127168
if err != nil {

pkg/config/settings/api.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package settings
77

88
import (
9+
"net/http"
10+
911
"github.com/DataDog/datadog-agent/comp/core/settings"
1012
"github.com/spf13/cobra"
1113
)
@@ -18,6 +20,7 @@ type Client interface {
1820
List() (map[string]settings.RuntimeSettingResponse, error)
1921
FullConfig() (string, error)
2022
FullConfigBySource() (string, error)
23+
HTTPClient() *http.Client
2124
}
2225

2326
// ClientBuilder represents a function returning a runtime settings API client

pkg/config/settings/http/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,7 @@ func (rc *runtimeSettingsHTTPClient) Set(key string, value string) (bool, error)
142142
}
143143
return hidden, nil
144144
}
145+
146+
func (rc *runtimeSettingsHTTPClient) HTTPClient() *http.Client {
147+
return rc.c
148+
}

test/new-e2e/tests/otel/otel-agent/minimal_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,53 @@ func (s *minimalTestSuite) TestCoreAgentStatus() {
100100
func (s *minimalTestSuite) TestOTelAgentStatus() {
101101
utils.TestOTelAgentStatusCmd(s)
102102
}
103+
104+
func (s *minimalTestSuite) TestCoreAgentConfigCmd() {
105+
const expectedCfg = ` service:
106+
extensions:
107+
- pprof/dd-autoconfigured
108+
- zpages/dd-autoconfigured
109+
- health_check/dd-autoconfigured
110+
- ddflare/dd-autoconfigured
111+
pipelines:
112+
logs:
113+
exporters:
114+
- datadog
115+
processors:
116+
- batch
117+
- infraattributes/dd-autoconfigured
118+
receivers:
119+
- otlp
120+
metrics:
121+
exporters:
122+
- datadog
123+
processors:
124+
- batch
125+
- infraattributes/dd-autoconfigured
126+
receivers:
127+
- otlp
128+
- datadog/connector
129+
metrics/dd-autoconfigured/datadog:
130+
exporters:
131+
- datadog
132+
processors: []
133+
receivers:
134+
- prometheus/dd-autoconfigured
135+
traces:
136+
exporters:
137+
- datadog/connector
138+
processors:
139+
- batch
140+
- infraattributes/dd-autoconfigured
141+
receivers:
142+
- otlp
143+
traces/send:
144+
exporters:
145+
- datadog
146+
processors:
147+
- batch
148+
- infraattributes/dd-autoconfigured
149+
receivers:
150+
- otlp`
151+
utils.TestCoreAgentConfigCmd(s, expectedCfg)
152+
}

test/new-e2e/tests/otel/utils/config_utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,18 @@ func validateStatus(t *testing.T, status string) {
267267
require.Contains(t, status, "Metric Points Sent:")
268268
require.Contains(t, status, "Log Records Sent:")
269269
}
270+
271+
// TestCoreAgentConfigCmd tests the output of core agent's config command contains the embedded collector's config
272+
func TestCoreAgentConfigCmd(s OTelTestSuite, expectedCfg string) {
273+
err := s.Env().FakeIntake.Client().FlushServerAndResetAggregators()
274+
require.NoError(s.T(), err)
275+
agent := getAgentPod(s)
276+
277+
s.T().Log("Calling config command in core agent")
278+
stdout, stderr, err := s.Env().KubernetesCluster.KubernetesClient.PodExec("datadog", agent.Name, "agent", []string{"agent", "config"})
279+
require.NoError(s.T(), err, "Failed to execute config")
280+
require.Empty(s.T(), stderr)
281+
require.NotNil(s.T(), stdout)
282+
s.T().Log("Full output of config command in core agent\n", stdout)
283+
assert.Contains(s.T(), stdout, expectedCfg)
284+
}

0 commit comments

Comments
 (0)