Skip to content

Commit

Permalink
Add xray
Browse files Browse the repository at this point in the history
  • Loading branch information
branberry committed Oct 31, 2023
1 parent c3db19b commit 2e55f24
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 9 deletions.
5 changes: 4 additions & 1 deletion api/controllers/v2/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as c from 'config';
import * as mongodb from 'mongodb';
import { APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda';
import { PushEvent } from '@octokit/webhooks-types';

import AWSXRay from 'aws-xray-sdk-core';
import { JobRepository } from '../../../src/repositories/jobRepository';
import { ConsoleLogger } from '../../../src/services/logger';
import { RepoBranchesRepository } from '../../../src/repositories/repoBranchesRepository';
Expand All @@ -13,6 +13,9 @@ import { getMonorepoPaths } from '../../../src/monorepo';
import { getUpdatedFilePaths } from '../../../src/monorepo/utils/path-utils';
import { ReposBranchesDocument } from '../../../modules/persistence/src/services/metadata/associated_products';

// eslint-disable-next-line @typescript-eslint/no-var-requires
AWSXRay.captureAWS(require('aws-sdk'));

async function prepGithubPushPayload(
githubEvent: PushEvent,
repoBranchesRepository: RepoBranchesRepository,
Expand Down
3 changes: 2 additions & 1 deletion cdk-infra/lib/constructs/api/webhook-api-construct.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Duration } from 'aws-cdk-lib';
import { Cors, CorsOptions, LambdaIntegration, LambdaRestApi } from 'aws-cdk-lib/aws-apigateway';
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
import { Code, Function, Runtime, Tracing } from 'aws-cdk-lib/aws-lambda';
import { SqsEventSource } from 'aws-cdk-lib/aws-lambda-event-sources';
import { BundlingOptions, NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { IQueue } from 'aws-cdk-lib/aws-sqs';
Expand Down Expand Up @@ -72,6 +72,7 @@ export class WebhookApiConstruct extends Construct {
bundling,
environment,
timeout,
tracing: Tracing.ACTIVE,
});

const githubDeleteArtifactsLambda = new NodejsFunction(this, 'githubDeleteArtifactsLambda', {
Expand Down
42 changes: 35 additions & 7 deletions cdk-infra/lib/constructs/worker/worker-construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
FargateService,
FargateTaskDefinition,
LogDrivers,
Protocol,
} from 'aws-cdk-lib/aws-ecs';
import { Effect, IRole, PolicyStatement, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam';
import { LogGroup } from 'aws-cdk-lib/aws-logs';
Expand Down Expand Up @@ -80,6 +81,17 @@ export class WorkerConstruct extends Construct {
executionRole,
});

const xrayTracingPolicy = new PolicyStatement({
effect: Effect.ALLOW,
actions: [
'xray:PutTraceSegments',
'xray:PutTelemetryRecords',
'xray:GetSamplingRules',
'xray:GetSamplingTargets',
'xray:GetSamplingStatisticSummaries',
],
resources: ['*'],
});
const updateTaskProtectionPolicy = new PolicyStatement({
effect: Effect.ALLOW,
actions: ['ecs:UpdateTaskProtection'],
Expand All @@ -91,17 +103,33 @@ export class WorkerConstruct extends Construct {
resources: ['*'],
});

taskRole.addToPolicy(xrayTracingPolicy);
taskRole.addToPolicy(updateTaskProtectionPolicy);

taskDefinition.addContainer('workerImage', {
image: ContainerImage.fromAsset(path.join(__dirname, '../../../../'), containerProps),
environment: dockerEnvironment,
logging: LogDrivers.awsLogs({
streamPrefix: 'autobuilderworker',
logGroup: taskDefLogGroup,
}),
const sideCar = taskDefinition.addContainer('xraySidecar', {
image: ContainerImage.fromRegistry('amazon/aws-xray-daemon'),
portMappings: [
{
hostPort: 0,
protocol: Protocol.UDP,
containerPort: 2000,
},
],
});

taskDefinition
.addContainer('workerImage', {
image: ContainerImage.fromAsset(path.join(__dirname, '../../../../'), containerProps),
environment: dockerEnvironment,
logging: LogDrivers.awsLogs({
streamPrefix: 'autobuilderworker',
logGroup: taskDefLogGroup,
}),
})
.addContainerDependencies({
container: sideCar,
});

const env = getEnv();

new FargateService(this, 'fargateService', {
Expand Down
Loading

0 comments on commit 2e55f24

Please sign in to comment.