@sentry/opentelemetry 8.0.0-alpha.7 → 8.0.0-alpha.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/cjs/index.js +376 -445
- package/cjs/index.js.map +1 -1
- package/esm/index.js +379 -448
- package/esm/index.js.map +1 -1
- package/package.json +4 -4
- package/types/custom/getCurrentHub.d.ts.map +1 -1
- package/types/propagator.d.ts +2 -10
- package/types/propagator.d.ts.map +1 -1
- package/types/sampler.d.ts +3 -3
- package/types/sampler.d.ts.map +1 -1
- package/types/spanProcessor.d.ts +1 -1
- package/types/spanProcessor.d.ts.map +1 -1
- package/types/trace.d.ts +0 -4
- package/types/trace.d.ts.map +1 -1
- package/types/utils/dynamicSamplingContext.d.ts.map +1 -1
- package/types/utils/getSamplingDecision.d.ts +10 -0
- package/types/utils/getSamplingDecision.d.ts.map +1 -0
- package/types-ts3.8/propagator.d.ts +2 -10
- package/types-ts3.8/sampler.d.ts +3 -3
- package/types-ts3.8/spanProcessor.d.ts +1 -1
- package/types-ts3.8/trace.d.ts +0 -4
- package/types-ts3.8/utils/getSamplingDecision.d.ts +10 -0
package/cjs/index.js
CHANGED
|
@@ -119,7 +119,7 @@ function getRequestSpanData(span) {
|
|
|
119
119
|
return data;
|
|
120
120
|
}
|
|
121
121
|
|
|
122
|
-
function _optionalChain$
|
|
122
|
+
function _optionalChain$9(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
123
123
|
|
|
124
124
|
// Typescript complains if we do not use `...args: any[]` for the mixin, with:
|
|
125
125
|
// A mixin class must have a constructor with a single rest parameter of type 'any[]'.ts(2545)
|
|
@@ -160,7 +160,7 @@ function wrapClientClass
|
|
|
160
160
|
*/
|
|
161
161
|
async flush(timeout) {
|
|
162
162
|
const provider = this.traceProvider;
|
|
163
|
-
const spanProcessor = _optionalChain$
|
|
163
|
+
const spanProcessor = _optionalChain$9([provider, 'optionalAccess', _ => _.activeSpanProcessor]);
|
|
164
164
|
|
|
165
165
|
if (spanProcessor) {
|
|
166
166
|
await spanProcessor.forceFlush();
|
|
@@ -265,6 +265,273 @@ function getContextFromScope(scope) {
|
|
|
265
265
|
return SCOPE_CONTEXT_MAP.get(scope);
|
|
266
266
|
}
|
|
267
267
|
|
|
268
|
+
function _optionalChain$8(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* OpenTelemetry only knows about SAMPLED or NONE decision,
|
|
272
|
+
* but for us it is important to differentiate between unset and unsampled.
|
|
273
|
+
*
|
|
274
|
+
* Both of these are identified as `traceFlags === TracegFlags.NONE`,
|
|
275
|
+
* but we additionally look at a special trace state to differentiate between them.
|
|
276
|
+
*/
|
|
277
|
+
function getSamplingDecision(spanContext) {
|
|
278
|
+
const { traceFlags, traceState } = spanContext;
|
|
279
|
+
|
|
280
|
+
const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
|
|
281
|
+
|
|
282
|
+
// If trace flag is `SAMPLED`, we interpret this as sampled
|
|
283
|
+
// If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
|
|
284
|
+
// For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
|
|
285
|
+
// to identify which it is
|
|
286
|
+
if (traceFlags === api.TraceFlags.SAMPLED) {
|
|
287
|
+
return true;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (sampledNotRecording) {
|
|
291
|
+
return false;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Fall back to DSC as a last resort, that may also contain `sampled`...
|
|
295
|
+
const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
|
|
296
|
+
const dsc = dscString ? utils.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
|
|
297
|
+
|
|
298
|
+
if (_optionalChain$8([dsc, 'optionalAccess', _ => _.sampled]) === 'true') {
|
|
299
|
+
return true;
|
|
300
|
+
}
|
|
301
|
+
if (_optionalChain$8([dsc, 'optionalAccess', _2 => _2.sampled]) === 'false') {
|
|
302
|
+
return false;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return undefined;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Extract better op/description from an otel span.
|
|
310
|
+
*
|
|
311
|
+
* Based on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/7422ce2a06337f68a59b552b8c5a2ac125d6bae5/exporter/sentryexporter/sentry_exporter.go#L306
|
|
312
|
+
*/
|
|
313
|
+
function parseSpanDescription(span) {
|
|
314
|
+
const attributes = spanHasAttributes(span) ? span.attributes : {};
|
|
315
|
+
const name = spanHasName(span) ? span.name : '<unknown>';
|
|
316
|
+
|
|
317
|
+
// if http.method exists, this is an http request span
|
|
318
|
+
const httpMethod = attributes[semanticConventions.SemanticAttributes.HTTP_METHOD];
|
|
319
|
+
if (httpMethod) {
|
|
320
|
+
return descriptionForHttpMethod({ attributes, name, kind: getSpanKind(span) }, httpMethod);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// If db.type exists then this is a database call span.
|
|
324
|
+
const dbSystem = attributes[semanticConventions.SemanticAttributes.DB_SYSTEM];
|
|
325
|
+
if (dbSystem) {
|
|
326
|
+
return descriptionForDbSystem({ attributes, name });
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// If rpc.service exists then this is a rpc call span.
|
|
330
|
+
const rpcService = attributes[semanticConventions.SemanticAttributes.RPC_SERVICE];
|
|
331
|
+
if (rpcService) {
|
|
332
|
+
return {
|
|
333
|
+
op: 'rpc',
|
|
334
|
+
description: name,
|
|
335
|
+
source: 'route',
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// If messaging.system exists then this is a messaging system span.
|
|
340
|
+
const messagingSystem = attributes[semanticConventions.SemanticAttributes.MESSAGING_SYSTEM];
|
|
341
|
+
if (messagingSystem) {
|
|
342
|
+
return {
|
|
343
|
+
op: 'message',
|
|
344
|
+
description: name,
|
|
345
|
+
source: 'route',
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// If faas.trigger exists then this is a function as a service span.
|
|
350
|
+
const faasTrigger = attributes[semanticConventions.SemanticAttributes.FAAS_TRIGGER];
|
|
351
|
+
if (faasTrigger) {
|
|
352
|
+
return { op: faasTrigger.toString(), description: name, source: 'route' };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return { op: undefined, description: name, source: 'custom' };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function descriptionForDbSystem({ attributes, name }) {
|
|
359
|
+
// Use DB statement (Ex "SELECT * FROM table") if possible as description.
|
|
360
|
+
const statement = attributes[semanticConventions.SemanticAttributes.DB_STATEMENT];
|
|
361
|
+
|
|
362
|
+
const description = statement ? statement.toString() : name;
|
|
363
|
+
|
|
364
|
+
return { op: 'db', description, source: 'task' };
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** Only exported for tests. */
|
|
368
|
+
function descriptionForHttpMethod(
|
|
369
|
+
{ name, kind, attributes },
|
|
370
|
+
httpMethod,
|
|
371
|
+
) {
|
|
372
|
+
const opParts = ['http'];
|
|
373
|
+
|
|
374
|
+
switch (kind) {
|
|
375
|
+
case api.SpanKind.CLIENT:
|
|
376
|
+
opParts.push('client');
|
|
377
|
+
break;
|
|
378
|
+
case api.SpanKind.SERVER:
|
|
379
|
+
opParts.push('server');
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const { urlPath, url, query, fragment, hasRoute } = getSanitizedUrl(attributes, kind);
|
|
384
|
+
|
|
385
|
+
if (!urlPath) {
|
|
386
|
+
return { op: opParts.join('.'), description: name, source: 'custom' };
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Ex. description="GET /api/users".
|
|
390
|
+
const description = `${httpMethod} ${urlPath}`;
|
|
391
|
+
|
|
392
|
+
// If `httpPath` is a root path, then we can categorize the transaction source as route.
|
|
393
|
+
const source = hasRoute || urlPath === '/' ? 'route' : 'url';
|
|
394
|
+
|
|
395
|
+
const data = {};
|
|
396
|
+
|
|
397
|
+
if (url) {
|
|
398
|
+
data.url = url;
|
|
399
|
+
}
|
|
400
|
+
if (query) {
|
|
401
|
+
data['http.query'] = query;
|
|
402
|
+
}
|
|
403
|
+
if (fragment) {
|
|
404
|
+
data['http.fragment'] = fragment;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return {
|
|
408
|
+
op: opParts.join('.'),
|
|
409
|
+
description,
|
|
410
|
+
source,
|
|
411
|
+
data,
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/** Exported for tests only */
|
|
416
|
+
function getSanitizedUrl(
|
|
417
|
+
attributes,
|
|
418
|
+
kind,
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
{
|
|
422
|
+
// This is the relative path of the URL, e.g. /sub
|
|
423
|
+
const httpTarget = attributes[semanticConventions.SemanticAttributes.HTTP_TARGET];
|
|
424
|
+
// This is the full URL, including host & query params etc., e.g. https://example.com/sub?foo=bar
|
|
425
|
+
const httpUrl = attributes[semanticConventions.SemanticAttributes.HTTP_URL];
|
|
426
|
+
// This is the normalized route name - may not always be available!
|
|
427
|
+
const httpRoute = attributes[semanticConventions.SemanticAttributes.HTTP_ROUTE];
|
|
428
|
+
|
|
429
|
+
const parsedUrl = typeof httpUrl === 'string' ? utils.parseUrl(httpUrl) : undefined;
|
|
430
|
+
const url = parsedUrl ? utils.getSanitizedUrlString(parsedUrl) : undefined;
|
|
431
|
+
const query = parsedUrl && parsedUrl.search ? parsedUrl.search : undefined;
|
|
432
|
+
const fragment = parsedUrl && parsedUrl.hash ? parsedUrl.hash : undefined;
|
|
433
|
+
|
|
434
|
+
if (typeof httpRoute === 'string') {
|
|
435
|
+
return { urlPath: httpRoute, url, query, fragment, hasRoute: true };
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (kind === api.SpanKind.SERVER && typeof httpTarget === 'string') {
|
|
439
|
+
return { urlPath: utils.stripUrlQueryAndFragment(httpTarget), url, query, fragment, hasRoute: false };
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (parsedUrl) {
|
|
443
|
+
return { urlPath: url, url, query, fragment, hasRoute: false };
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
// fall back to target even for client spans, if no URL is present
|
|
447
|
+
if (typeof httpTarget === 'string') {
|
|
448
|
+
return { urlPath: utils.stripUrlQueryAndFragment(httpTarget), url, query, fragment, hasRoute: false };
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return { urlPath: undefined, url, query, fragment, hasRoute: false };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function _optionalChain$7(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
455
|
+
/**
|
|
456
|
+
* Creates a dynamic sampling context from a span (and client and scope)
|
|
457
|
+
*
|
|
458
|
+
* @param span the span from which a few values like the root span name and sample rate are extracted.
|
|
459
|
+
*
|
|
460
|
+
* @returns a dynamic sampling context
|
|
461
|
+
*/
|
|
462
|
+
function getDynamicSamplingContextFromSpan(span) {
|
|
463
|
+
const client = core.getClient();
|
|
464
|
+
if (!client) {
|
|
465
|
+
return {};
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const traceState = span.spanContext().traceState;
|
|
469
|
+
const traceStateDsc = _optionalChain$7([traceState, 'optionalAccess', _ => _.get, 'call', _2 => _2(SENTRY_TRACE_STATE_DSC)]);
|
|
470
|
+
|
|
471
|
+
// If the span has a DSC, we want it to take precedence
|
|
472
|
+
const dscOnSpan = traceStateDsc ? utils.baggageHeaderToDynamicSamplingContext(traceStateDsc) : undefined;
|
|
473
|
+
|
|
474
|
+
if (dscOnSpan) {
|
|
475
|
+
return dscOnSpan;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const dsc = core.getDynamicSamplingContextFromClient(span.spanContext().traceId, client);
|
|
479
|
+
|
|
480
|
+
const attributes = spanHasAttributes(span) ? span.attributes : {};
|
|
481
|
+
|
|
482
|
+
const sampleRate = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
|
|
483
|
+
if (sampleRate != null) {
|
|
484
|
+
dsc.sample_rate = `${sampleRate}`;
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII
|
|
488
|
+
const source = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
|
|
489
|
+
|
|
490
|
+
// If the span has no name, we assume it is non-recording and want to opt out of using any description
|
|
491
|
+
const { description } = spanHasName(span) ? parseSpanDescription(span) : { description: '' };
|
|
492
|
+
|
|
493
|
+
if (source !== 'url' && description) {
|
|
494
|
+
dsc.transaction = description;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
const sampled = getSamplingDecision(span.spanContext());
|
|
498
|
+
if (sampled != null) {
|
|
499
|
+
dsc.sampled = String(sampled);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
client.emit('createDsc', dsc);
|
|
503
|
+
|
|
504
|
+
return dsc;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
*
|
|
509
|
+
* @param otelSpan Checks wheter a given OTEL Span is an http request to sentry.
|
|
510
|
+
* @returns boolean
|
|
511
|
+
*/
|
|
512
|
+
function isSentryRequestSpan(span) {
|
|
513
|
+
if (!spanHasAttributes(span)) {
|
|
514
|
+
return false;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const { attributes } = span;
|
|
518
|
+
|
|
519
|
+
const httpUrl = attributes[semanticConventions.SemanticAttributes.HTTP_URL];
|
|
520
|
+
|
|
521
|
+
if (!httpUrl) {
|
|
522
|
+
return false;
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
return core.isSentryRequestUrl(httpUrl.toString(), core.getClient());
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* Returns the currently active span.
|
|
530
|
+
*/
|
|
531
|
+
function getActiveSpan() {
|
|
532
|
+
return api.trace.getActiveSpan();
|
|
533
|
+
}
|
|
534
|
+
|
|
268
535
|
/**
|
|
269
536
|
* This serves as a build time flag that will be true by default, but false in non-debug builds or if users replace `__SENTRY_DEBUG__` in their generated code.
|
|
270
537
|
*
|
|
@@ -284,18 +551,25 @@ function setIsSetup(element) {
|
|
|
284
551
|
setupElements.add(element);
|
|
285
552
|
}
|
|
286
553
|
|
|
287
|
-
function _optionalChain$
|
|
554
|
+
function _optionalChain$6(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
288
555
|
|
|
289
556
|
/** Get the Sentry propagation context from a span context. */
|
|
290
|
-
function
|
|
557
|
+
function getPropagationContextFromSpan(span) {
|
|
558
|
+
const spanContext = span.spanContext();
|
|
291
559
|
const { traceId, spanId, traceState } = spanContext;
|
|
292
560
|
|
|
561
|
+
// When we have a dsc trace state, it means this came from the incoming trace
|
|
562
|
+
// Then this takes presedence over the root span
|
|
293
563
|
const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
|
|
294
|
-
const
|
|
564
|
+
const traceStateDsc = dscString ? utils.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
|
|
565
|
+
|
|
295
566
|
const parentSpanId = traceState ? traceState.get(SENTRY_TRACE_STATE_PARENT_SPAN_ID) : undefined;
|
|
296
567
|
|
|
297
568
|
const sampled = getSamplingDecision(spanContext);
|
|
298
569
|
|
|
570
|
+
// No trace state? --> Take DSC from root span
|
|
571
|
+
const dsc = traceStateDsc || getDynamicSamplingContextFromSpan(core.getRootSpan(span));
|
|
572
|
+
|
|
299
573
|
return {
|
|
300
574
|
traceId,
|
|
301
575
|
spanId,
|
|
@@ -329,8 +603,8 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
|
|
|
329
603
|
}
|
|
330
604
|
|
|
331
605
|
const activeSpan = api.trace.getSpan(context);
|
|
332
|
-
const url = activeSpan && _optionalChain$
|
|
333
|
-
const tracePropagationTargets = _optionalChain$
|
|
606
|
+
const url = activeSpan && _optionalChain$6([core.spanToJSON, 'call', _ => _(activeSpan), 'access', _2 => _2.data, 'optionalAccess', _3 => _3[semanticConventions.SemanticAttributes.HTTP_URL]]);
|
|
607
|
+
const tracePropagationTargets = _optionalChain$6([core.getClient, 'call', _4 => _4(), 'optionalAccess', _5 => _5.getOptions, 'call', _6 => _6(), 'optionalAccess', _7 => _7.tracePropagationTargets]);
|
|
334
608
|
if (
|
|
335
609
|
typeof url === 'string' &&
|
|
336
610
|
tracePropagationTargets &&
|
|
@@ -341,10 +615,21 @@ class SentryPropagator extends core$1.W3CBaggagePropagator {
|
|
|
341
615
|
return;
|
|
342
616
|
}
|
|
343
617
|
|
|
618
|
+
const existingBaggageHeader = getExistingBaggage(carrier);
|
|
344
619
|
let baggage = api.propagation.getBaggage(context) || api.propagation.createBaggage({});
|
|
345
620
|
|
|
346
621
|
const { dynamicSamplingContext, traceId, spanId, sampled } = getInjectionData(context);
|
|
347
622
|
|
|
623
|
+
if (existingBaggageHeader) {
|
|
624
|
+
const baggageEntries = utils.parseBaggageHeader(existingBaggageHeader);
|
|
625
|
+
|
|
626
|
+
if (baggageEntries) {
|
|
627
|
+
Object.entries(baggageEntries).forEach(([key, value]) => {
|
|
628
|
+
baggage = baggage.setEntry(key, { value });
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
348
633
|
if (dynamicSamplingContext) {
|
|
349
634
|
baggage = Object.entries(dynamicSamplingContext).reduce((b, [dscKey, dscValue]) => {
|
|
350
635
|
if (dscValue) {
|
|
@@ -442,12 +727,13 @@ function getInjectionData(context)
|
|
|
442
727
|
|
|
443
728
|
{
|
|
444
729
|
const span = api.trace.getSpan(context);
|
|
445
|
-
const spanIsRemote = _optionalChain$
|
|
730
|
+
const spanIsRemote = _optionalChain$6([span, 'optionalAccess', _8 => _8.spanContext, 'call', _9 => _9(), 'access', _10 => _10.isRemote]);
|
|
446
731
|
|
|
447
732
|
// If we have a local span, we can just pick everything from it
|
|
448
733
|
if (span && !spanIsRemote) {
|
|
449
734
|
const spanContext = span.spanContext();
|
|
450
|
-
|
|
735
|
+
|
|
736
|
+
const propagationContext = getPropagationContextFromSpan(span);
|
|
451
737
|
const dynamicSamplingContext = getDynamicSamplingContext(propagationContext, spanContext.traceId);
|
|
452
738
|
return {
|
|
453
739
|
dynamicSamplingContext,
|
|
@@ -458,7 +744,7 @@ function getInjectionData(context)
|
|
|
458
744
|
}
|
|
459
745
|
|
|
460
746
|
// Else we try to use the propagation context from the scope
|
|
461
|
-
const scope = _optionalChain$
|
|
747
|
+
const scope = _optionalChain$6([getScopesFromContext, 'call', _11 => _11(context), 'optionalAccess', _12 => _12.scope]);
|
|
462
748
|
if (scope) {
|
|
463
749
|
const propagationContext = scope.getPropagationContext();
|
|
464
750
|
const dynamicSamplingContext = getDynamicSamplingContext(propagationContext, propagationContext.traceId);
|
|
@@ -471,9 +757,9 @@ function getInjectionData(context)
|
|
|
471
757
|
}
|
|
472
758
|
|
|
473
759
|
// Else, we look at the remote span context
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
const propagationContext =
|
|
760
|
+
if (span) {
|
|
761
|
+
const spanContext = span.spanContext();
|
|
762
|
+
const propagationContext = getPropagationContextFromSpan(span);
|
|
477
763
|
const dynamicSamplingContext = getDynamicSamplingContext(propagationContext, spanContext.traceId);
|
|
478
764
|
|
|
479
765
|
return {
|
|
@@ -500,173 +786,66 @@ function getDynamicSamplingContext(
|
|
|
500
786
|
traceId,
|
|
501
787
|
) {
|
|
502
788
|
// If we have a DSC on the propagation context, we just use it
|
|
503
|
-
if (_optionalChain$
|
|
789
|
+
if (_optionalChain$6([propagationContext, 'optionalAccess', _13 => _13.dsc])) {
|
|
504
790
|
return propagationContext.dsc;
|
|
505
791
|
}
|
|
506
|
-
|
|
507
|
-
// Else, we try to generate a new one
|
|
508
|
-
const client = core.getClient();
|
|
509
|
-
|
|
510
|
-
if (client) {
|
|
511
|
-
return core.getDynamicSamplingContextFromClient(traceId || propagationContext.traceId, client);
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
return undefined;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
function getContextWithRemoteActiveSpan(
|
|
518
|
-
ctx,
|
|
519
|
-
{ sentryTrace, baggage },
|
|
520
|
-
) {
|
|
521
|
-
const propagationContext = utils.propagationContextFromHeaders(sentryTrace, baggage);
|
|
522
|
-
|
|
523
|
-
// We store the DSC as OTEL trace state on the span context
|
|
524
|
-
const traceState = makeTraceState({
|
|
525
|
-
parentSpanId: propagationContext.parentSpanId,
|
|
526
|
-
dsc: propagationContext.dsc,
|
|
527
|
-
sampled: propagationContext.sampled,
|
|
528
|
-
});
|
|
529
|
-
|
|
530
|
-
const spanContext = {
|
|
531
|
-
traceId: propagationContext.traceId,
|
|
532
|
-
spanId: propagationContext.parentSpanId || '',
|
|
533
|
-
isRemote: true,
|
|
534
|
-
traceFlags: propagationContext.sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
|
|
535
|
-
traceState,
|
|
536
|
-
};
|
|
537
|
-
|
|
538
|
-
return api.trace.setSpanContext(ctx, spanContext);
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
/**
|
|
542
|
-
* Takes trace strings and propagates them as a remote active span.
|
|
543
|
-
* This should be used in addition to `continueTrace` in OTEL-powered environments.
|
|
544
|
-
*/
|
|
545
|
-
function continueTraceAsRemoteSpan(
|
|
546
|
-
ctx,
|
|
547
|
-
options,
|
|
548
|
-
callback,
|
|
549
|
-
) {
|
|
550
|
-
const ctxWithSpanContext = getContextWithRemoteActiveSpan(ctx, options);
|
|
551
|
-
|
|
552
|
-
return api.context.with(ctxWithSpanContext, callback);
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
/**
|
|
556
|
-
* OpenTelemetry only knows about SAMPLED or NONE decision,
|
|
557
|
-
* but for us it is important to differentiate between unset and unsampled.
|
|
558
|
-
*
|
|
559
|
-
* Both of these are identified as `traceFlags === TracegFlags.NONE`,
|
|
560
|
-
* but we additionally look at a special trace state to differentiate between them.
|
|
561
|
-
*/
|
|
562
|
-
function getSamplingDecision(spanContext) {
|
|
563
|
-
const { traceFlags, traceState } = spanContext;
|
|
564
|
-
|
|
565
|
-
const sampledNotRecording = traceState ? traceState.get(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING) === '1' : false;
|
|
566
|
-
|
|
567
|
-
// If trace flag is `SAMPLED`, we interpret this as sampled
|
|
568
|
-
// If it is `NONE`, it could mean either it was sampled to be not recorder, or that it was not sampled at all
|
|
569
|
-
// For us this is an important difference, sow e look at the SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING
|
|
570
|
-
// to identify which it is
|
|
571
|
-
if (traceFlags === api.TraceFlags.SAMPLED) {
|
|
572
|
-
return true;
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
if (sampledNotRecording) {
|
|
576
|
-
return false;
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
// Fall back to DSC as a last resort, that may also contain `sampled`...
|
|
580
|
-
const dscString = traceState ? traceState.get(SENTRY_TRACE_STATE_DSC) : undefined;
|
|
581
|
-
const dsc = dscString ? utils.baggageHeaderToDynamicSamplingContext(dscString) : undefined;
|
|
582
|
-
|
|
583
|
-
if (_optionalChain$7([dsc, 'optionalAccess', _14 => _14.sampled]) === 'true') {
|
|
584
|
-
return true;
|
|
585
|
-
}
|
|
586
|
-
if (_optionalChain$7([dsc, 'optionalAccess', _15 => _15.sampled]) === 'false') {
|
|
587
|
-
return false;
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
return undefined;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
function _optionalChain$6(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
594
|
-
/**
|
|
595
|
-
* Creates a dynamic sampling context from a span (and client and scope)
|
|
596
|
-
*
|
|
597
|
-
* @param span the span from which a few values like the root span name and sample rate are extracted.
|
|
598
|
-
*
|
|
599
|
-
* @returns a dynamic sampling context
|
|
600
|
-
*/
|
|
601
|
-
function getDynamicSamplingContextFromSpan(span) {
|
|
602
|
-
const client = core.getClient();
|
|
603
|
-
if (!client) {
|
|
604
|
-
return {};
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
const traceState = span.spanContext().traceState;
|
|
608
|
-
const traceStateDsc = _optionalChain$6([traceState, 'optionalAccess', _ => _.get, 'call', _2 => _2(SENTRY_TRACE_STATE_DSC)]);
|
|
609
|
-
|
|
610
|
-
// If the span has a DSC, we want it to take precedence
|
|
611
|
-
const dscOnSpan = traceStateDsc ? utils.baggageHeaderToDynamicSamplingContext(traceStateDsc) : undefined;
|
|
612
|
-
|
|
613
|
-
if (dscOnSpan) {
|
|
614
|
-
return dscOnSpan;
|
|
615
|
-
}
|
|
616
|
-
|
|
617
|
-
const dsc = core.getDynamicSamplingContextFromClient(span.spanContext().traceId, client);
|
|
618
|
-
|
|
619
|
-
const attributes = spanHasAttributes(span) ? span.attributes : {};
|
|
620
|
-
|
|
621
|
-
const sampleRate = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE];
|
|
622
|
-
if (sampleRate != null) {
|
|
623
|
-
dsc.sample_rate = `${sampleRate}`;
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
// We don't want to have a transaction name in the DSC if the source is "url" because URLs might contain PII
|
|
627
|
-
const source = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE];
|
|
628
|
-
const name = spanHasName(span) ? span.name : '';
|
|
629
|
-
|
|
630
|
-
if (source !== 'url' && name) {
|
|
631
|
-
dsc.transaction = name;
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
const sampled = getSamplingDecision(span.spanContext());
|
|
635
|
-
if (sampled != null) {
|
|
636
|
-
dsc.sampled = String(sampled);
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
client.emit('createDsc', dsc);
|
|
640
|
-
|
|
641
|
-
return dsc;
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
/**
|
|
645
|
-
*
|
|
646
|
-
* @param otelSpan Checks wheter a given OTEL Span is an http request to sentry.
|
|
647
|
-
* @returns boolean
|
|
648
|
-
*/
|
|
649
|
-
function isSentryRequestSpan(span) {
|
|
650
|
-
if (!spanHasAttributes(span)) {
|
|
651
|
-
return false;
|
|
792
|
+
|
|
793
|
+
// Else, we try to generate a new one
|
|
794
|
+
const client = core.getClient();
|
|
795
|
+
|
|
796
|
+
if (client) {
|
|
797
|
+
return core.getDynamicSamplingContextFromClient(traceId || propagationContext.traceId, client);
|
|
652
798
|
}
|
|
653
799
|
|
|
654
|
-
|
|
800
|
+
return undefined;
|
|
801
|
+
}
|
|
655
802
|
|
|
656
|
-
|
|
803
|
+
function getContextWithRemoteActiveSpan(
|
|
804
|
+
ctx,
|
|
805
|
+
{ sentryTrace, baggage },
|
|
806
|
+
) {
|
|
807
|
+
const propagationContext = utils.propagationContextFromHeaders(sentryTrace, baggage);
|
|
657
808
|
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
809
|
+
// We store the DSC as OTEL trace state on the span context
|
|
810
|
+
const traceState = makeTraceState({
|
|
811
|
+
parentSpanId: propagationContext.parentSpanId,
|
|
812
|
+
dsc: propagationContext.dsc,
|
|
813
|
+
sampled: propagationContext.sampled,
|
|
814
|
+
});
|
|
661
815
|
|
|
662
|
-
|
|
816
|
+
const spanContext = {
|
|
817
|
+
traceId: propagationContext.traceId,
|
|
818
|
+
spanId: propagationContext.parentSpanId || '',
|
|
819
|
+
isRemote: true,
|
|
820
|
+
traceFlags: propagationContext.sampled ? api.TraceFlags.SAMPLED : api.TraceFlags.NONE,
|
|
821
|
+
traceState,
|
|
822
|
+
};
|
|
823
|
+
|
|
824
|
+
return api.trace.setSpanContext(ctx, spanContext);
|
|
663
825
|
}
|
|
664
826
|
|
|
665
827
|
/**
|
|
666
|
-
*
|
|
828
|
+
* Takes trace strings and propagates them as a remote active span.
|
|
829
|
+
* This should be used in addition to `continueTrace` in OTEL-powered environments.
|
|
667
830
|
*/
|
|
668
|
-
function
|
|
669
|
-
|
|
831
|
+
function continueTraceAsRemoteSpan(
|
|
832
|
+
ctx,
|
|
833
|
+
options,
|
|
834
|
+
callback,
|
|
835
|
+
) {
|
|
836
|
+
const ctxWithSpanContext = getContextWithRemoteActiveSpan(ctx, options);
|
|
837
|
+
|
|
838
|
+
return api.context.with(ctxWithSpanContext, callback);
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/** Try to get the existing baggage header so we can merge this in. */
|
|
842
|
+
function getExistingBaggage(carrier) {
|
|
843
|
+
try {
|
|
844
|
+
const baggage = (carrier )[SENTRY_BAGGAGE_HEADER];
|
|
845
|
+
return Array.isArray(baggage) ? baggage.join(',') : baggage;
|
|
846
|
+
} catch (e) {
|
|
847
|
+
return undefined;
|
|
848
|
+
}
|
|
670
849
|
}
|
|
671
850
|
|
|
672
851
|
function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
@@ -831,13 +1010,12 @@ function getContext(scope, forceTransaction) {
|
|
|
831
1010
|
|
|
832
1011
|
if (actualScope && client) {
|
|
833
1012
|
const propagationContext = actualScope.getPropagationContext();
|
|
834
|
-
const dynamicSamplingContext =
|
|
835
|
-
propagationContext.dsc || core.getDynamicSamplingContextFromClient(propagationContext.traceId, client);
|
|
836
1013
|
|
|
837
1014
|
// We store the DSC as OTEL trace state on the span context
|
|
838
1015
|
const traceState = makeTraceState({
|
|
839
1016
|
parentSpanId: propagationContext.parentSpanId,
|
|
840
|
-
|
|
1017
|
+
// Not defined yet, we want to pick this up on-demand only
|
|
1018
|
+
dsc: undefined,
|
|
841
1019
|
sampled: propagationContext.sampled,
|
|
842
1020
|
});
|
|
843
1021
|
|
|
@@ -872,6 +1050,8 @@ function getContext(scope, forceTransaction) {
|
|
|
872
1050
|
const { spanId, traceId } = parentSpan.spanContext();
|
|
873
1051
|
const sampled = getSamplingDecision(parentSpan.spanContext());
|
|
874
1052
|
|
|
1053
|
+
// In this case, when we are forcing a transaction, we want to treat this like continuing an incoming trace
|
|
1054
|
+
// so we set the traceState according to the root span
|
|
875
1055
|
const rootSpan = core.getRootSpan(parentSpan);
|
|
876
1056
|
const dsc = getDynamicSamplingContextFromSpan(rootSpan);
|
|
877
1057
|
|
|
@@ -939,25 +1119,11 @@ function _optionalChain$4(ops) { let lastAccessLHS = undefined; let value = ops[
|
|
|
939
1119
|
*/
|
|
940
1120
|
function getCurrentHub() {
|
|
941
1121
|
return {
|
|
942
|
-
isOlderThan(_version) {
|
|
943
|
-
return false;
|
|
944
|
-
},
|
|
945
|
-
|
|
946
1122
|
bindClient(client) {
|
|
947
1123
|
const scope = core.getCurrentScope();
|
|
948
1124
|
scope.setClient(client);
|
|
949
1125
|
},
|
|
950
1126
|
|
|
951
|
-
pushScope() {
|
|
952
|
-
// TODO: This does not work and is actually deprecated
|
|
953
|
-
return core.getCurrentScope();
|
|
954
|
-
},
|
|
955
|
-
|
|
956
|
-
popScope() {
|
|
957
|
-
// TODO: This does not work and is actually deprecated
|
|
958
|
-
return false;
|
|
959
|
-
},
|
|
960
|
-
|
|
961
1127
|
withScope: core.withScope,
|
|
962
1128
|
getClient: () => core.getClient() ,
|
|
963
1129
|
getScope: core.getCurrentScope,
|
|
@@ -995,11 +1161,6 @@ function getCurrentHub() {
|
|
|
995
1161
|
// only send the update
|
|
996
1162
|
_sendSessionUpdate();
|
|
997
1163
|
},
|
|
998
|
-
|
|
999
|
-
shouldSendDefaultPii() {
|
|
1000
|
-
const client = core.getClient();
|
|
1001
|
-
return Boolean(client ? client.getOptions().sendDefaultPii : false);
|
|
1002
|
-
},
|
|
1003
1164
|
};
|
|
1004
1165
|
}
|
|
1005
1166
|
|
|
@@ -1371,152 +1532,6 @@ function mapStatus(span) {
|
|
|
1371
1532
|
}
|
|
1372
1533
|
}
|
|
1373
1534
|
|
|
1374
|
-
/**
|
|
1375
|
-
* Extract better op/description from an otel span.
|
|
1376
|
-
*
|
|
1377
|
-
* Based on https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/7422ce2a06337f68a59b552b8c5a2ac125d6bae5/exporter/sentryexporter/sentry_exporter.go#L306
|
|
1378
|
-
*/
|
|
1379
|
-
function parseSpanDescription(span) {
|
|
1380
|
-
const attributes = spanHasAttributes(span) ? span.attributes : {};
|
|
1381
|
-
const name = spanHasName(span) ? span.name : '<unknown>';
|
|
1382
|
-
|
|
1383
|
-
// if http.method exists, this is an http request span
|
|
1384
|
-
const httpMethod = attributes[semanticConventions.SemanticAttributes.HTTP_METHOD];
|
|
1385
|
-
if (httpMethod) {
|
|
1386
|
-
return descriptionForHttpMethod({ attributes, name, kind: getSpanKind(span) }, httpMethod);
|
|
1387
|
-
}
|
|
1388
|
-
|
|
1389
|
-
// If db.type exists then this is a database call span.
|
|
1390
|
-
const dbSystem = attributes[semanticConventions.SemanticAttributes.DB_SYSTEM];
|
|
1391
|
-
if (dbSystem) {
|
|
1392
|
-
return descriptionForDbSystem({ attributes, name });
|
|
1393
|
-
}
|
|
1394
|
-
|
|
1395
|
-
// If rpc.service exists then this is a rpc call span.
|
|
1396
|
-
const rpcService = attributes[semanticConventions.SemanticAttributes.RPC_SERVICE];
|
|
1397
|
-
if (rpcService) {
|
|
1398
|
-
return {
|
|
1399
|
-
op: 'rpc',
|
|
1400
|
-
description: name,
|
|
1401
|
-
source: 'route',
|
|
1402
|
-
};
|
|
1403
|
-
}
|
|
1404
|
-
|
|
1405
|
-
// If messaging.system exists then this is a messaging system span.
|
|
1406
|
-
const messagingSystem = attributes[semanticConventions.SemanticAttributes.MESSAGING_SYSTEM];
|
|
1407
|
-
if (messagingSystem) {
|
|
1408
|
-
return {
|
|
1409
|
-
op: 'message',
|
|
1410
|
-
description: name,
|
|
1411
|
-
source: 'route',
|
|
1412
|
-
};
|
|
1413
|
-
}
|
|
1414
|
-
|
|
1415
|
-
// If faas.trigger exists then this is a function as a service span.
|
|
1416
|
-
const faasTrigger = attributes[semanticConventions.SemanticAttributes.FAAS_TRIGGER];
|
|
1417
|
-
if (faasTrigger) {
|
|
1418
|
-
return { op: faasTrigger.toString(), description: name, source: 'route' };
|
|
1419
|
-
}
|
|
1420
|
-
|
|
1421
|
-
return { op: undefined, description: name, source: 'custom' };
|
|
1422
|
-
}
|
|
1423
|
-
|
|
1424
|
-
function descriptionForDbSystem({ attributes, name }) {
|
|
1425
|
-
// Use DB statement (Ex "SELECT * FROM table") if possible as description.
|
|
1426
|
-
const statement = attributes[semanticConventions.SemanticAttributes.DB_STATEMENT];
|
|
1427
|
-
|
|
1428
|
-
const description = statement ? statement.toString() : name;
|
|
1429
|
-
|
|
1430
|
-
return { op: 'db', description, source: 'task' };
|
|
1431
|
-
}
|
|
1432
|
-
|
|
1433
|
-
/** Only exported for tests. */
|
|
1434
|
-
function descriptionForHttpMethod(
|
|
1435
|
-
{ name, kind, attributes },
|
|
1436
|
-
httpMethod,
|
|
1437
|
-
) {
|
|
1438
|
-
const opParts = ['http'];
|
|
1439
|
-
|
|
1440
|
-
switch (kind) {
|
|
1441
|
-
case api.SpanKind.CLIENT:
|
|
1442
|
-
opParts.push('client');
|
|
1443
|
-
break;
|
|
1444
|
-
case api.SpanKind.SERVER:
|
|
1445
|
-
opParts.push('server');
|
|
1446
|
-
break;
|
|
1447
|
-
}
|
|
1448
|
-
|
|
1449
|
-
const { urlPath, url, query, fragment, hasRoute } = getSanitizedUrl(attributes, kind);
|
|
1450
|
-
|
|
1451
|
-
if (!urlPath) {
|
|
1452
|
-
return { op: opParts.join('.'), description: name, source: 'custom' };
|
|
1453
|
-
}
|
|
1454
|
-
|
|
1455
|
-
// Ex. description="GET /api/users".
|
|
1456
|
-
const description = `${httpMethod} ${urlPath}`;
|
|
1457
|
-
|
|
1458
|
-
// If `httpPath` is a root path, then we can categorize the transaction source as route.
|
|
1459
|
-
const source = hasRoute || urlPath === '/' ? 'route' : 'url';
|
|
1460
|
-
|
|
1461
|
-
const data = {};
|
|
1462
|
-
|
|
1463
|
-
if (url) {
|
|
1464
|
-
data.url = url;
|
|
1465
|
-
}
|
|
1466
|
-
if (query) {
|
|
1467
|
-
data['http.query'] = query;
|
|
1468
|
-
}
|
|
1469
|
-
if (fragment) {
|
|
1470
|
-
data['http.fragment'] = fragment;
|
|
1471
|
-
}
|
|
1472
|
-
|
|
1473
|
-
return {
|
|
1474
|
-
op: opParts.join('.'),
|
|
1475
|
-
description,
|
|
1476
|
-
source,
|
|
1477
|
-
data,
|
|
1478
|
-
};
|
|
1479
|
-
}
|
|
1480
|
-
|
|
1481
|
-
/** Exported for tests only */
|
|
1482
|
-
function getSanitizedUrl(
|
|
1483
|
-
attributes,
|
|
1484
|
-
kind,
|
|
1485
|
-
)
|
|
1486
|
-
|
|
1487
|
-
{
|
|
1488
|
-
// This is the relative path of the URL, e.g. /sub
|
|
1489
|
-
const httpTarget = attributes[semanticConventions.SemanticAttributes.HTTP_TARGET];
|
|
1490
|
-
// This is the full URL, including host & query params etc., e.g. https://example.com/sub?foo=bar
|
|
1491
|
-
const httpUrl = attributes[semanticConventions.SemanticAttributes.HTTP_URL];
|
|
1492
|
-
// This is the normalized route name - may not always be available!
|
|
1493
|
-
const httpRoute = attributes[semanticConventions.SemanticAttributes.HTTP_ROUTE];
|
|
1494
|
-
|
|
1495
|
-
const parsedUrl = typeof httpUrl === 'string' ? utils.parseUrl(httpUrl) : undefined;
|
|
1496
|
-
const url = parsedUrl ? utils.getSanitizedUrlString(parsedUrl) : undefined;
|
|
1497
|
-
const query = parsedUrl && parsedUrl.search ? parsedUrl.search : undefined;
|
|
1498
|
-
const fragment = parsedUrl && parsedUrl.hash ? parsedUrl.hash : undefined;
|
|
1499
|
-
|
|
1500
|
-
if (typeof httpRoute === 'string') {
|
|
1501
|
-
return { urlPath: httpRoute, url, query, fragment, hasRoute: true };
|
|
1502
|
-
}
|
|
1503
|
-
|
|
1504
|
-
if (kind === api.SpanKind.SERVER && typeof httpTarget === 'string') {
|
|
1505
|
-
return { urlPath: utils.stripUrlQueryAndFragment(httpTarget), url, query, fragment, hasRoute: false };
|
|
1506
|
-
}
|
|
1507
|
-
|
|
1508
|
-
if (parsedUrl) {
|
|
1509
|
-
return { urlPath: url, url, query, fragment, hasRoute: false };
|
|
1510
|
-
}
|
|
1511
|
-
|
|
1512
|
-
// fall back to target even for client spans, if no URL is present
|
|
1513
|
-
if (typeof httpTarget === 'string') {
|
|
1514
|
-
return { urlPath: utils.stripUrlQueryAndFragment(httpTarget), url, query, fragment, hasRoute: false };
|
|
1515
|
-
}
|
|
1516
|
-
|
|
1517
|
-
return { urlPath: undefined, url, query, fragment, hasRoute: false };
|
|
1518
|
-
}
|
|
1519
|
-
|
|
1520
1535
|
function _optionalChain$2(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
1521
1536
|
|
|
1522
1537
|
/**
|
|
@@ -1628,7 +1643,10 @@ function maybeSend(spans) {
|
|
|
1628
1643
|
|
|
1629
1644
|
transactionEvent.spans = spans;
|
|
1630
1645
|
|
|
1631
|
-
|
|
1646
|
+
const measurements = core.timedEventsToMeasurements(span.events);
|
|
1647
|
+
if (Object.keys(measurements).length) {
|
|
1648
|
+
transactionEvent.measurements = measurements;
|
|
1649
|
+
}
|
|
1632
1650
|
|
|
1633
1651
|
core.captureEvent(transactionEvent);
|
|
1634
1652
|
});
|
|
@@ -1869,13 +1887,17 @@ function onSpanStart(span, parentContext) {
|
|
|
1869
1887
|
setSpanScopes(span, scopes);
|
|
1870
1888
|
}
|
|
1871
1889
|
|
|
1890
|
+
core.logSpanStart(span);
|
|
1891
|
+
|
|
1872
1892
|
const client = core.getClient();
|
|
1873
1893
|
_optionalChain$1([client, 'optionalAccess', _ => _.emit, 'call', _2 => _2('spanStart', span)]);
|
|
1874
1894
|
}
|
|
1875
1895
|
|
|
1876
1896
|
function onSpanEnd(span) {
|
|
1897
|
+
core.logSpanEnd(span);
|
|
1898
|
+
|
|
1877
1899
|
const client = core.getClient();
|
|
1878
|
-
_optionalChain$1([client, 'optionalAccess', _3 => _3.emit, 'call', _4 => _4('spanEnd', span
|
|
1900
|
+
_optionalChain$1([client, 'optionalAccess', _3 => _3.emit, 'call', _4 => _4('spanEnd', span)]);
|
|
1879
1901
|
}
|
|
1880
1902
|
|
|
1881
1903
|
/**
|
|
@@ -1908,22 +1930,10 @@ class SentrySpanProcessor {
|
|
|
1908
1930
|
*/
|
|
1909
1931
|
onStart(span, parentContext) {
|
|
1910
1932
|
onSpanStart(span, parentContext);
|
|
1911
|
-
|
|
1912
|
-
// TODO (v8): Trigger client `spanStart` & `spanEnd` in here,
|
|
1913
|
-
// once we decoupled opentelemetry from SentrySpan
|
|
1914
|
-
|
|
1915
|
-
DEBUG_BUILD && utils.logger.log(`[Tracing] Starting span "${span.name}" (${span.spanContext().spanId})`);
|
|
1916
1933
|
}
|
|
1917
1934
|
|
|
1918
1935
|
/** @inheritDoc */
|
|
1919
1936
|
onEnd(span) {
|
|
1920
|
-
if (span.spanContext().traceFlags !== api.TraceFlags.SAMPLED) {
|
|
1921
|
-
DEBUG_BUILD && utils.logger.log(`[Tracing] Finishing unsampled span "${span.name}" (${span.spanContext().spanId})`);
|
|
1922
|
-
return;
|
|
1923
|
-
}
|
|
1924
|
-
|
|
1925
|
-
DEBUG_BUILD && utils.logger.log(`[Tracing] Finishing span "${span.name}" (${span.spanContext().spanId})`);
|
|
1926
|
-
|
|
1927
1937
|
onSpanEnd(span);
|
|
1928
1938
|
|
|
1929
1939
|
this._exporter.export(span);
|
|
@@ -1931,7 +1941,7 @@ class SentrySpanProcessor {
|
|
|
1931
1941
|
}
|
|
1932
1942
|
|
|
1933
1943
|
/**
|
|
1934
|
-
* A custom OTEL sampler that uses Sentry sampling rates to make
|
|
1944
|
+
* A custom OTEL sampler that uses Sentry sampling rates to make its decision
|
|
1935
1945
|
*/
|
|
1936
1946
|
class SentrySampler {
|
|
1937
1947
|
|
|
@@ -1955,16 +1965,17 @@ class SentrySampler {
|
|
|
1955
1965
|
return { decision: sdkTraceBase.SamplingDecision.NOT_RECORD };
|
|
1956
1966
|
}
|
|
1957
1967
|
|
|
1958
|
-
const
|
|
1959
|
-
const
|
|
1968
|
+
const parentSpan = api.trace.getSpan(context);
|
|
1969
|
+
const parentContext = _optionalChain([parentSpan, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2()]);
|
|
1970
|
+
const traceState = _optionalChain([parentContext, 'optionalAccess', _3 => _3.traceState]) || new core$1.TraceState();
|
|
1960
1971
|
|
|
1961
1972
|
let parentSampled = undefined;
|
|
1962
1973
|
|
|
1963
1974
|
// Only inherit sample rate if `traceId` is the same
|
|
1964
1975
|
// Note for testing: `isSpanContextValid()` checks the format of the traceId/spanId, so we need to pass valid ones
|
|
1965
|
-
if (parentContext && api.isSpanContextValid(parentContext) && parentContext.traceId === traceId) {
|
|
1976
|
+
if (parentSpan && parentContext && api.isSpanContextValid(parentContext) && parentContext.traceId === traceId) {
|
|
1966
1977
|
if (parentContext.isRemote) {
|
|
1967
|
-
parentSampled = getParentRemoteSampled(
|
|
1978
|
+
parentSampled = getParentRemoteSampled(parentSpan);
|
|
1968
1979
|
DEBUG_BUILD &&
|
|
1969
1980
|
utils.logger.log(`[Tracing] Inheriting remote parent's sampled decision for ${spanName}: ${parentSampled}`);
|
|
1970
1981
|
} else {
|
|
@@ -1973,7 +1984,7 @@ class SentrySampler {
|
|
|
1973
1984
|
}
|
|
1974
1985
|
}
|
|
1975
1986
|
|
|
1976
|
-
const sampleRate =
|
|
1987
|
+
const [sampled, sampleRate] = core.sampleSpan(options, {
|
|
1977
1988
|
name: spanName,
|
|
1978
1989
|
attributes: spanAttributes,
|
|
1979
1990
|
transactionContext: {
|
|
@@ -1984,32 +1995,12 @@ class SentrySampler {
|
|
|
1984
1995
|
});
|
|
1985
1996
|
|
|
1986
1997
|
const attributes = {
|
|
1987
|
-
[core.SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]:
|
|
1998
|
+
[core.SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]: sampleRate,
|
|
1988
1999
|
};
|
|
1989
2000
|
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
DEBUG_BUILD && utils.logger.warn('[Tracing] Discarding span because of invalid sample rate.');
|
|
1994
|
-
|
|
1995
|
-
return {
|
|
1996
|
-
decision: sdkTraceBase.SamplingDecision.NOT_RECORD,
|
|
1997
|
-
attributes,
|
|
1998
|
-
traceState: traceState.set(SENTRY_TRACE_STATE_SAMPLED_NOT_RECORDING, '1'),
|
|
1999
|
-
};
|
|
2000
|
-
}
|
|
2001
|
-
|
|
2002
|
-
// if the function returned 0 (or false), or if `tracesSampleRate` is 0, it's a sign the transaction should be dropped
|
|
2003
|
-
if (!sampleRate) {
|
|
2004
|
-
DEBUG_BUILD &&
|
|
2005
|
-
utils.logger.log(
|
|
2006
|
-
`[Tracing] Discarding span because ${
|
|
2007
|
-
typeof options.tracesSampler === 'function'
|
|
2008
|
-
? 'tracesSampler returned 0 or false'
|
|
2009
|
-
: 'a negative sampling decision was inherited or tracesSampleRate is set to 0'
|
|
2010
|
-
}`,
|
|
2011
|
-
);
|
|
2012
|
-
|
|
2001
|
+
const method = `${spanAttributes[semanticConventions.SemanticAttributes.HTTP_METHOD]}`.toUpperCase();
|
|
2002
|
+
if (method === 'OPTIONS' || method === 'HEAD') {
|
|
2003
|
+
DEBUG_BUILD && utils.logger.log(`[Tracing] Not sampling span because HTTP method is '${method}' for ${spanName}`);
|
|
2013
2004
|
return {
|
|
2014
2005
|
decision: sdkTraceBase.SamplingDecision.NOT_RECORD,
|
|
2015
2006
|
attributes,
|
|
@@ -2017,19 +2008,7 @@ class SentrySampler {
|
|
|
2017
2008
|
};
|
|
2018
2009
|
}
|
|
2019
2010
|
|
|
2020
|
-
|
|
2021
|
-
// a boolean, the < comparison will cause it to be automatically cast to 1 if it's true and 0 if it's false.
|
|
2022
|
-
const isSampled = Math.random() < (sampleRate );
|
|
2023
|
-
|
|
2024
|
-
// if we're not going to keep it, we're done
|
|
2025
|
-
if (!isSampled) {
|
|
2026
|
-
DEBUG_BUILD &&
|
|
2027
|
-
utils.logger.log(
|
|
2028
|
-
`[Tracing] Discarding span because it's not included in the random sample (sampling rate = ${Number(
|
|
2029
|
-
sampleRate,
|
|
2030
|
-
)})`,
|
|
2031
|
-
);
|
|
2032
|
-
|
|
2011
|
+
if (!sampled) {
|
|
2033
2012
|
return {
|
|
2034
2013
|
decision: sdkTraceBase.SamplingDecision.NOT_RECORD,
|
|
2035
2014
|
attributes,
|
|
@@ -2049,57 +2028,9 @@ class SentrySampler {
|
|
|
2049
2028
|
}
|
|
2050
2029
|
}
|
|
2051
2030
|
|
|
2052
|
-
function
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
) {
|
|
2056
|
-
if (typeof options.tracesSampler === 'function') {
|
|
2057
|
-
return options.tracesSampler(samplingContext);
|
|
2058
|
-
}
|
|
2059
|
-
|
|
2060
|
-
if (samplingContext.parentSampled !== undefined) {
|
|
2061
|
-
return samplingContext.parentSampled;
|
|
2062
|
-
}
|
|
2063
|
-
|
|
2064
|
-
if (typeof options.tracesSampleRate !== 'undefined') {
|
|
2065
|
-
return options.tracesSampleRate;
|
|
2066
|
-
}
|
|
2067
|
-
|
|
2068
|
-
// When `enableTracing === true`, we use a sample rate of 100%
|
|
2069
|
-
if (options.enableTracing) {
|
|
2070
|
-
return 1;
|
|
2071
|
-
}
|
|
2072
|
-
|
|
2073
|
-
return 0;
|
|
2074
|
-
}
|
|
2075
|
-
|
|
2076
|
-
/**
|
|
2077
|
-
* Checks the given sample rate to make sure it is valid type and value (a boolean, or a number between 0 and 1).
|
|
2078
|
-
*/
|
|
2079
|
-
function isValidSampleRate(rate) {
|
|
2080
|
-
// we need to check NaN explicitly because it's of type 'number' and therefore wouldn't get caught by this typecheck
|
|
2081
|
-
if (utils.isNaN(rate) || !(typeof rate === 'number' || typeof rate === 'boolean')) {
|
|
2082
|
-
DEBUG_BUILD &&
|
|
2083
|
-
utils.logger.warn(
|
|
2084
|
-
`[Tracing] Given sample rate is invalid. Sample rate must be a boolean or a number between 0 and 1. Got ${JSON.stringify(
|
|
2085
|
-
rate,
|
|
2086
|
-
)} of type ${JSON.stringify(typeof rate)}.`,
|
|
2087
|
-
);
|
|
2088
|
-
return false;
|
|
2089
|
-
}
|
|
2090
|
-
|
|
2091
|
-
// in case sampleRate is a boolean, it will get automatically cast to 1 if it's true and 0 if it's false
|
|
2092
|
-
if (rate < 0 || rate > 1) {
|
|
2093
|
-
DEBUG_BUILD &&
|
|
2094
|
-
utils.logger.warn(`[Tracing] Given sample rate is invalid. Sample rate must be between 0 and 1. Got ${rate}.`);
|
|
2095
|
-
return false;
|
|
2096
|
-
}
|
|
2097
|
-
return true;
|
|
2098
|
-
}
|
|
2099
|
-
|
|
2100
|
-
function getParentRemoteSampled(spanContext) {
|
|
2101
|
-
const traceId = spanContext.traceId;
|
|
2102
|
-
const traceparentData = getPropagationContextFromSpanContext(spanContext);
|
|
2031
|
+
function getParentRemoteSampled(parentSpan) {
|
|
2032
|
+
const traceId = parentSpan.spanContext().traceId;
|
|
2033
|
+
const traceparentData = getPropagationContextFromSpan(parentSpan);
|
|
2103
2034
|
|
|
2104
2035
|
// Only inherit sampled if `traceId` is the same
|
|
2105
2036
|
return traceparentData && traceId === traceparentData.traceId ? traceparentData.sampled : undefined;
|