Skip to content

Commit 212a26b

Browse files
authored
Merge branch 'main' into joey/ddtracegoV2
2 parents b026979 + c0b16b2 commit 212a26b

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

internal/extension/extension.go

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"encoding/base64"
1515
"encoding/json"
1616
"fmt"
17+
"io"
1718
"net/http"
1819
"os"
1920
"reflect"
@@ -97,7 +98,14 @@ func (em *ExtensionManager) checkAgentRunning() {
9798
// Tell the extension not to create an execution span if universal instrumentation is disabled
9899
if !em.isUniversalInstrumentation {
99100
req, _ := http.NewRequest(http.MethodGet, em.helloRoute, nil)
100-
if response, err := em.httpClient.Do(req); err == nil && response.StatusCode == 200 {
101+
response, err := em.httpClient.Do(req)
102+
if response != nil && response.Body != nil {
103+
defer func() {
104+
_, _ = io.Copy(io.Discard, response.Body)
105+
response.Body.Close()
106+
}()
107+
}
108+
if err == nil && response.StatusCode == 200 {
101109
logger.Debug("Hit the extension /hello route")
102110
} else {
103111
logger.Debug("Will use the API since the Serverless Agent was detected but the hello route was unreachable")
@@ -111,7 +119,15 @@ func (em *ExtensionManager) SendStartInvocationRequest(ctx context.Context, even
111119
body := bytes.NewBuffer(eventPayload)
112120
req, _ := http.NewRequest(http.MethodPost, em.startInvocationUrl, body)
113121

114-
if response, err := em.httpClient.Do(req); err == nil && response.StatusCode == 200 {
122+
response, err := em.httpClient.Do(req)
123+
if response != nil && response.Body != nil {
124+
defer func() {
125+
_, _ = io.Copy(io.Discard, response.Body)
126+
response.Body.Close()
127+
}()
128+
}
129+
if err == nil && response.StatusCode == 200 {
130+
115131
// Propagate dd-trace context from the extension response if found in the response headers
116132
traceId := response.Header.Get(string(DdTraceId))
117133
if traceId != "" {
@@ -172,11 +188,21 @@ func (em *ExtensionManager) SendEndInvocationRequest(ctx context.Context, functi
172188
if priority, ok := spanContext.SamplingPriority(); ok {
173189
req.Header.Set(string(DdSamplingPriority), fmt.Sprint(priority))
174190
} else {
175-
logger.Error(fmt.Errorf("could not get sampling priority from spanContext.SamplingPriority()"))
191+
if priority, ok := getSamplingPriority(functionExecutionSpan); ok {
192+
req.Header.Set(string(DdSamplingPriority), fmt.Sprint(priority))
193+
} else {
194+
logger.Error(fmt.Errorf("could not get sampling priority from getSamplingPriority()"))
195+
}
176196
}
177197
}
178198

179199
resp, err := em.httpClient.Do(req)
200+
if resp != nil && resp.Body != nil {
201+
defer func() {
202+
_, _ = io.Copy(io.Discard, resp.Body)
203+
resp.Body.Close()
204+
}()
205+
}
180206
if err != nil || resp.StatusCode != 200 {
181207
logger.Error(fmt.Errorf("could not send end invocation payload to the extension: %v", err))
182208
}
@@ -225,7 +251,14 @@ func (em *ExtensionManager) IsExtensionRunning() bool {
225251

226252
func (em *ExtensionManager) Flush() error {
227253
req, _ := http.NewRequest(http.MethodGet, em.flushRoute, nil)
228-
if response, err := em.httpClient.Do(req); err != nil {
254+
response, err := em.httpClient.Do(req)
255+
if response != nil && response.Body != nil {
256+
defer func() {
257+
_, _ = io.Copy(io.Discard, response.Body)
258+
response.Body.Close()
259+
}()
260+
}
261+
if err != nil {
229262
err := fmt.Errorf("was not able to reach the Agent to flush: %s", err)
230263
logger.Error(err)
231264
return err
@@ -236,3 +269,40 @@ func (em *ExtensionManager) Flush() error {
236269
}
237270
return nil
238271
}
272+
273+
274+
// The SamplingPriority method is directly available in dd-trace-go <=v1.73.1 or dd-trace-go v2.
275+
// But for dd-trace-go v1.74.x, reflection is needed to access the SamplingPriority method because
276+
// the method hidden in the v2 SpanContextV2Adapter struct.
277+
func getSamplingPriority(span ddtrace.Span) (int, bool) {
278+
// Get the span context
279+
ctx := span.Context()
280+
281+
// Use reflection to access the underlying v2 SpanContext
282+
ctxValue := reflect.ValueOf(ctx)
283+
if ctxValue.Type().String() != "internal.SpanContextV2Adapter" {
284+
return 0, false
285+
}
286+
287+
// Get the Ctx field (the underlying v2.SpanContext)
288+
ctxField := ctxValue.FieldByName("Ctx")
289+
if !ctxField.IsValid() {
290+
return 0, false
291+
}
292+
293+
// Call SamplingPriority() on the underlying v2 SpanContext
294+
method := ctxField.MethodByName("SamplingPriority")
295+
if !method.IsValid() {
296+
return 0, false
297+
}
298+
299+
results := method.Call([]reflect.Value{})
300+
if len(results) != 2 {
301+
return 0, false
302+
}
303+
304+
priority := int(results[0].Int())
305+
ok := results[1].Bool()
306+
return priority, ok
307+
}
308+

internal/metrics/listener.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func MakeListener(config Config, extensionManager *extension.ExtensionManager) L
9797
// Agent instead of using this "discovery" implementation.
9898
if extensionManager.IsExtensionRunning() {
9999
var err error
100-
if statsdClient, err = statsd.New("127.0.0.1:8125"); err != nil {
100+
if statsdClient, err = statsd.New("127.0.0.1:8125", statsd.WithoutTelemetry()); err != nil {
101101
statsdClient = nil // force nil if an error occurred during statsd client init
102102
}
103103
}

internal/version/version.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package version
33

44
// DDLambdaVersion is the current version number of this library (datadog-lambda-go).
55
// It is automatically updated by the release script.
6-
const DDLambdaVersion = "1.23.0"
6+
const DDLambdaVersion = "1.25.1"
77

88
// DDTraceVersion is the version of dd-trace-go required by this libary.
99
// This should match the version number specified in go.mod.
1010
// It is automatically updated by the release script
11-
const DDTraceVersion = "1.72.1"
11+
const DDTraceVersion = "1.73.1"

0 commit comments

Comments
 (0)