1

I am using opentelemetry to instrument the nest js application. Suppose the number of requests to be made is large in number. Then how can we specify the number of traces to be collected.

1 Answers1

5

Sampling is a mechanism to control the noise and overhead introduced by OpenTelemetry by reducing the number of samples of traces collected and sent to the backend.

You need TraceIdRatioBasedSampler for this. When you are creating TracerProvider you can specify the sampler for it. The list of available samplers are:

  • AlwaysOnSampler
  • AlwaysOffSampler
  • TraceIdRatioBasedSampler
  • ParentBasedAlwaysOnSampler
  • ParentBasedAlwatsOffSampler
  • ParentBasedTraceIdRatioBasedSampler

You can read more about them here. For example if you wanted to collect 1 out of 100 traces then you would have to do something like this.

const samplingRate = 1/100;
const { NodeTracerProvider } = require("@opentelemetry/node");
const { TraceIdRatioBasedSampler } = require("@opentelemetry/core");

const tracerProvider = new NodeTracerProvider({
  sampler: new TraceIdRatioBasedSampler(samplingRate)
});

P.S: You can also customize the sampling behaviour by extending Sampler and implementing your own shouldSample.

Srikanth Chekuri
  • 1,944
  • 1
  • 9
  • 19