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

[sampler] treat invalid SpanContext as no SpanContext #2165

Merged
merged 1 commit into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SpanAttributes,
Context,
getSpanContext,
isSpanContextValid,
Link,
Sampler,
SamplingResult,
Expand Down Expand Up @@ -69,7 +70,7 @@ export class ParentBasedSampler implements Sampler {
): SamplingResult {
const parentContext = getSpanContext(context);

if (!parentContext) {
if (!parentContext || !isSpanContextValid(parentContext)) {
return this._root.shouldSample(
context,
traceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ describe('ParentBasedSampler', () => {
);
});

it('should return api.SamplingDecision.RECORD_AND_SAMPLED for invalid parent spanContext while composited with AlwaysOnSampler', () => {
const sampler = new ParentBasedSampler({ root: new AlwaysOnSampler() });

assert.deepStrictEqual(
sampler.shouldSample(
setSpanContext(api.ROOT_CONTEXT, api.INVALID_SPAN_CONTEXT),
traceId,
spanName,
SpanKind.CLIENT,
{},
[]
),
{
decision: api.SamplingDecision.RECORD_AND_SAMPLED,
}
);
});

it('should return api.SamplingDecision.RECORD_AND_SAMPLED while composited with AlwaysOnSampler', () => {
const sampler = new ParentBasedSampler({ root: new AlwaysOnSampler() });

Expand Down Expand Up @@ -112,6 +130,24 @@ describe('ParentBasedSampler', () => {
);
});

it('should return api.SamplingDecision.NOT_RECORD for invalid parent spanContext while composited with AlwaysOffSampler', () => {
const sampler = new ParentBasedSampler({ root: new AlwaysOffSampler() });

assert.deepStrictEqual(
sampler.shouldSample(
setSpanContext(api.ROOT_CONTEXT, api.INVALID_SPAN_CONTEXT),
traceId,
spanName,
SpanKind.CLIENT,
{},
[]
),
{
decision: api.SamplingDecision.NOT_RECORD,
}
);
});

it('should return api.SamplingDecision.RECORD_AND_SAMPLED while composited with AlwaysOffSampler', () => {
const sampler = new ParentBasedSampler({ root: new AlwaysOffSampler() });

Expand Down