Skip to content

Commit

Permalink
fix: add platform to the telemetry header
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasl committed Dec 2, 2024
1 parent 4ebb95c commit 4e3872b
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 11 deletions.
16 changes: 16 additions & 0 deletions packages/sdk/proto/confidence/telemetry/v1/telemetry.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,24 @@ syntax = "proto3";

package confidence.telemetry.v1;

enum Platform {
PLATFORM_UNSPECIFIED = 0;
PLATFORM_JAVA = 1;
PLATFORM_KOTLIN = 2;
PLATFORM_SWIFT = 3;
PLATFORM_JS_WEB = 4;
PLATFORM_JS_SERVER = 5;
PLATFORM_PYTHON = 6;
PLATFORM_GO = 7;
PLATFORM_RUBY = 8;
PLATFORM_RUST = 9;
PLATFORM_FLUTTER_IOS = 10;
PLATFORM_FLUTTER_ANDROID = 11;
}

message Monitoring {
repeated LibraryTraces library_traces = 1;
Platform platform = 2;
}

message LibraryTraces {
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/Confidence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ export class Confidence implements EventSender, Trackable, FlagResolver {
const telemetry = new Telemetry({
disabled: disableTelemetry,
logger,
environment,
});
let flagResolverClient: FlagResolverClient = new FetchingFlagResolverClient({
clientSecret,
Expand Down
13 changes: 8 additions & 5 deletions packages/sdk/src/FlagResolverClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { abortableSleep, FetchBuilder } from './fetch-util';
import { ApplyFlagsRequest, ResolveFlagsRequest } from './generated/confidence/flags/resolver/v1/api';
import { FlagResolution } from './FlagResolution';
import { Telemetry } from './Telemetry';
import { LibraryTraces_Library, LibraryTraces_TraceId } from './generated/confidence/telemetry/v1/telemetry';
import { LibraryTraces_Library, LibraryTraces_TraceId, Platform } from './generated/confidence/telemetry/v1/telemetry';
const RESOLVE_ENDPOINT = 'https://resolver.confidence.dev/v1/flags:resolve';
const APPLY_ENDPOINT = 'https://resolver.confidence.dev/v1/flags:apply';

Expand Down Expand Up @@ -59,7 +59,7 @@ describe('Client environment Evaluation', () => {
},
environment: 'client',
resolveTimeout: 10,
telemetry: new Telemetry({ disabled: true, logger: { warn: jest.fn() } }),
telemetry: new Telemetry({ disabled: true, logger: { warn: jest.fn() }, environment: 'client' }),
});

describe('apply', () => {
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('Backend environment Evaluation', () => {
},
environment: 'backend',
resolveTimeout: 10,
telemetry: new Telemetry({ disabled: true, logger: { warn: jest.fn() } }),
telemetry: new Telemetry({ disabled: true, logger: { warn: jest.fn() }, environment: 'backend' }),
});

it('should resolve a full flag object', async () => {
Expand Down Expand Up @@ -288,7 +288,9 @@ describe('intercept', () => {
});

describe('withTelemetryData', () => {
const telemetryMock = jest.mocked(new Telemetry({ disabled: false, logger: { warn: jest.fn() } }));
const telemetryMock = jest.mocked(
new Telemetry({ disabled: false, logger: { warn: jest.fn() }, environment: 'client' }),
);

beforeEach(() => {
underTest = withTelemetryData(fetchBuilder, telemetryMock).build(fetchMock);
Expand All @@ -300,6 +302,7 @@ describe('intercept', () => {
traces: [{ id: LibraryTraces_TraceId.TRACE_ID_FLAG_TYPE_MISMATCH }],
},
],
platform: Platform.PLATFORM_JS_WEB,
});
});

Expand All @@ -309,7 +312,7 @@ describe('intercept', () => {
expect(fetchMock).toBeCalledTimes(1);
const request = fetchMock.mock.calls[0][0];
expect(request.headers.has('X-Confidence-Telemetry')).toBeTruthy();
expect(request.headers.get('X-Confidence-Telemetry')).toEqual('CgwIAxIEdGVzdBoCCAM=');
expect(request.headers.get('X-Confidence-Telemetry')).toEqual('CgwIAxIEdGVzdBoCCAMQBA==');
});
});

Expand Down
8 changes: 4 additions & 4 deletions packages/sdk/src/Telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Telemetry } from './Telemetry';

describe('Telemetry', () => {
it('registerCounter and increment counter', () => {
const telemetry = new Telemetry({ disabled: false, logger: { warn: jest.fn() } });
const telemetry = new Telemetry({ disabled: false, logger: { warn: jest.fn() }, environment: 'backend' });
const counter = telemetry.registerCounter({
library: LibraryTraces_Library.LIBRARY_CONFIDENCE,
version: '9.9.9',
Expand All @@ -26,7 +26,7 @@ describe('Telemetry', () => {
});

it('registerMeter and add measurement', () => {
const telemetry = new Telemetry({ disabled: false, logger: { warn: jest.fn() } });
const telemetry = new Telemetry({ disabled: false, logger: { warn: jest.fn() }, environment: 'client' });
const meter = telemetry.registerMeter({
library: LibraryTraces_Library.LIBRARY_CONFIDENCE,
version: '9.9.9',
Expand All @@ -49,7 +49,7 @@ describe('Telemetry', () => {
});

it('snapshot is empty when telemetry is disabled', () => {
const telemetry = new Telemetry({ disabled: true, logger: { warn: jest.fn() } });
const telemetry = new Telemetry({ disabled: true, logger: { warn: jest.fn() }, environment: 'client' });
const counter = telemetry.registerCounter({
library: LibraryTraces_Library.LIBRARY_CONFIDENCE,
version: '9.9.9',
Expand All @@ -61,7 +61,7 @@ describe('Telemetry', () => {
});

it('monitoring gets cleared after snapshot is obtained', () => {
const telemetry = new Telemetry({ disabled: false, logger: { warn: jest.fn() } });
const telemetry = new Telemetry({ disabled: false, logger: { warn: jest.fn() }, environment: 'client' });
const counter = telemetry.registerCounter({
library: LibraryTraces_Library.LIBRARY_CONFIDENCE,
version: '9.9.9',
Expand Down
7 changes: 5 additions & 2 deletions packages/sdk/src/Telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
LibraryTraces_Trace,
LibraryTraces_TraceId,
Monitoring,
Platform,
} from './generated/confidence/telemetry/v1/telemetry';
import { Logger } from './logger';

export type TelemetryOptions = { disabled: boolean; logger: Logger };
export type TelemetryOptions = { disabled: boolean; logger: Logger; environment: 'backend' | 'client' };

export type Tag = {
library: LibraryTraces_Library;
Expand All @@ -22,10 +23,12 @@ export class Telemetry {
private readonly disabled: boolean;
private readonly logger: Logger;
private readonly libraryTraces: LibraryTraces[] = [];
private readonly platform: Platform;

constructor(opts: TelemetryOptions) {
this.disabled = opts.disabled;
this.logger = opts.logger;
this.platform = opts.environment === 'client' ? Platform.PLATFORM_JS_WEB : Platform.PLATFORM_JS_SERVER;
}

private registerLibraryTraces({ library, version, id }: Tag): TraceConsumer {
Expand Down Expand Up @@ -65,6 +68,6 @@ export class Telemetry {
libraryVersion,
traces: traces.splice(0, traces.length),
}));
return { libraryTraces };
return { libraryTraces, platform: this.platform };
}
}
101 changes: 101 additions & 0 deletions packages/sdk/src/generated/confidence/telemetry/v1/telemetry.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4e3872b

Please sign in to comment.