Skip to content

Commit

Permalink
fix: dont collect when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-karlsson committed Nov 28, 2024
1 parent e568bd4 commit 647a106
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 52 deletions.
7 changes: 5 additions & 2 deletions packages/sdk/src/FlagResolverClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import { SimpleFetch } from './types';
const FLAG_PREFIX = 'flags/';

export class ResolveError extends Error {
constructor(public readonly code: FlagEvaluation.ErrorCode, message: string) {
constructor(
public readonly code: FlagEvaluation.ErrorCode,
message: string,
) {
super(message);
}
}
Expand Down Expand Up @@ -304,7 +307,7 @@ export function withTelemetryData(
return new FetchBuilder()
.modifyRequest(async request => {
const monitoring = telemetry.getSnapshot();
if (monitoring) {
if (monitoring.libraryTraces.length > 0) {
const headers = new Headers(request.headers);
const base64Message = btoa(String.fromCharCode(...Monitoring.encode(monitoring).finish()));

Expand Down
6 changes: 2 additions & 4 deletions packages/sdk/src/Telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('Telemetry', () => {
});
counter();
const snapshot = telemetry.getSnapshot();
expect(snapshot).toBeUndefined();
expect(snapshot.libraryTraces.length).toBe(0);
});

it('monitoring gets cleared after snapshot is obtained', () => {
Expand All @@ -73,9 +73,7 @@ describe('Telemetry', () => {
expect(snapshotFirst?.libraryTraces.length).toEqual(1);
expect(snapshotFirst?.libraryTraces[0].traces).toEqual([{ id: LibraryTraces_TraceId.TRACE_ID_STALE_FLAG }]);
const snapshotSecond = telemetry.getSnapshot();
expect(snapshotSecond).toBeTruthy();
// the counter is still registered but the traces are cleared
expect(snapshotSecond?.libraryTraces.length).toEqual(1);
expect(snapshotSecond?.libraryTraces[0].traces).toEqual([]);
expect(snapshotSecond?.libraryTraces.length).toBe(0);
});
});
81 changes: 35 additions & 46 deletions packages/sdk/src/Telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
LibraryTraces,
LibraryTraces_Library,
LibraryTraces_Trace,
LibraryTraces_TraceId,
Monitoring,
} from './generated/confidence/telemetry/v1/telemetry';
Expand All @@ -15,67 +17,54 @@ export type Tag = {

export type Counter = () => void;
export type Meter = (value: number) => void;

export type TraceConsumer = (trace: Omit<LibraryTraces_Trace, 'id'>) => void;
export class Telemetry {
private disabled: boolean;
private logger: Logger;
private readonly disabled: boolean;
private readonly logger: Logger;
private readonly libraryTraces: LibraryTraces[] = [];

constructor(opts: TelemetryOptions) {
this.disabled = opts.disabled;
this.logger = opts.logger;
}

private monitoring: Monitoring = {
libraryTraces: [],
};

private pushTrace(tags: Tag, value: number | undefined = undefined): void {
const library = tags.library;
const version = tags.version;
const existing = this.monitoring.libraryTraces.find(trace => {
return trace.library === library && trace.libraryVersion === version;
private registerLibraryTraces({ library, version, id }: Tag): TraceConsumer {
if (this.disabled) {
return () => {};
}
const traces: LibraryTraces_Trace[] = [];
this.libraryTraces.push({
library: library,
libraryVersion: version,
traces,
});
if (existing) {
existing.traces.push({
id: tags.id,
millisecondDuration: value,
return data => {
this.logger.trace?.(LibraryTraces_TraceId[id], data);
traces.push({
id,
...data,
});
} else {
this.logger.warn?.(`pushTrace() got called before registering tag (${library}, ${version})`);
}
};
}

registerCounter(tag: Tag): Counter {
this.monitoring.libraryTraces.push({
library: tag.library,
libraryVersion: tag.version,
traces: [],
});
return () => {
this.pushTrace(tag);
};
const traceConsumer = this.registerLibraryTraces(tag);
return () => traceConsumer({});
}

registerMeter(tag: Tag): Meter {
this.monitoring.libraryTraces.push({
library: tag.library,
libraryVersion: tag.version,
traces: [],
});
return (value: number) => {
this.pushTrace(tag, value);
};
const traceConsumer = this.registerLibraryTraces(tag);
return (millisecondDuration: number) => traceConsumer({ millisecondDuration });
}

getSnapshot(): Monitoring | undefined {
if (this.disabled) {
return undefined;
}
// retrieve a snapshot with all monitoring data but deep copied
const snapshot = structuredClone(this.monitoring);
this.monitoring.libraryTraces.forEach(trace => {
// only clear traces. keep library and version since tags are registered on this.
trace.traces = [];
});
return snapshot;
getSnapshot(): Monitoring {
const libraryTraces = this.libraryTraces
.filter(({ traces }) => traces.length > 0)
.map(({ library, libraryVersion, traces }) => ({
library,
libraryVersion,
traces: traces.splice(0, traces.length),
}));
return { libraryTraces };
}
}

0 comments on commit 647a106

Please sign in to comment.