-
Notifications
You must be signed in to change notification settings - Fork 36
Add runtime and memorysize tags to enhanced metrics #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,63 @@ | ||
import { Context } from "aws-lambda"; | ||
import { _resetColdStart } from "../utils/cold-start"; | ||
import { getProcessVersion } from "../utils/process-version"; | ||
import { getEnhancedMetricTags, getRuntimeTag } from "./enhanced-metrics"; | ||
|
||
jest.mock("../utils/process-version"); | ||
|
||
const mockedGetProcessVersion = getProcessVersion as jest.Mock<string>; | ||
|
||
const mockARN = "arn:aws:lambda:us-east-1:123497598159:function:my-test-lambda"; | ||
const mockContext = ({ | ||
invokedFunctionArn: mockARN, | ||
memoryLimitInMB: "128", | ||
} as any) as Context; | ||
|
||
describe("getRuntimeTag", () => { | ||
it("returns a null runtime tag when version is not recognized", () => { | ||
mockedGetProcessVersion.mockReturnValue("v6.2.3"); | ||
expect(getRuntimeTag()).toBe(null); | ||
}); | ||
|
||
it("returns the expected tag for v8.10", () => { | ||
mockedGetProcessVersion.mockReturnValue("v8.10.0"); | ||
expect(getRuntimeTag()).toBe("runtime:nodejs8.10"); | ||
}); | ||
|
||
it("returns the expected tag for v10.x", () => { | ||
mockedGetProcessVersion.mockReturnValue("v10.1.0"); | ||
expect(getRuntimeTag()).toBe("runtime:nodejs10.x"); | ||
}); | ||
}); | ||
|
||
describe("getEnhancedMetricTags", () => { | ||
beforeEach(() => { | ||
_resetColdStart(); | ||
}); | ||
afterEach(() => { | ||
_resetColdStart(); | ||
}); | ||
|
||
it("generates tag list with runtime", () => { | ||
mockedGetProcessVersion.mockReturnValue("v8.10.0"); | ||
expect(getEnhancedMetricTags(mockContext)).toStrictEqual([ | ||
"region:us-east-1", | ||
"account_id:123497598159", | ||
"functionname:my-test-lambda", | ||
"cold_start:true", | ||
"memorysize:128", | ||
"runtime:nodejs8.10", | ||
]); | ||
}); | ||
|
||
it("doesn't add runtime tag if version is unrecognized", () => { | ||
mockedGetProcessVersion.mockReturnValue("v6.3.2"); | ||
expect(getEnhancedMetricTags(mockContext)).toStrictEqual([ | ||
"region:us-east-1", | ||
"account_id:123497598159", | ||
"functionname:my-test-lambda", | ||
"cold_start:true", | ||
"memorysize:128", | ||
]); | ||
}); | ||
}); |
This file contains hidden or 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,16 +1,61 @@ | ||
import { getEnvValue, 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"; | ||
|
||
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); | ||
// Same tag strings added to normal Lambda integration metrics | ||
enum RuntimeTagValues { | ||
Node8 = "nodejs8.10", | ||
Node10 = "nodejs10.x", | ||
} | ||
|
||
export function incrementErrorsMetric(functionARN: string): void { | ||
const tags = [...parseTagsFromARN(functionARN), getColdStartTag()]; | ||
sendDistributionMetric(`${ENHANCED_LAMBDA_METRICS_NAMESPACE}.errors`, 1, ...tags); | ||
/** | ||
* 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(): string | null { | ||
const processVersion = getProcessVersion(); | ||
let processVersionTagString: string | null = null; | ||
|
||
if (processVersion.startsWith("v8.10")) { | ||
processVersionTagString = RuntimeTagValues.Node8; | ||
} | ||
|
||
if (processVersion.startsWith("v10")) { | ||
processVersionTagString = RuntimeTagValues.Node10; | ||
} | ||
|
||
if (!processVersionTagString) { | ||
return null; | ||
} | ||
|
||
return `runtime:${processVersionTagString}`; | ||
} | ||
|
||
export function getEnhancedMetricTags(context: Context): string[] { | ||
const tags = [ | ||
...parseTagsFromARN(context.invokedFunctionArn), | ||
getColdStartTag(), | ||
`memorysize:${context.memoryLimitInMB}`, | ||
]; | ||
|
||
const runtimeTag = getRuntimeTag(); | ||
if (runtimeTag) { | ||
tags.push(runtimeTag); | ||
} | ||
|
||
return tags; | ||
} | ||
|
||
export function incrementInvocationsMetric(context: Context): void { | ||
sendDistributionMetric(`${ENHANCED_LAMBDA_METRICS_NAMESPACE}.invocations`, 1, ...getEnhancedMetricTags(context)); | ||
} | ||
|
||
export function incrementErrorsMetric(context: Context): void { | ||
sendDistributionMetric(`${ENHANCED_LAMBDA_METRICS_NAMESPACE}.errors`, 1, ...getEnhancedMetricTags(context)); | ||
} |
This file contains hidden or 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,3 @@ | ||
export function getProcessVersion() { | ||
return process.version; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if it might be useful to keep the minor version, since AWS seems to reserve the right to update it 🤔 . On one hand, it's more accurate and might reveal why a regression occurred, on the other it doesn't match the aws specified runtime.