@sentry/opentelemetry 8.24.0 → 8.26.0

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 CHANGED
@@ -12,8 +12,8 @@
12
12
 
13
13
  This package allows you to send your OpenTelemetry trace data to Sentry via OpenTelemetry SpanProcessors.
14
14
 
15
- This SDK is **considered experimental and in an alpha state**. It may experience breaking changes. Please reach out on
16
- [GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose) if you have any feedback/concerns.
15
+ If you are using `@sentry/node`, OpenTelemetry support is included out of the box. This package is only necessary if you
16
+ are setting up OpenTelemetry support for Sentry yourself.
17
17
 
18
18
  ## Installation
19
19
 
@@ -12,6 +12,12 @@ const core$1 = require('@opentelemetry/core');
12
12
  const sdkTraceBase = require('@opentelemetry/sdk-trace-base');
13
13
  const instrumentation = require('@opentelemetry/instrumentation');
14
14
 
15
+ /** If this attribute is true, it means that the parent is a remote span. */
16
+ const SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE = 'sentry.parentIsRemote';
17
+
18
+ // These are not standardized yet, but used by the graphql instrumentation
19
+ const SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION = 'sentry.graphql.operation';
20
+
15
21
  /**
16
22
  * Check if a given span has attributes.
17
23
  * This is necessary because the base `Span` type does not have attributes,
@@ -31,7 +37,7 @@ function spanHasAttributes(
31
37
  */
32
38
  function spanHasKind(span) {
33
39
  const castSpan = span ;
34
- return !!castSpan.kind;
40
+ return typeof castSpan.kind === 'number';
35
41
  }
36
42
 
37
43
  /**
@@ -370,8 +376,16 @@ function descriptionForHttpMethod(
370
376
  return { op: opParts.join('.'), description: name, source: 'custom' };
371
377
  }
372
378
 
373
- // Ex. description="GET /api/users".
374
- const description = `${httpMethod} ${urlPath}`;
379
+ const graphqlOperationsAttribute = attributes[SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION];
380
+
381
+ // Ex. GET /api/users
382
+ const baseDescription = `${httpMethod} ${urlPath}`;
383
+
384
+ // When the http span has a graphql operation, append it to the description
385
+ // We add these in the graphqlIntegration
386
+ const description = graphqlOperationsAttribute
387
+ ? `${baseDescription} (${getGraphqlOperationNamesFromAttribute(graphqlOperationsAttribute)})`
388
+ : baseDescription;
375
389
 
376
390
  // If `httpPath` is a root path, then we can categorize the transaction source as route.
377
391
  const source = hasRoute || urlPath === '/' ? 'route' : 'url';
@@ -388,14 +402,42 @@ function descriptionForHttpMethod(
388
402
  data['http.fragment'] = fragment;
389
403
  }
390
404
 
405
+ // If the span kind is neither client nor server, we use the original name
406
+ // this infers that somebody manually started this span, in which case we don't want to overwrite the name
407
+ const isClientOrServerKind = kind === api.SpanKind.CLIENT || kind === api.SpanKind.SERVER;
408
+
409
+ // If the span is an auto-span (=it comes from one of our instrumentations),
410
+ // we always want to infer the name
411
+ // this is necessary because some of the auto-instrumentation we use uses kind=INTERNAL
412
+ const origin = attributes[core.SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN] || 'manual';
413
+ const isManualSpan = !`${origin}`.startsWith('auto');
414
+
415
+ const useInferredDescription = isClientOrServerKind || !isManualSpan;
416
+
391
417
  return {
392
418
  op: opParts.join('.'),
393
- description,
394
- source,
419
+ description: useInferredDescription ? description : name,
420
+ source: useInferredDescription ? source : 'custom',
395
421
  data,
396
422
  };
397
423
  }
398
424
 
425
+ function getGraphqlOperationNamesFromAttribute(attr) {
426
+ if (Array.isArray(attr)) {
427
+ const sorted = attr.slice().sort();
428
+
429
+ // Up to 5 items, we just add all of them
430
+ if (sorted.length <= 5) {
431
+ return sorted.join(', ');
432
+ } else {
433
+ // Else, we add the first 5 and the diff of other operations
434
+ return `${sorted.slice(0, 5).join(', ')}, +${sorted.length - 5}`;
435
+ }
436
+ }
437
+
438
+ return `${attr}`;
439
+ }
440
+
399
441
  /** Exported for tests only */
400
442
  function getSanitizedUrl(
401
443
  attributes,
@@ -1156,6 +1198,25 @@ function setupEventContextTrace(client) {
1156
1198
  });
1157
1199
  }
1158
1200
 
1201
+ /**
1202
+ * Otel-specific implementation of `getTraceData`.
1203
+ * @see `@sentry/core` version of `getTraceData` for more information
1204
+ */
1205
+ function getTraceData() {
1206
+ const headersObject = {};
1207
+
1208
+ api.propagation.inject(api.context.active(), headersObject);
1209
+
1210
+ if (!headersObject['sentry-trace']) {
1211
+ return {};
1212
+ }
1213
+
1214
+ return utils.dropUndefinedKeys({
1215
+ 'sentry-trace': headersObject['sentry-trace'],
1216
+ baggage: headersObject.baggage,
1217
+ });
1218
+ }
1219
+
1159
1220
  /**
1160
1221
  * Sets the async context strategy to use follow the OTEL context under the hood.
1161
1222
  * We handle forking a hub inside of our custom OTEL Context Manager (./otelContextManager.ts)
@@ -1244,10 +1305,11 @@ function setOpenTelemetryContextAsyncContextStrategy() {
1244
1305
  startSpanManual,
1245
1306
  startInactiveSpan,
1246
1307
  getActiveSpan,
1308
+ suppressTracing,
1309
+ getTraceData,
1247
1310
  // The types here don't fully align, because our own `Span` type is narrower
1248
1311
  // than the OTEL one - but this is OK for here, as we now we'll only have OTEL spans passed around
1249
1312
  withActiveSpan: withActiveSpan ,
1250
- suppressTracing: suppressTracing,
1251
1313
  });
1252
1314
  }
1253
1315
 
@@ -1318,9 +1380,6 @@ function wrapContextManagerClass(
1318
1380
  return SentryContextManager ;
1319
1381
  }
1320
1382
 
1321
- /** If this attribute is true, it means that the parent is a remote span. */
1322
- const SEMANTIC_ATTRIBUTE_SENTRY_PARENT_IS_REMOTE = 'sentry.parentIsRemote';
1323
-
1324
1383
  /**
1325
1384
  * This function runs through a list of OTEL Spans, and wraps them in an `SpanNode`
1326
1385
  * where each node holds a reference to their parent node.
@@ -2106,6 +2165,7 @@ function addOpenTelemetryInstrumentation(...instrumentations) {
2106
2165
  exports.getClient = core.getClient;
2107
2166
  exports.getCurrentHubShim = core.getCurrentHubShim;
2108
2167
  exports.getDynamicSamplingContextFromSpan = core.getDynamicSamplingContextFromSpan;
2168
+ exports.SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION = SEMANTIC_ATTRIBUTE_SENTRY_GRAPHQL_OPERATION;
2109
2169
  exports.SentryPropagator = SentryPropagator;
2110
2170
  exports.SentrySampler = SentrySampler;
2111
2171
  exports.SentrySpanProcessor = SentrySpanProcessor;