diff --git a/src/main/integrations/net-breadcrumbs.ts b/src/main/integrations/net-breadcrumbs.ts index 08ba85bc8..af65ddd17 100644 --- a/src/main/integrations/net-breadcrumbs.ts +++ b/src/main/integrations/net-breadcrumbs.ts @@ -1,7 +1,14 @@ /* eslint-disable deprecation/deprecation */ +import { getDynamicSamplingContextFromClient } from '@sentry/core'; import { getCurrentHub } from '@sentry/node'; -import { EventProcessor, Hub, Integration, Span, TracePropagationTargets } from '@sentry/types'; -import { fill, stringMatchesSomePattern } from '@sentry/utils'; +import { DynamicSamplingContext, EventProcessor, Hub, Integration, Span, TracePropagationTargets } from '@sentry/types'; +import { + dynamicSamplingContextToSentryBaggageHeader, + fill, + generateSentryTraceHeader, + logger, + stringMatchesSomePattern, +} from '@sentry/utils'; import { ClientRequest, ClientRequestConstructorOptions, IncomingMessage, net } from 'electron'; import { LRUMap } from 'lru_map'; import * as urlModule from 'url'; @@ -202,6 +209,24 @@ function createWrappedRequestFactory( if (shouldAttachTraceData(method, url)) { request.setHeader('sentry-trace', span.toTraceparent()); } + } else { + if (shouldAttachTraceData(method, url)) { + const { traceId, sampled, dsc } = scope.getPropagationContext(); + const sentryTraceHeader = generateSentryTraceHeader(traceId, undefined, sampled); + + logger.log(`[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${url}": `); + + const client = hub.getClient(); + const dynamicSamplingContext = + dsc || (client ? getDynamicSamplingContextFromClient(traceId, client, scope) : undefined); + + request.setHeader('sentry-trace', sentryTraceHeader); + + const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext); + if (sentryBaggageHeader) { + request.setHeader('baggage', sentryBaggageHeader); + } + } } } diff --git a/test/unit/net.test.ts b/test/unit/net.test.ts index d921cb8ff..b4e522ca4 100644 --- a/test/unit/net.test.ts +++ b/test/unit/net.test.ts @@ -1,7 +1,7 @@ import { expect, should, use } from 'chai'; import * as http from 'http'; import chaiAsPromised = require('chai-as-promised'); -import { setAsyncContextStrategy, Span } from '@sentry/core'; +import { getActiveTransaction, setAsyncContextStrategy, Span } from '@sentry/core'; import { createTransport, Hub, NodeClient } from '@sentry/node'; import { ClientOptions, Transaction, TransactionContext } from '@sentry/types'; import { resolvedSyncPromise } from '@sentry/utils'; @@ -53,10 +53,7 @@ function mockAsyncContextStrategy(getHub: () => Hub): void { setAsyncContextStrategy({ getCurrentHub, runWithAsyncContext }); } -function createTransactionOnScope( - customOptions: Partial = {}, - customContext?: Partial, -): [Transaction, Hub] { +function createHubOnScope(customOptions: Partial = {}): Hub { const hub = new Hub(); mockAsyncContextStrategy(() => hub); @@ -78,6 +75,15 @@ function createTransactionOnScope( }), ); + return hub; +} + +function createTransactionOnScope( + customOptions: Partial = {}, + customContext?: Partial, +): [Transaction, Hub] { + const hub = createHubOnScope(customOptions); + const transaction = hub.startTransaction({ name: 'dogpark', traceId: '12312012123120121231201212312012', @@ -217,4 +223,18 @@ describe.skip('net integration', () => { expect(headers['sentry-trace']).to.be.undefined; }); }); + + describe('tracing without performance', () => { + it('adds headers without transaction', async () => { + createHubOnScope({ + tracePropagationTargets: ['localhost'], + integrations: [new Net()], + }); + const headers = await makeRequest(); + const transaction = getActiveTransaction(); + + expect(transaction).to.be.undefined; + expect(headers['sentry-trace']).not.to.be.empty; + }); + }); });