1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import * as api from '@opentelemetry/api';
|
17 | import { RandomIdGenerator, sanitizeAttributes, isTracingSuppressed, } from '@opentelemetry/core';
|
18 | import { Span } from './Span';
|
19 | import { mergeConfig } from './utility';
|
20 |
|
21 |
|
22 |
|
23 | var Tracer = (function () {
|
24 | |
25 |
|
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 |
|
38 |
|
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 |
|
55 | traceId = this._idGenerator.generateTraceId();
|
56 | }
|
57 | else {
|
58 |
|
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 |
|
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 |
|
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 |
|
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 | }());
|
116 | export { Tracer };
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | function getParent(options, context) {
|
125 | if (options.root)
|
126 | return undefined;
|
127 | return api.trace.getSpanContext(context);
|
128 | }
|
129 |
|
\ | No newline at end of file |