From 6a99eb190e976879a7af43c462057e118a4009d1 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Sat, 29 Jul 2023 15:46:24 +0200 Subject: [PATCH 1/3] feat: Tracing without performance --- src/main/integrations/net-breadcrumbs.ts | 27 ++++++++++++++++++++- test/unit/net.test.ts | 30 ++++++++++++++++++++---- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/main/integrations/net-breadcrumbs.ts b/src/main/integrations/net-breadcrumbs.ts index 08ba85bc..8d3ab013 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 { + 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 d921cb8f..b4e522ca 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; + }); + }); }); From d36e0fd7af15618fffaff70506b31e91adeb06fc Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 2 Aug 2023 10:44:10 +0200 Subject: [PATCH 2/3] add baggage --- src/main/integrations/net-breadcrumbs.ts | 31 ++++++++++++++++-------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/main/integrations/net-breadcrumbs.ts b/src/main/integrations/net-breadcrumbs.ts index 8d3ab013..898ea44f 100644 --- a/src/main/integrations/net-breadcrumbs.ts +++ b/src/main/integrations/net-breadcrumbs.ts @@ -1,7 +1,7 @@ /* eslint-disable deprecation/deprecation */ import { getDynamicSamplingContextFromClient } from '@sentry/core'; import { getCurrentHub } from '@sentry/node'; -import { EventProcessor, Hub, Integration, Span, TracePropagationTargets } from '@sentry/types'; +import { DynamicSamplingContext, EventProcessor, Hub, Integration, Span, TracePropagationTargets } from '@sentry/types'; import { dynamicSamplingContextToSentryBaggageHeader, fill, @@ -109,6 +109,21 @@ function parseOptions(optionsIn: ClientRequestConstructorOptions | string): { me }; } +function addHeadersToRequest( + request: Electron.ClientRequest, + url: string, + sentryTraceHeader: string, + dynamicSamplingContext?: Partial, +): void { + logger.log(`[Tracing] Adding sentry-trace header ${sentryTraceHeader} to outgoing request to "${url}": `); + request.setHeader('sentry-trace', sentryTraceHeader); + + const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext); + if (sentryBaggageHeader) { + request.setHeader('baggage', sentryBaggageHeader); + } +} + type RequestOptions = string | ClientRequestConstructorOptions; type RequestMethod = (opt: RequestOptions) => ClientRequest; type WrappedRequestMethodFactory = (original: RequestMethod) => RequestMethod; @@ -207,25 +222,21 @@ function createWrappedRequestFactory( }); if (shouldAttachTraceData(method, url)) { - request.setHeader('sentry-trace', span.toTraceparent()); + const sentryTraceHeader = span.toTraceparent(); + const dynamicSamplingContext = span?.transaction?.getDynamicSamplingContext(); + + addHeadersToRequest(request, url, sentryTraceHeader, dynamicSamplingContext); } } 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); - } + addHeadersToRequest(request, url, sentryTraceHeader, dynamicSamplingContext); } } } From 9a59c1edfaac24d32f5365eb7596558d80020d11 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Wed, 2 Aug 2023 14:15:24 +0200 Subject: [PATCH 3/3] Also check baggage header in test Co-authored-by: Abhijeet Prasad --- test/unit/net.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unit/net.test.ts b/test/unit/net.test.ts index b4e522ca..d4d98544 100644 --- a/test/unit/net.test.ts +++ b/test/unit/net.test.ts @@ -235,6 +235,7 @@ describe.skip('net integration', () => { expect(transaction).to.be.undefined; expect(headers['sentry-trace']).not.to.be.empty; + expect(headers['baggage']).not.to.be.empty; }); }); });