From e15d5b3bcd99a7e0cdfaa605b1e4af4cf48738bd Mon Sep 17 00:00:00 2001 From: David Luna Date: Wed, 25 Sep 2024 17:32:55 +0200 Subject: [PATCH] feat(sdk-trace-base): replace SpanAttributes with Attributes (#5009) --- CHANGELOG_NEXT.md | 1 + package-lock.json | 2 +- .../opentelemetry-sdk-trace-base/package.json | 2 +- .../src/Sampler.ts | 8 ++++---- .../opentelemetry-sdk-trace-base/src/Span.ts | 18 +++++++++--------- .../src/TimedEvent.ts | 4 ++-- .../src/export/ReadableSpan.ts | 4 ++-- .../src/sampler/ParentBasedSampler.ts | 4 ++-- .../test/common/Span.test.ts | 10 +++++----- .../test/common/Tracer.test.ts | 8 ++++---- 10 files changed, 31 insertions(+), 30 deletions(-) diff --git a/CHANGELOG_NEXT.md b/CHANGELOG_NEXT.md index a41332c0fc..2ecf08018b 100644 --- a/CHANGELOG_NEXT.md +++ b/CHANGELOG_NEXT.md @@ -10,6 +10,7 @@ * chore(otel-resources): replace deprecated SpanAttributes [#4428](https://github.com/open-telemetry/opentelemetry-js/pull/4428) @JamieDanielson * feat(sdk-metrics)!: remove MeterProvider.addMetricReader() in favor of constructor option [#4419](https://github.com/open-telemetry/opentelemetry-js/pull/4419) @pichlermarc * feat(sdk-metrics)!: replace attributeKeys with custom processors option [#4532](https://github.com/open-telemetry/opentelemetry-js/pull/4532) @pichlermarc +* refactor(sdk-trace-base)!: replace `SpanAttributes` with `Attributes` [#5009](https://github.com/open-telemetry/opentelemetry-js/pull/5009) @david-luna ### :rocket: (Enhancement) diff --git a/package-lock.json b/package-lock.json index a2f3981bca..605f2d7f56 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31742,7 +31742,7 @@ "node": ">=18" }, "peerDependencies": { - "@opentelemetry/api": ">=1.0.0 <1.10.0" + "@opentelemetry/api": ">=1.3.0 <1.10.0" } }, "packages/opentelemetry-sdk-trace-node": { diff --git a/packages/opentelemetry-sdk-trace-base/package.json b/packages/opentelemetry-sdk-trace-base/package.json index 5d69d0d4b7..e8525854c0 100644 --- a/packages/opentelemetry-sdk-trace-base/package.json +++ b/packages/opentelemetry-sdk-trace-base/package.json @@ -87,7 +87,7 @@ "webpack": "5.94.0" }, "peerDependencies": { - "@opentelemetry/api": ">=1.0.0 <1.10.0" + "@opentelemetry/api": ">=1.3.0 <1.10.0" }, "dependencies": { "@opentelemetry/core": "1.26.0", diff --git a/packages/opentelemetry-sdk-trace-base/src/Sampler.ts b/packages/opentelemetry-sdk-trace-base/src/Sampler.ts index 0a4236e882..6824fe3cce 100644 --- a/packages/opentelemetry-sdk-trace-base/src/Sampler.ts +++ b/packages/opentelemetry-sdk-trace-base/src/Sampler.ts @@ -17,7 +17,7 @@ import { Context, Link, - SpanAttributes, + Attributes, SpanKind, TraceState, } from '@opentelemetry/api'; @@ -58,7 +58,7 @@ export interface SamplingResult { * Caller may call {@link Sampler}.shouldSample any number of times and * can safely cache the returned value. */ - attributes?: Readonly; + attributes?: Readonly; /** * A {@link TraceState} that will be associated with the {@link Span} through * the new {@link SpanContext}. Samplers SHOULD return the TraceState from @@ -83,7 +83,7 @@ export interface Sampler { * span to be created starts a new trace. * @param spanName of the span to be created. * @param spanKind of the span to be created. - * @param attributes Initial set of SpanAttributes for the Span being constructed. + * @param attributes Initial set of Attributes for the Span being constructed. * @param links Collection of links that will be associated with the Span to * be created. Typically useful for batch operations. * @returns a {@link SamplingResult}. @@ -93,7 +93,7 @@ export interface Sampler { traceId: string, spanName: string, spanKind: SpanKind, - attributes: SpanAttributes, + attributes: Attributes, links: Link[] ): SamplingResult; diff --git a/packages/opentelemetry-sdk-trace-base/src/Span.ts b/packages/opentelemetry-sdk-trace-base/src/Span.ts index 3b7758f138..6f68cc2505 100644 --- a/packages/opentelemetry-sdk-trace-base/src/Span.ts +++ b/packages/opentelemetry-sdk-trace-base/src/Span.ts @@ -21,8 +21,8 @@ import { HrTime, Link, Span as APISpan, - SpanAttributes, - SpanAttributeValue, + Attributes, + AttributeValue, SpanContext, SpanKind, SpanStatus, @@ -64,7 +64,7 @@ export class Span implements APISpan, ReadableSpan { private readonly _spanContext: SpanContext; readonly kind: SpanKind; readonly parentSpanId?: string; - readonly attributes: SpanAttributes = {}; + readonly attributes: Attributes = {}; readonly links: Link[] = []; readonly events: TimedEvent[] = []; readonly startTime: HrTime; @@ -105,7 +105,7 @@ export class Span implements APISpan, ReadableSpan { links: Link[] = [], startTime?: TimeInput, _deprecatedClock?: unknown, // keeping this argument even though it is unused to ensure backwards compatibility - attributes?: SpanAttributes + attributes?: Attributes ) { this.name = spanName; this._spanContext = spanContext; @@ -139,7 +139,7 @@ export class Span implements APISpan, ReadableSpan { return this._spanContext; } - setAttribute(key: string, value?: SpanAttributeValue): this; + setAttribute(key: string, value?: AttributeValue): this; setAttribute(key: string, value: unknown): this { if (value == null || this._isSpanEnded()) return this; if (key.length === 0) { @@ -163,7 +163,7 @@ export class Span implements APISpan, ReadableSpan { return this; } - setAttributes(attributes: SpanAttributes): this { + setAttributes(attributes: Attributes): this { for (const [k, v] of Object.entries(attributes)) { this.setAttribute(k, v); } @@ -179,7 +179,7 @@ export class Span implements APISpan, ReadableSpan { */ addEvent( name: string, - attributesOrStartTime?: SpanAttributes | TimeInput, + attributesOrStartTime?: Attributes | TimeInput, timeStamp?: TimeInput ): this { if (this._isSpanEnded()) return this; @@ -301,7 +301,7 @@ export class Span implements APISpan, ReadableSpan { } recordException(exception: Exception, time?: TimeInput): void { - const attributes: SpanAttributes = {}; + const attributes: Attributes = {}; if (typeof exception === 'string') { attributes[SEMATTRS_EXCEPTION_MESSAGE] = exception; } else if (exception) { @@ -380,7 +380,7 @@ export class Span implements APISpan, ReadableSpan { * @param value Attribute value * @returns truncated attribute value if required, otherwise same value */ - private _truncateToSize(value: SpanAttributeValue): SpanAttributeValue { + private _truncateToSize(value: AttributeValue): AttributeValue { const limit = this._attributeValueLengthLimit; // Check limit if (limit <= 0) { diff --git a/packages/opentelemetry-sdk-trace-base/src/TimedEvent.ts b/packages/opentelemetry-sdk-trace-base/src/TimedEvent.ts index 1f835ba710..068caa432d 100644 --- a/packages/opentelemetry-sdk-trace-base/src/TimedEvent.ts +++ b/packages/opentelemetry-sdk-trace-base/src/TimedEvent.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { HrTime, SpanAttributes } from '@opentelemetry/api'; +import { HrTime, Attributes } from '@opentelemetry/api'; /** * Represents a timed event. @@ -25,7 +25,7 @@ export interface TimedEvent { /** The name of the event. */ name: string; /** The attributes of the event. */ - attributes?: SpanAttributes; + attributes?: Attributes; /** Count of attributes of the event that were dropped due to collection limits */ droppedAttributesCount?: number; } diff --git a/packages/opentelemetry-sdk-trace-base/src/export/ReadableSpan.ts b/packages/opentelemetry-sdk-trace-base/src/export/ReadableSpan.ts index 20ffea4c56..34741c0323 100644 --- a/packages/opentelemetry-sdk-trace-base/src/export/ReadableSpan.ts +++ b/packages/opentelemetry-sdk-trace-base/src/export/ReadableSpan.ts @@ -17,7 +17,7 @@ import { SpanKind, SpanStatus, - SpanAttributes, + Attributes, HrTime, Link, SpanContext, @@ -34,7 +34,7 @@ export interface ReadableSpan { readonly startTime: HrTime; readonly endTime: HrTime; readonly status: SpanStatus; - readonly attributes: SpanAttributes; + readonly attributes: Attributes; readonly links: Link[]; readonly events: TimedEvent[]; readonly duration: HrTime; diff --git a/packages/opentelemetry-sdk-trace-base/src/sampler/ParentBasedSampler.ts b/packages/opentelemetry-sdk-trace-base/src/sampler/ParentBasedSampler.ts index 6f89ac6431..eb621adcf9 100644 --- a/packages/opentelemetry-sdk-trace-base/src/sampler/ParentBasedSampler.ts +++ b/packages/opentelemetry-sdk-trace-base/src/sampler/ParentBasedSampler.ts @@ -18,7 +18,7 @@ import { Context, isSpanContextValid, Link, - SpanAttributes, + Attributes, SpanKind, TraceFlags, trace, @@ -64,7 +64,7 @@ export class ParentBasedSampler implements Sampler { traceId: string, spanName: string, spanKind: SpanKind, - attributes: SpanAttributes, + attributes: Attributes, links: Link[] ): SamplingResult { const parentContext = trace.getSpanContext(context); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts index 33808acd54..0648cbc298 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts @@ -23,8 +23,8 @@ import { SpanKind, TraceFlags, HrTime, - SpanAttributes, - SpanAttributeValue, + Attributes, + AttributeValue, } from '@opentelemetry/api'; import { DEFAULT_ATTRIBUTE_COUNT_LIMIT, @@ -266,7 +266,7 @@ describe('Span', () => { span.setAttribute(k, v); } for (const [k, v] of Object.entries(invalidAttributes)) { - span.setAttribute(k, v as unknown as SpanAttributeValue); + span.setAttribute(k, v as unknown as AttributeValue); } assert.deepStrictEqual(span.attributes, validAttributes); @@ -735,7 +735,7 @@ describe('Span', () => { ); span.setAttributes(validAttributes); - span.setAttributes(invalidAttributes as unknown as SpanAttributes); + span.setAttributes(invalidAttributes as unknown as Attributes); assert.deepStrictEqual(span.attributes, validAttributes); }); @@ -766,7 +766,7 @@ describe('Span', () => { span.addEvent('rev', { ...validAttributes, ...invalidAttributes, - } as unknown as SpanAttributes); + } as unknown as Attributes); span.end(); assert.strictEqual(span.events.length, 1); diff --git a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts index 70058ac79e..3d1dc91c46 100644 --- a/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts +++ b/packages/opentelemetry-sdk-trace-base/test/common/Tracer.test.ts @@ -14,7 +14,7 @@ * limitations under the License. */ import { - SpanAttributes, + Attributes, Context, context, createContextKey, @@ -65,7 +65,7 @@ describe('Tracer', () => { _traceId: string, _spanName: string, _spanKind: SpanKind, - attributes: SpanAttributes, + attributes: Attributes, links: Link[] ) { // The attributes object should be valid. @@ -82,7 +82,7 @@ describe('Tracer', () => { testAttribute: 'foobar', // invalid attributes should be sanitized. ...invalidAttributes, - } as unknown as SpanAttributes, + } as unknown as Attributes, traceState: this.traceState, }; } @@ -442,7 +442,7 @@ describe('Tracer', () => { const attributes = { ...validAttributes, ...invalidAttributes, - } as unknown as SpanAttributes; + } as unknown as Attributes; const links = [ { context: {