Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto detect connectiontype #424

Merged
merged 9 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions boostrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := setConnectionType(ddCfg, ConnectionTypeSocket)
assert.NoError(t, err)
assert.Equal(t, ConnectionTypeSocket, connectionType)

// Auto should detect
connectionType, err = setConnectionType(ddCfg, ConnectionTypeAuto)
assert.NoError(t, err)
assert.Equal(t, ConnectionTypeHTTP, connectionType)

// Fail on unable to detect when auto
ddCfg.APM = "tmp"
connectionType, err = setConnectionType(ddCfg, ConnectionTypeAuto)
assert.Error(t, err)
assert.Equal(t, ConnectionTypeAuto, connectionType)
}
29 changes: 29 additions & 0 deletions bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
AtzeDeVries marked this conversation as resolved.
Show resolved Hide resolved
)

// StartDatadog parallel process to collect data for Datadog.
Expand All @@ -43,6 +50,11 @@ func StartDatadog(cfg config.DatadogParameters, connectionType ConnectionType) e

compareConfigWithEnv(cfg)

connectionType, err = setConnectionType(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)
Expand Down Expand Up @@ -94,6 +106,23 @@ func GracefulDatadogShutdown() {
defer profiler.Stop()
}

func setConnectionType(cfg config.DatadogParameters, connectionType ConnectionType) (ConnectionType, error) {
AtzeDeVries marked this conversation as resolved.
Show resolved Hide resolved
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 {
Expand Down
Loading