From 1cffd2b033e39fc326595a536f1d018f9fcd07cd Mon Sep 17 00:00:00 2001 From: Michael Parker Date: Wed, 4 Sep 2024 14:31:30 -0500 Subject: [PATCH] fix: correctly parse environment variables when parsing datadog tracer config --- internal/gitdb/tracing/datadog/datadog.go | 2 +- .../gitdb/tracing/datadog/datadog_test.go | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 internal/gitdb/tracing/datadog/datadog_test.go diff --git a/internal/gitdb/tracing/datadog/datadog.go b/internal/gitdb/tracing/datadog/datadog.go index 60dcbf7..d87ab4a 100644 --- a/internal/gitdb/tracing/datadog/datadog.go +++ b/internal/gitdb/tracing/datadog/datadog.go @@ -51,7 +51,7 @@ func (c *config) statsFile() string { func envToStruct(env []string, into interface{}) error { m := make(map[string]string) for _, e := range env { - p := strings.SplitN(e, "=", 1) + p := strings.SplitN(e, "=", 2) if len(p) != 2 { continue } diff --git a/internal/gitdb/tracing/datadog/datadog_test.go b/internal/gitdb/tracing/datadog/datadog_test.go new file mode 100644 index 0000000..dfeec27 --- /dev/null +++ b/internal/gitdb/tracing/datadog/datadog_test.go @@ -0,0 +1,50 @@ +package datadog + +import ( + "testing" +) + +func TestEnvToStruct_ValidEnv(t *testing.T) { + env := []string{"DD_APM_RECEIVER_ADDR=localhost:8126", "DD_APM_RECEIVER_SOCKET=/var/run/datadog/apm.socket"} + var cfg config + err := envToStruct(env, &cfg) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if cfg.ApmAddress != "localhost:8126" { + t.Errorf("expected ApmAddress to be 'localhost:8126', got %s", cfg.ApmAddress) + } + if cfg.ApmFile != "/var/run/datadog/apm.socket" { + t.Errorf("expected ApmFile to be '/var/run/datadog/apm.socket', got %s", cfg.ApmFile) + } +} + +func TestEnvToStruct_EmptyEnv(t *testing.T) { + var env []string + var cfg config + err := envToStruct(env, &cfg) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if cfg.ApmAddress != "" { + t.Errorf("expected ApmAddress to be empty, got %s", cfg.ApmAddress) + } + if cfg.ApmFile != "" { + t.Errorf("expected ApmFile to be empty, got %s", cfg.ApmFile) + } +} + +func TestEnvToStruct_InvalidEnvFormat(t *testing.T) { + env := []string{"INVALID_ENV"} + var cfg config + err := envToStruct(env, &cfg) + if err != nil { + t.Fatalf("expected no error, got %v", err) + } + if cfg.ApmAddress != "" { + t.Errorf("expected ApmAddress to be empty, got %s", cfg.ApmAddress) + } + if cfg.ApmFile != "" { + t.Errorf("expected ApmFile to be empty, got %s", cfg.ApmFile) + } +}