All files / packages/telemetry/src propagation.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25                          6x 3x   3x   2x       3x    
import { context, propagation } from '@opentelemetry/api'
import { type ClientMiddleware, Metadata } from 'nice-grpc'
 
/**
 * W3C trace context propagation middleware for `@ydbjs/core`'s gRPC client.
 *
 * Injects the active OTel context — `traceparent` / `tracestate` by default,
 * plus any other propagator registered via `propagation.setGlobalPropagator`
 * (Baggage, B3, AWS X-Amzn, …) — into outgoing gRPC metadata. Without a
 * registered OTel SDK the global `propagation` is a no-op, so the middleware
 * adds no headers and makes no allocations beyond the empty record passed
 * to `inject`.
 */
export let propagator: ClientMiddleware = async function* (call, options) {
	let metadata = Metadata(options.metadata)
 
	propagation.inject(context.active(), metadata, {
		set(carrier, key, value) {
			carrier.set(key, String(value))
		},
	})
 
	return yield* call.next(call.request, Object.assign(options, { metadata }))
}