Deployment
Run a Rust service that reports to Sophonz in production — the exporter's threads and the tokio runtime, TLS and CA certificates in the image, environment and secrets in Kubernetes, and flushing on graceful shutdown.
A deployed Rust service needs three things for telemetry to arrive: an exporter that can make HTTPS requests from inside the container, the collector address and key in its environment, and a shutdown path that flushes the last batch. None of them fails loudly when it is wrong, so this page also shows how to check each one.
Exporter and runtime
The setup in Installation uses batch processors with the blocking reqwest client. How that fits with tokio:
- Each batch processor runs on its own OS thread (
OpenTelemetry.Traces.BatchProcessorand one for logs). Export never runs on a tokio worker, and it works the same under the multi-thread and the current-thread runtime, or with no runtime at all. - Ending a span only pushes it onto a bounded queue. When the queue is full the span is dropped; a request is never blocked on the collector.
- Calling
init_telemetry()inside#[tokio::main]is fine. The exporter creates its blocking HTTP client on a helper thread. - The
rt-tokiofeature ofopentelemetry_sdkis not required for this. It is used by the experimental async-runtime processors. - Do not use
.with_simple_exporter()with the OTLP exporter in a server. It exports on the thread that ends the span, once per span.
TLS
opentelemetry-otlp builds reqwest without a TLS backend unless you ask for one. The collector endpoint is an https:// URL, so the reqwest-rustls feature in the Installation Cargo.toml is required; without it every export fails with a network error.
CAUTION — CA certificates in the image
With reqwest-rustls, server certificates are verified against the operating system's trust store. Minimal images often have none. On Debian or Ubuntu base images install ca-certificates; on a scratch image copy /etc/ssl/certs/ca-certificates.crt from the build stage.
FROM rust:1.92-bookworm AS build
WORKDIR /app
COPY . .
RUN cargo build --release --locked
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates \
&& rm -rf /var/lib/apt/lists/*
COPY --from=build /app/target/release/checkout-api /usr/local/bin/checkout-api
USER 10001
ENTRYPOINT ["checkout-api"]Environment
The key appears twice, as a header and as a resource attribute. Keep it in a secret and build both variables from it.
env:
- name: SOPHONZ_API_KEY
valueFrom:
secretKeyRef:
name: sophonz
key: api-key
- name: OTEL_SERVICE_NAME
value: checkout-api
- name: OTEL_EXPORTER_OTLP_ENDPOINT
value: https://in.sophonz.ai
- name: OTEL_EXPORTER_OTLP_PROTOCOL
value: http/protobuf
- name: OTEL_EXPORTER_OTLP_HEADERS
value: authorization=$(SOPHONZ_API_KEY)
- name: OTEL_RESOURCE_ATTRIBUTES
value: service.key=$(SOPHONZ_API_KEY),service.namespace=shop,service.version=2.4.1,deployment.environment.name=production
terminationGracePeriodSeconds: 30Kubernetes expands $(SOPHONZ_API_KEY) only when that variable is defined earlier in the same list. terminationGracePeriodSeconds belongs to the pod spec, next to containers. Every variable is described in Configuration.
Graceful shutdown
Spans are exported every 5 seconds and logs every second. When Kubernetes stops a pod it sends SIGTERM, waits terminationGracePeriodSeconds, then sends SIGKILL; whatever is still queued at SIGKILL is lost. Stop the server on SIGTERM, then shut the providers down:
#[tokio::main]
async fn main() {
let (tracer_provider, logger_provider) = init_telemetry();
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app())
.with_graceful_shutdown(shutdown_signal())
.await
.unwrap();
// The server has stopped; export what the batch processors still hold.
let _ = tracer_provider.shutdown();
let _ = logger_provider.shutdown();
}
async fn shutdown_signal() {
use tokio::signal::unix::{signal, SignalKind};
let mut sigterm = signal(SignalKind::terminate()).expect("failed to install SIGTERM handler");
tokio::select! {
_ = tokio::signal::ctrl_c() => {}
_ = sigterm.recv() => {}
}
}- Shut down after the server has drained, not before: spans of in-flight requests end during the drain.
shutdown()blocks the calling thread until the final export finishes, for at most 5 seconds per provider.shutdown_with_timeout(Duration)changes the limit. Leave room for the request drain plus both flushes inside the grace period.- actix-web handles
SIGTERMitself:HttpServer::run()returns after its graceful stop, and the provider shutdown goes right after it, as in Framework Middleware. - tonic has
serve_with_shutdown(addr, shutdown_signal()). - A panic that aborts the process, or
std::process::exit, skips the shutdown. For a short job or CLI, calltracer_provider.force_flush()before exiting.
Checking a deployment
The OpenTelemetry crates report their own problems as tracing events, and the Installation snippet has no console output, so they go unseen. Add a console layer:
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(otel_trace_layer)
.with(otel_log_layer)
.init();- Export errors. A failed export prints an
ERRORfromopentelemetry_sdknamedBatchSpanProcessor.ExportError, after aWARNfromopentelemetry-otlpthat retries are exhausted. A network error here usually means the TLS feature or the CA certificates are missing, or egress toin.sophonz.aiis blocked. - At startup.
tracer_provider.force_flush()returns the export result, so a start-up probe can create one span, flush, and log the error. - Arrival. A clean export does not prove arrival. The collector accepts a payload with a missing or unknown
service.keyand drops it. Send a request and find the service in Sophonz; if nothing shows up, compare the key inOTEL_RESOURCE_ATTRIBUTESwith the one in the console.
Next steps
- Configuration — every variable used above, sampling, and batch sizes.
- Distributed Tracing — join browser and mobile sessions to this service.
TIP — This is just the beginning
Now ship your app. Sophonz surfaces the problems real users hit — so your app evolves on its own.