Last active
June 13, 2019 23:59
-
-
Save underscorenygren/fba8cb8ed6a813be710eb3c3563ee617 to your computer and use it in GitHub Desktop.
XrayForLambda.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tracing | |
import ( | |
"context" | |
"encoding/json" | |
"fmt" | |
"os" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-sdk-go/aws/session" | |
"github.com/aws/aws-sdk-go/service/dynamodb" | |
"github.com/aws/aws-xray-sdk-go/xray" | |
"github.com/aws/aws-xray-sdk-go/xraylog" | |
) | |
func Handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { | |
lgr := xraylog.NewDefaultLogger(os.Stdout, xraylog.LogLevelDebug) | |
xray.SetLogger(lgr) | |
traceID := "" | |
headers := req.Headers | |
if headers != nil { | |
traceID = headers["X-Amzn-Trace-Id"] | |
} | |
fmt.Printf("context %v", ctx) | |
b, _ := json.Marshal(req) | |
fmt.Printf("req %s", b) | |
dynamo := dynamodb.New(session.Must(session.NewSession())) | |
xray.AWS(dynamo.Client) | |
if _, err := dynamo.ListTablesWithContext(ctx, &dynamodb.ListTablesInput{}); err != nil { | |
fmt.Printf("dynamo error: %s", err) | |
return events.APIGatewayProxyResponse{ | |
Body: "", | |
StatusCode: 400, | |
}, fmt.Errorf("failed listing tables") | |
} | |
return events.APIGatewayProxyResponse{ | |
Body: fmt.Sprintf("trace: %s", traceID), | |
StatusCode: 200, | |
}, nil | |
} | |
/* this needs to live in some main package | |
func main() { | |
lambda.Start(Handler) | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment