Skip to content

Commit

Permalink
refactor: add optional logger
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasl committed Nov 27, 2024
1 parent d3db84c commit bf964ae
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
1 change: 1 addition & 0 deletions packages/sdk/src/Confidence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
} as const;
const telemetry = new Telemetry({
disabled: disableTelemetry,
logger,
});
let flagResolverClient: FlagResolverClient = new FetchingFlagResolverClient({
clientSecret,
Expand Down
1 change: 0 additions & 1 deletion packages/sdk/src/FlagResolverClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ export class FetchingFlagResolverClient implements FlagResolverClient {
telemetry,
}: FlagResolverClientOptions) {
this.markLatency = telemetry.registerMeter({
// TODO how to get library name?
library: LibraryTraces_Library.LIBRARY_CONFIDENCE,
version: sdk.version,
id: LibraryTraces_TraceId.TRACE_ID_RESOLVE_LATENCY,
Expand Down
21 changes: 8 additions & 13 deletions packages/sdk/src/Telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {
LibraryTraces_TraceId,
Monitoring,
} from './generated/confidence/telemetry/v1/telemetry';
import { Logger } from './logger';

export type TelemetryOptions = { disabled: boolean };
export type TelemetryOptions = { disabled: boolean; logger?: Logger };

export type Tag = {
library: LibraryTraces_Library;
Expand All @@ -17,8 +18,10 @@ export type Meter = (value: number) => void;

export class Telemetry {
private disabled: boolean;
private logger?: Logger;
constructor(opts: TelemetryOptions) {
this.disabled = opts.disabled;
this.logger = opts.logger;
}

private monitoring: Monitoring = {
Expand All @@ -37,17 +40,9 @@ export class Telemetry {
millisecondDuration: value,
});
} else {
// should never happen. remove?
this.monitoring.libraryTraces.push({
library,
libraryVersion: version,
traces: [
{
id: tags.id,
millisecondDuration: value,
},
],
});
if (this.logger?.warn) {
this.logger.warn(`pushTrace() got called before registering tag (${library}, ${version})`);
}
}
}

Expand Down Expand Up @@ -80,7 +75,7 @@ export class Telemetry {
// 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 they are registered.
// only clear traces. keep library and version since tags are registered on this.
trace.traces = [];
});
return snapshot;
Expand Down

0 comments on commit bf964ae

Please sign in to comment.