-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also renamed ALWAYS_SAMPLER and NEVER_SAMPLER to AlwaysOnSampler and AlwaysOffSampler respectively.
- Loading branch information
1 parent
a557b04
commit 95272d4
Showing
14 changed files
with
320 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
packages/opentelemetry-core/src/trace/sampler/AlwaysOffSampler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Sampler, SamplingDecision } from '@opentelemetry/api'; | ||
|
||
/** Sampler that samples no traces. */ | ||
export class AlwaysOffSampler implements Sampler { | ||
shouldSample() { | ||
return { | ||
decision: SamplingDecision.NOT_RECORD, | ||
}; | ||
} | ||
|
||
toString(): string { | ||
return `AlwaysOffSampler`; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/opentelemetry-core/src/trace/sampler/AlwaysOnSampler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Sampler, SamplingDecision } from '@opentelemetry/api'; | ||
|
||
/** Sampler that samples all traces. */ | ||
export class AlwaysOnSampler implements Sampler { | ||
shouldSample() { | ||
return { | ||
decision: SamplingDecision.RECORD_AND_SAMPLED, | ||
}; | ||
} | ||
|
||
toString(): string { | ||
return `AlwaysOnSampler`; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/opentelemetry-core/src/trace/sampler/ParentOrElseSampler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { | ||
Sampler, | ||
SpanContext, | ||
TraceFlags, | ||
SamplingDecision, | ||
SpanKind, | ||
Attributes, | ||
Link, | ||
} from '@opentelemetry/api'; | ||
|
||
/** Sampler that samples a given fraction of traces. */ | ||
export class ParentOrElseSampler implements Sampler { | ||
constructor(private _delegateSampler: Sampler) {} | ||
|
||
shouldSample( | ||
parentContext: SpanContext | undefined, | ||
traceId: string, | ||
spanName: string, | ||
spanKind: SpanKind, | ||
attributes: Attributes, | ||
links: Link[] | ||
) { | ||
// Respect the parent sampling decision if there is one | ||
if (parentContext && typeof parentContext.traceFlags !== 'undefined') { | ||
return { | ||
decision: | ||
(TraceFlags.SAMPLED & parentContext.traceFlags) === TraceFlags.SAMPLED | ||
? SamplingDecision.RECORD_AND_SAMPLED | ||
: SamplingDecision.NOT_RECORD, | ||
}; | ||
} | ||
return this._delegateSampler.shouldSample( | ||
parentContext, | ||
traceId, | ||
spanName, | ||
spanKind, | ||
attributes, | ||
links | ||
); | ||
} | ||
|
||
toString(): string { | ||
return `ParentOrElse{${this._delegateSampler.toString()}}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/opentelemetry-core/test/trace/AlwaysOffSampler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as assert from 'assert'; | ||
import * as api from '@opentelemetry/api'; | ||
import { AlwaysOffSampler } from '../../src/trace/sampler/AlwaysOffSampler'; | ||
|
||
describe('AlwaysOffSampler', () => { | ||
it('should return decision: api.SamplingDecision.NOT_RECORD for AlwaysOffSampler', () => { | ||
const sampler = new AlwaysOffSampler(); | ||
assert.deepStrictEqual(sampler.shouldSample(), { | ||
decision: api.SamplingDecision.NOT_RECORD, | ||
}); | ||
assert.strictEqual(sampler.toString(), 'AlwaysOffSampler'); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/opentelemetry-core/test/trace/AlwaysOnSampler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as assert from 'assert'; | ||
import * as api from '@opentelemetry/api'; | ||
import { AlwaysOnSampler } from '../../src/trace/sampler/AlwaysOnSampler'; | ||
|
||
describe('AlwaysOnSampler', () => { | ||
it('should return api.SamplingDecision.RECORD_AND_SAMPLED for AlwaysOnSampler', () => { | ||
const sampler = new AlwaysOnSampler(); | ||
assert.deepStrictEqual(sampler.shouldSample(), { | ||
decision: api.SamplingDecision.RECORD_AND_SAMPLED, | ||
}); | ||
assert.strictEqual(sampler.toString(), 'AlwaysOnSampler'); | ||
}); | ||
}); |
113 changes: 113 additions & 0 deletions
113
packages/opentelemetry-core/test/trace/ParentOrElseSampler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import * as assert from 'assert'; | ||
import * as api from '@opentelemetry/api'; | ||
import { AlwaysOnSampler } from '../../src/trace/sampler/AlwaysOnSampler'; | ||
import { ParentOrElseSampler } from '../../src/trace/sampler/ParentOrElseSampler'; | ||
import { TraceFlags, SpanKind } from '@opentelemetry/api'; | ||
import { AlwaysOffSampler } from '../../src/trace/sampler/AlwaysOffSampler'; | ||
|
||
const traceId = 'd4cda95b652f4a1592b449d5929fda1b'; | ||
const spanId = '6e0c63257de34c92'; | ||
const spanName = 'foobar'; | ||
|
||
describe('ParentOrElseSampler', () => { | ||
it('should return api.SamplingDecision.NOT_RECORD for not sampled parent while composited with AlwaysOnSampler', () => { | ||
const sampler = new ParentOrElseSampler(new AlwaysOnSampler()); | ||
assert.strictEqual(sampler.toString(), 'ParentOrElse{AlwaysOnSampler}'); | ||
|
||
const spanContext = { | ||
traceId, | ||
spanId, | ||
traceFlags: TraceFlags.NONE, | ||
}; | ||
assert.deepStrictEqual( | ||
sampler.shouldSample( | ||
spanContext, | ||
traceId, | ||
spanName, | ||
SpanKind.CLIENT, | ||
{}, | ||
[] | ||
), | ||
{ | ||
decision: api.SamplingDecision.NOT_RECORD, | ||
} | ||
); | ||
}); | ||
|
||
it('should return api.SamplingDecision.RECORD_AND_SAMPLED while composited with AlwaysOnSampler', () => { | ||
const sampler = new ParentOrElseSampler(new AlwaysOnSampler()); | ||
assert.strictEqual(sampler.toString(), 'ParentOrElse{AlwaysOnSampler}'); | ||
|
||
assert.deepStrictEqual( | ||
sampler.shouldSample( | ||
undefined, | ||
traceId, | ||
spanName, | ||
SpanKind.CLIENT, | ||
{}, | ||
[] | ||
), | ||
{ | ||
decision: api.SamplingDecision.RECORD_AND_SAMPLED, | ||
} | ||
); | ||
}); | ||
|
||
it('should return api.SamplingDecision.RECORD_AND_SAMPLED for sampled parent while composited with AlwaysOffSampler', () => { | ||
const sampler = new ParentOrElseSampler(new AlwaysOffSampler()); | ||
assert.strictEqual(sampler.toString(), 'ParentOrElse{AlwaysOffSampler}'); | ||
|
||
const spanContext = { | ||
traceId, | ||
spanId, | ||
traceFlags: TraceFlags.NONE, | ||
}; | ||
assert.deepStrictEqual( | ||
sampler.shouldSample( | ||
spanContext, | ||
traceId, | ||
spanName, | ||
SpanKind.CLIENT, | ||
{}, | ||
[] | ||
), | ||
{ | ||
decision: api.SamplingDecision.RECORD_AND_SAMPLED, | ||
} | ||
); | ||
}); | ||
|
||
it('should return api.SamplingDecision.RECORD_AND_SAMPLED while composited with AlwaysOffSampler', () => { | ||
const sampler = new ParentOrElseSampler(new AlwaysOffSampler()); | ||
assert.strictEqual(sampler.toString(), 'ParentOrElse{AlwaysOffSampler}'); | ||
|
||
assert.deepStrictEqual( | ||
sampler.shouldSample( | ||
undefined, | ||
traceId, | ||
spanName, | ||
SpanKind.CLIENT, | ||
{}, | ||
[] | ||
), | ||
{ | ||
decision: api.SamplingDecision.NOT_RECORD, | ||
} | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.