-
Notifications
You must be signed in to change notification settings - Fork 821
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement parent based sampler (#1577)
- Loading branch information
Showing
7 changed files
with
214 additions
and
90 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
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
138 changes: 138 additions & 0 deletions
138
packages/opentelemetry-core/src/trace/sampler/ParentBasedSampler.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,138 @@ | ||
/* | ||
* 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 { | ||
Attributes, | ||
Link, | ||
Sampler, | ||
SamplingResult, | ||
SpanContext, | ||
SpanKind, | ||
TraceFlags, | ||
} from '@opentelemetry/api'; | ||
import { AlwaysOffSampler } from './AlwaysOffSampler'; | ||
import { AlwaysOnSampler } from './AlwaysOnSampler'; | ||
import { globalErrorHandler } from '../../common/global-error-handler'; | ||
|
||
/** | ||
* A composite sampler that either respects the parent span's sampling decision | ||
* or delegates to `delegateSampler` for root spans. | ||
*/ | ||
export class ParentBasedSampler implements Sampler { | ||
private _root: Sampler; | ||
private _remoteParentSampled: Sampler; | ||
private _remoteParentNotSampled: Sampler; | ||
private _localParentSampled: Sampler; | ||
private _localParentNotSampled: Sampler; | ||
|
||
constructor(config: ParentBasedSamplerConfig) { | ||
this._root = config.root; | ||
|
||
if (!this._root) { | ||
globalErrorHandler( | ||
new Error('ParentBasedSampler must have a root sampler configured') | ||
); | ||
this._root = new AlwaysOnSampler(); | ||
} | ||
|
||
this._remoteParentSampled = | ||
config.remoteParentSampled ?? new AlwaysOnSampler(); | ||
this._remoteParentNotSampled = | ||
config.remoteParentNotSampled ?? new AlwaysOffSampler(); | ||
this._localParentSampled = | ||
config.localParentSampled ?? new AlwaysOnSampler(); | ||
this._localParentNotSampled = | ||
config.localParentNotSampled ?? new AlwaysOffSampler(); | ||
} | ||
|
||
shouldSample( | ||
parentContext: SpanContext | undefined, | ||
traceId: string, | ||
spanName: string, | ||
spanKind: SpanKind, | ||
attributes: Attributes, | ||
links: Link[] | ||
): SamplingResult { | ||
if (!parentContext) { | ||
return this._root.shouldSample( | ||
parentContext, | ||
traceId, | ||
spanName, | ||
spanKind, | ||
attributes, | ||
links | ||
); | ||
} | ||
|
||
if (parentContext.isRemote) { | ||
if (parentContext.traceFlags & TraceFlags.SAMPLED) { | ||
return this._remoteParentSampled.shouldSample( | ||
parentContext, | ||
traceId, | ||
spanName, | ||
spanKind, | ||
attributes, | ||
links | ||
); | ||
} | ||
return this._remoteParentNotSampled.shouldSample( | ||
parentContext, | ||
traceId, | ||
spanName, | ||
spanKind, | ||
attributes, | ||
links | ||
); | ||
} | ||
|
||
if (parentContext.traceFlags & TraceFlags.SAMPLED) { | ||
return this._localParentSampled.shouldSample( | ||
parentContext, | ||
traceId, | ||
spanName, | ||
spanKind, | ||
attributes, | ||
links | ||
); | ||
} | ||
|
||
return this._localParentNotSampled.shouldSample( | ||
parentContext, | ||
traceId, | ||
spanName, | ||
spanKind, | ||
attributes, | ||
links | ||
); | ||
} | ||
|
||
toString(): string { | ||
return `ParentBased{root=${this._root.toString()}, remoteParentSampled=${this._remoteParentSampled.toString()}, remoteParentNotSampled=${this._remoteParentNotSampled.toString()}, localParentSampled=${this._localParentSampled.toString()}, localParentNotSampled=${this._localParentNotSampled.toString()}}`; | ||
} | ||
} | ||
|
||
interface ParentBasedSamplerConfig { | ||
/** Sampler called for spans with no parent */ | ||
root: Sampler; | ||
/** Sampler called for spans with a remote parent which was sampled. Default AlwaysOn */ | ||
remoteParentSampled?: Sampler; | ||
/** Sampler called for spans with a remote parent which was not sampled. Default AlwaysOff */ | ||
remoteParentNotSampled?: Sampler; | ||
/** Sampler called for spans with a local parent which was sampled. Default AlwaysOn */ | ||
localParentSampled?: Sampler; | ||
/** Sampler called for spans with a local parent which was not sampled. Default AlwaysOff */ | ||
localParentNotSampled?: Sampler; | ||
} |
65 changes: 0 additions & 65 deletions
65
packages/opentelemetry-core/src/trace/sampler/ParentOrElseSampler.ts
This file was deleted.
Oops, something went wrong.
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
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.