Skip to content
Open
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
16 changes: 15 additions & 1 deletion src/metrics/listener.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ describe("MetricsListener", () => {
jest.spyOn(TracerWrapper.prototype, "flushMetrics").mockImplementation(() => {
order.push("tracer");
});
jest.spyOn(TracerWrapper.prototype, "flushFeatureFlags").mockImplementation(() => {
order.push("feature-flags");
});
jest.spyOn(LambdaDogStatsD.prototype, "flush").mockImplementation(async () => {
order.push("lambda");
if (throws) throw new Error("Lambda metrics flush failed");
Expand All @@ -72,10 +75,21 @@ describe("MetricsListener", () => {
await listener.onStartInvocation({});
await listener.onCompleteInvocation();

expect(order).toEqual(["tracer", "lambda", "extension"]);
expect(order).toEqual(["tracer", "feature-flags", "lambda", "extension"]);
expect(extensionFlush).toHaveBeenCalledWith(true);
});

it("starts the feature flag flush before awaiting the other flushes", async () => {
const featureFlagsFlush = jest.spyOn(TracerWrapper.prototype, "flushFeatureFlags").mockImplementation(() => {});
const listener = new MetricsListener(new MockKMS("") as any, config);
await listener.onStartInvocation({});

const completed = listener.onCompleteInvocation();

expect(featureFlagsFlush).toHaveBeenCalledTimes(1);
await completed;
});

it("flushes tracer metrics even when the extension is absent", async () => {
jest.spyOn(extension, "isExtensionRunning").mockResolvedValue(false);
const tracerFlush = jest.spyOn(TracerWrapper.prototype, "flushMetrics").mockImplementation(() => {});
Expand Down
1 change: 1 addition & 0 deletions src/metrics/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export class MetricsListener {
public async onCompleteInvocation() {
// Flush tracer metrics before the extension and independently of extension detection.
this.tracerWrapper.flushMetrics();
this.tracerWrapper.flushFeatureFlags();

// Flush any metrics
try {
Expand Down
11 changes: 11 additions & 0 deletions src/trace/tracer-wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ describe("TracerWrapper", () => {
});
});

it("flushFeatureFlags publishes to dd-trace's feature flag writers", () => {
const channel = require("dc-polyfill").channel("ffe:writers:flush");
const onFlush = jest.fn();
channel.subscribe(onFlush);

new TracerWrapper().flushFeatureFlags();

channel.unsubscribe(onFlush);
expect(onFlush).toHaveBeenCalledTimes(1);
});

it("should not call internal setConsumeCheckpoint when arn is not provided", () => {
process.env["DD_DATA_STREAMS_ENABLED"] = "true";
const wrapper = new TracerWrapper();
Expand Down
10 changes: 10 additions & 0 deletions src/trace/tracer-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { dirname, join } from "path";
import { logDebug, logWarning } from "../utils";
import { SpanContextWrapper } from "./span-context-wrapper";
import { TraceSource } from "./trace-context-service";
// tslint:disable-next-line:no-var-requires
const dc = require("dc-polyfill");

// dd-trace's feature flag writers send buffered evaluations and exposures when this channel publishes.
const featureFlagsFlushChannel = dc.channel("ffe:writers:flush");

export interface SpanContext {
toTraceId(): string;
Expand Down Expand Up @@ -136,6 +141,11 @@ export class TracerWrapper {
}
}

public flushFeatureFlags(): void {
// Starts delivery without awaiting it; the awaited metric flushes that follow give it time to finish.
featureFlagsFlushChannel.publish();
}

public injectSpan(span: SpanContext): any {
const dest = {};
this.tracer.inject(span, "text_map", dest);
Expand Down
Loading