UNPKG

5.03 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.Tracer = void 0;
19const api = require("@opentelemetry/api");
20const core_1 = require("@opentelemetry/core");
21const Span_1 = require("./Span");
22const utility_1 = require("./utility");
23/**
24 * This class represents a basic tracer.
25 */
26class Tracer {
27 /**
28 * Constructs a new Tracer instance.
29 */
30 constructor(instrumentationLibrary, config, _tracerProvider) {
31 this._tracerProvider = _tracerProvider;
32 const localConfig = utility_1.mergeConfig(config);
33 this._sampler = localConfig.sampler;
34 this._spanLimits = localConfig.spanLimits;
35 this._idGenerator = config.idGenerator || new core_1.RandomIdGenerator();
36 this.resource = _tracerProvider.resource;
37 this.instrumentationLibrary = instrumentationLibrary;
38 }
39 /**
40 * Starts a new Span or returns the default NoopSpan based on the sampling
41 * decision.
42 */
43 startSpan(name, options = {}, context = api.context.active()) {
44 var _a, _b;
45 if (core_1.isTracingSuppressed(context)) {
46 api.diag.debug('Instrumentation suppressed, returning Noop Span');
47 return api.trace.wrapSpanContext(api.INVALID_SPAN_CONTEXT);
48 }
49 const parentContext = getParent(options, context);
50 const spanId = this._idGenerator.generateSpanId();
51 let traceId;
52 let traceState;
53 let parentSpanId;
54 if (!parentContext || !api.trace.isSpanContextValid(parentContext)) {
55 // New root span.
56 traceId = this._idGenerator.generateTraceId();
57 }
58 else {
59 // New child span.
60 traceId = parentContext.traceId;
61 traceState = parentContext.traceState;
62 parentSpanId = parentContext.spanId;
63 }
64 const spanKind = (_a = options.kind) !== null && _a !== void 0 ? _a : api.SpanKind.INTERNAL;
65 const links = (_b = options.links) !== null && _b !== void 0 ? _b : [];
66 const attributes = core_1.sanitizeAttributes(options.attributes);
67 // make sampling decision
68 const samplingResult = this._sampler.shouldSample(options.root
69 ? api.trace.setSpanContext(context, api.INVALID_SPAN_CONTEXT)
70 : context, traceId, name, spanKind, attributes, links);
71 const traceFlags = samplingResult.decision === api.SamplingDecision.RECORD_AND_SAMPLED
72 ? api.TraceFlags.SAMPLED
73 : api.TraceFlags.NONE;
74 const spanContext = { traceId, spanId, traceFlags, traceState };
75 if (samplingResult.decision === api.SamplingDecision.NOT_RECORD) {
76 api.diag.debug('Recording is off, propagating context in a non-recording span');
77 return api.trace.wrapSpanContext(spanContext);
78 }
79 const span = new Span_1.Span(this, context, name, spanContext, spanKind, parentSpanId, links, options.startTime);
80 // Set default attributes
81 span.setAttributes(Object.assign(attributes, samplingResult.attributes));
82 return span;
83 }
84 startActiveSpan(name, arg2, arg3, arg4) {
85 let opts;
86 let ctx;
87 let fn;
88 if (arguments.length < 2) {
89 return;
90 }
91 else if (arguments.length === 2) {
92 fn = arg2;
93 }
94 else if (arguments.length === 3) {
95 opts = arg2;
96 fn = arg3;
97 }
98 else {
99 opts = arg2;
100 ctx = arg3;
101 fn = arg4;
102 }
103 const parentContext = ctx !== null && ctx !== void 0 ? ctx : api.context.active();
104 const span = this.startSpan(name, opts, parentContext);
105 const contextWithSpanSet = api.trace.setSpan(parentContext, span);
106 return api.context.with(contextWithSpanSet, fn, undefined, span);
107 }
108 /** Returns the active {@link SpanLimits}. */
109 getSpanLimits() {
110 return this._spanLimits;
111 }
112 getActiveSpanProcessor() {
113 return this._tracerProvider.getActiveSpanProcessor();
114 }
115}
116exports.Tracer = 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