Skip to content

Commit

Permalink
fix: correctly parse environment variables when parsing datadog trace…
Browse files Browse the repository at this point in the history
…r config (#212)
  • Loading branch information
mrparkers authored Sep 4, 2024
1 parent bb0e441 commit 9f28185
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/gitdb/tracing/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
50 changes: 50 additions & 0 deletions internal/gitdb/tracing/datadog/datadog_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 9f28185

Please sign in to comment.