-
Notifications
You must be signed in to change notification settings - Fork 48
[SLS-2330] Add support for universal instrumentation with the extension #116
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
Changes from all commits
adc5a9d
664915e
9a255d1
2b2351b
388731e
7421401
fe287c8
79a519b
4fe6817
5ba3dcf
047dc3b
73a5325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,12 +9,30 @@ | |
package extension | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/DataDog/datadog-lambda-go/internal/logger" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace" | ||
) | ||
|
||
type ddTraceContext string | ||
|
||
const ( | ||
DdTraceId ddTraceContext = "x-datadog-trace-id" | ||
DdParentId ddTraceContext = "x-datadog-parent-id" | ||
DdSpanId ddTraceContext = "x-datadog-span-id" | ||
DdSamplingPriority ddTraceContext = "x-datadog-sampling-priority" | ||
DdInvocationError ddTraceContext = "x-datadog-invocation-error" | ||
|
||
DdSeverlessSpan ddTraceContext = "dd-tracer-serverless-span" | ||
DdLambdaResponse ddTraceContext = "dd-response" | ||
) | ||
|
||
const ( | ||
|
@@ -23,30 +41,38 @@ const ( | |
// want to let it having some time for its cold start so we should not set this too low. | ||
timeout = 3000 * time.Millisecond | ||
|
||
helloUrl = "http://localhost:8124/lambda/hello" | ||
flushUrl = "http://localhost:8124/lambda/flush" | ||
helloUrl = "http://localhost:8124/lambda/hello" | ||
purple4reina marked this conversation as resolved.
Show resolved
Hide resolved
|
||
flushUrl = "http://localhost:8124/lambda/flush" | ||
startInvocationUrl = "http://localhost:8124/lambda/start-invocation" | ||
endInvocationUrl = "http://localhost:8124/lambda/end-invocation" | ||
|
||
extensionPath = "/opt/extensions/datadog-agent" | ||
) | ||
|
||
type ExtensionManager struct { | ||
helloRoute string | ||
flushRoute string | ||
extensionPath string | ||
httpClient HTTPClient | ||
isExtensionRunning bool | ||
helloRoute string | ||
flushRoute string | ||
extensionPath string | ||
startInvocationUrl string | ||
endInvocationUrl string | ||
httpClient HTTPClient | ||
isExtensionRunning bool | ||
isUniversalInstrumentation bool | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looked into adding |
||
} | ||
|
||
type HTTPClient interface { | ||
Do(req *http.Request) (*http.Response, error) | ||
} | ||
|
||
func BuildExtensionManager() *ExtensionManager { | ||
func BuildExtensionManager(isUniversalInstrumentation bool) *ExtensionManager { | ||
em := &ExtensionManager{ | ||
helloRoute: helloUrl, | ||
flushRoute: flushUrl, | ||
extensionPath: extensionPath, | ||
httpClient: &http.Client{Timeout: timeout}, | ||
helloRoute: helloUrl, | ||
flushRoute: flushUrl, | ||
startInvocationUrl: startInvocationUrl, | ||
endInvocationUrl: endInvocationUrl, | ||
extensionPath: extensionPath, | ||
httpClient: &http.Client{Timeout: timeout}, | ||
isUniversalInstrumentation: isUniversalInstrumentation, | ||
} | ||
em.checkAgentRunning() | ||
return em | ||
|
@@ -57,15 +83,81 @@ func (em *ExtensionManager) checkAgentRunning() { | |
logger.Debug("Will use the API") | ||
em.isExtensionRunning = false | ||
} else { | ||
req, _ := http.NewRequest(http.MethodGet, em.helloRoute, nil) | ||
if response, err := em.httpClient.Do(req); err == nil && response.StatusCode == 200 { | ||
DylanLovesCoffee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
logger.Debug("Will use the Serverless Agent") | ||
em.isExtensionRunning = true | ||
} else { | ||
logger.Debug("Will use the API since the Serverless Agent was detected but the hello route was unreachable") | ||
em.isExtensionRunning = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am wondering if you think we should still make this call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha yup, good catch. I made a note to add this back but totally forgot 👍 side note: |
||
logger.Debug("Will use the Serverless Agent") | ||
em.isExtensionRunning = true | ||
|
||
// Tell the extension not to create an execution span if universal instrumentation is disabled | ||
if !em.isUniversalInstrumentation { | ||
req, _ := http.NewRequest(http.MethodGet, em.helloRoute, nil) | ||
if response, err := em.httpClient.Do(req); err == nil && response.StatusCode == 200 { | ||
logger.Debug("Hit the extension /hello route") | ||
} else { | ||
logger.Debug("Will use the API since the Serverless Agent was detected but the hello route was unreachable") | ||
em.isExtensionRunning = false | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, if there was an error hitting the hello route, we'd set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woops, adding that back in! |
||
} | ||
} | ||
} | ||
|
||
func (em *ExtensionManager) SendStartInvocationRequest(ctx context.Context, eventPayload json.RawMessage) context.Context { | ||
body := bytes.NewBuffer(eventPayload) | ||
req, _ := http.NewRequest(http.MethodPost, em.startInvocationUrl, body) | ||
|
||
if response, err := em.httpClient.Do(req); err == nil && response.StatusCode == 200 { | ||
// Propagate dd-trace context from the extension response if found in the response headers | ||
traceId := response.Header.Get(string(DdTraceId)) | ||
if traceId != "" { | ||
ctx = context.WithValue(ctx, DdTraceId, traceId) | ||
} | ||
parentId := response.Header.Get(string(DdParentId)) | ||
if parentId != "" { | ||
ctx = context.WithValue(ctx, DdParentId, parentId) | ||
} | ||
samplingPriority := response.Header.Get(string(DdSamplingPriority)) | ||
if samplingPriority != "" { | ||
ctx = context.WithValue(ctx, DdSamplingPriority, samplingPriority) | ||
} | ||
} | ||
return ctx | ||
} | ||
|
||
func (em *ExtensionManager) SendEndInvocationRequest(ctx context.Context, functionExecutionSpan ddtrace.Span, err error) { | ||
// Handle Lambda response | ||
lambdaResponse := ctx.Value(DdLambdaResponse) | ||
content, responseErr := json.Marshal(lambdaResponse) | ||
if responseErr != nil { | ||
content = []byte("{}") | ||
} | ||
body := bytes.NewBuffer(content) | ||
req, _ := http.NewRequest(http.MethodPost, em.endInvocationUrl, body) | ||
|
||
// Mark the invocation as an error if any | ||
if err != nil { | ||
req.Header.Set(string(DdInvocationError), "true") | ||
} | ||
|
||
// Extract the DD trace context and pass them to the extension via request headers | ||
traceId, ok := ctx.Value(DdTraceId).(string) | ||
if ok { | ||
req.Header.Set(string(DdTraceId), traceId) | ||
if parentId, ok := ctx.Value(DdParentId).(string); ok { | ||
req.Header.Set(string(DdParentId), parentId) | ||
} | ||
if spanId, ok := ctx.Value(DdSpanId).(string); ok { | ||
req.Header.Set(string(DdSpanId), spanId) | ||
} | ||
if samplingPriority, ok := ctx.Value(DdSamplingPriority).(string); ok { | ||
req.Header.Set(string(DdSamplingPriority), samplingPriority) | ||
} | ||
} else { | ||
req.Header.Set(string(DdTraceId), fmt.Sprint(functionExecutionSpan.Context().TraceID())) | ||
req.Header.Set(string(DdSpanId), fmt.Sprint(functionExecutionSpan.Context().SpanID())) | ||
} | ||
|
||
resp, err := em.httpClient.Do(req) | ||
if err != nil || resp.StatusCode != 200 { | ||
logger.Error(fmt.Errorf("could not send end invocation payload to the extension: %v", err)) | ||
} | ||
} | ||
|
||
func (em *ExtensionManager) IsExtensionRunning() bool { | ||
|
Uh oh!
There was an error while loading. Please reload this page.