UNPKG

5.21 kBJavaScriptView Raw
1/*
2 * Copyright The OpenTelemetry Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import * as api from '@opentelemetry/api';
17import { RandomIdGenerator, sanitizeAttributes, isTracingSuppressed, } from '@opentelemetry/core';
18import { Span } from './Span';
19import { mergeConfig } from './utility';
20/**
21 * This class represents a basic tracer.
22 */
23var Tracer = /** @class */ (function () {
24 /**
25 * Constructs a new Tracer instance.
26 */
27 function Tracer(instrumentationLibrary, config, _tracerProvider) {
28 this._tracerProvider = _tracerProvider;
29 var localConfig = mergeConfig(config);
30 this._sampler = localConfig.sampler;
31 this._spanLimits = localConfig.spanLimits;
32 this._idGenerator = config.idGenerator || new RandomIdGenerator();
33 this.resource = _tracerProvider.resource;
34 this.instrumentationLibrary = instrumentationLibrary;
35 }
36 /**
37 * Starts a new Span or returns the default NoopSpan based on the sampling
38 * decision.
39 */
40 Tracer.prototype.startSpan = function (name, options, context) {
41 var _a, _b;
42 if (options === void 0) { options = {}; }
43 if (context === void 0) { context = api.context.active(); }
44 if (isTracingSuppressed(context)) {
45 api.diag.debug('Instrumentation suppressed, returning Noop Span');
46 return api.trace.wrapSpanContext(api.INVALID_SPAN_CONTEXT);
47 }
48 var parentContext = getParent(options, context);
49 var spanId = this._idGenerator.generateSpanId();
50 var traceId;
51 var traceState;
52 var parentSpanId;
53 if (!parentContext || !api.trace.isSpanContextValid(parentContext)) {
54 // New root span.
55 traceId = this._idGenerator.generateTraceId();
56 }
57 else {
58 // New child span.
59 traceId = parentContext.traceId;
60 traceState = parentContext.traceState;
61 parentSpanId = parentContext.spanId;
62 }
63 var spanKind = (_a = options.kind) !== null && _a !== void 0 ? _a : api.SpanKind.INTERNAL;
64 var links = (_b = options.links) !== null && _b !== void 0 ? _b : [];
65 var attributes = sanitizeAttributes(options.attributes);
66 // make sampling decision
67 var samplingResult = this._sampler.shouldSample(options.root
68 ? api.trace.setSpanContext(context, api.INVALID_SPAN_CONTEXT)
69 : context, traceId, name, spanKind, attributes, links);
70 var traceFlags = samplingResult.decision === api.SamplingDecision.RECORD_AND_SAMPLED
71 ? api.TraceFlags.SAMPLED
72 : api.TraceFlags.NONE;
73 var spanContext = { traceId: traceId, spanId: spanId, traceFlags: traceFlags, traceState: traceState };
74 if (samplingResult.decision === api.SamplingDecision.NOT_RECORD) {
75 api.diag.debug('Recording is off, propagating context in a non-recording span');
76 return api.trace.wrapSpanContext(spanContext);
77 }
78 var span = new Span(this, context, name, spanContext, spanKind, parentSpanId, links, options.startTime);
79 // Set default attributes
80 span.setAttributes(Object.assign(attributes, samplingResult.attributes));
81 return span;
82 };
83 Tracer.prototype.startActiveSpan = function (name, arg2, arg3, arg4) {
84 var opts;
85 var ctx;
86 var fn;
87 if (arguments.length < 2) {
88 return;
89 }
90 else if (arguments.length === 2) {
91 fn = arg2;
92 }
93 else if (arguments.length === 3) {
94 opts = arg2;
95 fn = arg3;
96 }
97 else {
98 opts = arg2;
99 ctx = arg3;
100 fn = arg4;
101 }
102 var parentContext = ctx !== null && ctx !== void 0 ? ctx : api.context.active();
103 var span = this.startSpan(name, opts, parentContext);
104 var contextWithSpanSet = api.trace.setSpan(parentContext, span);
105 return api.context.with(contextWithSpanSet, fn, undefined, span);
106 };
107 /** Returns the active {@link SpanLimits}. */
108 Tracer.prototype.getSpanLimits = function () {
109 return this._spanLimits;
110 };
111 Tracer.prototype.getActiveSpanProcessor = function () {
112 return this._tracerProvider.getActiveSpanProcessor();
113 };
114 return Tracer;
115}());
116export { Tracer };
117/**
118 * Get the parent to assign to a started span. If options.parent is null,
119 * do not assign a parent.
120 *
121 * @param options span options
122 * @param context context to check for parent
123 */
124function getParent(options, context) {
125 if (options.root)
126 return undefined;
127 return api.trace.getSpanContext(context);
128}
129//# sourceMappingURL=Tracer.js.map
\No newline at end of file