Skip to content

chore(tracing): Various small fixes to first tracestate implementation #3291

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

Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 0 additions & 7 deletions packages/hub/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,13 +351,6 @@ export class Hub implements HubInterface {
return this._callExtensionMethod('startTransaction', context, customSamplingContext);
}

/**
* @inheritDoc
*/
public traceHeaders(): { [key: string]: string } {
return this._callExtensionMethod<{ [key: string]: string }>('traceHeaders');
}

/**
* @inheritDoc
*/
Expand Down
11 changes: 7 additions & 4 deletions packages/node/test/integrations/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { NodeClient } from '../../src/client';
import { Http as HttpIntegration } from '../../src/integrations/http';

describe('tracing', () => {
function createTransactionOnScope() {
function createTransactionOnScope(): Transaction {
const hub = new Hub(
new NodeClient({
dsn: 'https://[email protected]/12312012',
Expand All @@ -24,7 +24,10 @@ describe('tracing', () => {
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub);

const transaction = hub.startTransaction({ name: 'dogpark' });
// we have to cast this to a Transaction (the class) because hub.startTransaction only returns a Transaction (the
// interface, which doesn't have things like spanRecorder) (and because @sentry/hub can't depend on @sentry/tracing,
// we can't fix that)
const transaction = hub.startTransaction({ name: 'dogpark' }) as Transaction;
hub.getScope()?.setSpan(transaction);

return transaction;
Expand All @@ -36,7 +39,7 @@ describe('tracing', () => {
.reply(200);

const transaction = createTransactionOnScope();
const spans = (transaction as Span).spanRecorder?.spans as Span[];
const spans = transaction.spanRecorder?.spans as Span[];

http.get('http://dogs.are.great/');

Expand All @@ -55,7 +58,7 @@ describe('tracing', () => {
.reply(200);

const transaction = createTransactionOnScope();
const spans = (transaction as Span).spanRecorder?.spans as Span[];
const spans = transaction.spanRecorder?.spans as Span[];

http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/');

Expand Down
17 changes: 0 additions & 17 deletions packages/tracing/src/hubextensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ import { IdleTransaction } from './idletransaction';
import { Transaction } from './transaction';
import { hasTracingEnabled } from './utils';

/** Returns all trace headers that are currently on the top scope. */
function traceHeaders(this: Hub): { [key: string]: string } {
const scope = this.getScope();
if (scope) {
const span = scope.getSpan();
if (span) {
return {
'sentry-trace': span.toTraceparent(),
};
}
}
return {};
}

/**
* Makes a sampling decision for the given transaction and stores it on the transaction.
*
Expand Down Expand Up @@ -212,9 +198,6 @@ export function _addTracingExtensions(): void {
if (!carrier.__SENTRY__.extensions.startTransaction) {
carrier.__SENTRY__.extensions.startTransaction = _startTransaction;
}
if (!carrier.__SENTRY__.extensions.traceHeaders) {
carrier.__SENTRY__.extensions.traceHeaders = traceHeaders;
}
}
}

Expand Down
9 changes: 3 additions & 6 deletions packages/tracing/src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TransactionContext,
TransactionMetadata,
} from '@sentry/types';
import { dropUndefinedKeys, isInstanceOf, logger } from '@sentry/utils';
import { dropUndefinedKeys, logger } from '@sentry/utils';

import { Span as SpanClass, SpanRecorder } from './span';

Expand All @@ -21,7 +21,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
/**
* The reference to the current hub.
*/
private readonly _hub: Hub = (getCurrentHub() as unknown) as Hub;
private readonly _hub: Hub;

private _trimEnd?: boolean;

Expand All @@ -35,13 +35,10 @@ export class Transaction extends SpanClass implements TransactionInterface {
public constructor(transactionContext: TransactionContext, hub?: Hub) {
super(transactionContext);

if (isInstanceOf(hub, Hub)) {
this._hub = hub as Hub;
}

this.name = transactionContext.name || '';
this.metadata = transactionContext.metadata || {};
this._trimEnd = transactionContext.trimEnd;
this._hub = hub || getCurrentHub();

// create a new sentry tracestate value if we didn't inherit one
if (!this.metadata.tracestate?.sentry) {
Expand Down
7 changes: 3 additions & 4 deletions packages/tracing/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,13 @@ export function extractSentrytraceData(header: string): TraceparentData | undefi
return undefined;
}

type TracestateHeaderData = { sentry?: string; thirdparty?: string };

/**
* Extract data from an incoming `tracestate` header
*
* @param header
* @returns Object containing data from the header
*/
export function extractTracestateData(header: string): TracestateHeaderData {
export function extractTracestateData(header: string): { sentry?: string; thirdparty?: string } {
let sentryEntry, thirdPartyEntry, before, after;

// find sentry's entry, if any
Expand Down Expand Up @@ -160,7 +158,8 @@ type SentryTracestateData = {
* @returns the base64-encoded header value
*/
export function computeTracestateValue(data: SentryTracestateData): string {
// `JSON.stringify` will drop keys with undefined values, but not ones with null values
// `JSON.stringify` will drop keys with undefined values, but not ones with null values, so this prevents
// `environment` and `release` from being dropped if they haven't been set by `Sentry.init`
data.environment = data.environment || null;
data.release = data.release || null;

Expand Down
3 changes: 0 additions & 3 deletions packages/types/src/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,6 @@ export interface Hub {
/** Returns the integration if installed on the current client. */
getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null;

/** Returns all trace headers that are currently on the top scope. */
traceHeaders(): { [key: string]: string };

/**
* @deprecated No longer does anything. Use use {@link Transaction.startChild} instead.
*/
Expand Down