Skip to content

Commit 0073868

Browse files
committed
chore(tracing): Various small fixes to first tracestate implementation (#3291)
1 parent 9560b13 commit 0073868

File tree

6 files changed

+13
-41
lines changed

6 files changed

+13
-41
lines changed

packages/hub/src/hub.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,6 @@ export class Hub implements HubInterface {
351351
return this._callExtensionMethod('startTransaction', context, customSamplingContext);
352352
}
353353

354-
/**
355-
* @inheritDoc
356-
*/
357-
public traceHeaders(): { [key: string]: string } {
358-
return this._callExtensionMethod<{ [key: string]: string }>('traceHeaders');
359-
}
360-
361354
/**
362355
* @inheritDoc
363356
*/

packages/node/test/integrations/http.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { NodeClient } from '../../src/client';
99
import { Http as HttpIntegration } from '../../src/integrations/http';
1010

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

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

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

3841
const transaction = createTransactionOnScope();
39-
const spans = (transaction as Span).spanRecorder?.spans as Span[];
42+
const spans = transaction.spanRecorder?.spans as Span[];
4043

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

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

5760
const transaction = createTransactionOnScope();
58-
const spans = (transaction as Span).spanRecorder?.spans as Span[];
61+
const spans = transaction.spanRecorder?.spans as Span[];
5962

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

packages/tracing/src/hubextensions.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,6 @@ import { IdleTransaction } from './idletransaction';
1313
import { Transaction } from './transaction';
1414
import { hasTracingEnabled } from './utils';
1515

16-
/** Returns all trace headers that are currently on the top scope. */
17-
function traceHeaders(this: Hub): { [key: string]: string } {
18-
const scope = this.getScope();
19-
if (scope) {
20-
const span = scope.getSpan();
21-
if (span) {
22-
return {
23-
'sentry-trace': span.toTraceparent(),
24-
};
25-
}
26-
}
27-
return {};
28-
}
29-
3016
/**
3117
* Makes a sampling decision for the given transaction and stores it on the transaction.
3218
*
@@ -212,9 +198,6 @@ export function _addTracingExtensions(): void {
212198
if (!carrier.__SENTRY__.extensions.startTransaction) {
213199
carrier.__SENTRY__.extensions.startTransaction = _startTransaction;
214200
}
215-
if (!carrier.__SENTRY__.extensions.traceHeaders) {
216-
carrier.__SENTRY__.extensions.traceHeaders = traceHeaders;
217-
}
218201
}
219202
}
220203

packages/tracing/src/transaction.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
TransactionContext,
77
TransactionMetadata,
88
} from '@sentry/types';
9-
import { dropUndefinedKeys, isInstanceOf, logger } from '@sentry/utils';
9+
import { dropUndefinedKeys, logger } from '@sentry/utils';
1010

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

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

2626
private _trimEnd?: boolean;
2727

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

38-
if (isInstanceOf(hub, Hub)) {
39-
this._hub = hub as Hub;
40-
}
41-
4238
this.name = transactionContext.name || '';
4339
this.metadata = transactionContext.metadata || {};
4440
this._trimEnd = transactionContext.trimEnd;
41+
this._hub = hub || getCurrentHub();
4542

4643
// create a new sentry tracestate value if we didn't inherit one
4744
if (!this.metadata.tracestate?.sentry) {

packages/tracing/src/utils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,13 @@ export function extractSentrytraceData(header: string): TraceparentData | undefi
8080
return undefined;
8181
}
8282

83-
type TracestateHeaderData = { sentry?: string; thirdparty?: string };
84-
8583
/**
8684
* Extract data from an incoming `tracestate` header
8785
*
8886
* @param header
8987
* @returns Object containing data from the header
9088
*/
91-
export function extractTracestateData(header: string): TracestateHeaderData {
89+
export function extractTracestateData(header: string): { sentry?: string; thirdparty?: string } {
9290
let sentryEntry, thirdPartyEntry, before, after;
9391

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

packages/types/src/hub.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,6 @@ export interface Hub {
177177
/** Returns the integration if installed on the current client. */
178178
getIntegration<T extends Integration>(integration: IntegrationClass<T>): T | null;
179179

180-
/** Returns all trace headers that are currently on the top scope. */
181-
traceHeaders(): { [key: string]: string };
182-
183180
/**
184181
* @deprecated No longer does anything. Use use {@link Transaction.startChild} instead.
185182
*/

0 commit comments

Comments
 (0)