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
feat(tracing): Propagate environment and release values in baggage Http headers #5193
Changes from 4 commits
97fbd3a
296f4c2
d243198
8e78e7c
dd44ae7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,15 @@ | ||||||||
/* eslint-disable max-lines */ | ||||||||
import { Baggage, Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types'; | ||||||||
import { dropUndefinedKeys, timestampWithMs, uuid4 } from '@sentry/utils'; | ||||||||
import { getCurrentHub } from '@sentry/hub'; | ||||||||
import { Baggage, Hub, Primitive, Span as SpanInterface, SpanContext, Transaction } from '@sentry/types'; | ||||||||
import { | ||||||||
createBaggage, | ||||||||
dropUndefinedKeys, | ||||||||
isBaggageEmpty, | ||||||||
isSentryBaggageEmpty, | ||||||||
setBaggageValue, | ||||||||
timestampWithMs, | ||||||||
uuid4, | ||||||||
} from '@sentry/utils'; | ||||||||
|
||||||||
/** | ||||||||
* Keeps track of finished spans for a given transaction | ||||||||
|
@@ -302,7 +311,14 @@ export class Span implements SpanInterface { | |||||||
* @inheritdoc | ||||||||
*/ | ||||||||
public getBaggage(): Baggage | undefined { | ||||||||
return this.transaction && this.transaction.metadata.baggage; | ||||||||
const existingBaggage = this.transaction && this.transaction.metadata.baggage; | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Took me a while to get what's happening here. Wdyt about adding a comment like this to speed things up for future readers? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||
const finalBaggage = | ||||||||
!existingBaggage || isSentryBaggageEmpty(existingBaggage) | ||||||||
? this._getBaggageWithSentryValues(existingBaggage) | ||||||||
: existingBaggage; | ||||||||
|
||||||||
return isBaggageEmpty(finalBaggage) ? undefined : finalBaggage; | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -334,6 +350,24 @@ export class Span implements SpanInterface { | |||||||
trace_id: this.traceId, | ||||||||
}); | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
* | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we forgot to add a comment here ^^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||
* @param baggage | ||||||||
* @returns | ||||||||
*/ | ||||||||
private _getBaggageWithSentryValues(baggage: Baggage = createBaggage({})): Baggage { | ||||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any | ||||||||
const hub: Hub = ((this.transaction as any) && (this.transaction as any)._hub) || getCurrentHub(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me this seems a bit dangerous. I was wondering if we can get away with only having I'll let you make the final call on this though. It's probably a bit dangerous but should also be fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be fine if we take the Hub from the transaction. When creating a Transaction, users can pass in a hub as an option in the Do you think it's dangerous because there might be a "wrong" hub in the transaction or because of the casts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of the casts. |
||||||||
const client = hub.getClient(); | ||||||||
|
||||||||
const { environment, release } = (client && client.getOptions()) || {}; | ||||||||
|
||||||||
environment && setBaggageValue(baggage, 'environment', environment); | ||||||||
release && setBaggageValue(baggage, 'release', release); | ||||||||
|
||||||||
return baggage; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
export type SpanStatusType = | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { BrowserClient } from '@sentry/browser'; | ||
import { Hub, makeMain, Scope } from '@sentry/hub'; | ||
import { BaseTransportOptions, ClientOptions } from '@sentry/types'; | ||
import { createBaggage, getSentryBaggageItems, getThirdPartyBaggage, isSentryBaggageEmpty } from '@sentry/utils'; | ||
|
||
import { Span, Transaction } from '../src'; | ||
import { TRACEPARENT_REGEXP } from '../src/utils'; | ||
|
@@ -390,4 +392,57 @@ describe('Span', () => { | |
expect(span.data).toStrictEqual({ data0: 'foo', data1: 'bar' }); | ||
}); | ||
}); | ||
|
||
describe('getBaggage and _getBaggageWithSentryValues', () => { | ||
beforeEach(() => { | ||
hub.getClient()!.getOptions = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think it's necessary to add a cleanup for this? Wanna avoid flakeyness in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking the same thing when I wrote the tests here but then I saw that at the top of this file there already is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. Seems good. |
||
return { | ||
release: '1.0.1', | ||
environment: 'production', | ||
} as ClientOptions<BaseTransportOptions>; | ||
}; | ||
}); | ||
|
||
test('leave baggage content untouched and just return baggage if there already is Sentry content in it', () => { | ||
const transaction = new Transaction( | ||
{ | ||
name: 'tx', | ||
metadata: { baggage: createBaggage({ environment: 'myEnv' }, '') }, | ||
}, | ||
hub, | ||
); | ||
|
||
const hubSpy = jest.spyOn(hub.getClient()!, 'getOptions'); | ||
|
||
const span = transaction.startChild(); | ||
|
||
const baggage = span.getBaggage(); | ||
|
||
expect(hubSpy).toHaveBeenCalledTimes(0); | ||
expect(baggage && isSentryBaggageEmpty(baggage)).toBeFalsy(); | ||
expect(baggage && getSentryBaggageItems(baggage)).toStrictEqual({ environment: 'myEnv' }); | ||
expect(baggage && getThirdPartyBaggage(baggage)).toStrictEqual(''); | ||
}); | ||
|
||
test('add Sentry baggage data to baggage if Sentry content is empty', () => { | ||
const transaction = new Transaction( | ||
{ | ||
name: 'tx', | ||
metadata: { baggage: createBaggage({}, '') }, | ||
}, | ||
hub, | ||
); | ||
|
||
const hubSpy = jest.spyOn(hub.getClient()!, 'getOptions'); | ||
|
||
const span = transaction.startChild(); | ||
|
||
const baggage = span.getBaggage(); | ||
|
||
expect(hubSpy).toHaveBeenCalledTimes(1); | ||
expect(baggage && isSentryBaggageEmpty(baggage)).toBeFalsy(); | ||
expect(baggage && getSentryBaggageItems(baggage)).toStrictEqual({ release: '1.0.1', environment: 'production' }); | ||
expect(baggage && getThirdPartyBaggage(baggage)).toStrictEqual(''); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we say in this test title explaining, that we don't overwrite baggage that's already on the outgoing request?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, thanks for bringing this up. Changed this title and the one below it
(this makes me think that I should add more tests covering different mutability cases here in #5205)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dd44ae7