Skip to content

Commit e477285

Browse files
EmrysMyrddingithub-actions[bot]
authored andcommitted
feat(opentelemetry): add sampling rate (#968)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 55669fe commit e477285

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

.changeset/nine-pears-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/plugin-opentelemetry': minor
3+
---
4+
5+
Add a configurable sampling rate. The sampling strategy relies on a determenistic probability sampler with a parent priority, meaning that if a span is sampled, all its children spans will also be sampled.

packages/plugins/opentelemetry/src/plugin.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ import {
3030
resourceFromAttributes,
3131
} from '@opentelemetry/resources';
3232
import { type SpanProcessor } from '@opentelemetry/sdk-trace-base';
33-
import { WebTracerProvider } from '@opentelemetry/sdk-trace-web';
33+
import {
34+
AlwaysOnSampler,
35+
ParentBasedSampler,
36+
TraceIdRatioBasedSampler,
37+
WebTracerProvider,
38+
} from '@opentelemetry/sdk-trace-web';
3439
import { unfakePromise } from '@whatwg-node/promise-helpers';
3540
import { YogaLogger } from 'graphql-yoga';
3641
import { ATTR_SERVICE_VERSION, SEMRESATTRS_SERVICE_NAME } from './attributes';
@@ -125,6 +130,13 @@ interface OpenTelemetryGatewayPluginOptionsWithInit {
125130
* - `ContextManager`: rely on this provided `ContextManger` instance.
126131
*/
127132
contextManager?: ContextManager | boolean;
133+
/**
134+
* Sampling rate of spans. The value should be between 0 and 1.
135+
* By default, all spans are recorded and exported, which correspond to a sampling rate of 1.
136+
*
137+
* Note: The sampling strategy used is parent based, meaning spans will be always sampled if a sampled parent span is found in the OTEL context.
138+
*/
139+
samplingRate?: number;
128140
}
129141

130142
type OpenTelemetryGatewayPluginOptionsInit =
@@ -332,14 +344,20 @@ export function useOpenTelemetry(
332344

333345
let contextManager$ = getContextManager(options.contextManager);
334346

347+
const sampler = options.samplingRate
348+
? new ParentBasedSampler({
349+
root: new TraceIdRatioBasedSampler(options.samplingRate),
350+
})
351+
: new AlwaysOnSampler();
352+
335353
setGlobalErrorHandler((err) => {
336354
diag.error('Uncaught Error', err);
337355
});
338356

339357
return exporters$
340358
.then((exporters) => {
341359
spanProcessors = exporters;
342-
provider = new WebTracerProvider({ resource, spanProcessors });
360+
provider = new WebTracerProvider({ resource, spanProcessors, sampler });
343361
return contextManager$;
344362
})
345363
.then((contextManager) => {

packages/plugins/opentelemetry/tests/useOpenTelemetry.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let gw: typeof import('../../../runtime/src');
1313
describe('useOpenTelemetry', () => {
1414
mockModule('@opentelemetry/sdk-trace-web', () => ({
1515
WebTracerProvider: vi.fn(() => ({ register: mockRegisterProvider })),
16+
AlwaysOnSampler: vi.fn(),
1617
}));
1718

1819
beforeAll(async () => {

0 commit comments

Comments
 (0)