Skip to content

Commit

Permalink
feat(datadog): Add support for getting RUM metrics for the TypeScript…
Browse files Browse the repository at this point in the history
… client (#65)

* feat(datadog): Add support for getting RUM metrics for the TypeScript client

* chore(README.md): Add missing import in README example

* chore(README.md): add instructions on how to install datadog RUM
  • Loading branch information
yquansah authored Jan 15, 2024
1 parent cde2cfe commit 664c0b3
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
45 changes: 45 additions & 0 deletions flipt-node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,48 @@ npm i @flipt-io/flipt@{version}
## Usage

In the [example](./example) directory, there is an example TypeScript program which imports in the flipt client, and uses it appropriately, please refer to that for how to use the client.

### Metrics

There is support for [Datadog RUM](https://docs.datadoghq.com/real_user_monitoring/) through this client. This allows you to track the values of feature flag evaluation and how it relates to active browser sessions.

You can first install the datadog RUM client like so:

```
npm install --save @datadog/browser-rum
```

To start tracking feature flags on Datadog:

```typescript
import { datadogRum } from '@datadog/browser-rum';
import { FliptClient, FliptMetrics } from '@flipt-io/flipt';

datadogRum.init({
applicationId: '<APPLICATION_ID>',
clientToken: '<CLIENT_TOKEN>',
site: 'datadoghq.com',
service:'<SERVICE_NAME>',
env:'<ENV_NAME>',
enableExperimentalFeatures: ["feature_flags"],
sessionSampleRate:100,
sessionReplaySampleRate: 20,
trackUserInteractions: true,
trackResources: true,
trackLongTasks: true,
defaultPrivacyLevel:'mask-user-input'
});

datadogRum.startSessionReplayRecording();

const metricsClient = new FliptMetrics(new FliptClient({
url: "http://localhost:8080",
}).evaluation, datadogRum);

const response = await metricsClient.variant({
namespaceKey: "default",
flagKey: "hello-this",
entityId: uuidv4(),
context: {},
});
```
39 changes: 39 additions & 0 deletions flipt-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Evaluation } from "./evaluation";
import {
BooleanEvaluationResponse,
EvaluationRequest,
VariantEvaluationResponse
} from "./evaluation/models";

interface FliptClientOptions {
url?: string;
Expand Down Expand Up @@ -66,3 +71,37 @@ export class FliptClient {
);
}
}

export class FliptMetrics {
evaluationClient: Evaluation;
datadogRum: any;

constructor(evaluationClient: Evaluation, datadogRum: any) {
this.evaluationClient = evaluationClient;
this.datadogRum = datadogRum;
}

public async boolean(
request: EvaluationRequest
): Promise<BooleanEvaluationResponse> {
const response = await this.evaluationClient.boolean(request);

this.datadogRum.addFeatureFlagEvaluation(
`${request.namespaceKey}/${request.flagKey}`,
response.enabled
);
return response;
}

public async variant(
request: EvaluationRequest
): Promise<VariantEvaluationResponse> {
const response = await this.evaluationClient.variant(request);

this.datadogRum.addFeatureFlagEvaluation(
`${request.namespaceKey}/${request.flagKey}`,
response.variantKey
);
return response;
}
}

0 comments on commit 664c0b3

Please sign in to comment.