From bf964aec2d1653603d9ce8f3579cae1f43704674 Mon Sep 17 00:00:00 2001 From: Nicklas Lundin Date: Wed, 27 Nov 2024 14:23:41 +0100 Subject: [PATCH] refactor: add optional logger --- packages/sdk/src/Confidence.ts | 1 + packages/sdk/src/FlagResolverClient.ts | 1 - packages/sdk/src/Telemetry.ts | 21 ++++++++------------- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/packages/sdk/src/Confidence.ts b/packages/sdk/src/Confidence.ts index 3625b09..4c69426 100644 --- a/packages/sdk/src/Confidence.ts +++ b/packages/sdk/src/Confidence.ts @@ -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, diff --git a/packages/sdk/src/FlagResolverClient.ts b/packages/sdk/src/FlagResolverClient.ts index 53bbb70..363d8cf 100644 --- a/packages/sdk/src/FlagResolverClient.ts +++ b/packages/sdk/src/FlagResolverClient.ts @@ -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, diff --git a/packages/sdk/src/Telemetry.ts b/packages/sdk/src/Telemetry.ts index ef9c4ec..7fe5414 100644 --- a/packages/sdk/src/Telemetry.ts +++ b/packages/sdk/src/Telemetry.ts @@ -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; @@ -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 = { @@ -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})`); + } } } @@ -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;