Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jest.mock("aws-embedded-metrics", () => ({
Count: "Count",
Milliseconds: "Milliseconds",
},
StorageResolution: { High: 1, Standard: 60 },
}));

import { GetObjectCommand, NoSuchKey } from "@aws-sdk/client-s3";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Unit, createMetricsLogger } from "aws-embedded-metrics";
import {
StorageResolution,
Unit,
createMetricsLogger,
} from "aws-embedded-metrics";
import { CallbackMetrics, createMetricLogger } from "services/metrics";

jest.mock("aws-embedded-metrics");
jest.mock("aws-embedded-metrics", () => ({
Unit: { Count: "Count" },
StorageResolution: { High: 1, Standard: 60 },
createMetricsLogger: jest.fn(),
}));

const mockPutMetric = jest.fn();
const mockSetDimensions = jest.fn();
Expand Down Expand Up @@ -80,6 +88,7 @@ describe("CallbackMetrics", () => {
"EventsReceived",
1,
Unit.Count,
StorageResolution.High,
);
});
});
Expand All @@ -92,6 +101,7 @@ describe("CallbackMetrics", () => {
"TransformationsSuccessful",
1,
Unit.Count,
StorageResolution.High,
);
});
});
Expand All @@ -104,6 +114,7 @@ describe("CallbackMetrics", () => {
"TransformationsFailed",
1,
Unit.Count,
StorageResolution.High,
);
});
});
Expand All @@ -116,6 +127,7 @@ describe("CallbackMetrics", () => {
"CallbacksInitiated",
1,
Unit.Count,
StorageResolution.High,
);
});
});
Expand All @@ -128,6 +140,7 @@ describe("CallbackMetrics", () => {
"ValidationErrors",
1,
Unit.Count,
StorageResolution.High,
);
});
});
Expand All @@ -140,6 +153,7 @@ describe("CallbackMetrics", () => {
"FilteringStarted",
1,
Unit.Count,
StorageResolution.High,
);
});
});
Expand All @@ -152,6 +166,7 @@ describe("CallbackMetrics", () => {
"FilteringMatched",
1,
Unit.Count,
StorageResolution.High,
);
});
});
Expand Down
55 changes: 47 additions & 8 deletions lambdas/client-transform-filter-lambda/src/services/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Unit, createMetricsLogger } from "aws-embedded-metrics";
import {
StorageResolution,
Unit,
createMetricsLogger,
} from "aws-embedded-metrics";
import type { MetricsLogger } from "aws-embedded-metrics";

