Skip to content

Commit

Permalink
feat: Tracing without performance
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Jul 29, 2023
1 parent ecd978a commit e4a4e08
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
29 changes: 27 additions & 2 deletions src/main/integrations/net-breadcrumbs.ts
Original file line number Diff line number Diff line change
@@ -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';

Check failure on line 4 in src/main/integrations/net-breadcrumbs.ts

View workflow job for this annotation

GitHub Actions / Lint

'DynamicSamplingContext' is defined but never used
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';
Expand Down Expand Up @@ -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);
}
}
}
}

Expand Down
30 changes: 25 additions & 5 deletions test/unit/net.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -53,10 +53,7 @@ function mockAsyncContextStrategy(getHub: () => Hub): void {
setAsyncContextStrategy({ getCurrentHub, runWithAsyncContext });
}

function createTransactionOnScope(
customOptions: Partial<ClientOptions> = {},
customContext?: Partial<TransactionContext>,
): [Transaction, Hub] {
function createHubOnScope(customOptions: Partial<ClientOptions> = {}): Hub {
const hub = new Hub();
mockAsyncContextStrategy(() => hub);

Expand All @@ -78,6 +75,15 @@ function createTransactionOnScope(
}),
);

return hub;
}

function createTransactionOnScope(
customOptions: Partial<ClientOptions> = {},
customContext?: Partial<TransactionContext>,
): [Transaction, Hub] {
const hub = createHubOnScope(customOptions);

const transaction = hub.startTransaction({
name: 'dogpark',
traceId: '12312012123120121231201212312012',
Expand Down Expand Up @@ -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;
});
});
});

0 comments on commit e4a4e08

Please sign in to comment.