Skip to content

feat(goff web): Support tracking events #1268

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion libs/providers/go-feature-flag-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"current-version": "echo $npm_package_version"
},
"peerDependencies": {
"@openfeature/web-sdk": "^1.0.0"
"@openfeature/web-sdk": "^1.0.0",
"@openfeature/core": "^1.0.0"
},
"dependencies": {
"copy-anything": "^3.0.5"
Expand Down
71 changes: 71 additions & 0 deletions libs/providers/go-feature-flag-web/src/lib/collector-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Logger } from '@openfeature/web-sdk';
import { ExporterMetadataValue, FeatureEvent, GoFeatureFlagWebProviderOptions, TrackingEvent } from './model';
import { GoffApiController } from './controller/goff-api';
import { CollectorError } from './errors/collector-error';
import { copy } from 'copy-anything';

type Timer = ReturnType<typeof setInterval>;

export class CollectorManager {
// bgSchedulerId contains the id of the setInterval that is running.
private bgScheduler?: Timer;
// dataCollectorBuffer contains all the FeatureEvents that we need to send to the relay-proxy for data collection.
private dataCollectorBuffer?: Array<FeatureEvent<any> | TrackingEvent>;
// dataFlushInterval interval time (in millisecond) we use to call the relay proxy to collect data.
private readonly dataFlushInterval: number;
// logger is the Open Feature logger to use
private logger?: Logger;
// dataCollectorMetadata are the metadata used when calling the data collector endpoint
private readonly dataCollectorMetadata: Record<string, ExporterMetadataValue>;

private readonly goffApiController: GoffApiController;

constructor(options: GoFeatureFlagWebProviderOptions, logger?: Logger) {
this.dataFlushInterval = options.dataFlushInterval || 1000 * 60;
this.logger = logger;
this.goffApiController = new GoffApiController(options);

this.dataCollectorMetadata = {
provider: 'web',
openfeature: true,
...options.exporterMetadata,
};
}

init() {
this.bgScheduler = setInterval(async () => await this.callGoffDataCollection(), this.dataFlushInterval);
this.dataCollectorBuffer = [];
}

async close() {
clearInterval(this.bgScheduler);
// We call the data collector with what is still in the buffer.
await this.callGoffDataCollection();
}

add(event: FeatureEvent<any> | TrackingEvent) {
if (this.dataCollectorBuffer) {
this.dataCollectorBuffer.push(event);
}
}

/**
* callGoffDataCollection is a function called periodically to send the usage of the flag to the
* central service in charge of collecting the data.
*/
async callGoffDataCollection() {
const dataToSend = copy(this.dataCollectorBuffer) || [];
this.dataCollectorBuffer = [];
try {
await this.goffApiController.collectData(dataToSend, this.dataCollectorMetadata);
} catch (e) {
if (!(e instanceof CollectorError)) {
throw e;
}
this.logger?.error(e);
// if we have an issue calling the collector, we put the data back in the buffer
this.dataCollectorBuffer = [...this.dataCollectorBuffer, ...dataToSend];
return;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { DataCollectorRequest, ExporterMetadataValue, FeatureEvent, GoFeatureFlagWebProviderOptions } from '../model';
import {
DataCollectorRequest,
ExporterMetadataValue,
FeatureEvent,
GoFeatureFlagWebProviderOptions,
TrackingEvent,
} from '../model';
import { CollectorError } from '../errors/collector-error';

export class GoffApiController {
Expand All @@ -15,7 +21,10 @@ export class GoffApiController {
this.options = options;
}

async collectData(events: FeatureEvent<any>[], dataCollectorMetadata: Record<string, ExporterMetadataValue>) {
async collectData(
events: Array<FeatureEvent<any> | TrackingEvent>,
dataCollectorMetadata: Record<string, ExporterMetadataValue>,
) {
if (events?.length === 0) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,65 +1,14 @@
import { EvaluationDetails, FlagValue, Hook, HookContext, Logger } from '@openfeature/web-sdk';
import { ExporterMetadataValue, FeatureEvent, GoFeatureFlagWebProviderOptions } from './model';
import { copy } from 'copy-anything';
import { CollectorError } from './errors/collector-error';
import { GoffApiController } from './controller/goff-api';
import { EvaluationDetails, FlagValue, Hook, HookContext } from '@openfeature/web-sdk';
import { CollectorManager } from './collector-manager';

const defaultTargetingKey = 'undefined-targetingKey';
type Timer = ReturnType<typeof setInterval>;

export class GoFeatureFlagDataCollectorHook implements Hook {
// bgSchedulerId contains the id of the setInterval that is running.
private bgScheduler?: Timer;
// dataCollectorBuffer contains all the FeatureEvents that we need to send to the relay-proxy for data collection.
private dataCollectorBuffer?: FeatureEvent<any>[];
// dataFlushInterval interval time (in millisecond) we use to call the relay proxy to collect data.
private readonly dataFlushInterval: number;
// dataCollectorMetadata are the metadata used when calling the data collector endpoint
private readonly dataCollectorMetadata: Record<string, ExporterMetadataValue>;
private readonly goffApiController: GoffApiController;
// logger is the Open Feature logger to use
private logger?: Logger;
private collectorManagger?: CollectorManager;

constructor(options: GoFeatureFlagWebProviderOptions, logger?: Logger) {
this.dataFlushInterval = options.dataFlushInterval || 1000 * 60;
this.logger = logger;
this.goffApiController = new GoffApiController(options);
this.dataCollectorMetadata = {
provider: 'web',
openfeature: true,
...options.exporterMetadata,
};
}

init() {
this.bgScheduler = setInterval(async () => await this.callGoffDataCollection(), this.dataFlushInterval);
this.dataCollectorBuffer = [];
}

async close() {
clearInterval(this.bgScheduler);
// We call the data collector with what is still in the buffer.
await this.callGoffDataCollection();
}

/**
* callGoffDataCollection is a function called periodically to send the usage of the flag to the
* central service in charge of collecting the data.
*/
async callGoffDataCollection() {
const dataToSend = copy(this.dataCollectorBuffer) || [];
this.dataCollectorBuffer = [];
try {
await this.goffApiController.collectData(dataToSend, this.dataCollectorMetadata);
} catch (e) {
if (!(e instanceof CollectorError)) {
throw e;
}
this.logger?.error(e);
// if we have an issue calling the collector, we put the data back in the buffer
this.dataCollectorBuffer = [...this.dataCollectorBuffer, ...dataToSend];
return;
}
constructor(collectorManager: CollectorManager) {
this.collectorManagger = collectorManager;
}

after(hookContext: HookContext, evaluationDetails: EvaluationDetails<FlagValue>) {
Expand All @@ -74,7 +23,7 @@ export class GoFeatureFlagDataCollectorHook implements Hook {
userKey: hookContext.context.targetingKey || defaultTargetingKey,
source: 'PROVIDER_CACHE',
};
this.dataCollectorBuffer?.push(event);
this.collectorManagger?.add(event);
}

error(hookContext: HookContext) {
Expand All @@ -89,6 +38,6 @@ export class GoFeatureFlagDataCollectorHook implements Hook {
userKey: hookContext.context.targetingKey || defaultTargetingKey,
source: 'PROVIDER_CACHE',
};
this.dataCollectorBuffer?.push(event);
this.collectorManagger?.add(event);
}
}
Loading