export const createMetricLogger = (): MetricsLogger => {
Expand All @@ -21,30 +25,65 @@ export class CallbackMetrics {
constructor(private readonly metrics: MetricsLogger) {}

emitEventReceived(): void {
this.metrics.putMetric("EventsReceived", 1, Unit.Count);
this.metrics.putMetric(
"EventsReceived",
1,
Unit.Count,
StorageResolution.High,
);
}

emitTransformationSuccess(): void {
this.metrics.putMetric("TransformationsSuccessful", 1, Unit.Count);
this.metrics.putMetric(
"TransformationsSuccessful",
1,
Unit.Count,
StorageResolution.High,
);
}

emitTransformationFailure(): void {
this.metrics.putMetric("TransformationsFailed", 1, Unit.Count);
this.metrics.putMetric(
"TransformationsFailed",
1,
Unit.Count,
StorageResolution.High,
);
}

emitDeliveryInitiated(): void {
this.metrics.putMetric("CallbacksInitiated", 1, Unit.Count);
this.metrics.putMetric(
"CallbacksInitiated",
1,
Unit.Count,
StorageResolution.High,
);
}

emitValidationError(): void {
this.metrics.putMetric("ValidationErrors", 1, Unit.Count);
this.metrics.putMetric(
"ValidationErrors",
1,
Unit.Count,
StorageResolution.High,
);
}

emitFilteringStarted(): void {
this.metrics.putMetric("FilteringStarted", 1, Unit.Count);
this.metrics.putMetric(
"FilteringStarted",
1,
Unit.Count,
StorageResolution.High,
);
}

emitFilteringMatched(): void {
this.metrics.putMetric("FilteringMatched", 1, Unit.Count);
this.metrics.putMetric(
"FilteringMatched",
1,
Unit.Count,
StorageResolution.High,
);
}
}
143 changes: 139 additions & 4 deletions lambdas/https-client-lambda/src/__tests__/admit-lua.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import admitLuaSrc from "services/admit.lua";
import { createRedisStore, evalLua } from "__tests__/helpers/lua-redis-mock";

// ARGV: [now, capacity, targetRateLimit, cooldownMs, recoveryPeriodMs, probeRateLimit, targetBatchSize]
// ARGV: [now, capacity, targetRateLimit, cooldownMs, recoveryPeriodMs, probeRateLimit, targetBatchSize, cbEnabled]
// KEYS: [epKey]
// Returns: [consumedTokens, reason, retryAfterMs, effectiveRate]

Expand All @@ -13,6 +13,7 @@ type AdmitArgs = {
recoveryPeriodMs: number;
probeRateLimit: number;
targetBatchSize: number;
cbEnabled: boolean;
};

const defaultArgs: AdmitArgs = {
Expand All @@ -23,6 +24,7 @@ const defaultArgs: AdmitArgs = {
recoveryPeriodMs: 600_000,
probeRateLimit: 1 / 60,
targetBatchSize: 1,
cbEnabled: true,
};

type AdmitResult = {
Expand All @@ -49,6 +51,7 @@ function runAdmit(
merged.recoveryPeriodMs.toString(),
merged.probeRateLimit.toString(),
merged.targetBatchSize.toString(),
merged.cbEnabled ? "1" : "0",
],
store,
) as [number, string, number, number];
Expand All @@ -62,7 +65,7 @@ function runAdmit(

describe("admit.lua", () => {
describe("rate limiting", () => {
it("enters half-open probe on a fresh endpoint with no prior state", () => {
it("rate-limits on a fresh endpoint with no prior state", () => {
const store = createRedisStore();
const now = 1_000_000;

Expand All @@ -76,6 +79,21 @@ describe("admit.lua", () => {
expect(effectiveRate).toBeCloseTo(1 / 60, 5);
});

it("generates a probe token on the second call after enough elapsed time", () => {
const store = createRedisStore();

runAdmit(store, { now: 1_000_000, targetRateLimit: 10 });

const { consumedTokens, effectiveRate, reason } = runAdmit(store, {
now: 1_060_001,
targetRateLimit: 10,
});

expect(effectiveRate).toBeCloseTo(1 / 60, 5);
expect(consumedTokens).toBe(1);
expect(reason).toBe("some_allowed");
});

it("does not persist circuit state on first contact", () => {
const store = createRedisStore();
const now = 1_000_000;
Expand Down Expand Up @@ -109,7 +127,7 @@ describe("admit.lua", () => {
});

expect(consumedTokens).toBeGreaterThanOrEqual(1);
expect(reason).toBe("allowed");
expect(reason).toBe("some_allowed");
});

it("allows a single request when bucket has tokens from refill", () => {
Expand All @@ -131,7 +149,7 @@ describe("admit.lua", () => {
});

expect(consumedTokens).toBe(1);
expect(reason).toBe("allowed");
expect(reason).toBe("some_allowed");
expect(retryAfterMs).toBe(0);
});

Expand Down Expand Up @@ -511,4 +529,121 @@ describe("admit.lua", () => {
expect(store.has("ep:target-b")).toBe(true);
});
});

describe("circuit breaker disabled (cbEnabled = false)", () => {
it("uses full targetRateLimit on a fresh endpoint with no prior state", () => {
const store = createRedisStore();
const now = 1_000_000;

const { effectiveRate } = runAdmit(store, {
now,
targetRateLimit: 10,
cbEnabled: false,
});

expect(effectiveRate).toBe(10);
});

it("applies initial values on fresh endpoint so first call has no tokens", () => {
const store = createRedisStore();
const now = 1_000_000;

const { consumedTokens, effectiveRate, reason } = runAdmit(store, {
now,
targetRateLimit: 10,
cbEnabled: false,
});

expect(effectiveRate).toBe(10);
expect(consumedTokens).toBe(0);
expect(reason).toBe("rate_limited");
});

it("generates tokens at full rate after initial contact", () => {
const store = createRedisStore();

runAdmit(store, {
now: 1_000_000,
targetRateLimit: 10,
cbEnabled: false,
});
const { consumedTokens, reason } = runAdmit(store, {
now: 1_000_100,
targetRateLimit: 10,
cbEnabled: false,
});

expect(consumedTokens).toBe(1);
expect(reason).toBe("some_allowed");
});

it("ignores is_open state when CB is disabled", () => {
const store = createRedisStore();
const now = 1_000_000;
store.set(
"ep:t1",
new Map([
["is_open", "1"],
["switched_at", now.toString()],
["bucket_tokens", "5"],
["bucket_refilled_at", now.toString()],
]),
);

const { consumedTokens, effectiveRate, reason } = runAdmit(store, {
now,
targetRateLimit: 10,
cbEnabled: false,
});

expect(effectiveRate).toBe(10);
expect(consumedTokens).toBe(1);
expect(reason).toBe("some_allowed");
});

it("does not zero bucket tokens when is_open and CB disabled", () => {
const store = createRedisStore();
const now = 1_000_000;
store.set(
"ep:t1",
new Map([
["is_open", "1"],
["switched_at", now.toString()],
["bucket_tokens", "5"],
["bucket_refilled_at", now.toString()],
]),
);

const { consumedTokens } = runAdmit(store, {
now,
targetRateLimit: 10,
cbEnabled: false,
targetBatchSize: 3,
});

expect(consumedTokens).toBe(3);
});

it("never returns circuit_open when CB is disabled", () => {
const store = createRedisStore();
const now = 1_000_000;
store.set(
"ep:t1",
new Map([
["is_open", "1"],
["switched_at", (now - 10_000).toString()],
["bucket_tokens", "0"],
["bucket_refilled_at", now.toString()],
]),
);

const { reason } = runAdmit(store, {
now,
cooldownMs: 120_000,
cbEnabled: false,
});

expect(reason).not.toBe("circuit_open");
});
});
});
Loading
Loading