Skip to content

Commit

Permalink
Fix ParentBasedSampler
Browse files Browse the repository at this point in the history
  • Loading branch information
thisthat committed Apr 28, 2021
1 parent b9c8426 commit d021756
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
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
36 changes: 36 additions & 0 deletions packages/opentelemetry-core/test/trace/ParentBasedSampler.test.ts
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

0 comments on commit d021756

Please sign in to comment.