Skip to main content

Rust Opentelemetry Instrumentation

  • If you have your own Rust application, follow along the steps mentioned below.

  • If you don't have a Rust application, we have prepared a sample Rust application which is already instrumented with OpenTelemetry. This should help you get started.

Step 1: Instrument your application with OpenTelemetry
​

To configure your application to send data we will need a function to initialize OpenTelemetry. Add the following snippet of code in your main.rs file.

use opentelemetry::sdk::Resource;
use opentelemetry::trace::TraceError;
use opentelemetry::{global, sdk::trace as sdktrace};
use opentelemetry::{trace::Tracer};
use opentelemetry_otlp::WithExportConfig;


fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
opentelemetry_otlp::new_pipeline()
.tracing()
.with_exporter(opentelemetry_otlp::new_exporter().tonic().with_env())
.with_trace_config(
sdktrace::config().with_resource(Resource::default()),
)
.install_batch(opentelemetry::runtime::Tokio)
}

Step 2: Initialize the tracer in main.rs
​

Modify the main function to initialise the tracer in main.rs

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _ = init_tracer()?;

...
}

Step 3: Add the OpenTelemetry instrumentation for your sample Rust app
​

    let parent_cx = global::get_text_map_propagator(|propagator| {
propagator.extract(&HeaderExtractor(req.headers()))
});
tracer.start_with_context("fibonacci", &parent_cx);

Step 4: Set environment variables and run your Rust application
​

Now that you have instrumented your Rust application with OpenTelemetry, you need to set some environment variables to send data to SigNoz backend:

OTEL_RESOURCE_ATTRIBUTES: service.name=rust-app (you can name it whatever you want)

OTEL_EXPORTER_OTLP_ENDPOINT: http://localhost:4317

Since, we have installed SigNoz on our local machine, we use the above IP. If you install SigNoz on a different machine, you can update it with the relevant IP.

Hence, the final run command looks like this:

OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 OTEL_RESOURCE_ATTRIBUTES=service.name=rust-app cargo run

Tutorial​

Here's a tutorial with step by step guide on how to install SigNoz and start monitoring a sample Rust app.

Frequently Asked Questions​

  1. How to find what to use in IP of SigNoz if I have installed SigNoz in Kubernetes cluster?

    Based on where you have installed your application and where you have installed SigNoz, you need to find the right value for this. Please use this grid to find the value you should use for IP of SigNoz

  2. I am sending data from my application to SigNoz, but I don't see any events or graphs in the SigNoz dashboard. What should I do?

    This could be because of one of the following reasons:

    1. Your application is generating telemetry data, but not able to connect with SigNoz installation

      Please use this troubleshooting guide to find if your application is able to access SigNoz installation and send data to it.

    2. Your application is not actually generating telemetry data

      Please check if the application is generating telemetry data first. You can use Console Exporter to just print your telemetry data in console first. Join our Slack Community if you need help on how to export your telemetry data in console

    3. Your SigNoz installation is not running or behind a firewall

      Please double check if the pods in SigNoz installation are running fine. docker ps or kubectl get pods -n platform are your friends for this.