Skip to content

Commit 5c17cc9

Browse files
committed
Update service binding example
1 parent 6ff7f89 commit 5c17cc9

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

examples/service-binding/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ export default {
2626

2727
const serviceBindingSpan = span.startSpan(SPAN_NAME.SERVICE_FETCH, { attributes: { service: 'basic' } });
2828
const res = await env.SERVICE.fetch('https://service/test', {
29-
headers: { 'x-trace-id': `${serviceBindingSpan.getContext().traceId}:${serviceBindingSpan.getContext().spanId}` },
29+
cf: {
30+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
31+
// @ts-ignore
32+
spanContext: serviceBindingSpan.getContext(),
33+
},
3034
});
3135
serviceBindingSpan.end();
3236

src/trace.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,14 @@ export function createTrace(
99
tracerOptions: TracerOptions,
1010
spanOptions?: SpanCreationOptions,
1111
): Trace {
12-
// This is ugly
13-
// TODO: Fix this - https://www.w3.org/TR/trace-context/#traceparent-header
14-
// https://zipkin.io/pages/architecture.html - https://github.com/openzipkin/b3-propagation#overall-process
1512
// This parent context will allow properly tracing across services (and other Workers)
1613
let parentContext: SpanContext | undefined;
14+
// Allow passing SpanContext through the `cf` object for service bindings
1715
if ((req.cf as CfWithTrace)?.traceContext) {
1816
parentContext = (req.cf as CfWithTrace)?.traceContext;
1917
}
20-
if (req.headers.get('x-trace-id')) {
21-
const ids = req.headers.get('x-trace-id')?.split(':', 2);
22-
if (ids?.length === 2) {
23-
parentContext = { traceId: ids[0], spanId: ids[1] };
24-
}
25-
}
2618

19+
// Read context from the spec-compliant ways
2720
const exporter = tracerOptions.collector.exporter ?? new OtlpExporter();
2821
const context = exporter.readContextHeaders(req.headers);
2922
if (context !== null) {

0 commit comments

Comments
 (0)