Skip to content

Commit b673bc3

Browse files
authored
feat(tracing): Log start and end of span (#5446)
Right now, if one of our SDKs is running on a cloud provider and therefore all the user has is logs, there's no real way to debug missing spans. Are they not finishing in time and getting dropped? Are they even starting in the first place? No way to know. This fixes that by adding logging to the start and end of spans., the same way we have with transactions.
1 parent 049e6e7 commit b673bc3

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

packages/tracing/src/span.ts

Lines changed: 23 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,16 @@ 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+
const logMessage = `[Tracing] Starting '${opStr}' span on transaction '${nameStr}' (${idStr}).`;
178+
childSpan.transaction.metadata.spanMetadata[childSpan.spanId] = { logMessage };
179+
logger.log(logMessage);
180+
}
181+
172182
return childSpan;
173183
}
174184

@@ -220,6 +230,18 @@ export class Span implements SpanInterface {
220230
* @inheritDoc
221231
*/
222232
public finish(endTimestamp?: number): void {
233+
if (
234+
__DEBUG_BUILD__ &&
235+
// Don't call this for transactions
236+
this.transaction &&
237+
this.transaction.spanId !== this.spanId
238+
) {
239+
const { logMessage } = this.transaction.metadata.spanMetadata[this.spanId];
240+
if (logMessage) {
241+
logger.log((logMessage as string).replace('Starting', 'Finishing'));
242+
}
243+
}
244+
223245
this.endTimestamp = typeof endTimestamp === 'number' ? endTimestamp : timestampWithMs();
224246
}
225247

0 commit comments

Comments
 (0)