-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
OJ-2708: Add context to lambda logs
- Loading branch information
Showing
15 changed files
with
210 additions
and
61 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
File renamed without changes.
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,23 @@ | ||
import { Metrics, MetricUnits } from "@aws-lambda-powertools/metrics"; | ||
import { MetricDimensions, MetricNames } from "./metric-types"; | ||
|
||
export class MetricsHelper { | ||
singleMetric: Metrics; | ||
|
||
constructor(metrics = new Metrics()) { | ||
this.singleMetric = metrics.singleMetric(); | ||
} | ||
|
||
captureResponseLatency(start: number, metricValue: string): number { | ||
const latency = Math.floor(performance.now()) - start; | ||
|
||
this.singleMetric.addDimension(MetricDimensions.HTTP, metricValue); | ||
this.singleMetric.addMetric( | ||
MetricNames.ResponseLatency, | ||
MetricUnits.Milliseconds, | ||
latency | ||
); | ||
|
||
return latency; | ||
} | ||
} |
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,44 @@ | ||
import { MetricUnits } from "@aws-lambda-powertools/metrics"; | ||
import { MetricsHelper } from "../metrics-helper"; | ||
import { MetricDimensions, MetricNames } from "../metric-types"; | ||
|
||
jest.mock("@aws-lambda-powertools/metrics", () => ({ | ||
...jest.requireActual("@aws-lambda-powertools/metrics"), | ||
Metrics: jest.fn(() => ({ | ||
singleMetric: () => ({ | ||
addDimension: jest.fn(), | ||
addMetric: jest.fn(), | ||
}), | ||
})), | ||
})); | ||
|
||
const monday31st2021InMilliseconds = 1622502000000; | ||
jest.spyOn(performance, "now").mockReturnValue(monday31st2021InMilliseconds); | ||
|
||
describe("metrics-helper", () => { | ||
let metricsHelper: MetricsHelper; | ||
|
||
beforeEach(() => { | ||
metricsHelper = new MetricsHelper(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it("should add dimension and metric", () => { | ||
const value = "TestHandler"; | ||
const latency = metricsHelper.captureResponseLatency(1622501999000, value); | ||
|
||
expect(latency).toEqual(1000); | ||
expect(metricsHelper.singleMetric.addDimension).toHaveBeenCalledWith( | ||
MetricDimensions.HTTP, | ||
value | ||
); | ||
expect(metricsHelper.singleMetric.addMetric).toHaveBeenCalledWith( | ||
MetricNames.ResponseLatency, | ||
MetricUnits.Milliseconds, | ||
latency | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.