Skip to content

Commit

Permalink
feat: auto detect connectiontype (#424)
Browse files Browse the repository at this point in the history
* feat: Automaticly detect connection type for APM conneciton

This is a preview PR for a simple attempt to detect connection socket
type. This will not break the current API.

* tests are not added yet.

fixes: #423

* fix fmt, fix input move return

* return inputted connection type on error

* add test for testSetConnection

* fix fmt

* add deprecation

* fix linting

* fix double deprecated statement

Co-authored-by: Filip Stawicki <[email protected]>

* rename setConnectionType to determineConnectionType

---------

Co-authored-by: Filip Stawicki <[email protected]>
  • Loading branch information
AtzeDeVries and hawi74 authored Nov 14, 2024
1 parent 9ae156b commit 59fe6e7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
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 := 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)
}
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
)

// 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 = 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)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 59fe6e7

Please sign in to comment.