Skip to content
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

feat: add OTEL_SAMPLING_PROBABILITY env var #975

Closed

Conversation

naseemkullah
Copy link
Member

@naseemkullah naseemkullah commented Apr 18, 2020

User can now control the trace sampling rate by means of env var.

JS implementation of open-telemetry/opentelemetry-specification#567

@naseemkullah
Copy link
Member Author

Hello, I cannot seem to get OTEL_SAMPLING_RATE to work for the BasicTracerProvider test, any suggestions?

readonly resource: Resource;

constructor(private _config: TracerConfig = DEFAULT_CONFIG) {
this.logger = _config.logger || new ConsoleLogger(_config.logLevel);
this.sampler =
_config.sampler ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by default _config is the DEFAULT_CONFIG so your fallback is never called here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @vmarchaud I did this because of ts error:

Type 'Sampler | undefined' is not assignable to type 'Sampler'.
  Type 'undefined' is not assignable to type 'Sampler'.

But have worked around this with:

readonly sampler: api.Sampler | undefined;

Copy link
Member

@vmarchaud vmarchaud Apr 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should always have a sampler though so you approach was fine, however i would use ?? instead of ||

EDIt: The current approach is the correct way, but you shouldn't need to add | undefined to the _sampler

@@ -32,7 +32,7 @@ export const DEFAULT_MAX_LINKS_PER_SPAN = 32;
export const DEFAULT_CONFIG = {
defaultAttributes: {},
logLevel: LogLevel.INFO,
sampler: ALWAYS_SAMPLER,
sampler: new ProbabilitySampler(Number(process.env.OTEL_SAMPLING_RATE || 1)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This config is created when this file is required so well before your test tries to set it into the environment.
I don't think you should change the default config. I would have checked the existence of the env variable inside the BasicTracer and override the sampler there. Also you'll like the other PR to have a different implementation for the browser since process will not be available

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This config is created when this file is required so well before your test tries to set it into the environment.
I don't think you should change the default config. I would have checked the existence of the env variable inside the BasicTracer and override the sampler there.

Also tried this approach but the env var is not working as expected 🤔

  1) BasicTracerProvider
       .startSpan()
         should not sample a trace when OTEL_SAMPLING_RATE is 0:

      AssertionError [ERR_ASSERTION]: Input A expected to strictly equal input B:
+ expected - actual

- 1
+ 0

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know then, maybe try to use the ConsoleLogger and increase the verbosity to debug ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm in the end I only had to make changes to Tracer.ts but not BasicTracerProvider,ts 🤷‍♂️

@naseemkullah naseemkullah force-pushed the otel-sampling-rate branch 3 times, most recently from 11eef5a to 72e9da1 Compare April 20, 2020 12:01
@naseemkullah naseemkullah changed the title feat: add OTEL_SAMPLING_RATE env var feat: add OTEL_SAMPLING_PROBABILITY env var Apr 21, 2020
@naseemkullah naseemkullah force-pushed the otel-sampling-rate branch 2 times, most recently from f5c1ca1 to 8a24a66 Compare April 21, 2020 02:49
@codecov-io
Copy link

Codecov Report

Merging #975 into master will increase coverage by 1.46%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #975      +/-   ##
==========================================
+ Coverage   95.02%   96.49%   +1.46%     
==========================================
  Files         209      189      -20     
  Lines        8541     7757     -784     
  Branches      766      627     -139     
==========================================
- Hits         8116     7485     -631     
+ Misses        425      272     -153     
Impacted Files Coverage Δ
packages/opentelemetry-tracing/src/Tracer.ts 100.00% <100.00%> (ø)
...telemetry-tracing/test/BasicTracerProvider.test.ts 100.00% <100.00%> (ø)
...ckages/opentelemetry-core/src/common/NoopLogger.ts 60.00% <0.00%> (-20.00%) ⬇️
...metry-core/src/trace/instrumentation/BasePlugin.ts 81.57% <0.00%> (-5.27%) ⬇️
...es/opentelemetry-exporter-collector/src/version.ts
...es/opentelemetry-exporter-collector/test/helper.ts
...entelemetry-plugin-xml-http-request/src/version.ts
packages/opentelemetry-web/src/utils.ts
...ry-context-zone-peer-dep/src/ZoneContextManager.ts
...y-exporter-collector/test/browser/index-webpack.ts
... and 14 more

User can now control the trace sampling probability by means of env var.

Signed-off-by: Naseem <[email protected]>
User can now control the trace sampling probability by means of env var.

Signed-off-by: Naseem <[email protected]>
@naseemkullah
Copy link
Member Author

Not sure how 637f982 wound up in this branch. I tried rebasing from master.

@naseemkullah
Copy link
Member Author

naseemkullah commented May 18, 2020

Anyone know why isn't CI being triggered?

@naseemkullah
Copy link
Member Author

Closing and opening a new PR due to git complications.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants