|
| 1 | +<% if (options.tracing) { %> |
| 2 | +import { dynamicSamplingContextToSentryBaggageHeader } from '~@sentry/utils' |
| 3 | +<% } %> |
| 4 | + |
1 | 5 | /** @type {import('@nuxt/types').Module} */
|
2 | 6 | export default function (ctx, inject) {
|
3 |
| - const sentry = process.sentry || {} |
| 7 | + const sentry = process.sentry || null |
| 8 | + if (!sentry) { |
| 9 | + return |
| 10 | + } |
4 | 11 | inject('sentry', sentry)
|
5 | 12 | ctx.$sentry = sentry
|
6 |
| - |
| 13 | + <% if (options.tracing) { %> |
| 14 | + connectBackendTraces(ctx) |
| 15 | + <% } %> |
7 | 16 | <% if (options.lazy) { %>
|
8 | 17 | const sentryReady = () => Promise.resolve(sentry)
|
9 | 18 | inject('sentryReady', sentryReady)
|
10 | 19 | ctx.$sentryReady = sentryReady
|
11 | 20 | <% } %>
|
12 | 21 | }
|
| 22 | + |
| 23 | +<% if (options.tracing) { %> |
| 24 | +function connectBackendTraces (ctx) { |
| 25 | + const { head } = ctx.app |
| 26 | + if (!head || head instanceof Function) { |
| 27 | + console.warn('[@nuxtjs/sentry] can not connect backend and frontend traces because app.head is a function or missing!') |
| 28 | + return |
| 29 | + } |
| 30 | + const scope = ctx.$sentry.getCurrentHub().getScope() |
| 31 | + if (!scope) { |
| 32 | + return |
| 33 | + } |
| 34 | + const span = scope.getSpan() |
| 35 | + const transaction = scope.getTransaction() |
| 36 | + if (!span || !transaction) { |
| 37 | + return |
| 38 | + } |
| 39 | + head.meta = head.meta || [] |
| 40 | + head.meta.push({ hid: 'sentry-trace', name: 'sentry-trace', content: span.toTraceparent() }) |
| 41 | + const dsc = transaction.getDynamicSamplingContext() |
| 42 | + if (dsc) { |
| 43 | + head.meta.push({ hid: 'sentry-baggage', name: 'baggage', content: dynamicSamplingContextToSentryBaggageHeader(dsc) }) |
| 44 | + } |
| 45 | +} |
| 46 | +<% } %> |
0 commit comments