Skip to content

Commit 3f94dc1

Browse files
authored
feat(tracing): Allow storing span metadata (#5464)
This adds span metadata to the `TransactionMetadata` type and the default value given to the `metadata` property in the `Transaction` constructor. I chose to add a property to `TransactionMetadata` rather than create a new `SpanMetadata` type/property which would live on the `Span` class because by doing the former, we don't have to worry about looping over the spans and deleting the metadata before the transaction is sent - everything in `transaction.metadata` (which becomes `event.sdkProcessingMetadata`) is already cleared when the envelope is created[1]. In order to not have to assert on its existence every time it's used, I made `spanMetadata` a required property of `TransactionMetadata`. In order to satisfy that required-ness, a few other things had to happen: - It had to be initialized in the `Transaction` constructor. (Note that there should never be incoming span metadata because the only span at that point is the transaction itself, and its metadata can and does already live in the main `metadata` object. This means there's no need to do `span: metadata.span || {}` when initializing it. ) - The metadata passed to the constructor in the transaction context needed to become a partial (which it effectively already was, since until now all properties have been optional). - The metadata passed to `setMetadata` similarly had to become a partial. [1] https://github.com/getsentry/sentry-javascript/blob/8a06b16d605ca3a1fa6c9af2a701f332e39799ed/packages/core/src/envelope.ts#L86
1 parent db9ab7c commit 3f94dc1

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

packages/tracing/src/transaction.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export class Transaction extends SpanClass implements TransactionInterface {
4242

4343
this._name = transactionContext.name || '';
4444

45-
this.metadata = transactionContext.metadata || {};
45+
this.metadata = {
46+
...transactionContext.metadata,
47+
spanMetadata: {},
48+
};
49+
4650
this._trimEnd = transactionContext.trimEnd;
4751

4852
// this is because transactions are also spans, and spans have a transaction pointer
@@ -89,7 +93,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
8993
/**
9094
* @inheritDoc
9195
*/
92-
public setMetadata(newMetadata: TransactionMetadata): void {
96+
public setMetadata(newMetadata: Partial<TransactionMetadata>): void {
9397
this.metadata = { ...this.metadata, ...newMetadata };
9498
}
9599

packages/types/src/transaction.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface TransactionContext extends SpanContext {
2727
/**
2828
* Metadata associated with the transaction, for internal SDK use.
2929
*/
30-
metadata?: TransactionMetadata;
30+
metadata?: Partial<TransactionMetadata>;
3131
}
3232

3333
/**
@@ -93,7 +93,7 @@ export interface Transaction extends TransactionContext, Span {
9393
* Set metadata for this transaction.
9494
* @hidden
9595
*/
96-
setMetadata(newMetadata: TransactionMetadata): void;
96+
setMetadata(newMetadata: Partial<TransactionMetadata>): void;
9797

9898
/** return the baggage for dynamic sampling and trace propagation */
9999
getBaggage(): Baggage;
@@ -147,6 +147,9 @@ export interface TransactionMetadata {
147147

148148
/** Information on how a transaction name was generated. */
149149
source?: TransactionSource;
150+
151+
/** Metadata for the transaction's spans, keyed by spanId */
152+
spanMetadata: { [spanId: string]: { [key: string]: unknown } };
150153
}
151154

152155
/**

0 commit comments

Comments
 (0)