Skip to content

Commit 80b9286

Browse files
fixed the isremote exporter flag passing
1 parent 98f9d72 commit 80b9286

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ export interface ISpan {
9797

9898
/** Span status */
9999
status: IStatus;
100+
101+
/** Span flags */
102+
flags?: number;
100103
}
101104

102105
/**
@@ -185,4 +188,7 @@ export interface ILink {
185188

186189
/** Link droppedAttributesCount */
187190
droppedAttributesCount: number;
191+
192+
/** Link flags */
193+
flags?: number;
188194
}

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

Lines changed: 19 additions & 1 deletion
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 } from '@opentelemetry/api';
16+
import type { Link, SpanContext } 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';
@@ -34,6 +34,22 @@ import {
3434
import { OtlpEncodingOptions } from '../common/internal-types';
3535
import { getOtlpEncoder } from '../common/utils';
3636

37+
// Span flags constants matching the OTLP specification
38+
const SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK = 0x100;
39+
const SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK = 0x200;
40+
41+
/**
42+
* Builds span flags based on the parent span context's isRemote property.
43+
* This follows the OTLP specification for span flags.
44+
*/
45+
function buildSpanFlags(parentSpanContext?: SpanContext): number {
46+
let flags = SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK;
47+
if (parentSpanContext?.isRemote) {
48+
flags |= SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK;
49+
}
50+
return flags;
51+
}
52+
3753
export function sdkSpanToOtlpSpan(span: ReadableSpan, encoder: Encoder): ISpan {
3854
const ctx = span.spanContext();
3955
const status = span.status;
@@ -61,6 +77,7 @@ export function sdkSpanToOtlpSpan(span: ReadableSpan, encoder: Encoder): ISpan {
6177
},
6278
links: span.links.map(link => toOtlpLink(link, encoder)),
6379
droppedLinksCount: span.droppedLinksCount,
80+
flags: buildSpanFlags(span.parentSpanContext),
6481
};
6582
}
6683

@@ -71,6 +88,7 @@ export function toOtlpLink(link: Link, encoder: Encoder): ILink {
7188
traceId: encoder.encodeSpanContext(link.context.traceId),
7289
traceState: link.context.traceState?.serialize(),
7390
droppedAttributesCount: link.droppedAttributesCount || 0,
91+
flags: buildSpanFlags(link.context),
7492
};
7593
}
7694

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ function createExpectedSpanJson(options: OtlpEncodingOptions) {
9797
},
9898
},
9999
],
100+
flags: 0x100, // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
100101
},
101102
],
102103
startTimeUnixNano: startTime,
@@ -129,6 +130,7 @@ function createExpectedSpanJson(options: OtlpEncodingOptions) {
129130
code: EStatusCode.STATUS_CODE_OK,
130131
message: undefined,
131132
},
133+
flags: 0x100, // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
132134
},
133135
],
134136
schemaUrl: 'http://url.to.schema',
@@ -187,6 +189,7 @@ function createExpectedSpanProtobuf() {
187189
},
188190
},
189191
],
192+
flags: 0x100, // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
190193
},
191194
],
192195
startTimeUnixNano: startTime,
@@ -218,6 +221,7 @@ function createExpectedSpanProtobuf() {
218221
status: {
219222
code: EStatusCode.STATUS_CODE_OK,
220223
},
224+
flags: 0x100, // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
221225
},
222226
],
223227
schemaUrl: 'http://url.to.schema',
@@ -574,4 +578,74 @@ describe('Trace', () => {
574578
);
575579
});
576580
});
581+
582+
describe('span flags', () => {
583+
it('sets flags to 0x100 for local parent span context', () => {
584+
const exportRequest = createExportTraceServiceRequest([span], {
585+
useHex: true,
586+
});
587+
assert.ok(exportRequest);
588+
const spanFlags = exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].flags;
589+
assert.strictEqual(spanFlags, 0x100); // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
590+
});
591+
592+
it('sets flags to 0x300 for remote parent span context', () => {
593+
// Create a span with a remote parent context
594+
const remoteParentSpanContext = {
595+
spanId: '0000000000000001',
596+
traceId: '00000000000000000000000000000001',
597+
traceFlags: TraceFlags.SAMPLED,
598+
isRemote: true, // This is the key difference
599+
};
600+
601+
const spanWithRemoteParent = {
602+
...span,
603+
parentSpanContext: remoteParentSpanContext,
604+
};
605+
606+
const exportRequest = createExportTraceServiceRequest([spanWithRemoteParent], {
607+
useHex: true,
608+
});
609+
assert.ok(exportRequest);
610+
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
612+
});
613+
614+
it('sets flags to 0x100 for links with local context', () => {
615+
const exportRequest = createExportTraceServiceRequest([span], {
616+
useHex: true,
617+
});
618+
assert.ok(exportRequest);
619+
const linkFlags = exportRequest.resourceSpans?.[0].scopeSpans[0].spans?.[0].links?.[0].flags;
620+
assert.strictEqual(linkFlags, 0x100); // SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
621+
});
622+
623+
it('sets flags to 0x300 for links with remote context', () => {
624+
// Create a span with a remote link context
625+
const remoteLinkContext = {
626+
spanId: '0000000000000003',
627+
traceId: '00000000000000000000000000000002',
628+
traceFlags: TraceFlags.SAMPLED,
629+
isRemote: true, // This is the key difference
630+
};
631+
632+
const remoteLink = {
633+
context: remoteLinkContext,
634+
attributes: { 'link-attribute': 'string value' },
635+
droppedAttributesCount: 0,
636+
};
637+
638+
const spanWithRemoteLink = {
639+
...span,
640+
links: [remoteLink],
641+
};
642+
643+
const exportRequest = createExportTraceServiceRequest([spanWithRemoteLink], {
644+
useHex: true,
645+
});
646+
assert.ok(exportRequest);
647+
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
649+
});
650+
});
577651
});

0 commit comments

Comments
 (0)