Simplify baggage getting to a single method

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2026-05-18 16:50:43 +02:00
parent 3914351b84
commit 00bdd871a3
8 changed files with 23 additions and 64 deletions
@@ -1013,13 +1013,6 @@ describe('McpService', () => {
it('includes gen_ai baggage entries as span attributes when present', async () => {
const tracing = tracingServiceMock.mock();
tracing.propagation.getActiveBaggage.mockReturnValue({
getEntry: (key: string) => {
const entries: Record<string, { value: string }> = {
'gen_ai.conversation.id': { value: 'conv-123' },
'gen_ai.agent.id': { value: 'agent-456' },
};
return entries[key];
},
getAllEntries: () => [
['gen_ai.conversation.id', { value: 'conv-123' }],
['gen_ai.agent.id', { value: 'agent-456' }],
@@ -1036,15 +1029,6 @@ describe('McpService', () => {
it('only forwards allowlisted baggage keys onto the span', async () => {
const tracing = tracingServiceMock.mock();
tracing.propagation.getActiveBaggage.mockReturnValue({
getEntry: (key: string) => {
const entries: Record<string, { value: string }> = {
'gen_ai.conversation.id': { value: 'conv-123' },
'gen_ai.tool.call.result': { value: 'injected-result' },
'gen_ai.prompt': { value: 'injected-prompt' },
'gen_ai.user.message': { value: 'injected-user-message' },
};
return entries[key];
},
getAllEntries: () => [
['gen_ai.conversation.id', { value: 'conv-123' }],
['gen_ai.tool.call.result', { value: 'injected-result' }],
@@ -46,13 +46,13 @@ function safeStringify(value: unknown): string {
// Baggage is propagated from untrusted callers, so we forward only an
// explicit allowlist of low-cardinality identifier keys from the OTel
// `gen_ai.*` registry.
const PROPAGATED_BAGGAGE_ATTRIBUTES: readonly string[] = [
const PROPAGATED_BAGGAGE_ATTRIBUTES: ReadonlySet<string> = new Set([
'gen_ai.agent.id',
'gen_ai.agent.name',
'gen_ai.conversation.id',
'gen_ai.provider.name',
'gen_ai.request.model',
];
]);
function baggageAttributes(
tracingService: TracingService,
@@ -60,9 +60,8 @@ function baggageAttributes(
const baggage = tracingService.propagation.getActiveBaggage();
if (!baggage) return {};
const attrs: Record<string, string> = {};
for (const key of PROPAGATED_BAGGAGE_ATTRIBUTES) {
const entry = baggage.getEntry(key);
if (entry) {
for (const [key, entry] of baggage.getAllEntries()) {
if (PROPAGATED_BAGGAGE_ATTRIBUTES.has(key)) {
attrs[key] = entry.value;
}
}