Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: better support for event sender analytics paths #259

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type LDInternalOptions = {
analyticsEventPath?: string;
diagnosticEventPath?: string;
includeAuthorizationHeader?: boolean;
};
10 changes: 9 additions & 1 deletion packages/shared/common/src/internal/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@ import InputCustomEvent from './InputCustomEvent';
import InputEvalEvent from './InputEvalEvent';
import InputEvent from './InputEvent';
import InputIdentifyEvent from './InputIdentifyEvent';
import type { LDInternalOptions } from './LDInternalOptions';

export { InputCustomEvent, InputEvalEvent, InputEvent, InputIdentifyEvent, EventProcessor };
export {
EventProcessor,
InputCustomEvent,
InputEvalEvent,
InputEvent,
InputIdentifyEvent,
LDInternalOptions,
};
29 changes: 28 additions & 1 deletion packages/shared/common/src/options/ServiceEndpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,36 @@ export default class ServiceEndpoints {

public readonly events: string;

public constructor(streaming: string, polling: string, events: string) {
/** Valid paths are:
* /bulk
* /events/bulk/envId
* /mobile
*/
public readonly analyticsEventPath: string;

/** Valid paths are:
* /diagnostic
* /events/diagnostic/envId
* /mobile/events/diagnostic
*/
public readonly diagnosticEventPath: string;
Comment on lines +15 to +27
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can be more pedantic and make these literal types instead of freeform strings. Any opinions?


// if true the sdk key will be included as authorization header
public readonly includeAuthorizationHeader: boolean;

public constructor(
streaming: string,
polling: string,
events: string,
analyticsEventPath: string = '/bulk',
diagnosticEventPath: string = '/diagnostic',
includeAuthorizationHeader: boolean = true,
) {
this.streaming = canonicalizeUri(streaming);
this.polling = canonicalizeUri(polling);
this.events = canonicalizeUri(events);
this.analyticsEventPath = analyticsEventPath;
this.diagnosticEventPath = diagnosticEventPath;
this.includeAuthorizationHeader = includeAuthorizationHeader;
}
}
8 changes: 7 additions & 1 deletion packages/shared/sdk-server-edge/src/api/LDClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { EventEmitter } from 'node:events';

import { internal } from '@launchdarkly/js-sdk-common';
import { Info, LDClientImpl, LDOptions } from '@launchdarkly/js-server-sdk-common';

import EdgePlatform from '../platform';
Expand All @@ -16,7 +17,12 @@ export class LDClient extends LDClientImpl {
constructor(clientSideID: string, platformInfo: Info, options: LDOptions) {
const em = new EventEmitter();
const platform = new EdgePlatform(platformInfo);
super(clientSideID, platform, createOptions(options), createCallbacks(em));
const internalOptions: internal.LDInternalOptions = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I like this much better.

analyticsEventPath: `/events/bulk/${clientSideID}`,
diagnosticEventPath: `/events/diagnostic/${clientSideID}`,
includeAuthorizationHeader: false,
};
super(clientSideID, platform, createOptions(options), createCallbacks(em), internalOptions);
this.emitter = em;
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/shared/sdk-server/src/LDClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ export default class LDClientImpl implements LDClient {
private platform: Platform,
options: LDOptions,
callbacks: LDClientCallbacks,
internalOptions?: internal.LDInternalOptions,
) {
this.onError = callbacks.onError;
this.onFailed = callbacks.onFailed;
this.onReady = callbacks.onReady;

const { onUpdate, hasEventListeners } = callbacks;
const config = new Configuration(options);
const config = new Configuration(options, internalOptions);
if (!sdkKey && !config.offline) {
throw new Error('You must configure the client with an SDK key');
}
Expand Down
6 changes: 4 additions & 2 deletions packages/shared/sdk-server/src/data_sources/defaultHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default function defaultHeaders(
sdkKey: string,
config: DefaultHeaderOptions,
info: Info,
includeAuthorizationHeader: boolean = true,
): { [key: string]: string } {
const sdkData = info.sdkData();
const headers: { [key: string]: string } = {
Expand All @@ -16,8 +17,9 @@ export default function defaultHeaders(
}`,
};

// edge sdks use clientSideID and don't need the authorization header
if (sdkKey.startsWith('sdk-')) {
// edge sdks sets this to false because they use the clientSideID
// and they don't need the authorization header
if (includeAuthorizationHeader) {
headers.authorization = sdkKey;
}

Expand Down
18 changes: 9 additions & 9 deletions packages/shared/sdk-server/src/events/EventSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,22 @@ export default class EventSender implements subsystem.LDEventSender {
const {
basicConfiguration: {
sdkKey,
serviceEndpoints: { events },
serviceEndpoints: {
events,
analyticsEventPath,
diagnosticEventPath,
includeAuthorizationHeader,
},
},
platform: { info, requests, crypto },
} = clientContext;

this.defaultHeaders = {
...defaultHeaders(sdkKey, config, info),
...defaultHeaders(sdkKey, config, info, includeAuthorizationHeader),
};

// edge sdks use clientSideID to send events
const isClientSideID = !sdkKey.startsWith('sdk-');
this.eventsUri = isClientSideID ? `${events}/events/bulk/${sdkKey}` : `${events}/bulk`;
this.diagnosticEventsUri = isClientSideID
? `${events}/events/diagnostic/${sdkKey}`
: `${events}/diagnostic`;

this.eventsUri = `${events}${analyticsEventPath}`;
this.diagnosticEventsUri = `${events}${diagnosticEventPath}`;
this.requests = requests;
this.crypto = crypto;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/shared/sdk-server/src/options/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ApplicationTags,
internal,
LDClientContext,
LDLogger,
NumberWithMinimum,
Expand Down Expand Up @@ -209,7 +210,7 @@ export default class Configuration {

public readonly bigSegments?: LDBigSegmentsOptions;

constructor(options: LDOptions = {}) {
constructor(options: LDOptions = {}, internalOptions: internal.LDInternalOptions = {}) {
// The default will handle undefined, but not null.
// Because we can be called from JS we need to be extra defensive.
// eslint-disable-next-line no-param-reassign
Expand All @@ -228,6 +229,8 @@ export default class Configuration {
validatedOptions.streamUri,
validatedOptions.baseUri,
validatedOptions.eventsUri,
internalOptions.analyticsEventPath,
internalOptions.diagnosticEventPath,
);
this.eventsCapacity = validatedOptions.capacity;
this.timeout = validatedOptions.timeout;
Expand Down