OpenTelemetry export: three signals, bounded cardinality and an honest lifecycle
1. The problem we were solving
Back in Chapter 4, sciencekit_common taught every public estimator operation — fit, partial_fit, fit_transform, transform, predict, fit_predict and score — to open a structured tracing span (sciencekit.operation) through sk_run_operation. Each span records its operation, rows, columns, mode, backend, fallback_reason, duration_ms and, when something goes wrong, error.
Emitting spans in library code is only half of production observability. The moment a team deploys a training worker or an online scoring service, five practical questions appear immediately:
- Where do logs and metrics go? Operators rarely debug latency spikes from distributed traces alone. They want aggregated p95 latency and error-rate metrics on dashboards, correlated structured logs when a numerical guard or GPU host fallback fires, and traces for slow requests — all tagged with the same
service.name,service.versionanddeployment.environment.name. - How do we keep algorithm crates lightweight?
sciencekit_preprocessingorsciencekit_linear_modelmust never compiletonic,hyper,reqwestor an OpenTelemetry SDK just to center a matrix. - How do we guarantee silence by default? A library that opens background threads or attempts network connections to
localhost:4317when imported into a unit test or CLI script is broken by design. - How do we prevent metric cardinality explosions? In OpenTelemetry and Prometheus, every distinct combination of metric label values allocates a separate time series. Letting arbitrary strings leak into metric labels turns user identifiers into memory leaks.
- How does a short-lived batch training job exit without losing its last spans? Batch processors buffer data in memory; without deterministic
force_flushandshutdown(plus aDropsafety net), a process that finishesfitand exitsmaindrops the very telemetry that explained the run.
crates/sciencekit_telemetry solves all five problems in a dedicated opt-in sub-crate. Algorithm crates keep calling sk_run_operation from sciencekit_common with zero SDK dependencies; applications that want OpenTelemetry build a validated SKTelemetryConfiguration and call SKTelemetry::init once at startup.
2. The idea in two pictures
Three-signal pipeline architecture
flowchart LR
accTitle: Three-signal OpenTelemetry export pipeline in sciencekit_telemetry
accDescr: Library operations emit tracing spans and structured events into SKGlobalTelemetrySubscriber, which fans out to the trace layer with error-status enrichment, the log bridge with trace correlation, and SKTelemetryMetricsLayer with closed-enum labels, exporting via OTLP gRPC, OTLP HTTP or in-memory test exporters.
OP["sk_run_operation<br/>+ tracing events"]:::violet --> PROXY["SubscriberProxy<br/>blocks transport loops"]:::blue
PROXY --> TL["Trace layer<br/>+ error status"]:::green
PROXY --> LL["Log bridge<br/>severity + trace ID"]:::amber
PROXY --> ML["Metrics layer<br/>closed enum labels"]:::orange
TL --> EXP["OTLP gRPC :4317<br/>OTLP HTTP :4318<br/>or InMemory"]:::rose
LL --> EXP
ML --> EXP
classDef blue fill:#e0f2fe,stroke:#0284c7,color:#0c4a6e
classDef green fill:#f0fdf4,stroke:#16a34a,color:#14532d
classDef orange fill:#fff7ed,stroke:#ea580c,color:#7c2d12
classDef violet fill:#ede9fe,stroke:#7c3aed,color:#3b0764
classDef amber fill:#fef3c7,stroke:#d97706,color:#78350f
classDef rose fill:#ffe4e6,stroke:#e11d48,color:#7f1d1d
Process-wide lifecycle state machine
flowchart LR
accTitle: Process-wide SKTelemetry lifecycle state machine
accDescr: Process starts Uninitialized with zero exporters and zero traffic. SKTelemetry::init claims an atomic once-per-process slot; invalid config or missing transport feature releases the slot, whereas success enters Active state supporting bounded force_flush and idempotent shutdown or Drop detachment.
IDLE["Uninitialized<br/>0 exporters · silent"]:::blue -->|"SKTelemetry::init"| CAS["Atomic slot check<br/>validate + build"]:::amber
CAS -->|"build error"| IDLE
CAS -->|"installed"| ACT["Active pipeline<br/>3 signal providers"]:::green
ACT -->|"2nd init call"| REJ["Structured SKError<br/>UnsupportedOperation"]:::rose
ACT -->|"force_flush"| FLUSH["Flush all 3<br/>within flush_timeout"]:::violet
FLUSH --> ACT
ACT -->|"shutdown / Drop"| DONE["Detached & shut down<br/>0 active exporters"]:::orange
classDef blue fill:#e0f2fe,stroke:#0284c7,color:#0c4a6e
classDef green fill:#f0fdf4,stroke:#16a34a,color:#14532d
classDef orange fill:#fff7ed,stroke:#ea580c,color:#7c2d12
classDef violet fill:#ede9fe,stroke:#7c3aed,color:#3b0764
classDef amber fill:#fef3c7,stroke:#d97706,color:#78350f
classDef rose fill:#ffe4e6,stroke:#e11d48,color:#7f1d1d
3. The decisions — and the roads not taken
| Decision | Why we chose it | Alternatives we discarded |
|---|---|---|
Isolate the OpenTelemetry SDK and OTLP exporters in sciencekit_telemetry | Every estimator crate continues to depend only on sciencekit_common and tracing. Applications that never export OTLP pay zero compile-time or binary-size cost for opentelemetry_sdk, tonic or reqwest. | Putting OTLP exporters inside sciencekit_common — bloats the foundation crate at the root of the entire workspace dependency graph. |
Derive traces, logs and metrics from the existing tracing instrumentation | Estimators already emit sciencekit.operation spans and structured tracing events without knowing which subscriber is listening. Bridging all three signals in subscriber layers preserves the span contract unchanged. | Sprinkling direct opentelemetry::global::meter() calls inside every algorithm — duplicates instrumentation and couples math kernels to telemetry APIs. |
Enrich span Status::Error inside a SpanExporter wrapper (SKOperationSpanExporter) | SKOperationObservation::record_error records the error string field on a failed span without mutating tracing_opentelemetry internals. Inspecting finished SpanData right before export promotes any non-empty error attribute into Status::Error { description } cleanly. | Changing sciencekit_common span field names to otel.status_code — violates the non-goal of keeping the existing span field contract stable. |
Canonicalize metric labels to closed &'static str vocabularies | Mapping operation, backend and mode onto the closed SKOperationKind, SKBackendKind and SKExecutionMode sets (with "unknown" fallback) caps total time-series cardinality by construction and avoids per-span String allocations on on_close. | Forwarding raw span string fields into KeyValue metric attributes — risks unbounded cardinality if caller code emits custom spans. |
Install a swappable SubscriberProxy behind OnceLock<Arc<SKGlobalTelemetrySubscriber>> | tracing::subscriber::set_global_default can only succeed once per OS process. Delegating through an RwLock<Option<Arc<dyn Subscriber + Send + Sync>>> lets shutdown() and Drop detach the active pipeline cleanly and rebuild tracing’s callsite interest cache so post-shutdown operations become silent again. | Installing the concrete Registry directly as the global default — makes shutdown unable to detach the subscriber layers for the remainder of the process. |
Gate network transports behind off-by-default otlp-grpc and otlp-http Cargo features | In-memory pipelines (SKTelemetryPipeline::from_exporters and SKTelemetry::init_with_exporters) work with default features for deterministic tests, while network clients compile only when otlp-grpc, otlp-http or the umbrella telemetry feature is requested. | Enabling gRPC/HTTP clients in default features — pulls TLS/HTTP/protobuf stacks into builds that only want local or custom exporters. |
4. The concepts, taught from zero
4.1 One instrumentation seam, three signals
When an estimator runs scaler.fit(&matrix), sk_run_operation opens a tracing span named "sciencekit.operation" with structured fields (operation = "fit", rows = 4, columns = 2, mode = "InProcessSynchronous", backend = "faer"). Inside that span, code may emit structured events such as tracing::warn!(fallback_reason = "no_enumerated_device", "falling back to host backend"). When the closure finishes, SKOperationObservation records error (if Err) and duration_ms, then drops the span.
SKTelemetryPipeline attaches three independent tracing_subscriber::Layer implementations to a single Registry:
- Traces (
sk_build_trace_layer):tracing_opentelemetryconverts each span enter/exit into an OpenTelemetrySpanDataexported throughBatchSpanProcessorandSKOperationSpanExporter, preserving attributes and promoting recordederrormessages into OpenTelemetry’sStatus::Error. - Logs (
sk_build_log_layer):OpenTelemetryTracingBridgeconverts eachtracing::Event(info!,warn!,error!, …) into an OpenTelemetryLogRecordwith matchingSeverity, target, body message, structured key-value attributes, and — whenever emitted inside an active span — the enclosing span’sTraceIdandSpanId. - Metrics (
SKTelemetryMetricsLayer): a customLayerstores anOperationSpanStatein the span’s extensions onon_new_span, updates recorded fields onon_record/on_event, and onon_closerecords five OpenTelemetry instruments:sciencekit.operation.duration(f64histogram, unit"ms")sciencekit.operation.count(u64counter, unit"{operation}")sciencekit.operation.rows(u64histogram, unit"{row}")sciencekit.operation.columns(u64histogram, unit"{column}")sciencekit.operation.errors(u64counter, unit"{error}", incremented only when the operation failed)
4.2 Bounding metric cardinality mathematically
Unlike traces and logs, metric backends maintain an in-memory aggregation slot per unique tuple of attribute values. SKTelemetryMetricsLayer restricts every observation’s three labels to closed sets:
operation: $7$SKOperationKindnames (fit,partial_fit,fit_transform,transform,predict,fit_predict,score) plus"unknown"$\Rightarrow 8$ values.backend: $6$SKBackendKindnames (faer,matrixmultiply,ndarray-linalg,opencl,cuda,rocm) plus"unknown"$\Rightarrow 7$ values.mode: $5$SKExecutionModenames (Automatic,InProcessSynchronous,InProcessAsynchronous,OutOfCoreStreaming,OutOfCoreMemoryMapped) plus"unknown"$\Rightarrow 6$ values.
Even in the worst case where every combination is exercised in one process, the number of distinct attribute series per instrument is strictly bounded by:
$$N_{\text{series}} \le 8 \times 7 \times 6 = 336$$
Row counts, column counts and elapsed milliseconds are recorded as histogram values, never as attribute labels, and arbitrary caller strings outside those closed enums collapse deterministically to "unknown".
4.3 Breaking recursive export feedback loops
An OTLP exporter sends batches over tonic/hyper/h2 (gRPC) or reqwest (HTTP). Those networking crates themselves emit tracing spans and events! Without target filtering, exporting one fit span would emit HTTP spans, whose export would emit more HTTP spans in an infinite feedback loop.
sciencekit_telemetry blocks that loop at two levels:
SubscriberProxyrejects callsites whose target starts with"opentelemetry","tonic","hyper","h2"or"reqwest"(Interest::never()).- Both
sk_build_trace_layerandsk_build_log_layerattach atracing_subscriber::filter::Targetsfilter setting those internal transport targets toLevelFilter::OFFso standaloneSKTelemetryPipeline::layer()compositions enjoy the exact same protection.
4.4 Deterministic flushing and flush_timeout
OpenTelemetry’s BatchSpanProcessor, BatchLogProcessor and PeriodicReader run background worker channels whose built-in force_flush waits use fixed multi-second internal defaults and can reject overlapping control messages when called concurrently from several worker threads.
sciencekit_telemetry hardens both edges:
SKTelemetry::force_flushserializes concurrent callers on an internalMutex<()>(recovering cleanly from poisoning) and rejects calls aftershutdown.SKTelemetryPipeline::force_flushexecutes the three provider flushes behind a bounded channel receive (recv_timeout(configuration.flush_timeout())), guaranteeingforce_flusheither hands pending signals to the exporters or returns a structured timeout error withinflush_timeout.
5. Each public object, explained
All public items live in sciencekit_telemetry and follow PRD §3.4 (SK prefix on types, sk_/SK_ on free functions and constants, full English words without abbreviations, pure-dispatcher mod.rs files, and every source file at or under 200 lines):
SKTelemetryTransport: wire protocol enum with variantsGrpc(default, standard endpoint"http://localhost:4317") andHttp(OTLP/HTTP binary protobuf over/v1/traces,/v1/logs,/v1/metrics, standard endpoint"http://localhost:4318").SKTelemetrySampler: trace sampling policy enum withAlwaysOn(default),AlwaysOff,TraceIdRatioBased(f64)andParentBased(f64). Validates that ratios are finite and lie in $[0.0, 1.0]$.SKTelemetryResource: validated OpenTelemetry resource metadata holding requiredservice.name, optionalservice.version, optionaldeployment.environment.name, and deterministicBTreeMap<String, String>custom attributes, convertible viato_sdk_resource().SKTelemetryBatchConfiguration&SKTelemetryBatchConfigurationBuilder: builder-validated batching configuration (maximum_queue_size,maximum_export_batch_size,scheduled_delay,export_timeout,periodic_metric_interval, plus uniformbackendandexecution_mode). Enforces $1 \le \text{maximum_export_batch_size} \le \text{maximum_queue_size}$ and strictly positive durations.SKTelemetryConfiguration&SKTelemetryConfigurationBuilder: main builder implementingSKBuilder<SKTelemetryConfiguration>, configuring service identity, resource attributes,endpoint,transport,header/headers,timeout(default10s),flush_timeout(default5s),sampler,batch,backend(defaultFaer) andexecution_mode(defaultAutomatic).SKTelemetryPipeline: ownsSdkTracerProvider,SdkLoggerProviderandSdkMeterProviderbuilt from oneSKTelemetryConfiguration. Exposesfrom_exporters,layer,subscriber,force_flushandshutdown, alongside lower-level builders (sk_build_tracer_provider,sk_build_trace_layer,sk_build_logger_provider,sk_build_log_layer,sk_build_meter_provider,sk_build_metrics_layer,SKTelemetryMetricsLayer, andsk_build_otlp_pipeline) and metric name constants (SK_METRIC_OPERATION_DURATION,SK_METRIC_OPERATION_COUNT,SK_METRIC_OPERATION_ROWS,SK_METRIC_OPERATION_COLUMNS,SK_METRIC_OPERATION_ERRORS).SKTelemetry: process-wide lifecycle guard returned bySKTelemetry::init(configuration)orSKTelemetry::init_with_exporters(...). Exposesis_initialized(),active_exporter_count(),is_shut_down(),configuration(),force_flush(&self)andshutdown(self), plus aDropimplementation that detaches the global subscriber and shuts down all three providers if the caller drops the guard without callingshutdownexplicitly.
use std::time::Duration;
use sciencekit_common::SKBuilder;
use sciencekit_telemetry::{
SKTelemetry, SKTelemetryConfiguration, SKTelemetrySampler, SKTelemetryTransport,
};
let configuration = SKTelemetryConfiguration::builder()
.service_name("sciencekit-scoring-api")
.service_version(env!("CARGO_PKG_VERSION"))
.deployment_environment("production")
.endpoint("http://localhost:4317")
.transport(SKTelemetryTransport::Grpc)
.header("x-api-key", "collector-token")
.timeout(Duration::from_secs(5))
.flush_timeout(Duration::from_secs(3))
.sampler(SKTelemetrySampler::ParentBased(0.1))
.build()?;
let telemetry = SKTelemetry::init(configuration)?;
// ... run estimators normally (fit, transform, predict, score) ...
telemetry.force_flush()?;
telemetry.shutdown()?;
6. Validation & error contract
Every invalid configuration or lifecycle misuse fails deterministically with a structured SKError — never a panic:
| Failure condition | Returned SKError |
|---|---|
Missing or blank service_name | SKError::InvalidHyperparameter { name: "service_name", .. } |
Blank service_version, deployment_environment or resource attribute key | SKError::InvalidHyperparameter |
Endpoint missing http:// / https://, containing whitespace/control bytes, missing host, unclosed IPv6 bracket, or port outside 1..=65535 | SKError::InvalidHyperparameter { name: "endpoint", .. } |
Non-token ASCII header name or header value containing \r, \n or NUL | SKError::InvalidHyperparameter { name: "headers", .. } |
Zero timeout, flush_timeout, scheduled_delay, export_timeout or periodic_metric_interval | SKError::InvalidHyperparameter |
| Non-finite sampler ratio or ratio outside $[0.0, 1.0]$ | SKError::InvalidHyperparameter { name: "sampler", .. } |
maximum_queue_size == 0 or maximum_export_batch_size outside 1..=maximum_queue_size | SKError::InvalidHyperparameter |
Calling SKTelemetry::init when the transport feature (otlp-grpc / otlp-http) is disabled, when a global subscriber is already installed externally, or a second time in the same process | SKError::UnsupportedOperation { operation: "telemetry_init", .. } |
Calling force_flush after shutdown, or when an exporter exceeds flush_timeout | SKError::UnsupportedOperation { operation: "telemetry_force_flush", .. } |
7. Walking through the companion tests
The test suites sit right next to their implementation modules under crates/sciencekit_telemetry/src/ and test every requirement using deterministic InMemorySpanExporter, InMemoryLogExporter, InMemoryMetricExporter and custom probe exporters:
- Configuration & validation (
configuration/configuration_tests.rs,configuration/validation_tests.rs):transport_defaults_to_grpc_and_exposes_standard_endpoints,resource_projects_service_identity_and_custom_attributes_into_sdk_resource,sampler_validates_ratios_and_builds_sdk_samplersandconfiguration_builder_constructs_complete_configuration_with_defaults_and_overridesverify defaults,Resourcekey-value projection, sampling ratios and full builder overrides.configuration_builder_rejects_missing_or_blank_service_name,configuration_builder_rejects_malformed_endpointsandconfiguration_builder_rejects_invalid_headers_timeouts_samplers_and_batch_boundsexercise missing names, malformed schemes/ports/whitespace/IPv6 brackets, CRLF header injection, zero timeouts, out-of-range ratios and inverted batch bounds.
- Trace, log & metric bridges (
pipeline/traces_tests.rs,pipeline/logs_tests.rs,pipeline/metrics_tests.rs,pipeline/metrics_cardinality_tests.rs,pipeline/pipeline_tests.rs):fit_operation_exports_span_with_rows_columns_backend_and_execution_moderuns a realSKStandardScaler::fitand verifies the exportedsciencekit.operationspan carriesoperation = "fit",rows = "4",columns = "2",backend = "matrixmultiply",mode = "InProcessSynchronous"andduration_ms.failed_operation_exports_span_with_error_status_and_messageruns a failingTransformoperation and confirmsStatus::Errorcarries the shape-mismatch description.structured_tracing_events_export_with_matching_severity_target_body_and_fieldsandstructured_event_inside_operation_span_preserves_both_span_and_log_recordsverifySeverity::Info/Warn/Error, structured attributes and active span trace-context correlation.completed_operations_export_duration_count_rows_and_columns_metrics_with_labels,failed_operation_increments_error_counter_labelled_by_operation_and_backendandmetrics_layer_bounds_label_cardinality_to_closed_vocabularyverify all five instruments, error counting and"unknown"canonicalization for arbitrary caller strings.configured_resource_attributes_propagate_to_traces_logs_and_metricsandsampler_and_batch_settings_govern_exported_spans_and_batchesprove identical resource metadata reaches all three exporters and verifyAlwaysOffvsParentBased(1.0)batching.
- Silent default, lifecycle & concurrency (
lifecycle/noop_tests.rs,lifecycle/lifecycle_tests.rs,lifecycle/flush_timeout_tests.rs,lifecycle/concurrency_tests.rs,acceptance_tests.rs):uninitialized_default_creates_no_exporter_and_produces_no_network_trafficbinds a non-blocking loopbackTcpListener, builds a configuration pointing at that port without callinginit, runsSKStandardScaler::fit+transform, and provesactive_exporter_count() == 0andaccept()returnsErrorKind::WouldBlock.default_feature_set_compiles_out_otlp_network_exportersverifies OTLP transports are absent unless enabled.force_flush_exports_pending_signals_and_shutdown_stops_further_exports,double_initialization_returns_structured_error_without_replacing_pipeline,dropping_telemetry_guard_flushes_pending_signals_and_detaches_safelyandforce_flush_times_out_promptly_when_exporter_exceeds_configured_flush_timeoutverify every lifecycle transition, including aStallingSpanExportertiming out cleanly at30ms.concurrent_initialization_race_elects_single_winner_and_rejects_otherssynchronizes 8 threads on aBarrierracinginit_with_exporters, asserting 1 winner and 7 structured rejections.concurrent_small_and_large_operations_export_consistent_spans_logs_and_metricsruns 6 threads across small (8 × 4) and larger (1024 × 4) matrices, fittingSKStandardScaler, exportingSKModelState, computingsk_mean_squared_errorand callingforce_flushconcurrently.dense_and_sparse_csr_operations_export_matching_telemetry_signalsandotlp_grpc_and_http_pipelines_construct_and_shut_down_cleanlyverify dense/CSR parity without densification and exercise real OTLP gRPC and HTTP exporter construction under--features telemetry.