diff --git a/boostrap_test.go b/boostrap_test.go index 7938d9d9..250995d3 100644 --- a/boostrap_test.go +++ b/boostrap_test.go @@ -27,3 +27,30 @@ func TestDatadog(t *testing.T) { GracefulDatadogShutdown() } + +func TestSetConnectionType(t *testing.T) { + ddCfg := config.DatadogConfig{ + Env: "local", + Service: "Test-Go-Datadog-lib", + ServiceVersion: "na", + DSD: "unix:///tmp/", + APM: "http://localhost:3899", + EnableExtraProfiling: true, + } + + // If not auto it should just pass through + connectionType, err := determineConnectionType(ddCfg, ConnectionTypeSocket) + assert.NoError(t, err) + assert.Equal(t, ConnectionTypeSocket, connectionType) + + // Auto should detect + connectionType, err = determineConnectionType(ddCfg, ConnectionTypeAuto) + assert.NoError(t, err) + assert.Equal(t, ConnectionTypeHTTP, connectionType) + + // Fail on unable to detect when auto + ddCfg.APM = "tmp" + connectionType, err = determineConnectionType(ddCfg, ConnectionTypeAuto) + assert.Error(t, err) + assert.Equal(t, ConnectionTypeAuto, connectionType) +} diff --git a/bootstrap.go b/bootstrap.go index e3437dd6..ffaf08ec 100644 --- a/bootstrap.go +++ b/bootstrap.go @@ -3,6 +3,7 @@ package coopdatadog import ( "fmt" "os" + "strings" "github.com/coopnorge/go-datadog-lib/v2/config" "github.com/coopnorge/go-datadog-lib/v2/internal" @@ -19,9 +20,15 @@ type ConnectionType byte const ( // ConnectionTypeSocket sets the connection to Datadog to go throug a UNIX socket + // + // Deprecated: ConnectionTypeSocket. ConnectionTypeAuto should be used. ConnectionTypeSocket ConnectionType = iota // ConnectionTypeHTTP sets the connection to Datadog to go over HTTP + // + // Deprecated: ConnectionTypeHTTP. ConnectionTypeAuto should be used. ConnectionTypeHTTP + // ConnectionTypeAuto sets connection to HTTP or UNIX depending on supplied configuration of DD_TRACE_AGENT_URL + ConnectionTypeAuto ) // StartDatadog parallel process to collect data for Datadog. @@ -43,6 +50,11 @@ func StartDatadog(cfg config.DatadogParameters, connectionType ConnectionType) e compareConfigWithEnv(cfg) + connectionType, err = determineConnectionType(cfg, connectionType) + if err != nil { + return err + } + initTracer(cfg, connectionType) if initProfilerErr := initProfiler(cfg, connectionType); initProfilerErr != nil { return fmt.Errorf("Failed to start Datadog profiler: %w", initProfilerErr) @@ -94,6 +106,23 @@ func GracefulDatadogShutdown() { defer profiler.Stop() } +func determineConnectionType(cfg config.DatadogParameters, connectionType ConnectionType) (ConnectionType, error) { + switch connectionType { + case ConnectionTypeSocket: + return connectionType, nil + case ConnectionTypeHTTP: + return connectionType, nil + case ConnectionTypeAuto: + switch { + case strings.HasPrefix(cfg.GetApmEndpoint(), "http://"): + return ConnectionTypeHTTP, nil + case strings.HasPrefix(cfg.GetApmEndpoint(), "/"): + return ConnectionTypeSocket, nil + } + } + return connectionType, fmt.Errorf("Unable to automatically detect connection type based on DD_TRACE_AGENT_URL=%s", cfg.GetApmEndpoint()) +} + func initTracer(cfg config.DatadogParameters, connectionType ConnectionType) { var tracerOptions []tracer.StartOption switch connectionType {