-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from DataDog/stephenf/add-enhanced-metrics
Add enhanced lambda metrics
- Loading branch information
Showing
15 changed files
with
270 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { getEnvValue, sendDistributionMetric } from "../index"; | ||
|
||
import { parseTagsFromARN } from "../utils/arn"; | ||
import { getColdStartTag } from "../utils/cold-start"; | ||
|
||
const ENHANCED_LAMBDA_METRICS_NAMESPACE = "aws.lambda.enhanced"; | ||
|
||
export function incrementInvocationsMetric(functionARN: string): void { | ||
const tags = [...parseTagsFromARN(functionARN), getColdStartTag()]; | ||
sendDistributionMetric(`${ENHANCED_LAMBDA_METRICS_NAMESPACE}.invocations`, 1, ...tags); | ||
} | ||
|
||
export function incrementErrorsMetric(functionARN: string): void { | ||
const tags = [...parseTagsFromARN(functionARN), getColdStartTag()]; | ||
sendDistributionMetric(`${ENHANCED_LAMBDA_METRICS_NAMESPACE}.errors`, 1, ...tags); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { MetricsConfig, MetricsListener } from "./listener"; | ||
export { KMSService } from "./kms-service"; | ||
export { incrementErrorsMetric, incrementInvocationsMetric } from "./enhanced-metrics"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { parseLambdaARN, parseTagsFromARN } from "./arn"; | ||
|
||
describe("arn utils", () => { | ||
it("parses arn properties", () => { | ||
expect(parseLambdaARN("arn:aws:lambda:us-east-1:123497598159:function:my-test-lambda")).toEqual({ | ||
account_id: "123497598159", | ||
functionname: "my-test-lambda", | ||
region: "us-east-1", | ||
}); | ||
}); | ||
|
||
it("parses arn properties with version alias", () => { | ||
expect(parseLambdaARN("arn:aws:lambda:us-east-1:123497598159:function:my-test-lambda:my-version-alias")).toEqual({ | ||
account_id: "123497598159", | ||
functionname: "my-test-lambda", | ||
region: "us-east-1", | ||
}); | ||
}); | ||
|
||
it("parses arn tags", () => { | ||
const parsedTags = parseTagsFromARN("arn:aws:lambda:us-east-1:123497598159:function:my-test-lambda"); | ||
for (const tag of ["account_id:123497598159", "functionname:my-test-lambda", "region:us-east-1"]) { | ||
expect(parsedTags).toContain(tag); | ||
} | ||
}); | ||
|
||
it("parses arn tags with version", () => { | ||
const parsedTags = parseTagsFromARN( | ||
"arn:aws:lambda:us-east-1:123497598159:function:my-test-lambda:my-version-alias", | ||
); | ||
for (const tag of ["account_id:123497598159", "functionname:my-test-lambda", "region:us-east-1"]) { | ||
expect(parsedTags).toContain(tag); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** Parse properties of the ARN into an object */ | ||
export function parseLambdaARN(functionARN: string) { | ||
// Disabling variable name because account_id is the key we need to use for the tag | ||
// tslint:disable-next-line: variable-name | ||
const [, , , region, account_id, , functionname] = functionARN.split(":", 7); | ||
return { region, account_id, functionname }; | ||
} | ||
|
||
/** | ||
* Parse keyValueObject to get the array of key:value strings to use in Datadog metric submission | ||
* @param obj The object whose properties and values we want to get key:value strings from | ||
*/ | ||
function makeTagStringsFromObject(keyValueObject: { [key: string]: string }) { | ||
return Object.entries(keyValueObject).map(([tagKey, tagValue]) => `${tagKey}:${tagValue}`); | ||
} | ||
|
||
/** Get the array of "key:value" string tags from the Lambda ARN */ | ||
export function parseTagsFromARN(functionARN: string) { | ||
return makeTagStringsFromObject(parseLambdaARN(functionARN)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { _resetColdStart, didFunctionColdStart, setColdStart } from "./cold-start"; | ||
|
||
beforeEach(_resetColdStart); | ||
afterAll(_resetColdStart); | ||
|
||
describe("cold-start", () => { | ||
it("identifies cold starts on the first execution", () => { | ||
setColdStart(); | ||
expect(didFunctionColdStart()).toEqual(true); | ||
}); | ||
|
||
it("identifies non-cold starts on subsequent executions", () => { | ||
setColdStart(); | ||
expect(didFunctionColdStart()).toEqual(true); | ||
|
||
setColdStart(); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
|
||
setColdStart(); | ||
expect(didFunctionColdStart()).toEqual(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
let functionDidColdStart = true; | ||
|
||
let isColdStartSet = false; | ||
|
||
/** | ||
* Use global variables to determine whether the container cold started | ||
* On the first container run, isColdStartSet and functionDidColdStart are true | ||
* For subsequent executions isColdStartSet will be true and functionDidColdStart will be false | ||
*/ | ||
export function setColdStart() { | ||
functionDidColdStart = !isColdStartSet; | ||
isColdStartSet = true; | ||
} | ||
|
||
export function didFunctionColdStart() { | ||
return functionDidColdStart; | ||
} | ||
|
||
export function getColdStartTag() { | ||
return `cold_start:${didFunctionColdStart()}`; | ||
} | ||
|
||
// For testing, reset the globals to their original values | ||
export function _resetColdStart() { | ||
functionDidColdStart = true; | ||
isColdStartSet = false; | ||
} |
Oops, something went wrong.