-
Notifications
You must be signed in to change notification settings - Fork 47
[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 1 commit
adc5a9d
664915e
9a255d1
2b2351b
388731e
7421401
fe287c8
79a519b
4fe6817
5ba3dcf
047dc3b
73a5325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,12 +9,29 @@ | |||||
| 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,8 +40,10 @@ 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" | ||||||
| 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" | ||||||
| ) | ||||||
|
|
@@ -33,6 +52,8 @@ type ExtensionManager struct { | |||||
| helloRoute string | ||||||
| flushRoute string | ||||||
| extensionPath string | ||||||
| startInvocationUrl string | ||||||
| endInvocationUrl string | ||||||
| httpClient HTTPClient | ||||||
| isExtensionRunning bool | ||||||
| } | ||||||
|
|
@@ -43,10 +64,12 @@ type HTTPClient interface { | |||||
|
|
||||||
| func BuildExtensionManager() *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}, | ||||||
| } | ||||||
| em.checkAgentRunning() | ||||||
| return em | ||||||
|
|
@@ -57,14 +80,71 @@ 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 | ||||||
|
Contributor
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
Contributor
Author
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 | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| 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.Values(string(DdTraceId)) | ||||||
DylanLovesCoffee marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| if len(traceId) > 0 { | ||||||
|
||||||
| if len(traceId) > 0 { | |
| if traceId != "" { |
Nit pick. In case you decide to use response.Header.Get, this is more performant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the record, I actually looked this up. It turns out that go compiles these two options to the same byte code. So, my recommendation here was in fact incorrect.
myStr := "hello world!"
// this line
if len(myStr) > 0 {
...
}
// compiles to the same byte code as this line
if myStr != "" {
...
}
Uh oh!
There was an error while loading. Please reload this page.