|
1 | 1 | import { getEnvValue, sendDistributionMetric } from "../index"; |
2 | 2 |
|
| 3 | +import { Context } from "aws-lambda"; |
3 | 4 | import { parseTagsFromARN } from "../utils/arn"; |
4 | 5 | import { getColdStartTag } from "../utils/cold-start"; |
| 6 | +import { getProcessVersion } from "../utils/process-version"; |
5 | 7 |
|
6 | 8 | const ENHANCED_LAMBDA_METRICS_NAMESPACE = "aws.lambda.enhanced"; |
7 | 9 |
|
8 | | -export function incrementInvocationsMetric(functionARN: string): void { |
9 | | - const tags = [...parseTagsFromARN(functionARN), getColdStartTag()]; |
10 | | - sendDistributionMetric(`${ENHANCED_LAMBDA_METRICS_NAMESPACE}.invocations`, 1, ...tags); |
| 10 | +// Same tag strings added to normal Lambda integration metrics |
| 11 | +enum RuntimeTagValues { |
| 12 | + Node8 = "nodejs8.10", |
| 13 | + Node10 = "nodejs10.x", |
11 | 14 | } |
12 | 15 |
|
13 | | -export function incrementErrorsMetric(functionARN: string): void { |
14 | | - const tags = [...parseTagsFromARN(functionARN), getColdStartTag()]; |
15 | | - sendDistributionMetric(`${ENHANCED_LAMBDA_METRICS_NAMESPACE}.errors`, 1, ...tags); |
| 16 | +/** |
| 17 | + * Uses process.version to create a runtime tag |
| 18 | + * If a version cannot be identified, returns null |
| 19 | + * See https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html |
| 20 | + */ |
| 21 | +export function getRuntimeTag(): string | null { |
| 22 | + const processVersion = getProcessVersion(); |
| 23 | + let processVersionTagString: string | null = null; |
| 24 | + |
| 25 | + if (processVersion.startsWith("v8.10")) { |
| 26 | + processVersionTagString = RuntimeTagValues.Node8; |
| 27 | + } |
| 28 | + |
| 29 | + if (processVersion.startsWith("v10")) { |
| 30 | + processVersionTagString = RuntimeTagValues.Node10; |
| 31 | + } |
| 32 | + |
| 33 | + if (!processVersionTagString) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + return `runtime:${processVersionTagString}`; |
| 38 | +} |
| 39 | + |
| 40 | +export function getEnhancedMetricTags(context: Context): string[] { |
| 41 | + const tags = [ |
| 42 | + ...parseTagsFromARN(context.invokedFunctionArn), |
| 43 | + getColdStartTag(), |
| 44 | + `memorysize:${context.memoryLimitInMB}`, |
| 45 | + ]; |
| 46 | + |
| 47 | + const runtimeTag = getRuntimeTag(); |
| 48 | + if (runtimeTag) { |
| 49 | + tags.push(runtimeTag); |
| 50 | + } |
| 51 | + |
| 52 | + return tags; |
| 53 | +} |
| 54 | + |
| 55 | +export function incrementInvocationsMetric(context: Context): void { |
| 56 | + sendDistributionMetric(`${ENHANCED_LAMBDA_METRICS_NAMESPACE}.invocations`, 1, ...getEnhancedMetricTags(context)); |
| 57 | +} |
| 58 | + |
| 59 | +export function incrementErrorsMetric(context: Context): void { |
| 60 | + sendDistributionMetric(`${ENHANCED_LAMBDA_METRICS_NAMESPACE}.errors`, 1, ...getEnhancedMetricTags(context)); |
16 | 61 | } |
0 commit comments