Skip to content
This repository has been archived by the owner on Aug 11, 2020. It is now read-only.

Commit

Permalink
X-Ray support improved
Browse files Browse the repository at this point in the history
  • Loading branch information
darccio committed Jan 25, 2018
1 parent 00a47f5 commit c24ca50
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
10 changes: 6 additions & 4 deletions gluo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ const testRequest = `{
"sourceIp":"127.0.0.1",
"accountId":null
},
"stage":"prod"
"stage":"prod",
"X-Amzn-Trace-Id":"Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=1"
},
"queryStringParameters":{
"foo":"bar"
Expand All @@ -66,7 +67,6 @@ const testRequest = `{
"Host":"1234567890.execute-api.us-east-1.amazonaws.com",
"X-Forwarded-Proto":"https",
"X-Amz-Cf-Id":"cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
"X-Amz-Trace-Id":"Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=1",
"CloudFront-Is-Tablet-Viewer":"false",
"Cache-Control":"max-age=0",
"User-Agent":"Custom User Agent String",
Expand All @@ -90,6 +90,7 @@ type helloRequest struct {
func TestLambdaServe(t *testing.T) {
la := LambdaAdapter{
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
buffer := new(bytes.Buffer)
buffer.ReadFrom(r.Body)
r.Body.Close()
Expand All @@ -104,12 +105,13 @@ func TestLambdaServe(t *testing.T) {
hr.Name = "stranger"
}
result := fmt.Sprintf("Hello, %s.", hr.Name)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(result))
}),
}
req := events.APIGatewayProxyRequest{}
json.Unmarshal([]byte(testRequest), &req)
if err := json.Unmarshal([]byte(testRequest), &req); err != nil {
t.Errorf("unexpected error on json.Unmarshal: %v", err)
}
res, err := la.Handle(context.TODO(), req)
if err != nil {
t.Error("LambdaHandler.Handle must return nil")
Expand Down
4 changes: 2 additions & 2 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ func request(ctx context.Context, event events.APIGatewayProxyRequest) (*http.Re
req.Header.Set("X-Request-ID", event.RequestContext.RequestID)
req.Header.Set("X-Stage", event.RequestContext.Stage)
// AWS X-Ray
if traceID := ctx.Value("X-Amzn-Trace-ID"); traceID != nil {
req.Header.Set("X-Amzn-Trace-ID", fmt.Sprintf("%v", traceID))
if traceID := ctx.Value("X-Amzn-Trace-Id"); traceID != nil {
req.Header.Set("X-Amzn-Trace-Id", fmt.Sprintf("%v", traceID))
}
req.RemoteAddr = event.RequestContext.Identity.SourceIP
return req, nil
Expand Down
15 changes: 15 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package gluo

import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"testing"
)
Expand Down Expand Up @@ -49,3 +51,16 @@ func TestWrongBase64Body(t *testing.T) {
t.Errorf("expected error reading body in base 64 from getBodyReader, got nil")
}
}

func TestXRayHeader(t *testing.T) {
event := events.APIGatewayProxyRequest{}
json.Unmarshal([]byte(testRequest), &event)
ctx := context.WithValue(context.Background(), "X-Amzn-Trace-Id", "Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=1")
req, err := request(ctx, event)
if err != nil {
t.Errorf("unexpected error on request: %v", err)
}
if req.Header.Get("X-Amzn-Trace-Id") == "" {
t.Errorf("expected X-Amzn-Trace-Id injected but it wasn't found")
}
}

0 comments on commit c24ca50

Please sign in to comment.