Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
DarcyRaynerDD committed May 22, 2019
1 parent 8845061 commit ecfddc3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
44 changes: 31 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,49 @@
# dd-lambda-go

Datadog's Lambda Go client library enables distributed tracing between serverful and serverless environments.

## Installation

```bash
go get github.com/DataDog/dd-lambda-go
```

## Usage

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.
Datadog needs to be able to read headers from the incoming Lambda event. Wrap your Lambda handler function like so:

```go
package main

import (
"github.com/aws/aws-lambda-go/lambda"
"github.com/DataDog/dd-lambda-go"
"github.com/aws/aws-lambda-go/lambda"
"github.com/DataDog/dd-lambda-go"
)

func main() {
// Wrap your lambda handler like this
lambda.Start( ddlambda.WrapHandler(myHandler))
// 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)
Make sure any outbound requests have Datadog's tracing headers.

return "Success", nil
```go
req, err := http.NewRequest("GET", "http://api.youcompany.com/status")
// Use the same Context object given to your lambda handler.
ddlambda.AddTraceHeaders(ctx, req)

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

## Non-proxy integration

If your Lambda function is triggered by API Gateway via the non-proxy integration, then you have to set up a mapping template, which passes the Datadog trace context from the incoming HTTP request headers to the Lambda function via the event object.

If your Lambda function is deployed by the Serverless Framework, such a mapping template gets created by default.
4 changes: 2 additions & 2 deletions ddlambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func GetTraceHeaders(ctx context.Context) map[string]string {
}

// AddTraceHeaders adds DataDog trace headers to a HTTP Request
func AddTraceHeaders(req *http.Request) {
headers := GetTraceHeaders(req.Context())
func AddTraceHeaders(ctx context.Context, req *http.Request) {
headers := GetTraceHeaders(ctx)
for key, value := range headers {
req.Header.Add(key, value)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/trace/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func unmarshalEventForTraceContext(ev json.RawMessage) (map[string]string, bool)
func convertTraceContextFromXRay(ctx context.Context) (map[string]string, error) {
traceContext := map[string]string{}

header := getLambdaTraceHeaderFromContext(ctx)
header := getXrayTraceHeaderFromContext(ctx)
if header == nil {
return traceContext, fmt.Errorf("xray segment doesn't exist, couldn't read trace context")
}
Expand All @@ -137,10 +137,10 @@ func convertTraceContextFromXRay(ctx context.Context) (map[string]string, error)
return traceContext, nil
}

// getLambdaTraceHeaderFromContext is used to extract xray segment metadata from the lambda context object.
// getXrayTraceHeaderFromContext is used to extract xray segment metadata from the lambda context object.
// By default, the context object won't have any Segment, (xray.GetSegment(ctx) will return nil). However it
// will have the "LambdaTraceHeader" object, which contains the traceID/parentID/sampling info.
func getLambdaTraceHeaderFromContext(ctx context.Context) *header.Header {
func getXrayTraceHeaderFromContext(ctx context.Context) *header.Header {
var traceHeader string

if traceHeaderValue := ctx.Value(xray.LambdaTraceHeaderKey); traceHeaderValue != nil {
Expand Down

0 comments on commit ecfddc3

Please sign in to comment.