import { datadogLambdaVersion } from "../constants" ; import { sendDistributionMetric } from "../index" ; import { Context } from "aws-lambda" ; import { parseTagsFromARN } from "../utils/arn" ; import { getColdStartTag } from "../utils/cold-start" ; import { getProcessVersion } from "../utils/process-version" ; import { writeMetricToStdout } from "./metric-log" ; import { MetricsListener } from "./listener" ; const ENHANCED_LAMBDA_METRICS_NAMESPACE = "aws.lambda.enhanced" ; enum RuntimeTagValues { Node12 = "nodejs12.x" , Node14 = "nodejs14.x" } export function getVersionTag ( ) { return `datadog_lambda:v${ datadogLambdaVersion }` ; } /**
 * Uses process.version to create a runtime tag
 * If a version cannot be identified, returns null
 * See https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
 */ export function getRuntimeTag ( ) { const processVersion = getProcessVersion ( ) ; let processVersionTagString = null ; if ( processVersion . startsWith ( "v12" ) ) { processVersionTagString = RuntimeTagValues . Node12 ; } if ( processVersion . startsWith ( "v14" ) ) { processVersionTagString = RuntimeTagValues . Node14 ; } if ( ! processVersionTagString ) { return null ; } return `runtime:${ processVersionTagString }` ; } export function getEnhancedMetricTags ( context ) { let arnTags = [ `functionname:${ context . functionName }` ] ; if ( context . invokedFunctionArn ) { arnTags = parseTagsFromARN ( context . invokedFunctionArn , context . functionVersion ) ; } const tags = [ ... arnTags , getColdStartTag ( ) , `memorysize:${ context . memoryLimitInMB }` , getVersionTag ( ) ] ; const runtimeTag = getRuntimeTag ( ) ; if ( runtimeTag ) { tags . push ( runtimeTag ) ; } return tags ; } param context param metricName function incrementEnhancedMetric ( listener , metricName , context ) { listener . sendDistributionMetric ( `aws.lambda.enhanced.${ metricName }` , 1 , true , ... getEnhancedMetricTags ( context ) ) ; } export function incrementInvocationsMetric ( listener , context ) { incrementEnhancedMetric ( listener , "invocations" , context ) ; } export function incrementErrorsMetric ( listener , context ) { incrementEnhancedMetric ( listener , "errors" , context ) ; }