|
| 1 | +/* |
| 2 | + * Unless explicitly stated otherwise all files in this repository are licensed |
| 3 | + * under the Apache License Version 2.0. |
| 4 | + * |
| 5 | + * This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 6 | + * Copyright 2021 Datadog, Inc. |
| 7 | + */ |
| 8 | + |
| 9 | +import * as path from "path"; |
| 10 | +import { App, Stack, StackProps, Tags } from "aws-cdk-lib"; |
| 11 | +import * as lambda from "aws-cdk-lib/aws-lambda"; |
| 12 | +import { Construct } from "constructs"; |
| 13 | +import { DatadogLambda } from "../../src/index"; |
| 14 | +import { E2E_NODE_LAYER_VERSION, E2E_EXTENSION_LAYER_VERSION, E2E_RUNTIME } from "../helpers/versions"; |
| 15 | +import { FRESHNESS_TAG_KEY, RUN_ID_TAG_KEY } from "../helpers/naming"; |
| 16 | + |
| 17 | +// The CDK construct is the instrumentation mechanism under test. This same stack |
| 18 | +// is provisioned uninstrumented first (E2E_INSTRUMENT=false, no DatadogLambda), |
| 19 | +// then APPLY re-deploys it with E2E_INSTRUMENT=true (DatadogLambda applied). REMOVE |
| 20 | +// is `cdk destroy` of the stack -- the function is deleted, leaving a clean |
| 21 | +// end-state -- so the uninstrumented path only serves the initial baseline. |
| 22 | +const instrument = process.env.E2E_INSTRUMENT === "true"; |
| 23 | +const serviceName = requireEnv("E2E_SERVICE_NAME"); |
| 24 | +const runId = requireEnv("E2E_RUN_ID"); |
| 25 | +const createdTs = requireEnv("E2E_CREATED_TS"); |
| 26 | +const site = process.env.DD_SITE ?? "datadoghq.com"; |
| 27 | +const env = process.env.E2E_ENV ?? "e2e"; |
| 28 | +const version = process.env.E2E_VERSION ?? "1.0.0"; |
| 29 | + |
| 30 | +class WorkloadStack extends Stack { |
| 31 | + constructor(scope: Construct, id: string, props?: StackProps) { |
| 32 | + super(scope, id, props); |
| 33 | + |
| 34 | + const fn = new lambda.Function(this, "Handler", { |
| 35 | + functionName: serviceName, |
| 36 | + runtime: E2E_RUNTIME, |
| 37 | + handler: "index.handler", |
| 38 | + // Resolved from cwd (the repo root, where cdk runs) so it survives bundling |
| 39 | + // the app to a single .cjs whose __dirname is the build output dir. |
| 40 | + code: lambda.Code.fromAsset(path.resolve(process.cwd(), "e2e/app/handler")), |
| 41 | + }); |
| 42 | + |
| 43 | + // Freshness tag is set at creation on the uninstrumented baseline too, so the |
| 44 | + // cross-repo sweeper can find and reap this function even if the run crashes |
| 45 | + // before REMOVE's `cdk destroy` deletes it. |
| 46 | + Tags.of(fn).add(FRESHNESS_TAG_KEY, createdTs); |
| 47 | + |
| 48 | + if (!instrument) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const datadogLambda = new DatadogLambda(this, "Datadog", { |
| 53 | + nodeLayerVersion: E2E_NODE_LAYER_VERSION, |
| 54 | + extensionLayerVersion: E2E_EXTENSION_LAYER_VERSION, |
| 55 | + enableDatadogTracing: true, |
| 56 | + enableDatadogLogs: true, |
| 57 | + injectLogContext: true, |
| 58 | + sourceCodeIntegration: false, |
| 59 | + // Only needed when instrumenting; the uninstrumented baseline carries no key. |
| 60 | + apiKey: requireEnv("DD_API_KEY"), |
| 61 | + site, |
| 62 | + service: serviceName, |
| 63 | + env, |
| 64 | + version, |
| 65 | + // Surfaces as a `one_e2e_run_id` resource tag *and* in DD_TAGS, so the run id |
| 66 | + // rides along on every emitted span and log -- the dimension the telemetry |
| 67 | + // checker filters on to prove identity. |
| 68 | + tags: `${RUN_ID_TAG_KEY}:${runId}`, |
| 69 | + }); |
| 70 | + datadogLambda.addLambdaFunctions([fn]); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function requireEnv(name: string): string { |
| 75 | + const value = process.env[name]; |
| 76 | + if (!value) { |
| 77 | + throw new Error(`Missing required env var ${name}`); |
| 78 | + } |
| 79 | + |
| 80 | + return value; |
| 81 | +} |
| 82 | + |
| 83 | +const app = new App(); |
| 84 | +new WorkloadStack(app, serviceName, { |
| 85 | + env: { |
| 86 | + account: process.env.CDK_DEFAULT_ACCOUNT, |
| 87 | + region: process.env.CDK_DEFAULT_REGION ?? process.env.AWS_REGION, |
| 88 | + }, |
| 89 | +}); |
| 90 | +app.synth(); |
0 commit comments