Configuration
What the Sophonz collector needs from a Rust service and where each value is set — OTLP environment variables, resource attributes, propagators, sampling, batching, and log filtering with the official opentelemetry crates.
There is no Sophonz configuration object for Rust. Everything is either a standard OTEL_* environment variable that the official crates read, or a builder call in the setup function from Installation. This page lists what the collector requires, which variables the crates read by themselves, and the few that Rust does not read and you set in code.
The API names on this page were checked against opentelemetry 0.33, opentelemetry_sdk 0.33, opentelemetry-otlp 0.33, and tracing-opentelemetry 0.34.
What the collector requires
| Item | Value |
|---|---|
| Protocol | OTLP over HTTP with protobuf (http/protobuf) |
| Traces | https://in.sophonz.ai/v1/traces |
| Logs | https://in.sophonz.ai/v1/logs |
| HTTP header | authorization: sk_your_api_key |
Resource attribute service.key | The same sk_... key. Telemetry without it is dropped silently. |
Resource attribute service.name | The service name registered in the console |
Resource attribute service.namespace | The project. Use the same value as the browser or mobile app that calls this service. |
Resource attributes service.version, deployment.environment.name | Release version and environment (production, staging, ...) |
Environment variables
These are read without any code, as long as you build the resource with Resource::builder() and the exporters with .with_http() and no programmatic override.
| Variable | Read by | Notes |
|---|---|---|
OTEL_SERVICE_NAME | Resource::builder() | Sets service.name. Do not also put service.name in OTEL_RESOURCE_ATTRIBUTES: in 0.33 that value replaces this one. |
OTEL_RESOURCE_ATTRIBUTES | Resource::builder() | Comma-separated key=value pairs. Put service.key, service.namespace, service.version, and deployment.environment.name here. |
OTEL_EXPORTER_OTLP_ENDPOINT | OTLP exporters | Base URL. /v1/traces and /v1/logs are appended. |
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT, OTEL_EXPORTER_OTLP_LOGS_ENDPOINT | OTLP exporters | Full URL for one signal, used as given. |
OTEL_EXPORTER_OTLP_PROTOCOL | OTLP exporters | http/protobuf. |
OTEL_EXPORTER_OTLP_HEADERS | OTLP exporters | authorization=sk_your_api_key. |
OTEL_EXPORTER_OTLP_TIMEOUT | OTLP exporters | Milliseconds per export request. Default 10000. |
OTEL_TRACES_SAMPLER, OTEL_TRACES_SAMPLER_ARG | SdkTracerProvider | Default parentbased_always_on. |
OTEL_BSP_SCHEDULE_DELAY, OTEL_BSP_MAX_QUEUE_SIZE, OTEL_BSP_MAX_EXPORT_BATCH_SIZE | Span batch processor | Defaults 5000 ms, 2048, 512. |
OTEL_BLRP_SCHEDULE_DELAY, OTEL_BLRP_MAX_QUEUE_SIZE, OTEL_BLRP_MAX_EXPORT_BATCH_SIZE | Log batch processor | Defaults 1000 ms, 2048, 512. |
CAUTION — OTEL_PROPAGATORS is not read
The Rust crates do not read OTEL_PROPAGATORS, and the global propagator is a no-op until you set one. Without the set_text_map_propagator call below, incoming traceparent headers are ignored and nothing is injected into outgoing requests, with no error.
Propagators
Set the W3C trace context and baggage propagators once at startup. The setup function in Installation already includes this.
use opentelemetry::global;
use opentelemetry::propagation::TextMapCompositePropagator;
use opentelemetry_sdk::propagation::{BaggagePropagator, TraceContextPropagator};
global::set_text_map_propagator(TextMapCompositePropagator::new(vec![
Box::new(TraceContextPropagator::new()),
Box::new(BaggagePropagator::new()),
]));traceparent joins this service's spans to the caller's trace. baggage carries the session.id that the Sophonz browser SDK sends; see Distributed Tracing.
Setting values in code
If you would rather not depend on the environment, set the same values on the builders. The key still comes from a secret, not from source.
use std::collections::HashMap;
use opentelemetry::KeyValue;
use opentelemetry_otlp::{LogExporter, SpanExporter, WithExportConfig, WithHttpConfig};
use opentelemetry_sdk::Resource;
let key = std::env::var("SOPHONZ_API_KEY").expect("SOPHONZ_API_KEY is not set");
let headers = HashMap::from([("authorization".to_string(), key.clone())]);
let resource = Resource::builder()
.with_service_name("checkout-api")
.with_attributes([
KeyValue::new("service.key", key),
KeyValue::new("service.version", env!("CARGO_PKG_VERSION")),
KeyValue::new("service.namespace", "shop"),
KeyValue::new("deployment.environment.name", "production"),
])
.build();
// A programmatic endpoint is used as given, so it carries the signal path.
let span_exporter = SpanExporter::builder()
.with_http()
.with_endpoint("https://in.sophonz.ai/v1/traces")
.with_headers(headers.clone())
.build()
.expect("failed to build span exporter");
let log_exporter = LogExporter::builder()
.with_http()
.with_endpoint("https://in.sophonz.ai/v1/logs")
.with_headers(headers)
.build()
.expect("failed to build log exporter");CAUTION — with_endpoint does not append the path
Only OTEL_EXPORTER_OTLP_ENDPOINT gets /v1/traces and /v1/logs appended. A URL passed to .with_endpoint() is used exactly as written, so .with_endpoint("https://in.sophonz.ai") posts to / and nothing arrives.
Resource::builder() still reads OTEL_RESOURCE_ATTRIBUTES; attributes set in code win over it. SOPHONZ_API_KEY above is just the name this example chose for the secret. The crates do not read it.
Sampling
The default sampler keeps every trace and follows the caller's decision when a request arrives with a sampled traceparent. To keep a share of the traces that start in this service, either set OTEL_TRACES_SAMPLER=parentbased_traceidratio and OTEL_TRACES_SAMPLER_ARG=0.1, or:
use opentelemetry_sdk::trace::{Sampler, SdkTracerProvider};
let tracer_provider = SdkTracerProvider::builder()
.with_sampler(Sampler::ParentBased(Box::new(Sampler::TraceIdRatioBased(0.1))))
// .with_batch_exporter(...).with_resource(...)
.build();Keep a ParentBased sampler. With a plain ratio sampler, a trace the browser recorded can be cut at the server.
Batching
.with_batch_exporter(exporter) uses the defaults in the table above. The OTEL_BSP_* and OTEL_BLRP_* variables change them without a rebuild. In code, build the processor yourself:
use std::time::Duration;
use opentelemetry_sdk::trace::{BatchConfigBuilder, BatchSpanProcessor, SdkTracerProvider};
let tracer_provider = SdkTracerProvider::builder()
.with_span_processor(
BatchSpanProcessor::builder(span_exporter)
.with_batch_config(
BatchConfigBuilder::default()
.with_max_queue_size(8192)
.with_scheduled_delay(Duration::from_secs(2))
.build(),
)
.build(),
)
.build();When the queue is full, new spans are dropped; the request is never blocked. Raise max_queue_size if a busy service loses spans.
Log and span levels
The registry in the Installation snippet has no filter, so every tracing event from your code and from your dependencies is exported as a log record, down to TRACE. Put a filter on each layer:
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
use tracing_subscriber::prelude::*;
tracing_subscriber::registry()
.with(otel_trace_layer.with_filter(LevelFilter::INFO))
.with(otel_log_layer.with_filter(EnvFilter::new("info")))
.init();A span below the trace layer's level is not exported, and its children attach to the nearest exported ancestor. Check the level your framework middleware uses before filtering: axum-tracing-opentelemetry creates its server span at TRACE unless its tracing_level_info feature is on. See Framework Middleware.
Next steps
- Framework Middleware — a server span for every request in axum, actix-web, and tonic.
- Deployment — these variables in a container, and flushing on shutdown.