Skip to content

Commit 0e74809

Browse files
committed
log start and end of span
1 parent c7fc025 commit 0e74809

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

packages/tracing/src/span.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable max-lines */
22
import { Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types';
3-
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils';
3+
import { dropUndefinedKeys, logger, timestampWithMs, uuid4 } from '@sentry/utils';
44

55
/**
66
* Keeps track of finished spans for a given transaction
@@ -169,6 +169,18 @@ export class Span implements SpanInterface {
169169

170170
childSpan.transaction = this.transaction;
171171

172+
if (__DEBUG_BUILD__ && childSpan.transaction) {
173+
const opStr = (spanContext && spanContext.op) || '< unknown op >';
174+
const nameStr = childSpan.transaction.name || '< unknown name >';
175+
const idStr = childSpan.transaction.spanId;
176+
177+
// TODO: It would be safer to shove this in metadata (would need to move `metadata` from the `Transaction` class
178+
// to the `Span` class)
179+
childSpan.data.__log_message__ = `[Tracing] Starting ${opStr} span on transaction ${nameStr} (${idStr}).`;
180+
181+
logger.log(childSpan.data.__log_message__);
182+
}
183+
172184
return childSpan;
173185
}
174186

@@ -220,6 +232,17 @@ export class Span implements SpanInterface {
220232
* @inheritDoc
221233
*/
222234
public finish(endTimestamp?: number): void {
235+
if (
236+
__DEBUG_BUILD__ &&
237+
this.transaction &&
238+
// We only want to log this for spans, not for transactions
239+
this.transaction.spanId !== this.spanId &&
240+
this.data.__log_message__
241+
) {
242+
logger.log((this.data.__log_message__ as string).replace('Starting', 'Finishing'));
243+
delete this.data.__log_message__;
244+
}
245+
223246
this.endTimestamp = typeof endTimestamp === 'number' ? endTimestamp : timestampWithMs();
224247
}
225248

packages/tracing/test/browser/request.test.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,13 @@ describe('callbacks', () => {
134134

135135
expect(newSpan).toBeDefined();
136136
expect(newSpan).toBeInstanceOf(Span);
137-
expect(newSpan.data).toEqual({
138-
method: 'GET',
139-
type: 'fetch',
140-
url: 'http://dogs.are.great/',
141-
});
137+
expect(newSpan.data).toEqual(
138+
expect.objectContaining({
139+
method: 'GET',
140+
type: 'fetch',
141+
url: 'http://dogs.are.great/',
142+
}),
143+
);
142144
expect(newSpan.description).toBe('GET http://dogs.are.great/');
143145
expect(newSpan.op).toBe('http.client');
144146
expect(fetchHandlerData.fetchData?.__span).toBeDefined();
@@ -241,11 +243,13 @@ describe('callbacks', () => {
241243
const newSpan = transaction.spanRecorder?.spans[1] as Span;
242244

243245
expect(newSpan).toBeInstanceOf(Span);
244-
expect(newSpan.data).toEqual({
245-
method: 'GET',
246-
type: 'xhr',
247-
url: 'http://dogs.are.great/',
248-
});
246+
expect(newSpan.data).toEqual(
247+
expect.objectContaining({
248+
method: 'GET',
249+
type: 'xhr',
250+
url: 'http://dogs.are.great/',
251+
}),
252+
);
249253
expect(newSpan.description).toBe('GET http://dogs.are.great/');
250254
expect(newSpan.op).toBe('http.client');
251255
expect(xhrHandlerData.xhr.__sentry_xhr_span_id__).toBeDefined();

0 commit comments

Comments
 (0)