Skip to content

Commit

Permalink
Include Feature data sending to APM
Browse files Browse the repository at this point in the history
  • Loading branch information
leslie-lau committed Jan 9, 2025
1 parent b4519da commit 7adb0d1
Showing 1 changed file with 25 additions and 60 deletions.
85 changes: 25 additions & 60 deletions docs/integrations/datadog-apm.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,44 @@ DevCycle's clientside Javascript SDKs - including JS and React, can be easily in

## Configuration

The DevCycle APM integration provides Traces with more depth by passing in DevCycle variable data during its time of execution, allowing Developers to get a better understanding of the performance impact of each Feature.
The DevCycle APM integration provides Traces with more depth by passing in DevCycle Feature and/or Variable data during its time of execution, allowing Developers to get a better understanding of the performance impact of each Feature.

To add variable data to Traces, we'll retrieve variable data from a DevCycle SDK and add that data as metadata to the active Trace. See the example below.
### Sending Feature Data

#### JavaScript
To add Feature data to Traces, we'll retrieve all of our Feature and Variation data using a DevCycle SDK and add that data as metadata to the active Trace. In our example, we send the Feature key and Variation name to DataDog. This can be customized to your liking.

Fetch the DevCycle variable and format it to our liking:

``` javascript
class DevCycleTracingPatchService {
constructor(devcycleClient) {
this.devcycleClient = devcycleClient;

const originalVariable = this.devcycleClient.variable.bind(this.devcycleClient);

this.devcycleClient.variable = (user, key, defaultValue) => {
const variable = originalVariable(user, key, defaultValue);
addTraceMetadata({
[`devcycle.${key}`]: JSON.stringify(variable.value),
});
return variable;
};
}
const features = devcycleClient.allFeatures()

const span = tracer.scope().active();
if (span) {
const rootSpan = span.context()._trace.started[0];
Object.entries(features).forEach(([key, metadata]) => {
rootSpan?.setTag(key, metadata['variationName']);
});
}
```
Add the variable to the Trace as Metadata:
### Sending Variable Data
To add DevCycle Variables to Traces, we'll retrieve Variable data from a DevCycle SDK and add that data as metadata to the active Trace. A simple way to pass along multiple Variables and its values to DataDog is shown below.
``` javascript
function addTraceMetadata(metadata) {
const span = tracer.scope().active();
if (span) {
const rootSpan = span.context()._trace.started[0]; // Assumes specific tracer structure
Object.entries(metadata).forEach(([key, value]) => {
rootSpan?.setTag(key, value);
});
}
}
```
const variableA = dvcClient.variable('new-homepage-hero', 'defaultValue');
const variableB = dvcClient.variable('new-homepage-layout', 'defaultValue');

#### React
Fetch the DevCycle variable and format it to our liking:
``` jsx
@Injectable()
export class DevCycleTracingPatchService {
constructor(private devcycleClient: DevCycleClient) {
const originalVariable = this.devcycleClient.variable.bind(
this.devcycleClient,
)
this.devcycleClient.variable = (user, key, defaultValue) => {
const variable = originalVariable(user, key, defaultValue)
addTraceMetadata({
[`devcycle.${key}`]: JSON.stringify(variable.value),
})
return variable
}
}
const variables = {
variableA: variableA.value,
variableB: variableB.value,
}
```

Add the variable to the Trace as Metadata:
``` jsx
export function addTraceMetadata(metadata: Record<string, unknown>) {
const span = tracer.scope().active()
if (span) {
const rootSpan = (span.context() as any)._trace.started[0]
Object.entries(metadata).forEach(([key, value]) => {
rootSpan?.setTag(key, value)
})
}
const span = tracer.scope().active();
if (span) {
const rootSpan = span.context()._trace.started[0];
Object.entries(variables).forEach(([key, value]) => {
rootSpan?.setTag(key, value);
});
}
```
Expand Down

0 comments on commit 7adb0d1

Please sign in to comment.