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

Commit

Permalink
Merge pull request #39 from hayd/xray
Browse files Browse the repository at this point in the history
Propagate the tracing header
  • Loading branch information
hayd authored Jan 18, 2020
2 parents 708abab + 998b2ac commit 5d95c87
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 15 deletions.
1 change: 1 addition & 0 deletions hello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export async function handler(
): Promise<APIGatewayProxyResult> {
return {
statusCode: 200,
headers: { "content-type": "text/html;charset=utf8" },
body: `Welcome to deno ${Deno.version.deno} 🦕`
};
}
15 changes: 9 additions & 6 deletions runtime/bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ function investigate {
echo "
import { $HANDLER_NAME } from '$LAMBDA_TASK_ROOT/$HANDLER_FILE';
const INVOCATION = '${API_ROOT}invocation/';
function tryParse(headers, headerName) {
function maybeJson(headers, headerName) {
const json = headers.get(headerName);
if (json) {
try {
return JSON.parse(json)
return JSON.parse(json);
} catch (e) {
console.error(
'Unable to parse header \'' + headerName + '\' value as JSON: ' + json)
'Unable to parse header', headerName, 'value as JSON:', json
);
}
}
}
Expand All @@ -109,6 +111,7 @@ while (true) {
const next = await fetch(INVOCATION + 'next');
const headers = next.headers;
const reqId = headers.get('lambda-runtime-aws-request-id');
Deno.env()._X_AMZN_TRACE_ID = headers.get('lambda-runtime-trace-id') || '';
const context = {
functionName: '$AWS_LAMBDA_FUNCTION_NAME',
functionVersion: '$AWS_LAMBDA_FUNCTION_VERSION',
Expand All @@ -117,12 +120,12 @@ while (true) {
awsRequestId: headers.get('lambda-runtime-aws-request-id'),
logGroupName: '$AWS_LAMBDA_LOG_GROUP_NAME',
logStreamName: '$AWS_LAMBDA_LOG_STREAM_NAME',
identity: tryParse(headers, 'lambda-runtime-cognito-identity'),
clientContext: tryParse(headers, 'lambda-runtime-client-context'),
identity: maybeJson(headers, 'lambda-runtime-cognito-identity'),
clientContext: maybeJson(headers, 'lambda-runtime-client-context'),
getRemainingTimeInMillis: function() {
return Number(headers.get('lambda-runtime-deadline-ms')) - Date.now();
},
// FIXME: we add these for type compatibility with Definitely Typed.
// NOTE: we add these for type compatibility with Definitely Typed.
callbackWaitsForEmptyEventLoop: undefined,
done: undefined,
fail: undefined,
Expand Down
4 changes: 4 additions & 0 deletions tests/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ export async function noArgs() {
export async function wrongArgs(a: number, b: number, c: number) {
return { result: a * b * c };
}

export async function xray(event, context) {
return { "_X_AMZN_TRACE_ID": Deno.env("_X_AMZN_TRACE_ID") }
}
5 changes: 4 additions & 1 deletion tests/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export async function serveEvents(testJson) {
});
if (testJson.headers) {
for (let [k, v] of Object.entries(testJson.headers)) {
headers.append(k, JSON.stringify(v));
if (Object.prototype.toString.call(v) !== "[object String]") {
v = JSON.stringify(v);
}
headers.append(k, v.toString());
}
}
await req.respond({
Expand Down
4 changes: 2 additions & 2 deletions tests/test_bundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"expected": [
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.29.0 🦕\"}"
"content": "{\"statusCode\":200,\"headers\":{\"content-type\":\"text/html;charset=utf8\"},\"body\":\"Welcome to deno 0.29.0 🦕\"}"
},
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.29.0 🦕\"}"
"content": "{\"statusCode\":200,\"headers\":{\"content-type\":\"text/html;charset=utf8\"},\"body\":\"Welcome to deno 0.29.0 🦕\"}"
}
],
"files": ["hello.bundle.js"],
Expand Down
4 changes: 2 additions & 2 deletions tests/test_example_zip.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"expected": [
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.29.0 🦕\"}"
"content": "{\"statusCode\":200,\"headers\":{\"content-type\":\"text/html;charset=utf8\"},\"body\":\"Welcome to deno 0.29.0 🦕\"}"
},
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.29.0 🦕\"}"
"content": "{\"statusCode\":200,\"headers\":{\"content-type\":\"text/html;charset=utf8\"},\"body\":\"Welcome to deno 0.29.0 🦕\"}"
}
],
"files": "deno-lambda-example.zip",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_self_contained.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"expected": [
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.29.0 🦕\"}"
"content": "{\"statusCode\":200,\"headers\":{\"content-type\":\"text/html;charset=utf8\"},\"body\":\"Welcome to deno 0.29.0 🦕\"}"
},
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.29.0 🦕\"}"
"content": "{\"statusCode\":200,\"headers\":{\"content-type\":\"text/html;charset=utf8\"},\"body\":\"Welcome to deno 0.29.0 🦕\"}"
}
],
"files": ["hello.ts", "bootstrap", "amz-deno"]
Expand Down
4 changes: 2 additions & 2 deletions tests/test_simple.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"expected": [
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.29.0 🦕\"}"
"content": "{\"statusCode\":200,\"headers\":{\"content-type\":\"text/html;charset=utf8\"},\"body\":\"Welcome to deno 0.29.0 🦕\"}"
},
{
"status": "ok",
"content": "{\"statusCode\":200,\"body\":\"Welcome to deno 0.29.0 🦕\"}"
"content": "{\"statusCode\":200,\"headers\":{\"content-type\":\"text/html;charset=utf8\"},\"body\":\"Welcome to deno 0.29.0 🦕\"}"
}
],
"files": ["hello.ts"],
Expand Down
15 changes: 15 additions & 0 deletions tests/test_xray.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"env": { "_HANDLER": "handlers.xray" },
"events": [{ "hello": "deno" }],
"expected": [
{
"status": "ok",
"content": "{\"_X_AMZN_TRACE_ID\":\"Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=1\"}"
}
],
"files": ["handlers.ts"],
"layer": "deno-lambda-layer.zip",
"headers": {
"Lambda-Runtime-Trace-Id": "Root=1-5759e988-bd862e3fe1be46a994272793;Sampled=1"
}
}

0 comments on commit 5d95c87

Please sign in to comment.