-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# HTTP API with X-Ray and Lambda OpenTelemetry | ||
|
||
This is a [HTTP API](https://www.winglang.io/docs/standard-library/cloud/api) example where both the API Gateway and Lambda handlers are enabled with X-Ray. | ||
|
||
Each Lambda Handler will have OpenTelemetry config injected, without changing any code or adding aws-xray sdks. This works by adding an OpenTelemetry Lambda Layer, which unfortunately adds a bit of cold start time (500ms - 1s). | ||
|
||
This got extracted from building internal tools for [wing.cloud](https://wing.cloud). | ||
|
||
![diagram](./diagram.png) | ||
|
||
## Prerequisite | ||
|
||
Please make sure to use a current and working setup of the [wing cli](https://docs.winglang.io/getting-started/installation) | ||
|
||
## Usage | ||
|
||
### Wing Console | ||
|
||
``` | ||
wing it | ||
``` | ||
|
||
### Wing Tests | ||
|
||
``` | ||
wing test --debug main.w | ||
``` |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
bring cloud; | ||
bring util; | ||
bring http; | ||
bring expect; | ||
|
||
let api = new cloud.Api(); | ||
|
||
api.get("/hello", inflight (req) => { | ||
return { | ||
status: 200, | ||
headers: { | ||
"Content-Type" => "text/plain" | ||
}, | ||
body: "hello world" | ||
}; | ||
}); | ||
|
||
|
||
test "it should respond with 200" { | ||
let response = http.get("{api.url}/hello"); | ||
expect.equal(response.status, 200); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { App, Function } from '@winglang/sdk/lib/target-tf-aws'; | ||
import { Effect } from '@winglang/sdk/lib/shared-aws'; | ||
import { Construct } from 'constructs'; | ||
|
||
export class EnableXray { | ||
constructor(private app: App) {} | ||
|
||
isApiGatewayStage(node: Construct): boolean { | ||
return node && (node as any)["terraformResourceType"] === 'aws_api_gateway_stage'; | ||
} | ||
|
||
isLambdaFunction(node: Construct): boolean { | ||
return node && (node as any)['function'] && (node as any)['function']['terraformResourceType'] === 'aws_lambda_function'; | ||
} | ||
|
||
enableXrayForApiGateway(node: Construct) { | ||
(node as any).addOverride('xray_tracing_enabled', true); | ||
} | ||
|
||
enableXrayForLambda(node: Construct) { | ||
const cloudFunction = node as Function; | ||
const lambdaArchitecture = "arm64" | ||
const region = this.app.region; | ||
const version = "1-17-1:1" | ||
const lambdaLayer = `arn:aws:lambda:${region}:901920570463:layer:aws-otel-nodejs-${lambdaArchitecture}-ver-${version}` | ||
cloudFunction.addPolicyStatements({ | ||
effect: Effect.ALLOW, | ||
actions: [ | ||
'xray:PutTraceSegments', | ||
'xray:PutTelemetryRecords', | ||
"xray:GetSamplingRules", | ||
"xray:GetSamplingTargets", | ||
"xray:GetSamplingStatisticSummaries", | ||
], | ||
resources: ['*'] | ||
}) | ||
|
||
cloudFunction.addEnvironment('AWS_LAMBDA_EXEC_WRAPPER', '/opt/otel-handler') | ||
|
||
const cdktfFunction = cloudFunction['function']; | ||
|
||
cdktfFunction.putTracingConfig({ | ||
mode: 'Active' | ||
}); | ||
|
||
cdktfFunction.memorySize = 1024; | ||
|
||
cdktfFunction.layers = [lambdaLayer]; | ||
} | ||
|
||
visit(node: Construct) { | ||
if (this.isApiGatewayStage(node)) { | ||
this.enableXrayForApiGateway(node); | ||
} | ||
if (this.isLambdaFunction(node)) { | ||
this.enableXrayForLambda(node); | ||
} | ||
} | ||
} |