Skip to content

Commit

Permalink
refactor: improve track interface for providers (#1100)
Browse files Browse the repository at this point in the history
## This PR

- updates the context and trackingEventDetails on the tracking interface
to not be optional since they're always supplied by the SDK.

### Notes

This is a small QoL improvement for provider devs implementing the
tracking interface.

### How to test

The SDK still compiles, and the tests don't need to be modified.

---------

Signed-off-by: Michael Beemer <beeme1mr@users.noreply.github.com>
Signed-off-by: Todd Baert <todd.baert@dynatrace.com>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
beeme1mr and toddbaert authored Dec 11, 2024
1 parent ba8d1ae commit 5e5b160
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/client/internal/open-feature-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class OpenFeatureClient implements Client {
return this.evaluate<T>(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', context, options);
}

track(occurrenceKey: string, context: EvaluationContext, occurrenceDetails: TrackingEventDetails): void {
track(occurrenceKey: string, context: EvaluationContext = {}, occurrenceDetails: TrackingEventDetails = {}): void {
try {
this.shortCircuitIfNotReady();

Expand Down
12 changes: 12 additions & 0 deletions packages/server/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,18 @@ describe('OpenFeatureClient', () => {
}).not.toThrow();
});

it('provide empty tracking details to provider if not supplied in call', async () => {
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER });
const client = OpenFeature.getClient();
client.track(eventName);

expect(MOCK_PROVIDER.track).toHaveBeenCalledWith(
eventName,
expect.any(Object),
expect.any(Object),
);
});

it('should call provider with correct context', async () => {
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER });
OpenFeature.setContext({ [globalContextKey]: globalContextValue });
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,5 @@ export interface CommonProvider<S extends ClientProviderStatus | ServerProviderS
* @param context
* @param trackingEventDetails
*/
track?(trackingEventName: string, context?: EvaluationContext, trackingEventDetails?: TrackingEventDetails): void;
track?(trackingEventName: string, context: EvaluationContext, trackingEventDetails: TrackingEventDetails): void;
}
2 changes: 1 addition & 1 deletion packages/web/src/client/internal/open-feature-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class OpenFeatureClient implements Client {
return this.evaluate<T>(flagKey, this._provider.resolveObjectEvaluation, defaultValue, 'object', options);
}

track(occurrenceKey: string, occurrenceDetails: TrackingEventDetails): void {
track(occurrenceKey: string, occurrenceDetails: TrackingEventDetails = {}): void {
try {
this.shortCircuitIfNotReady();

Expand Down
12 changes: 12 additions & 0 deletions packages/web/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,18 @@ describe('OpenFeatureClient', () => {
}).not.toThrow();
});

it('provide empty tracking details to provider if not supplied in call', async () => {
await OpenFeature.setProviderAndWait({ ...MOCK_PROVIDER });
const client = OpenFeature.getClient();
client.track(eventName);

expect(MOCK_PROVIDER.track).toHaveBeenCalledWith(
eventName,
expect.any(Object),
expect.any(Object),
);
});

it('should no-op and not throw if provider throws', async () => {
await OpenFeature.setProviderAndWait({
...MOCK_PROVIDER,
Expand Down

0 comments on commit 5e5b160

Please sign in to comment.