Skip to content

Commit 047dc3b

Browse files
clean up env var check
1 parent 5ba3dcf commit 047dc3b

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

ddlambda.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,14 @@ func initializeListeners(cfg *Config) []wrapper.HandlerListener {
220220
if strings.EqualFold(logLevel, "debug") || (cfg != nil && cfg.DebugLogging) {
221221
logger.SetLogLevel(logger.LevelDebug)
222222
}
223-
extensionManager := extension.BuildExtensionManager()
223+
traceConfig := cfg.toTraceConfig()
224+
extensionManager := extension.BuildExtensionManager(traceConfig.UniversalInstrumentation)
224225
isExtensionRunning := extensionManager.IsExtensionRunning()
226+
metricsConfig := cfg.toMetricsConfig(isExtensionRunning)
225227

226228
// Wrap the handler with listeners that add instrumentation for traces and metrics.
227-
tl := trace.MakeListener(cfg.toTraceConfig(), extensionManager)
228-
ml := metrics.MakeListener(cfg.toMetricsConfig(isExtensionRunning), extensionManager)
229+
tl := trace.MakeListener(traceConfig, extensionManager)
230+
ml := metrics.MakeListener(metricsConfig, extensionManager)
229231
return []wrapper.HandlerListener{
230232
&tl, &ml,
231233
}

internal/extension/extension.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ import (
1515
"fmt"
1616
"net/http"
1717
"os"
18-
"strconv"
1918
"time"
2019

2120
"github.com/DataDog/datadog-lambda-go/internal/logger"
21+
2222
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
2323
)
2424

@@ -50,27 +50,29 @@ const (
5050
)
5151

5252
type ExtensionManager struct {
53-
helloRoute string
54-
flushRoute string
55-
extensionPath string
56-
startInvocationUrl string
57-
endInvocationUrl string
58-
httpClient HTTPClient
59-
isExtensionRunning bool
53+
helloRoute string
54+
flushRoute string
55+
extensionPath string
56+
startInvocationUrl string
57+
endInvocationUrl string
58+
httpClient HTTPClient
59+
isExtensionRunning bool
60+
isUniversalInstrumentation bool
6061
}
6162

6263
type HTTPClient interface {
6364
Do(req *http.Request) (*http.Response, error)
6465
}
6566

66-
func BuildExtensionManager() *ExtensionManager {
67+
func BuildExtensionManager(isUniversalInstrumentation bool) *ExtensionManager {
6768
em := &ExtensionManager{
68-
helloRoute: helloUrl,
69-
flushRoute: flushUrl,
70-
startInvocationUrl: startInvocationUrl,
71-
endInvocationUrl: endInvocationUrl,
72-
extensionPath: extensionPath,
73-
httpClient: &http.Client{Timeout: timeout},
69+
helloRoute: helloUrl,
70+
flushRoute: flushUrl,
71+
startInvocationUrl: startInvocationUrl,
72+
endInvocationUrl: endInvocationUrl,
73+
extensionPath: extensionPath,
74+
httpClient: &http.Client{Timeout: timeout},
75+
isUniversalInstrumentation: isUniversalInstrumentation,
7476
}
7577
em.checkAgentRunning()
7678
return em
@@ -85,8 +87,7 @@ func (em *ExtensionManager) checkAgentRunning() {
8587
em.isExtensionRunning = true
8688

8789
// Tell the extension not to create an execution span if universal instrumentation is disabled
88-
isUniversalInstrumentation, _ := strconv.ParseBool(os.Getenv("DD_UNIVERSAL_INSTRUMENTATION"))
89-
if !isUniversalInstrumentation {
90+
if !em.isUniversalInstrumentation {
9091
req, _ := http.NewRequest(http.MethodGet, em.helloRoute, nil)
9192
if response, err := em.httpClient.Do(req); err == nil && response.StatusCode == 200 {
9293
logger.Debug("Hit the extension /hello route")

internal/extension/extension_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ func captureLog(f func()) string {
7272
}
7373

7474
func TestBuildExtensionManager(t *testing.T) {
75-
em := BuildExtensionManager()
75+
em := BuildExtensionManager(false)
7676
assert.Equal(t, "http://localhost:8124/lambda/hello", em.helloRoute)
7777
assert.Equal(t, "http://localhost:8124/lambda/flush", em.flushRoute)
7878
assert.Equal(t, "http://localhost:8124/lambda/start-invocation", em.startInvocationUrl)
7979
assert.Equal(t, "http://localhost:8124/lambda/end-invocation", em.endInvocationUrl)
8080
assert.Equal(t, "/opt/extensions/datadog-agent", em.extensionPath)
81+
assert.Equal(t, false, em.isUniversalInstrumentation)
8182
assert.NotNil(t, em.httpClient)
8283
}
8384

0 commit comments

Comments
 (0)