UNPKG

3.61 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright The OpenTelemetry Authors
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.ParentBasedSampler = void 0;
19const api_1 = require("@opentelemetry/api");
20const global_error_handler_1 = require("../../common/global-error-handler");
21const AlwaysOffSampler_1 = require("./AlwaysOffSampler");
22const AlwaysOnSampler_1 = require("./AlwaysOnSampler");
23/**
24 * @deprecated Use the one defined in @opentelemetry/sdk-trace-base instead.
25 * A composite sampler that either respects the parent span's sampling decision
26 * or delegates to `delegateSampler` for root spans.
27 */
28class ParentBasedSampler {
29 constructor(config) {
30 var _a, _b, _c, _d;
31 this._root = config.root;
32 if (!this._root) {
33 (0, global_error_handler_1.globalErrorHandler)(new Error('ParentBasedSampler must have a root sampler configured'));
34 this._root = new AlwaysOnSampler_1.AlwaysOnSampler();
35 }
36 this._remoteParentSampled =
37 (_a = config.remoteParentSampled) !== null && _a !== void 0 ? _a : new AlwaysOnSampler_1.AlwaysOnSampler();
38 this._remoteParentNotSampled =
39 (_b = config.remoteParentNotSampled) !== null && _b !== void 0 ? _b : new AlwaysOffSampler_1.AlwaysOffSampler();
40 this._localParentSampled =
41 (_c = config.localParentSampled) !== null && _c !== void 0 ? _c : new AlwaysOnSampler_1.AlwaysOnSampler();
42 this._localParentNotSampled =
43 (_d = config.localParentNotSampled) !== null && _d !== void 0 ? _d : new AlwaysOffSampler_1.AlwaysOffSampler();
44 }
45 shouldSample(context, traceId, spanName, spanKind, attributes, links) {
46 const parentContext = api_1.trace.getSpanContext(context);
47 if (!parentContext || !(0, api_1.isSpanContextValid)(parentContext)) {
48 return this._root.shouldSample(context, traceId, spanName, spanKind, attributes, links);
49 }
50 if (parentContext.isRemote) {
51 if (parentContext.traceFlags & api_1.TraceFlags.SAMPLED) {
52 return this._remoteParentSampled.shouldSample(context, traceId, spanName, spanKind, attributes, links);
53 }
54 return this._remoteParentNotSampled.shouldSample(context, traceId, spanName, spanKind, attributes, links);
55 }
56 if (parentContext.traceFlags & api_1.TraceFlags.SAMPLED) {
57 return this._localParentSampled.shouldSample(context, traceId, spanName, spanKind, attributes, links);
58 }
59 return this._localParentNotSampled.shouldSample(context, traceId, spanName, spanKind, attributes, links);
60 }
61 toString() {
62 return `ParentBased{root=${this._root.toString()}, remoteParentSampled=${this._remoteParentSampled.toString()}, remoteParentNotSampled=${this._remoteParentNotSampled.toString()}, localParentSampled=${this._localParentSampled.toString()}, localParentNotSampled=${this._localParentNotSampled.toString()}}`;
63 }
64}
65exports.ParentBasedSampler = ParentBasedSampler;
66//# sourceMappingURL=ParentBasedSampler.js.map
\No newline at end of file