Skip to content

Commit

Permalink
refactor: withTelemetry act on fetchBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklasl committed Dec 2, 2024
1 parent 52babcf commit 4ebb95c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
6 changes: 3 additions & 3 deletions packages/sdk/src/FlagResolverClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from './FlagResolverClient';
import { setMaxListeners } from 'node:events';
import { SdkId } from './generated/confidence/flags/resolver/v1/types';
import { abortableSleep } from './fetch-util';
import { abortableSleep, FetchBuilder } from './fetch-util';
import { ApplyFlagsRequest, ResolveFlagsRequest } from './generated/confidence/flags/resolver/v1/api';
import { FlagResolution } from './FlagResolution';
import { Telemetry } from './Telemetry';
Expand Down Expand Up @@ -274,7 +274,7 @@ describe('Backend environment Evaluation', () => {

describe('intercept', () => {
const fetchMock = jest.fn<Promise<Response>, [Request]>();

const fetchBuilder = new FetchBuilder();
let underTest: typeof fetch;

beforeEach(() => {
Expand All @@ -291,7 +291,7 @@ describe('intercept', () => {
const telemetryMock = jest.mocked(new Telemetry({ disabled: false, logger: { warn: jest.fn() } }));

beforeEach(() => {
underTest = withTelemetryData(fetchMock, telemetryMock);
underTest = withTelemetryData(fetchBuilder, telemetryMock).build(fetchMock);
telemetryMock.getSnapshot = jest.fn().mockReturnValue({
libraryTraces: [
{
Expand Down
34 changes: 14 additions & 20 deletions packages/sdk/src/FlagResolverClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ export class FetchingFlagResolverClient implements FlagResolverClient {
version: sdk.version,
id: LibraryTraces_TraceId.TRACE_ID_RESOLVE_LATENCY,
});
const fetchBuilderWithTelemetry = withTelemetryData(new FetchBuilder(), telemetry);
// TODO think about both resolve and apply request logic for backends
this.fetchImplementation =
environment === 'backend'
? withTelemetryData(fetchImplementation, telemetry)
: withRequestLogic(withTelemetryData(fetchImplementation, telemetry));
const fetchWithTelemetry = fetchBuilderWithTelemetry.build(fetchImplementation);
this.fetchImplementation = environment === 'backend' ? fetchWithTelemetry : withRequestLogic(fetchWithTelemetry);

this.clientSecret = clientSecret;
this.sdk = sdk;
Expand Down Expand Up @@ -297,23 +296,18 @@ export class CachingFlagResolverClient implements FlagResolverClient {
}
}

export function withTelemetryData(
fetchImplementation: (request: Request) => Promise<Response>,
telemetry: Telemetry,
): typeof fetch {
return new FetchBuilder()
.modifyRequest(async request => {
const monitoring = telemetry.getSnapshot();
if (monitoring.libraryTraces.length > 0) {
const headers = new Headers(request.headers);
const base64Message = btoa(String.fromCharCode(...Monitoring.encode(monitoring).finish()));
export function withTelemetryData(fetchBuilder: FetchBuilder, telemetry: Telemetry): FetchBuilder {
return fetchBuilder.modifyRequest(async request => {
const monitoring = telemetry.getSnapshot();
if (monitoring.libraryTraces.length > 0) {
const headers = new Headers(request.headers);
const base64Message = btoa(String.fromCharCode(...Monitoring.encode(monitoring).finish()));

headers.set('X-CONFIDENCE-TELEMETRY', base64Message);
return new Request(request, { headers });
}
return request;
})
.build(fetchImplementation);
headers.set('X-CONFIDENCE-TELEMETRY', base64Message);
return new Request(request, { headers });
}
return request;
});
}

export function withRequestLogic(fetchImplementation: (request: Request) => Promise<Response>): typeof fetch {
Expand Down

0 comments on commit 4ebb95c

Please sign in to comment.