Skip to content

Commit

Permalink
Add init attributes in constructor and sync tracing to work with new …
Browse files Browse the repository at this point in the history
…constructor
  • Loading branch information
ArtAhmetaj committed Oct 15, 2023
1 parent b6e532b commit c3978d3
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,40 @@ describe('transform', () => {
version: '1',
});
});
it('should map OpenTelemetry constructor attributes to a Zipkin tag',()=>{
const span = new Span(
tracer,
api.ROOT_CONTEXT,
'my-span',
spanContext,
api.SpanKind.SERVER,
parentId,
[],
undefined,
undefined,
{
key1: 'value1',
key2: 'value2',
}
);
const tags: zipkinTypes.Tags = _toZipkinTags(
span,
defaultStatusCodeTagName,
defaultStatusErrorTagName
);

assert.deepStrictEqual(tags, {
key1: 'value1',
key2: 'value2',
[SemanticResourceAttributes.SERVICE_NAME]: 'zipkin-test',
'telemetry.sdk.language': language,
'telemetry.sdk.name': 'opentelemetry',
'telemetry.sdk.version': VERSION,
cost: '112.12',
service: 'ui',
version: '1',
});
})
it('should map OpenTelemetry SpanStatus.code to a Zipkin tag', () => {
const span = new Span(
tracer,
Expand Down
6 changes: 5 additions & 1 deletion packages/opentelemetry-sdk-trace-base/src/Span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ export class Span implements APISpan, ReadableSpan {
parentSpanId?: string,
links: Link[] = [],
startTime?: TimeInput,
_deprecatedClock?: unknown // keeping this argument even though it is unused to ensure backwards compatibility
_deprecatedClock?: unknown, // keeping this argument even though it is unused to ensure backwards compatibility
initAttributes?:SpanAttributes
) {
this.name = spanName;
this._spanContext = spanContext;
this.parentSpanId = parentSpanId;
this.kind = kind;
this.links = links;
if(initAttributes){
this.setAttributes(initAttributes);
}

const now = Date.now();
this._performanceStartTime = otperformance.now();
Expand Down
16 changes: 9 additions & 7 deletions packages/opentelemetry-sdk-trace-base/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ export class Tracer implements api.Tracer {
return nonRecordingSpan;
}

// Set initial span attributes. The attributes object may have been mutated
// by the sampler, so we sanitize the merged attributes before setting them.
const initAttributes = sanitizeAttributes(
Object.assign(attributes, samplingResult.attributes)
);

const span = new Span(
this,
context,
Expand All @@ -140,14 +146,10 @@ export class Tracer implements api.Tracer {
spanKind,
parentSpanId,
links,
options.startTime
);
// Set initial span attributes. The attributes object may have been mutated
// by the sampler, so we sanitize the merged attributes before setting them.
const initAttributes = sanitizeAttributes(
Object.assign(attributes, samplingResult.attributes)
options.startTime,
initAttributes
);
span.setAttributes(initAttributes);

return span;
}

Expand Down
59 changes: 59 additions & 0 deletions packages/opentelemetry-sdk-trace-base/test/common/Span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,5 +1222,64 @@ describe('Span', () => {
});
});
});

describe('when attributes are added in constructor', () => {
it('should not map undefined value', () => {
const span = new Span(
tracer,
ROOT_CONTEXT,
name,
spanContext,
SpanKind.CLIENT
);
assert.deepStrictEqual(span.attributes,{});
});

it('should map correctly sent keys', () => {
const span = new Span(
tracer,
ROOT_CONTEXT,
name,
spanContext,
SpanKind.CLIENT,
"",
[],
undefined,
undefined,
{
"key1":"value",
"key2":"value"
}
);
assert.deepStrictEqual(span.attributes,{
"key1":"value",
"key2":"value"
});
});

it('should correctly map new keys set ', () => {
const span = new Span(
tracer,
ROOT_CONTEXT,
name,
spanContext,
SpanKind.CLIENT,
"",
[],
undefined,
undefined,
{
"key1":"value",
"key2":"value"
}
);
span.setAttributes({"key3":"value"});
assert.deepStrictEqual(span.attributes,{
"key1":"value",
"key2":"value",
"key3":"value"
});
});
});
});
});

0 comments on commit c3978d3

Please sign in to comment.