Skip to content

Commit 21fe278

Browse files
authored
[OTAGENT-375] otel-agent exits with 0 when disabled (#36500)
1 parent d0036d9 commit 21fe278

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

cmd/otel-agent/main_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed
2+
// under the Apache License Version 2.0.
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
// Copyright 2016-present Datadog, Inc.
5+
6+
//go:build otlp
7+
8+
package main
9+
10+
import (
11+
"os/exec"
12+
"testing"
13+
14+
"github.com/stretchr/testify/require"
15+
)
16+
17+
func TestExitCode_Disabled(t *testing.T) {
18+
t.Setenv("DD_OTELCOLLECTOR_ENABLED", "false")
19+
cmd := exec.Command("go", "run", "-tags", "otlp", "main.go", "--config", "test_config.yaml")
20+
err := cmd.Run()
21+
require.NoError(t, err)
22+
}

cmd/otel-agent/subcommands/run/command.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ func runOTelAgentCommand(ctx context.Context, params *subcommands.GlobalParams,
102102
if err != nil && err != agentConfig.ErrNoDDExporter {
103103
return err
104104
}
105+
if !acfg.GetBool("otelcollector.enabled") {
106+
fmt.Println("*** OpenTelemetry Collector is not enabled, exiting application ***. Set the config option `otelcollector.enabled` or the environment variable `DD_OTELCOLLECTOR_ENABLED` at true to enable it.")
107+
return nil
108+
}
105109
uris := append(params.ConfPaths, params.Sets...)
106110
if err == agentConfig.ErrNoDDExporter {
107111
return fxutil.Run(

cmd/otel-agent/subcommands/run/command_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
)
1717

1818
func TestFxRun_WithDatadogExporter(t *testing.T) {
19+
t.Setenv("DD_OTELCOLLECTOR_ENABLED", "true")
1920
fxutil.TestRun(t, func() error {
2021
ctx := context.Background()
2122
params := &subcommands.GlobalParams{
@@ -26,6 +27,7 @@ func TestFxRun_WithDatadogExporter(t *testing.T) {
2627
}
2728

2829
func TestFxRun_NoDatadogExporter(t *testing.T) {
30+
t.Setenv("DD_OTELCOLLECTOR_ENABLED", "true")
2931
fxutil.TestRun(t, func() error {
3032
ctx := context.Background()
3133
params := &subcommands.GlobalParams{

cmd/otel-agent/test_config.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
receivers:
2+
otlp:
3+
protocols:
4+
http:
5+
endpoint: "localhost:4318"
6+
grpc:
7+
endpoint: "localhost:4317"
8+
9+
exporters:
10+
datadog:
11+
api:
12+
key: "abc"
13+
14+
service:
15+
pipelines:
16+
traces:
17+
receivers: [otlp]
18+
exporters: [datadog]
19+
telemetry:
20+
metrics:
21+
readers:
22+
- pull:
23+
exporter:
24+
prometheus:
25+
host: "localhost"
26+
port: 8888
27+
without_scope_info: true
28+
without_type_suffix: true
29+
without_units: true

0 commit comments

Comments
 (0)