Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Tracing without performance #710

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 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';
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 @@ -102,6 +109,21 @@ function parseOptions(optionsIn: ClientRequestConstructorOptions | string): { me
};
}

function addHeadersToRequest(
request: Electron.ClientRequest,
url: string,
sentryTraceHeader: string,
dynamicSamplingContext?: Partial<DynamicSamplingContext>,
): 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;
Expand Down Expand Up @@ -200,7 +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);

const client = hub.getClient();
const dynamicSamplingContext =
dsc || (client ? getDynamicSamplingContextFromClient(traceId, client, scope) : undefined);

addHeadersToRequest(request, url, sentryTraceHeader, dynamicSamplingContext);
}
}
}
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;
timfish marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
Loading