Skip to content

Commit ebf64bc

Browse files
chore: added functionality for constructing full trace flags with lower bits as well
1 parent 93f9f2f commit ebf64bc

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

experimental/packages/otlp-transformer/src/trace/internal.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import type { Link, SpanContext } from '@opentelemetry/api';
16+
import type { Link } from '@opentelemetry/api';
1717
import { Resource } from '@opentelemetry/resources';
1818
import type { ReadableSpan, TimedEvent } from '@opentelemetry/sdk-trace-base';
1919
import type { Encoder } from '../common/utils';
@@ -39,12 +39,13 @@ const SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK = 0x100;
3939
const SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK = 0x200;
4040

4141
/**
42-
* Builds span flags based on the parent span context's isRemote property.
43-
* This follows the OTLP specification for span flags.
42+
* Builds the 32-bit span flags value combining the low 8-bit W3C TraceFlags
43+
* with the HAS_IS_REMOTE and IS_REMOTE bits according to the OTLP spec.
4444
*/
45-
function buildSpanFlags(parentSpanContext?: SpanContext): number {
46-
let flags = SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK;
47-
if (parentSpanContext?.isRemote) {
45+
function buildSpanFlagsFrom(traceFlags: number, isRemote?: boolean): number {
46+
// low 8 bits are W3C TraceFlags (e.g., sampled)
47+
let flags = (traceFlags & 0xff) | SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK;
48+
if (isRemote) {
4849
flags |= SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK;
4950
}
5051
return flags;
@@ -77,7 +78,7 @@ export function sdkSpanToOtlpSpan(span: ReadableSpan, encoder: Encoder): ISpan {
7778
},
7879
links: span.links.map(link => toOtlpLink(link, encoder)),
7980
droppedLinksCount: span.droppedLinksCount,
80-
flags: buildSpanFlags(span.parentSpanContext),
81+
flags: buildSpanFlagsFrom(ctx.traceFlags, span.parentSpanContext?.isRemote),
8182
};
8283
}
8384

@@ -88,7 +89,7 @@ export function toOtlpLink(link: Link, encoder: Encoder): ILink {
8889
traceId: encoder.encodeSpanContext(link.context.traceId),
8990
traceState: link.context.traceState?.serialize(),
9091
droppedAttributesCount: link.droppedAttributesCount || 0,
91-
flags: buildSpanFlags(link.context),
92+
flags: buildSpanFlagsFrom(link.context.traceFlags, link.context.isRemote),
9293
};
9394
}
9495

experimental/packages/otlp-transformer/test/trace.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function createExpectedSpanJson(options: OtlpEncodingOptions) {
9797
},
9898
},
9999
],
100-
flags: 0x100, // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
100+
flags: 0x101, // TraceFlags (0x01) | HAS_IS_REMOTE
101101
},
102102
],
103103
startTimeUnixNano: startTime,
@@ -130,7 +130,7 @@ function createExpectedSpanJson(options: OtlpEncodingOptions) {
130130
code: EStatusCode.STATUS_CODE_OK,
131131
message: undefined,
132132
},
133-
flags: 0x100, // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
133+
flags: 0x101, // TraceFlags (0x01) | HAS_IS_REMOTE
134134
},
135135
],
136136
schemaUrl: 'http://url.to.schema',
@@ -189,7 +189,7 @@ function createExpectedSpanProtobuf() {
189189
},
190190
},
191191
],
192-
flags: 0x100, // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
192+
flags: 0x101, // TraceFlags (0x01) | HAS_IS_REMOTE
193193
},
194194
],
195195
startTimeUnixNano: startTime,
@@ -221,7 +221,7 @@ function createExpectedSpanProtobuf() {
221221
status: {
222222
code: EStatusCode.STATUS_CODE_OK,
223223
},
224-
flags: 0x100, // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
224+
flags: 0x101, // TraceFlags (0x01) | HAS_IS_REMOTE
225225
},
226226
],
227227
schemaUrl: 'http://url.to.schema',
@@ -580,16 +580,16 @@ describe('Trace', () => {
580580
});
581581

582582
describe('span flags', () => {
583-
it('sets flags to 0x100 for local parent span context', () => {
583+
it('sets flags to 0x101 for local parent span context', () => {
584584
const exportRequest = createExportTraceServiceRequest([span], {
585585
useHex: true,
586586
});
587587
assert.ok(exportRequest);
588588
const spanFlags = exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].flags;
589-
assert.strictEqual(spanFlags, 0x100); // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
589+
assert.strictEqual(spanFlags, 0x101); // TraceFlags (0x01) | HAS_IS_REMOTE
590590
});
591591

592-
it('sets flags to 0x300 for remote parent span context', () => {
592+
it('sets flags to 0x301 for remote parent span context', () => {
593593
// Create a span with a remote parent context
594594
const remoteParentSpanContext = {
595595
spanId: '0000000000000001',
@@ -608,19 +608,19 @@ describe('Trace', () => {
608608
});
609609
assert.ok(exportRequest);
610610
const spanFlags = exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].flags;
611-
assert.strictEqual(spanFlags, 0x300); // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK | SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
611+
assert.strictEqual(spanFlags, 0x301); // TraceFlags (0x01) | HAS_IS_REMOTE | IS_REMOTE
612612
});
613613

614-
it('sets flags to 0x100 for links with local context', () => {
614+
it('sets flags to 0x101 for links with local context', () => {
615615
const exportRequest = createExportTraceServiceRequest([span], {
616616
useHex: true,
617617
});
618618
assert.ok(exportRequest);
619619
const linkFlags = exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].links?.[0].flags;
620-
assert.strictEqual(linkFlags, 0x100); // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
620+
assert.strictEqual(linkFlags, 0x101); // TraceFlags (0x01) | HAS_IS_REMOTE
621621
});
622622

623-
it('sets flags to 0x300 for links with remote context', () => {
623+
it('sets flags to 0x301 for links with remote context', () => {
624624
// Create a span with a remote link context
625625
const remoteLinkContext = {
626626
spanId: '0000000000000003',
@@ -645,7 +645,7 @@ describe('Trace', () => {
645645
});
646646
assert.ok(exportRequest);
647647
const linkFlags = exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].links?.[0].flags;
648-
assert.strictEqual(linkFlags, 0x300); // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK | SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
648+
assert.strictEqual(linkFlags, 0x301); // TraceFlags (0x01) | HAS_IS_REMOTE | IS_REMOTE
649649
});
650650
});
651651
});

0 commit comments

Comments
 (0)