Skip to content

Commit

Permalink
Add AddTraceHeaders helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
DarcyRaynerDD committed May 21, 2019
1 parent 29f5da6 commit 28edc04
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Usage

Datadog needs to be able to read context from the incoming Lambda event, in order to enable distributed tracing.
Datadog needs to be able to read headers from the incoming Lambda event, in order to add datadog metadata to the go context.
Wrap your lambda handler like so.

```go
Expand All @@ -13,11 +13,19 @@ import (
"github.com/DataDog/dd-lambda-go"
)

func myHandler() (string, error) {
return "Success", nil
}

func main() {
// Wrap your lambda handler like this
lambda.Start( ddlambda.WrapHandler(myHandler))
}

func myHandler(ctx context.Context, event MyEvent) (string, error) {
// Add headers to outbound using the context object
req, err := http.NewRequest("GET", "http://api.youcompany.com/status")
ddlambda.AddTraceHeaders(ctx)

client := http.Client{}
client.Do(req)

return "Success", nil
}
```
9 changes: 9 additions & 0 deletions ddlambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ddlambda
import (
"context"
"encoding/json"
"net/http"

"github.com/DataDog/dd-lambda-go/internal/trace"
)
Expand All @@ -24,6 +25,14 @@ func GetTraceHeaders(ctx context.Context) map[string]string {
return result
}

// AddTraceHeaders adds DataDog trace headers to a HTTP Request
func AddTraceHeaders(req *http.Request) {
headers := GetTraceHeaders(req.Context())
for key, value := range headers {
req.Header.Add(key, value)
}
}

func (hl *handlerListener) HandlerStarted(ctx context.Context, msg json.RawMessage) context.Context {
ctx, _ = trace.ExtractTraceContext(ctx, msg)
return ctx
Expand Down

0 comments on commit 28edc04

Please sign in to comment.