Skip to content

Commit 7aff54c

Browse files
JGAntunespieh
andauthored
feat(otel-utils): add addAttributesToActiveSpan method (#5587)
Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 8301161 commit 7aff54c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

Diff for: packages/opentelemetry-utils/src/index.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { context, Context, propagation, trace, SpanStatusCode, Attributes } from '@opentelemetry/api'
22

33
/**
4-
* Sets attributes to be propagated across child spans under the current active context
4+
* Sets attributes to be propagated across child spans under the current active context. Contexts are immutable so the
5+
* newly returned context must be used when instantiating a new span for these new properties to be used.
56
*/
67
export const setMultiSpanAttributes = function (attributes: { [key: string]: string }) {
78
const currentBaggage = propagation.getBaggage(context.active())
@@ -14,6 +15,18 @@ export const setMultiSpanAttributes = function (attributes: { [key: string]: str
1415
return propagation.setBaggage(context.active(), baggage)
1516
}
1617

18+
/**
19+
* Add the provided attributes to the current active span (if any).
20+
*/
21+
export const addAttributesToActiveSpan = function (attributes?: Attributes) {
22+
const span = trace.getActiveSpan()
23+
if (!span) return
24+
25+
if (attributes !== undefined) {
26+
span.setAttributes(attributes)
27+
}
28+
}
29+
1730
/**
1831
* Add error information to the current active span (if any). Optionally sets the provided attributes on the span too.
1932
*/

Diff for: packages/opentelemetry-utils/tests/main.test.ts

+34
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { expect, test, beforeAll, afterAll } from 'vitest'
66
import {
77
getGlobalContext,
88
setGlobalContext,
9+
addAttributesToActiveSpan,
910
addEventToActiveSpan,
1011
addErrorToActiveSpan,
1112
setMultiSpanAttributes,
@@ -94,6 +95,39 @@ test('addErrorToActiveSpan - attributes are added', async () => {
9495
})
9596
})
9697

98+
test('addAttributesToActiveSpan - attributes are added', async () => {
99+
const tracer = trace.getTracer('default')
100+
const span = tracer.startSpan('my-span') as Span
101+
const ctx = trace.setSpan(context.active(), span)
102+
103+
context.with(ctx, async () => {
104+
const attributes = {
105+
'build.test': 'something',
106+
'build.another-test': 'something-else',
107+
}
108+
addAttributesToActiveSpan(attributes)
109+
110+
expect(span.attributes).toStrictEqual(attributes)
111+
})
112+
})
113+
114+
test('addAttributesToActiveSpan - no attributes are added', async () => {
115+
const tracer = trace.getTracer('default')
116+
const span = tracer.startSpan('my-span') as Span
117+
const ctx = trace.setSpan(context.active(), span)
118+
119+
context.with(ctx, async () => {
120+
const attributes = {}
121+
expect(() => addAttributesToActiveSpan()).not.toThrowError()
122+
123+
expect(span.attributes).toStrictEqual(attributes)
124+
})
125+
})
126+
127+
test('addAttributesToActiveSpan - does not throw without active span', async () => {
128+
expect(() => addAttributesToActiveSpan()).not.toThrowError()
129+
})
130+
97131
test('setMultiSpanAttributes - baggage is populated', async () => {
98132
const ctx = setMultiSpanAttributes({ some: 'test', foo: 'bar' })
99133
const baggage = propagation.getBaggage(ctx)

0 commit comments

Comments
 (0)