Skip to content

Commit

Permalink
draft: add xray enabled api
Browse files Browse the repository at this point in the history
  • Loading branch information
skorfmann committed Feb 26, 2024
1 parent c812994 commit cf78ae7
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/api-xray-enabled/Readme.md
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
```
Binary file added examples/api-xray-enabled/diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions examples/api-xray-enabled/main.w
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);
}
59 changes: 59 additions & 0 deletions examples/api-xray-enabled/platform.ts
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);
}
}
}

0 comments on commit cf78ae7

Please sign in to comment.