-
Notifications
You must be signed in to change notification settings - Fork 1
/
lumigo.ts
1214 lines (963 loc) · 46.2 KB
/
lumigo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { dirname, join } from 'path';
/*
* We really only need the type, not the code. By using `import type`,
* we do not need the dependency at runtime.
*/
import type { PythonFunction } from '@aws-cdk/aws-lambda-python-alpha';
import { App, Annotations, IAspect, SecretValue, Stack, Aspects, Tags, TagManager, CfnDynamicReference, CfnDynamicReferenceService } from 'aws-cdk-lib';
import {
CfnTaskDefinition,
ContainerDefinition,
ContainerDependencyCondition,
ContainerImage,
Ec2Service,
Ec2TaskDefinition,
FargateService,
FargateTaskDefinition,
ITaskDefinitionExtension,
Secret,
TaskDefinition,
Volume,
} from 'aws-cdk-lib/aws-ecs';
import {
ApplicationLoadBalancedEc2Service,
ApplicationLoadBalancedFargateService,
ApplicationMultipleTargetGroupsEc2Service,
ApplicationMultipleTargetGroupsFargateService,
NetworkLoadBalancedEc2Service,
NetworkLoadBalancedFargateService,
NetworkMultipleTargetGroupsEc2Service,
NetworkMultipleTargetGroupsFargateService,
QueueProcessingEc2Service,
QueueProcessingFargateService,
ScheduledEc2Task,
ScheduledFargateTask,
} from 'aws-cdk-lib/aws-ecs-patterns';
import { Function, LayerVersion, Runtime } from 'aws-cdk-lib/aws-lambda';
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs';
import { Secret as SecretManagerSecret } from 'aws-cdk-lib/aws-secretsmanager';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import { Construct, IConstruct, IValidation } from 'constructs';
/* eslint-disable */
const { name, version } = require(join(dirname(__dirname), 'package.json'));
/* eslint-enable */
import * as lambdaLayersNodejs from './lambda_layers_nodejs.json';
import * as lambdaLayersPython from './lambda_layers_python.json';
import { image as lumigo_autotrace_image } from './lumigo_autotrace_image.json';
export const DEFAULT_LUMIGO_INJECTOR_IMAGE_NAME = lumigo_autotrace_image;
type SupportedLambdaFunction = (
Function |
NodejsFunction |
PythonFunction
);
type SupportedEcsTaskDefinition = (
Ec2TaskDefinition |
FargateTaskDefinition
);
type SupportedEcsScheduledTask = (
ScheduledEc2Task |
ScheduledFargateTask
);
type SupportedEcsPatternsService = (
ApplicationLoadBalancedEc2Service |
ApplicationLoadBalancedFargateService |
ApplicationMultipleTargetGroupsEc2Service |
ApplicationMultipleTargetGroupsFargateService |
NetworkLoadBalancedEc2Service |
NetworkLoadBalancedFargateService |
NetworkMultipleTargetGroupsEc2Service |
NetworkMultipleTargetGroupsFargateService |
QueueProcessingEc2Service |
QueueProcessingFargateService
);
type SupportedEcsService = (
FargateService |
Ec2Service |
SupportedEcsPatternsService
);
export interface LumigoProps {
/**
* A reference to a secret containing of the Lumigo token of the Lumigo project to be used with instrumented Lambda functions and ECS workloads.
* Instructions on how to retrieve your Lumigo token are available in the [Lumigo tokens](https://docs.lumigo.io/docs/tags) documentation.
* For more information concerning how AWS CDK 2 handles secrets, consult the [AWS SDK `SecretValue`](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.SecretValue.html) documentation.
*/
readonly lumigoToken: SecretValue;
}
interface CommonTraceProps {
/**
* Which Lumigo tag to apply to your instrumented Lambda functions and ECS workloads.
* Lumigo Tags add dimension to your Lambda functions so that they can be identified, managed, organized, searched for, and filtered in Lumigo.
* For more information on Lumigo tags, refer to the [Lumigo tokens](https://docs.lumigo.io/docs/tags) documentation.
*/
readonly lumigoTag?: string;
/**
* Whether the Lumigo CDK constructs should automatically add the `lumigo:auto-trace` AWS tag with the version of the construct in use.
* @default true
*/
readonly applyAutoTraceTag?: boolean;
}
export interface LumigoTraceProps extends CommonTraceProps, CommonTraceEcsProps {
/**
* Whether to automatically trace all the Node.js and Python Lambda functions in this construct using [Lumigo Lambda auto-instrumentation](https://docs.lumigo.io/docs/auto-instrumentation).
*
* @default true
*/
readonly traceLambda?: boolean;
/**
* Whether to automatically trace all the Java, Node.js and Python Lambda functions deployed on ECS by this construct using the respective [Lumigo OpenTelemetry distributions](https://docs.lumigo.io/docs/containerized-applications).
*
* @default true
*/
readonly traceEcs?: boolean;
/**
* Which version of the `lumigo-node-tracer` AWS Lambda layer to be used when instrumenting AWS Lambda functions using a supported Node.js runtime.
* Available layer versions depend on the AWS region your Lambda function is deployed in, see the [`lumigo-node-tracer` versions](https://github.com/lumigo-io/lumigo-node/tree/master/layers) list.
* The default value is the latest Node.js layer at the time of release of this version of the Lumigo CDK constructs: [default Node.js versions](./src/lambda_layers_nodejs.json).
*/
readonly lambdaNodejsLayerVersion?: number;
/**
* Which version of the `lumigo-python-tracer` AWS Lambda layer to be used when instrumenting AWS Lambda functions using a supported Python runtime.
* Available layer versions depend on the AWS region your Lambda function is deployed in, see the [`lumigo-python-tracer` versions](https://github.com/lumigo-io/python_tracer/tree/master/layers) list.
* The default value is the latest Python layer at the time of release of this version of the Lumigo CDK constructs: [default Python versions](./src/lambda_layers_python.json).
*/
readonly lambdaPythonLayerVersion?: number;
/**
* Whether the Lumigo Lambda tracers will add the [W3C Trace Context](https://www.w3.org/TR/trace-context/) `traceparent` and `tracestate` HTTP headers to outgoing HTTP/HTTPS requests.
* These headers are necessary to correctly correlate the HTTP requests from Lambda to workloads instrumented with the Lumigo OpenTelemetry distributions.
* The only real case in which this property should be set to false, is if there is some HTTP request issued by the Lambda function that is going towards an API with request signature that is affected negatively by the additional headers.
* If you encounter such an occurrence, please get in touch with [Lumigo's support](https://support.lumigo.io); we will issue an update to the Lumigo Lambda tracers to automatically not add [W3C Trace Context](https://www.w3.org/TR/trace-context/) to those APIs.
*
* @default true
*/
readonly lambdaEnableW3CTraceContext?: boolean;
}
export interface TraceLambdaProps extends CommonTraceProps {
/**
* Which version of the appropriate Lumigo layer to be used; layer versions change based on runtime and region.
* Layer versions: [Node.js](https://github.com/lumigo-io/lumigo-node/tree/master/layers) and [Python](https://github.com/lumigo-io/python_tracer/tree/master/layers).
* The default value is the latest layers at the time of release of this version of the Lumigo CDK constructs: [default Node.js versions](./src/lambda_layers_nodejs.json), [default Python versions](./src/lambda_layers_python.json)
*/
readonly layerVersion?: number;
/**
* Whether the Lumigo Lambda tracers will add the `traceparent` and `tracestate` [W3C Trace Context](https://www.w3.org/TR/trace-context/) headers to outgoing HTTP/HTTPS requests.
* These headers are necessary to correctly correlate the HTTP requests from Lambda to workloads instrumented with the Lumigo OpenTelemetry distributions.
* The only real case in which this property should be set to false, is if there is some HTTP request issued by the Lambda function that is going towards an API with request signature that is affected negatively by the additional headers.
* If you encounter such an occurrence, please get in touch with [Lumigo's support](https://support.lumigo.io); we will issue an update to the Lumigo Lambda tracers to automatically not add [W3C Trace Context](https://www.w3.org/TR/trace-context/) to those APIs.
*
* @default true
*/
readonly enableW3CTraceContext?: boolean;
}
interface CommonTraceEcsProps extends CommonTraceProps {
/**
* Which container image to use to instrument ECS workloads. Use a valid, full image name of the [`lumigo/lumigo-autotrace` image](https://gallery.ecr.aws/lumigo/lumigo-autotrace), e.g., `public.ecr.aws/lumigo/lumigo-autotrace:v14`.
*
* This property is exposed to support two use-cases: pinning a specific tag of the `lumigo/lumigo-autotrace` image, or supporting use-cases where Amazon ECS will not be able to pull from the Amazon ECS Public Gallery registry.
* The available tags are listed on the [`lumigo/lumigo-autotrace` Amazon ECR Public Gallery](https://gallery.ecr.aws/lumigo/lumigo-autotrace) page.
* The default value is the latest tag at the time of release of this version of the Lumigo CDK constructs: [default `lumigo/lumigo-autotrace` image](./src/lumigo_autotrace_image.json)
*/
readonly lumigoAutoTraceImage?: string;
}
export interface TraceEcsTaskDefinitionProps extends CommonTraceEcsProps {
}
export interface TraceEcsScheduledTaskProps extends CommonTraceEcsProps {
}
export interface TraceEcsServiceProps extends CommonTraceEcsProps {
}
// Layer type to layer name
enum LambdaLayerType {
NODE = 'lumigo-node-tracer',
PYTHON = 'lumigo-python-tracer',
}
const AWS_LAMBDA_EXEC_WRAPPER_ENV_VAR_NAME = 'AWS_LAMBDA_EXEC_WRAPPER';
const AWS_LAMBDA_EXEC_WRAPPER_ENV_VAR_VALUE = '/opt/lumigo_wrapper';
const LUMIGO_ORIGINAL_HANDLER_ENV_VAR_NAME = 'LUMIGO_ORIGINAL_HANDLER';
const LUMIGO_PROPAGATE_W3C_ENV_VAR_NAME = 'LUMIGO_PROPAGATE_W3C';
const LUMIGO_TRACER_TOKEN_ENV_VAR_NAME = 'LUMIGO_TRACER_TOKEN';
const LUMIGO_AUTOTRACE_TAG_NAME = 'lumigo:auto-trace';
const LUMIGO_AUTOTRACE_TAG_VALUE = `${name}@${version}`;
const LUMIGO_TAG_TAG_NAME = 'LUMIGO_TAG';
const LUMIGO_LAMBDA_PYTHON_HANDLER = 'lumigo_tracer._handler';
const LUMIGO_INJECTOR_CONTAINER_NAME = 'lumigo-injector';
const LUMIGO_INJECTOR_VOLUME_NAME = 'lumigo-injector';
const LUMIGO_INJECTOR_VOLUME_MOUNT_POINT = '/opt/lumigo';
const LUMIGO_INJECTOR_ENV_VAR_NAME = 'LD_PRELOAD';
const LUMIGO_INJECTOR_ENV_VAR_VALUE = `${LUMIGO_INJECTOR_VOLUME_MOUNT_POINT}/injector/lumigo_injector.so`;
const DEFAULT_LUMIGO_TRACE_PROPS: LumigoTraceProps = {
traceLambda: true,
traceEcs: true,
lambdaEnableW3CTraceContext: true,
};
const DEFAULT_TRACE_ECS_TASK_DEFINITION_PROPS: TraceEcsTaskDefinitionProps = {
applyAutoTraceTag: true,
lumigoAutoTraceImage: DEFAULT_LUMIGO_INJECTOR_IMAGE_NAME,
};
const isTrueOrUndefined = (value: boolean | undefined) => value === true || value === undefined;
/**
* The `Lumigo` class is the entry point for instrumenting workloads deployed via CDK constructs with Lumigo.
* You usually would need only one instance of `Lumigo` per CDK application.
*/
export class Lumigo {
props: LumigoProps;
private ecsSecrets: Map<Construct, Secret> = new Map<Construct, Secret>();
constructor(props: LumigoProps) {
this.props = props;
}
/**
* @private This is an internal API; it is not meant to be invoked directly by end-user code.
*/
visit(construct: IConstruct): void {
if (construct instanceof Function) {
try {
this.traceLambda(construct);
} catch (e) {
if (e instanceof UnsupportedLambdaRuntimeError) {
this.info(construct, `The '${e.unsupportedRuntime}' cannot be automatically traced by Lumigo.`);
} else {
throw e;
}
}
}
}
protected info(node: IConstruct, message: string): void {
Annotations.of(node).addInfo(message);
}
protected error(node: IConstruct, message: string): void {
Annotations.of(node).addError(message);
}
protected warning(node: IConstruct, message: string): void {
Annotations.of(node).addWarning(message);
}
public traceEverything(root: App | Stack, props: LumigoTraceProps = DEFAULT_LUMIGO_TRACE_PROPS) {
// For the people not using TypeScript, we need to perform some basic type validation
assertExpectedConstructType('traceEverything', 'App or Stack', root, (c) => c instanceof App || c instanceof Stack);
Aspects.of(root).add(this.asAspect(props));
}
private asAspect(props: LumigoTraceProps = DEFAULT_LUMIGO_TRACE_PROPS): IAspect {
const lumigo = this;
/*
* Tags are implemented as aspects in CDK, and unfortunately aspects like
* this cannot apply other aspects.
*/
const applyAutoTraceTag = false;
return <IAspect>{
visit: function(construct: IConstruct): void {
function applyAwsTagThroughTagManager(key: string, value: string) {
try {
/**
* To overcome the limitation that (1) tags are implemented as aspects, (2) that
* we are already inside an aspect and (3) an aspect cannot add aspects, we need
* to access the TagManager of the function and add the tag manually.
*/
const scope = construct.node?.scope;
if (!!scope) {
/* eslint-disable */
const tags = (scope as any)['tags'];
/* eslint-enable */
if (!!tags && tags instanceof TagManager) {
tags.setTag(key, value, 100, true);
}
}
} catch (e) {
lumigo.warning(construct, `Cannot set the '${key}' tag with value '${value}'.`);
}
}
if (construct instanceof Function && props.traceLambda !== false) {
try {
const layerType = lumigo.getLayerType(construct);
if (!layerType) {
lumigo.warning(construct, 'The runtime used by this function cannot be auto-traced by Lumigo.');
return;
}
var layerVersion;
if (layerType === LambdaLayerType.NODE) {
layerVersion = props.lambdaNodejsLayerVersion;
} else if (layerType === LambdaLayerType.PYTHON) {
layerVersion = props.lambdaPythonLayerVersion;
}
lumigo.traceLambda(construct, {
layerVersion,
enableW3CTraceContext: isTrueOrUndefined(props.lambdaEnableW3CTraceContext),
applyAutoTraceTag,
lumigoTag: props.lumigoTag,
});
if (props.lumigoTag) {
applyAwsTagThroughTagManager(LUMIGO_TAG_TAG_NAME, props.lumigoTag!);
}
if (props.applyAutoTraceTag) {
applyAwsTagThroughTagManager(LUMIGO_AUTOTRACE_TAG_NAME, LUMIGO_AUTOTRACE_TAG_VALUE);
}
} catch (e) {
if (e instanceof UnsupportedLambdaRuntimeError) {
lumigo.info(construct, `The '${e.unsupportedRuntime}' cannot be automatically traced by Lumigo.`);
} else {
throw e;
}
}
} else if (props.traceEcs !== false) {
if (isSupportedEcsServiceConstruct(construct)) {
lumigo.traceEcsService(construct, {
applyAutoTraceTag,
lumigoAutoTraceImage: props.lumigoAutoTraceImage,
});
if (props.lumigoTag) {
applyAwsTagThroughTagManager(LUMIGO_TAG_TAG_NAME, props.lumigoTag!);
}
if (props.applyAutoTraceTag) {
applyAwsTagThroughTagManager(LUMIGO_AUTOTRACE_TAG_NAME, LUMIGO_AUTOTRACE_TAG_VALUE);
}
} else if (isSupportedEcsScheduledTaskConstruct(construct)) {
lumigo.traceEcsScheduledTask(construct, {
applyAutoTraceTag,
lumigoAutoTraceImage: props.lumigoAutoTraceImage,
});
if (props.lumigoTag) {
applyAwsTagThroughTagManager(LUMIGO_TAG_TAG_NAME, props.lumigoTag!);
}
if (props.applyAutoTraceTag) {
applyAwsTagThroughTagManager(LUMIGO_AUTOTRACE_TAG_NAME, LUMIGO_AUTOTRACE_TAG_VALUE);
}
} else if (isSupportedEcsTaskDefinitionConstruct(construct)) {
lumigo.traceEcsTaskDefinition(construct, {
applyAutoTraceTag,
lumigoAutoTraceImage: props.lumigoAutoTraceImage,
});
if (props.lumigoTag) {
applyAwsTagThroughTagManager(LUMIGO_TAG_TAG_NAME, props.lumigoTag!);
}
if (props.applyAutoTraceTag) {
applyAwsTagThroughTagManager(LUMIGO_AUTOTRACE_TAG_NAME, LUMIGO_AUTOTRACE_TAG_VALUE);
}
}
}
},
};
}
/**
* This method returns a wrapper that can be used in conjunction with the {@link https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.TaskDefinition.html#addwbrextensionextension|TaskDefinition.addExtension} method.
* The effect is the same as using the {@link Lumigo#traceEcsTaskDefinition} method on the `TaskDefinition` on which you would invoke `TaskDefinition.addExtension`.
*
* @returns A wrapper that invokes {@link Lumigo#traceEcsTaskDefinition} on the task definition to be extended.
*/
public asEcsExtension(): ITaskDefinitionExtension {
return {
extend: this.traceEcsTaskDefinition.bind(this),
};
}
/**
* Apply Lumigo autotracing for Java, Node.js and Python applications deployed through the provided ECS ScheduledTask construct.
*/
public traceEcsScheduledTask(scheduledTask: SupportedEcsScheduledTask, props: TraceEcsScheduledTaskProps = {
applyAutoTraceTag: true,
}) {
// For the people not using TypeScript, we need to perform some basic type validation
assertExpectedConstructType('traceEcsScheduledTask', 'ScheduledEc2Task or ScheduledFargateTask', scheduledTask, (c) => isSupportedEcsScheduledTaskConstruct(c));
this.doTraceEcsTaskDefinition(scheduledTask.taskDefinition, {
applyAutoTraceTag: false,
lumigoAutoTraceImage: props.lumigoAutoTraceImage,
});
if (!!props.applyAutoTraceTag) {
this.applyAutotraceTag(scheduledTask);
}
if (!!props.lumigoTag) {
this.applyLumigoTag(scheduledTask, props.lumigoTag!);
}
}
/**
* Apply Lumigo autotracing for Java, Node.js and Python applications deployed through the provided ECS Service construct.
*/
public traceEcsService(service: SupportedEcsService, props: TraceEcsServiceProps = {
applyAutoTraceTag: true,
}) {
// For the people not using TypeScript, we need to perform some basic type validation
assertExpectedConstructType('traceEcsService', 'EcsService', service, (c) => isSupportedEcsServiceConstruct(c));
this.doTraceEcsTaskDefinition(service.taskDefinition, {
applyAutoTraceTag: false,
lumigoAutoTraceImage: props.lumigoAutoTraceImage,
});
if (!!props.applyAutoTraceTag) {
this.applyAutotraceTag(service);
}
if (!!props.lumigoTag) {
this.applyLumigoTag(service, props.lumigoTag!);
}
}
/**
* Apply Lumigo autotracing for Java, Node.js and Python applications deployed through the provided `TaskDefinition`.
* If the ECS workload does not contain Java, Node.js or Python applications, no distributed-tracing data will be reported to Lumigo.
*/
public traceEcsTaskDefinition(
taskDefinition: SupportedEcsTaskDefinition,
props: TraceEcsTaskDefinitionProps = DEFAULT_TRACE_ECS_TASK_DEFINITION_PROPS,
) {
// For the people not using TypeScript, we need to perform some basic type validation
assertExpectedConstructType('traceEcsTaskDefinition', 'TaskDefinition', taskDefinition, (c) => isSupportedEcsTaskDefinitionConstruct(c));
this.doTraceEcsTaskDefinition(taskDefinition, props);
}
private lumigoTokenAsEcsSecret(scope: Construct): Secret | undefined {
if (!this.ecsSecrets.has(scope)) {
const ref = (this.props.lumigoToken as any).rawValue;
if (ref.value.match(/{{resolve:\S+}}/)) {
const tokens = ref.value.substring('{{resolve:'.length, ref.value.length - 2).split(':');
switch (tokens[0]) {
case CfnDynamicReferenceService.SECRETS_MANAGER: {
// Secret Manager: '{{resolve:secretsmanager:<secretName>:SecretString:<fieldName>::}}'
const secretName = tokens[1];
const fieldName = (tokens.length > 3) ? tokens[3] : undefined;
this.ecsSecrets.set(scope, Secret.fromSecretsManager(SecretManagerSecret.fromSecretNameV2(scope, 'lumigoTokenSMS', secretName), fieldName));
break;
}
case CfnDynamicReferenceService.SSM_SECURE: {
// SSM-secure: '{{resolve:ssm-secure:<parameterName>:<version>}}'
const parameterName = tokens[1];
const parameterVersion = (tokens.length > 2) ? Number(tokens[2]) : undefined;
this.ecsSecrets.set(scope, Secret.fromSsmParameter(StringParameter.fromSecureStringParameterAttributes(scope, 'lumigoTokenSSM', {
parameterName,
version: parameterVersion,
})));
break;
}
}
}
}
return this.ecsSecrets.get(scope);
}
private doTraceEcsTaskDefinition(taskDefinition: TaskDefinition, props: TraceEcsTaskDefinitionProps = DEFAULT_TRACE_ECS_TASK_DEFINITION_PROPS) {
/*
* This function must be idempotent, as `Lumigo.traceEverything()` will apply to
* both the ECS Service and its ECS TaskDefinition.
*/
if (!getTaskDefinitionVolumes(taskDefinition).find(volume => volume.name === LUMIGO_INJECTOR_VOLUME_NAME)) {
taskDefinition.addVolume({
name: LUMIGO_INJECTOR_VOLUME_NAME,
});
}
// Add injector container
const TARGET_DIRECTORY_PATH = '/target';
const injectorContainer = getTaskDefinitionContainers(taskDefinition)
.find(container => container.containerName === LUMIGO_INJECTOR_CONTAINER_NAME)
|| // We did not find the injector container yet, time to add it
taskDefinition.addContainer(LUMIGO_INJECTOR_CONTAINER_NAME, {
image: ContainerImage.fromRegistry(props.lumigoAutoTraceImage || DEFAULT_LUMIGO_INJECTOR_IMAGE_NAME),
containerName: LUMIGO_INJECTOR_CONTAINER_NAME,
environment: {
TARGET_DIRECTORY: TARGET_DIRECTORY_PATH,
},
essential: false,
});
if (!injectorContainer.mountPoints?.find(mountPoint => mountPoint.sourceVolume === LUMIGO_INJECTOR_VOLUME_NAME)) {
injectorContainer.addMountPoints({
sourceVolume: LUMIGO_INJECTOR_VOLUME_NAME,
containerPath: TARGET_DIRECTORY_PATH,
readOnly: false,
});
}
/*
* See if we can construct an ECS secret from the SecretValue
*/
let tokenSecret: Secret | undefined;
if ((this.props.lumigoToken as any).rawValue instanceof CfnDynamicReference) {
tokenSecret = this.lumigoTokenAsEcsSecret(taskDefinition);
}
// We wait to start any other container until the inject has done its work
const otherContainers: ContainerDefinition[] = getTaskDefinitionContainers(taskDefinition)
.filter((it: ContainerDefinition) => it !== injectorContainer);
otherContainers.forEach(container => {
if (!container.containerDependencies?.find(containerDependency => containerDependency.container === injectorContainer)) {
container.addContainerDependencies({
container: injectorContainer,
condition: ContainerDependencyCondition.COMPLETE,
});
}
if (!container.mountPoints?.find(mountPoint => mountPoint.sourceVolume === LUMIGO_INJECTOR_VOLUME_NAME)) {
container.addMountPoints({
sourceVolume: LUMIGO_INJECTOR_VOLUME_NAME,
containerPath: LUMIGO_INJECTOR_VOLUME_MOUNT_POINT,
readOnly: true,
});
}
// Trigger the injector
// The environment is implemented as a dictionary, no need for idempotency checks
container.addEnvironment(LUMIGO_INJECTOR_ENV_VAR_NAME, LUMIGO_INJECTOR_ENV_VAR_VALUE);
if (tokenSecret) {
/*
* The implementation of ContainerDefinition.addSecret does not check for the secret being specified multiple times,
* so we need to avoid duplication ourselves.
*/
const secrets: CfnTaskDefinition.SecretProperty[] = (container as any).secrets as CfnTaskDefinition.SecretProperty[];
if (!secrets.find((entry) => entry.name === LUMIGO_TRACER_TOKEN_ENV_VAR_NAME)) {
container.addSecret(LUMIGO_TRACER_TOKEN_ENV_VAR_NAME, tokenSecret);
}
} else {
// Could not figure out which type of secret value it is, we go for the unwrap
container.addEnvironment(LUMIGO_TRACER_TOKEN_ENV_VAR_NAME, this.props.lumigoToken.unsafeUnwrap());
}
});
taskDefinition.node.addValidation(new EcsTaskDefinitionLumigoInjectorVolumeValidation(taskDefinition));
taskDefinition.node.addValidation(new EcsTaskDefinitionLumigoInjectorContainerValidation(taskDefinition));
otherContainers.forEach(container => {
taskDefinition.node.addValidation(new EcsContainerDefinitionLumigoInjectorVolumeMountPointValidation(container));
taskDefinition.node.addValidation(new EcsContainerDefinitionLumigoInjectorContainerConditionValidation(container, injectorContainer));
taskDefinition.node.addValidation(new EcsContainerDefinitionHasLumigoInjectorEnvVarValidation(container));
if (tokenSecret) {
taskDefinition.node.addValidation(new EcsContainerDefinitionHasLumigoTracerTokenSecretValidation(container));
} else {
taskDefinition.node.addValidation(new EcsContainerDefinitionHasLumigoTracerTokenEnvVarValidation(
container, this.props.lumigoToken.unsafeUnwrap(),
));
}
});
if (!!props.applyAutoTraceTag) {
this.applyAutotraceTag(taskDefinition);
}
if (!!props.lumigoTag) {
this.applyLumigoTag(taskDefinition, props.lumigoTag!);
}
}
/**
* Apply Lumigo autotracing for the provided Lambda function if it uses a supported Node.js or Python runtime.
* If the runtime used by the provided function is not supported by [Lumigo Lambda Auto-Tracing](https://docs.lumigo.io/docs/auto-instrumentation),
* a warning will be added to the CloudFormation template.
*/
public traceLambda(lambda: SupportedLambdaFunction, props: TraceLambdaProps = {
enableW3CTraceContext: true,
applyAutoTraceTag: true,
}) {
// For the people not using TypeScript, we need to perform some basic type validation
assertExpectedConstructType('traceLambda', 'Function', lambda, (c) => c instanceof Function);
const layerType = this.getLayerType(lambda);
if (!layerType) {
this.warning(lambda, 'The runtime used by this function cannot be auto-traced by Lumigo.');
return;
}
const region = Stack.of(lambda).region;
const layerArn = !!props.layerVersion ? `arn:aws:lambda:${region}:114300393969:layer:${layerType}:${props.layerVersion}` : this.getLayerLatestArn(region, layerType);
lambda.addLayers(LayerVersion.fromLayerVersionArn(lambda, 'LumigoLayer', layerArn));
lambda.addEnvironment(LUMIGO_TRACER_TOKEN_ENV_VAR_NAME, this.props.lumigoToken.unsafeUnwrap());
lambda.node.addValidation(new HasExactlyOneLumigoLayerValidation(lambda));
lambda.node.addValidation(new HasLumigoTracerEnvVarValidation(lambda));
if (layerType === LambdaLayerType.NODE) {
lambda.addEnvironment(AWS_LAMBDA_EXEC_WRAPPER_ENV_VAR_NAME, AWS_LAMBDA_EXEC_WRAPPER_ENV_VAR_VALUE);
lambda.node.addValidation(new HasAwsLambdaExecWrapperEnvVarValidation(lambda));
} else if (layerType == LambdaLayerType.PYTHON) {
/* eslint-disable */
/*
* The handler is well hidden in the CfnFunction resource :-(
*/
const nodeAny = (lambda.node as any);
const handler = nodeAny?._children['Resource']?.handler;
if (!!nodeAny && !!nodeAny._children['Resource']) {
nodeAny._children['Resource'].handler = LUMIGO_LAMBDA_PYTHON_HANDLER;
}
/* eslint-enable */
lambda.addEnvironment(LUMIGO_ORIGINAL_HANDLER_ENV_VAR_NAME, handler);
lambda.node.addValidation(new HasAwsLambdaOriginalHandlerEnvVarValidation(lambda));
lambda.node.addValidation(new HasLumigoPythonHandlerInResourceValidation(lambda));
}
const enableW3CTraceContext = isTrueOrUndefined(props.enableW3CTraceContext);
lambda.addEnvironment(LUMIGO_PROPAGATE_W3C_ENV_VAR_NAME, String(enableW3CTraceContext));
lambda.node.addValidation(new HasLumigoPropagateW3CEnvVarValidation(lambda, enableW3CTraceContext));
if (!!props.applyAutoTraceTag) {
this.applyAutotraceTag(lambda);
}
if (!!props.lumigoTag) {
this.applyLumigoTag(lambda, props.lumigoTag!);
}
this.info(lambda, `This function has been modified with Lumigo auto-tracing by the '${LUMIGO_AUTOTRACE_TAG_VALUE}' package.`);
}
private applyAutotraceTag(construct: Construct): void {
Tags.of(construct).add(LUMIGO_AUTOTRACE_TAG_NAME, LUMIGO_AUTOTRACE_TAG_VALUE);
}
private applyLumigoTag(construct: Construct, value: string): void {
Tags.of(construct).add(LUMIGO_TAG_TAG_NAME, value);
}
private getLayerType(lambda: SupportedLambdaFunction): LambdaLayerType | undefined {
switch (lambda.runtime) {
case Runtime.NODEJS_10_X:
case Runtime.NODEJS_12_X:
case Runtime.NODEJS_14_X:
case Runtime.NODEJS_16_X:
return LambdaLayerType.NODE;
case Runtime.PYTHON_3_7:
case Runtime.PYTHON_3_8:
case Runtime.PYTHON_3_9:
return LambdaLayerType.PYTHON;
default:
if (!lambda.runtime && lambda instanceof NodejsFunction) {
/*
* The NodejsType has the runtime as optional, and then the
* function will use the default one. The default runtime type
* is set up conservatively in AWS CDK, with older versions,
* long supported by Lumigo. So we can rely on the type of
* the construct to tell us the layer type.
*/
return LambdaLayerType.NODE;
}
/*
* Check if it is enumeration entries that do not
* exist in the minimum CDK version we support.
*/
if (lambda.runtime.name.startsWith('nodejs')) {
return LambdaLayerType.NODE;
} else if (lambda.runtime.name.startsWith('python3.')) {
return LambdaLayerType.PYTHON;
}
}
return undefined;
}
private getLayerLatestArn(region: string, type: LambdaLayerType): string {
const latestLayerArnByRegion = (type === LambdaLayerType.NODE ? lambdaLayersNodejs : lambdaLayersPython);
const latestLayerArn = (new Map(Object.entries(latestLayerArnByRegion))).get(region);
if (!latestLayerArn) {
throw new UnsupportedLambdaLayerRegion(type, region);
}
return latestLayerArn;
}
}
interface TypeTestFn {
(arg: Construct): boolean;
}
const isSupportedEcsTaskDefinitionConstruct = (construct: Construct): construct is SupportedEcsTaskDefinition => {
return construct instanceof FargateTaskDefinition || construct instanceof Ec2TaskDefinition;
};
const isSupportedEcsScheduledTaskConstruct = (construct: Construct): construct is SupportedEcsScheduledTask => {
return construct instanceof ScheduledEc2Task || construct instanceof ScheduledFargateTask;
};
const isSupportedEcsServiceConstruct = (construct: Construct): construct is SupportedEcsPatternsService => {
return construct instanceof Ec2Service ||
construct instanceof FargateService ||
construct instanceof ApplicationLoadBalancedEc2Service ||
construct instanceof ApplicationLoadBalancedFargateService ||
construct instanceof ApplicationMultipleTargetGroupsEc2Service ||
construct instanceof ApplicationMultipleTargetGroupsFargateService ||
construct instanceof NetworkLoadBalancedEc2Service ||
construct instanceof NetworkLoadBalancedFargateService ||
construct instanceof NetworkMultipleTargetGroupsEc2Service ||
construct instanceof NetworkMultipleTargetGroupsFargateService ||
construct instanceof QueueProcessingEc2Service ||
construct instanceof QueueProcessingFargateService;
};
function assertExpectedConstructType(invokedMethod: string, expectedType: string, construct: Construct, test: TypeTestFn) {
if (!test(construct)) {
let message = `Lumigo.${invokedMethod} needs a ${expectedType} as input`;
const additionalHint = getTypeErrorHint(construct);
if (additionalHint) {
message = `${message}; ${additionalHint}`;
}
throw new Error(message);
}
}
function getTypeErrorHint(construct: Construct): (string | undefined) {
if (construct instanceof App) {
return 'are you maybe looking for Lumigo.traceEverything instead?';
} else if (construct instanceof Function) {
return 'are you maybe looking for Lumigo.traceLambda instead?';
} else if (construct instanceof TaskDefinition) {
return 'are you maybe looking for Lumigo.traceEcsTaskDefinition instead?';
} else if (isSupportedEcsScheduledTaskConstruct(construct)) {
return 'are you maybe looking for Lumigo.traceEcsScheduledTask instead?';
} else if (isSupportedEcsServiceConstruct(construct)) {
return 'are you maybe looking for Lumigo.traceEcsService instead?';
}
return undefined;
}
abstract class TaskDefinitionValidation implements IValidation {
private readonly taskDefinition: TaskDefinition;
private readonly issues: string[] = [];
constructor(taskDefinition: TaskDefinition) {
this.taskDefinition = taskDefinition;
}
protected addIssue = (issue: string) => this.issues.push(issue);
public validate(): string[] {
this.validateTaskDefinition(this.taskDefinition);
return this.issues;
}
protected abstract validateTaskDefinition(taskDefinition: TaskDefinition): void;
}
class EcsTaskDefinitionLumigoInjectorContainerValidation extends TaskDefinitionValidation {
constructor(taskDefinition: TaskDefinition) {
super(taskDefinition);
}
protected validateTaskDefinition(taskDefinition: TaskDefinition): void {
var lumigoInjectorContainers = getTaskDefinitionContainers(taskDefinition).filter(container => container.containerName == 'lumigo-injector');
switch (lumigoInjectorContainers.length) {
case 0: {
this.addIssue(`No container called '${LUMIGO_INJECTOR_CONTAINER_NAME}' found; did you modify the task definition after adding Lumigo tracing to it?`);
return;
}
case 1: {
// TODO Validate container image
break;
}
default: {
this.addIssue(`${lumigoInjectorContainers.length} containers called '${LUMIGO_INJECTOR_CONTAINER_NAME}' found; did you set Lumigo tracing up multiple times for this task definition?`);
return;
}
}
}
}
class EcsTaskDefinitionLumigoInjectorVolumeValidation extends TaskDefinitionValidation {
constructor(taskDefinition: TaskDefinition) {
super(taskDefinition);
}
protected validateTaskDefinition(taskDefinition: TaskDefinition): void {
var lumigoInjectorVolumes = getTaskDefinitionVolumes(taskDefinition)?.filter(volume => volume.name == 'lumigo-injector');
switch (lumigoInjectorVolumes?.length || 0) {
case 0: {
this.addIssue(`No volume called '${LUMIGO_INJECTOR_VOLUME_NAME}' found; did you modify the task definition after adding Lumigo tracing to it?`);
return;
}
case 1: {
break;
}
default: {
this.addIssue(`${lumigoInjectorVolumes.length} volume called '${LUMIGO_INJECTOR_VOLUME_NAME}' found; did you set Lumigo tracing up multiple times for this task definition?`);
return;
}
}
const lumigoInjectorVolume = lumigoInjectorVolumes[0];
if (!!lumigoInjectorVolume.dockerVolumeConfiguration) {
this.addIssue(`The '${LUMIGO_INJECTOR_VOLUME_NAME}' volume has 'dockerVolumeConfiguration's attached to it; did you modify the task definition after adding Lumigo tracing to it?`);
}
if (!!lumigoInjectorVolume.efsVolumeConfiguration) {
this.addIssue(`The '${LUMIGO_INJECTOR_VOLUME_NAME}' volume has 'efsVolumeConfiguration's attached to it; did you modify the task definition after adding Lumigo tracing to it?`);
}
}
}
abstract class ContainerDefinitionValidation implements IValidation {
private readonly containerDefinition: ContainerDefinition;
private readonly issues: string[] = [];
constructor(containerDefinition: ContainerDefinition) {
this.containerDefinition = containerDefinition;
}
protected addIssue = (issue: string) => this.issues.push(`Container '${this.containerDefinition.containerName}': ${issue}`);
public validate(): string[] {
this.validateContainerDefinition(this.containerDefinition);
return this.issues;
}
protected abstract validateContainerDefinition(containerDefinition: ContainerDefinition): void;
}
class EcsContainerDefinitionLumigoInjectorVolumeMountPointValidation extends ContainerDefinitionValidation {
constructor(containerDefinition: ContainerDefinition) {
super(containerDefinition);
}
protected validateContainerDefinition(containerDefinition: ContainerDefinition) {
const injectorVolumeMountPoint = containerDefinition.mountPoints?.find(mountPoint => mountPoint.sourceVolume === LUMIGO_INJECTOR_VOLUME_NAME);
if (!injectorVolumeMountPoint) {
this.addIssue(`No mount point '${LUMIGO_INJECTOR_VOLUME_NAME}' found`);
} else {
if (!injectorVolumeMountPoint.readOnly) {
this.addIssue(`The mount point for the '${LUMIGO_INJECTOR_VOLUME_NAME}' volume is not set to read-only`);
}
if (injectorVolumeMountPoint.containerPath !== LUMIGO_INJECTOR_VOLUME_MOUNT_POINT) {
this.addIssue(`The container path of the mount point for the '${LUMIGO_INJECTOR_VOLUME_NAME}' volume is not set to '${LUMIGO_INJECTOR_VOLUME_MOUNT_POINT}'`);
}
}
}
}
class EcsContainerDefinitionLumigoInjectorContainerConditionValidation extends ContainerDefinitionValidation {
readonly injectorContainer: ContainerDefinition;
constructor(containerDefinition: ContainerDefinition, injectorContainer: ContainerDefinition) {
super(containerDefinition);
this.injectorContainer = injectorContainer;
}
protected validateContainerDefinition(containerDefinition: ContainerDefinition) {
const lumigoInjectorContainerDependency = containerDefinition.containerDependencies
.find(containerDependency => containerDependency.container === this.injectorContainer);
if (lumigoInjectorContainerDependency?.condition !== ContainerDependencyCondition.COMPLETE) {
this.addIssue(`The container dependency condition of the '${containerDefinition.containerName}' on the '${this.injectorContainer.containerName}' is not set to '${ContainerDependencyCondition.COMPLETE}'`);
}
}
}
class EcsContainerDefinitionHasLumigoInjectorEnvVarValidation extends ContainerDefinitionValidation {
constructor(containerDefinition: ContainerDefinition) {
super(containerDefinition);
}
protected validateContainerDefinition(containerDefinition: ContainerDefinition) {
const environment: { [key: string]: string } = (containerDefinition as any).environment;
if (environment[LUMIGO_INJECTOR_ENV_VAR_NAME] !== LUMIGO_INJECTOR_ENV_VAR_VALUE) {
this.addIssue(`Container '${containerDefinition.containerName}': The '${LUMIGO_INJECTOR_ENV_VAR_NAME}' does not have the expected value '${LUMIGO_INJECTOR_ENV_VAR_VALUE}'`);
}
}
}
class EcsContainerDefinitionHasLumigoTracerTokenSecretValidation extends ContainerDefinitionValidation {
constructor(containerDefinition: ContainerDefinition) {
super(containerDefinition);
}
protected validateContainerDefinition(containerDefinition: ContainerDefinition) {
const secrets: CfnTaskDefinition.SecretProperty[] = (containerDefinition as any).secrets;
if (!secrets.find((entry) => entry.name === LUMIGO_TRACER_TOKEN_ENV_VAR_NAME)) {
// Don't print out the token value, who knows where these logs end up
this.addIssue(`The '${LUMIGO_TRACER_TOKEN_ENV_VAR_NAME}' does is not mounted as a secret built from the SecretValue passed to the Lumigo object`);
}
}
}
class EcsContainerDefinitionHasLumigoTracerTokenEnvVarValidation extends ContainerDefinitionValidation {
readonly expectedToken: string;
constructor(containerDefinition: ContainerDefinition, expectedToken: string) {
super(containerDefinition);
this.expectedToken = expectedToken;
}
protected validateContainerDefinition(containerDefinition: ContainerDefinition) {
const environment: { [key: string]: string } = (containerDefinition as any).environment;
if (environment[LUMIGO_TRACER_TOKEN_ENV_VAR_NAME] !== this.expectedToken) {
// Don't print out the token value, who knows where these logs end up
this.addIssue(`The '${LUMIGO_TRACER_TOKEN_ENV_VAR_NAME}' does not have the expected value provided in the SecretValue passed to the Lumigo object`);
}
}
}
class HasExactlyOneLumigoLayerValidation implements IValidation {
private readonly lambda: SupportedLambdaFunction;
constructor(lambda: SupportedLambdaFunction) {
this.lambda = lambda;
}