Skip to content

Commit

Permalink
fix: include TraceState in trace exports (#3569)
Browse files Browse the repository at this point in the history
* fix: include TraceState in trace exports

Include TraceState in OTEL spans and links.
Print span.traceState in ConsoleSpanExporter.

* Update experimental/CHANGELOG.md

* Update CHANGELOG.md
  • Loading branch information
Flarna authored Jan 27, 2023
1 parent dcb09b7 commit 5b070b8
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ For experimental package changes, see the [experimental CHANGELOG](experimental/
* fix: avoid grpc types dependency [#3551](https://github.com/open-telemetry/opentelemetry-js/pull/3551) @flarna
* fix(otlp-proto-exporter-base): Match Accept header with Content-Type in the proto exporter
[#3562](https://github.com/open-telemetry/opentelemetry-js/pull/3562) @scheler
* fix: include tracestate in export [#3569](https://github.com/open-telemetry/opentelemetry-js/pull/3569) @flarna

### :books: (Refine Doc)

Expand Down
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix: include tracestate in export [#3569](https://github.com/open-telemetry/opentelemetry-js/pull/3569) @flarna

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
2 changes: 2 additions & 0 deletions experimental/packages/otlp-transformer/src/trace/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function sdkSpanToOtlpSpan(span: ReadableSpan, useHex?: boolean): ISpan {
traceId: useHex ? ctx.traceId : core.hexToBase64(ctx.traceId),
spanId: useHex ? ctx.spanId : core.hexToBase64(ctx.spanId),
parentSpanId: parentSpanId,
traceState: ctx.traceState?.serialize(),
name: span.name,
// Span kind is offset by 1 because the API does not define a value for unset
kind: span.kind == null ? 0 : span.kind + 1,
Expand Down Expand Up @@ -60,6 +61,7 @@ export function toOtlpLink(link: Link, useHex?: boolean): ILink {
traceId: useHex
? link.context.traceId
: core.hexToBase64(link.context.traceId),
traceState: link.context.traceState?.serialize(),
droppedAttributesCount: 0,
};
}
Expand Down
6 changes: 4 additions & 2 deletions experimental/packages/otlp-transformer/test/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ function createExpectedSpanJson(useHex: boolean) {
traceId: traceId,
spanId: spanId,
parentSpanId: parentSpanId,
traceState: 'span=bar',
name: 'span-name',
kind: ESpanKind.SPAN_KIND_CLIENT,
links: [
{
droppedAttributesCount: 0,
spanId: linkSpanId,
traceId: linkTraceId,
traceState: 'link=foo',
attributes: [
{
key: 'link-attribute',
Expand Down Expand Up @@ -134,7 +136,7 @@ describe('Trace', () => {
traceFlags: 1,
traceId: '00000000000000000000000000000001',
isRemote: false,
traceState: new TraceState(''),
traceState: new TraceState('span=bar'),
}),
parentSpanId: '0000000000000001',
attributes: { 'string-attribute': 'some attribute value' },
Expand Down Expand Up @@ -163,7 +165,7 @@ describe('Trace', () => {
traceId: '00000000000000000000000000000002',
traceFlags: 1,
isRemote: false,
traceState: new TraceState(''),
traceState: new TraceState('link=foo'),
},
attributes: {
'link-attribute': 'string value',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class ConsoleSpanExporter implements SpanExporter {
return {
traceId: span.spanContext().traceId,
parentId: span.parentSpanId,
traceState: span.spanContext().traceState?.serialize(),
name: span.name,
id: span.spanContext().spanId,
kind: span.kind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

import { SpanContext, TraceFlags } from '@opentelemetry/api';
import { TraceState } from '@opentelemetry/core';
import * as assert from 'assert';
import * as sinon from 'sinon';
import {
Expand Down Expand Up @@ -63,6 +64,7 @@ describe('ConsoleSpanExporter', () => {
const span = tracer.startSpan('foo', {
links: [{ context, attributes: { anAttr: 'aValue' } }],
});
span.spanContext().traceState = new TraceState('trace=state');
span.addEvent('foobar');
span.end();

Expand All @@ -85,6 +87,7 @@ describe('ConsoleSpanExporter', () => {
'status',
'timestamp',
'traceId',
'traceState',
].join(',');

assert.ok(firstSpan.name === 'foo');
Expand Down

0 comments on commit 5b070b8

Please sign in to comment.