Implementing OpenTelemetry in a Rust application for performance monitoring πŸš€

OpenTelemetry can be utilized to hint Rust functions for efficiency points and bugs. OpenTelemetry is an open-source challenge underneath the Cloud Native Computing Basis (CNCF) that goals to standardize the technology and assortment of telemetry information. Telemetry information consists of logs, metrics, and traces.

Rust is a multi-paradigm, general-purpose programming language designed for efficiency and security, particularly protected concurrency. On this tutorial, we are going to display use the OpenTelemetry to generate end-to-end tracing.

Earlier than we display implement the OpenTelemetry libraries, let’s have a short overview of OpenTelemetry.



What’s OpenTelemetry?

OpenTelemetry is an open-source vendor-agnostic set of instruments, APIs, and SDKs used to instrument functions to create and handle telemetry information(logs, metrics, and traces). It goals to make telemetry information(logs, metrics, and traces) a built-in function of cloud-native software program functions.

The telemetry information is then despatched to an observability device for storage and visualization.

OpenTelemetry libraries instrument utility code to generate telemetry information that’s then despatched to an observability device for storage & visualization

OpenTelemetry is the bedrock for establishing an observability framework. It additionally supplies you the liberty to decide on a backend evaluation device of your alternative.



OpenTelemetry and SigNoz

On this tutorial, we are going to use SigNoz as our backend evaluation device. SigNoz is a full-stack open-source APM device that can be utilized for storing and visualizing the telemetry information collected with OpenTelemetry. It’s constructed natively on OpenTelemetry and works on the OTLP information codecs.

SigNoz supplies question and visualization capabilities for the end-user and comes with out-of-box charts for utility metrics and traces.

Now let’s get right down to implement OpenTelemetry in Rust functions after which visualize the collected information in SigNoz.



Operating Rust utility with OpenTelemetry

Step 1: Set up SigNoz

First, it’s good to set up SigNoz in order that OpenTelemetry can ship the info to it.

SigNoz will be put in on macOS or Linux computer systems in simply three steps by utilizing a easy set up script.

The set up script routinely installs Docker Engine on Linux. Nevertheless, on macOS, you should manually set up Docker Engine earlier than working the set up script.

git clone -b foremost https://github.com/SigNoz/signoz.git
cd signoz/deploy/
./set up.sh
Enter fullscreen mode

Exit fullscreen mode

You may go to our documentation for directions on set up SigNoz utilizing Docker Swarm and Helm Charts.

Deployment Docs

When you’re executed putting in SigNoz, you may entry the UI atΒ http://localhost:3301

SigNoz dashboard
SigNoz dashboard – It reveals providers from a pattern app that comes bundled with the set up

Step 3: Get pattern Rust utility

You probably have your personal Rust utility, observe alongside the steps talked about under. We have now ready a sample Rust application which is already instrumented with OpenTelemetry.

Step 2: Instrument your utility with OpenTelemetry

To configure your utility to ship information we are going to want a perform to initialize OpenTelemetry. Add the next snippet of code in your foremost.rs file.

use opentelemetry::sdk::Useful resource;
use opentelemetry::hint::TraceError;
use opentelemetry::{world, sdk::hint as sdktrace};
use opentelemetry::{hint::Tracer};
use opentelemetry_otlp::WithExportConfig;


fn init_tracer() -> Outcome<sdktrace::Tracer, TraceError> {
    opentelemetry_otlp::new_pipeline()
        .tracing()
        .with_exporter(opentelemetry_otlp::new_exporter().tonic().with_env())
        .with_trace_config(
            sdktrace::config().with_resource(Useful resource::default()),
        )
        .install_batch(opentelemetry::runtime::Tokio)
}
Enter fullscreen mode

Exit fullscreen mode

Step 3: Initialize the tracer in foremost.rs

Modify the primary perform to initialise the tracer in foremost.rs

#[tokio::main]
async fn foremost() -> Outcome<(), Field<dyn Error + Ship + Sync + 'static>> {
    let _ = init_tracer()?;

    ...
}
Enter fullscreen mode

Exit fullscreen mode

Step 4: Add the OpenTelemetry instrumentation in your pattern Rust app

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

Exit fullscreen mode

Step 5: Set surroundings variables and run your Rust utility

Now that you’ve instrumented your Rust utility with OpenTelemetry, it’s good to set some surroundings variables to ship information to SigNoz backend:

OTEL_RESOURCE_ATTRIBUTES: service.identify=rust-app (you may identify it no matter you need)

OTEL_EXPORTER_OTLP_ENDPOINT: http://localhost:4317

Since, we now have put in SigNoz on our native machine, we use the above IP. Should you set up SigNoz on a distinct machine, you may replace it with the related IP.

Therefore, the ultimate run command appears to be like like this:

OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 OTEL_RESOURCE_ATTRIBUTES=service.identify=rust-app cargo run
Enter fullscreen mode

Exit fullscreen mode

Step 8: Generate some information

With the intention to monitor your Rust utility with SigNoz, you first have to generate some information.

Go to residence web page of your Rust utility at localhost:1337 and enter some particulars. Alternatively you may simply ship curl request

curl -d "identify=Baymax&quantity=42" 
-H "Content material-Kind: utility/x-www-form-urlencoded" 
-X POST http://localhost:1337/publish
Enter fullscreen mode

Exit fullscreen mode

Step 9: Visualize the collected information in SigNoz

Entry the signoz UI on http://localhost:3301/application. You will see your pattern Rust utility within the checklist of functions being monitored by SigNoz.

Rust app being monitored on SigNoz dashboard
Rust utility being monitored on the SigNoz dashboard. The opposite functions are pattern apps that come bundled with SigNoz set up.

Go to Traces and select rust-app from the checklist of providers to see the tracing information of your utility. Tracing information may help you visualize how person requests carry out throughout providers in a multi-service utility.

Within the Traces tab of SigNoz, you may analyze the tracing information utilizing filters primarily based on tags, standing codes, service names, operations, and so forth.

Traces tab of SigNoz dashboard
Use highly effective filters to investigate the tracing information of your Rust utility

You may see a whole breakdown of the request with Flamegraphs and Gantt charts. You may click on on any span within the spans desk to entry it.

Flamegraphs and Gantt charts in SigNoz dashboard
You may see the entire breakdown of your requests with particulars like how a lot time every operation took, span attributes, and so forth



Conclusion

Utilizing OpenTelemetry libraries, you may instrument your Rust functions for end-to-end tracing. You may then use an open-source APM device like SigNoz to make sure the sleek efficiency of your functions.

OpenTelemetry is the longer term for establishing observability for cloud-native apps. It’s backed by an enormous group and covers all kinds of expertise and frameworks. Utilizing OpenTelemetry, engineering groups can instrument polyglot and distributed functions with peace of thoughts.

SigNoz is an open-source observability device that comes with a SaaS-like expertise. You may check out SigNoz by visiting its GitHub repo πŸ‘‡

SigNoz GitHub repo

Should you face any points whereas making an attempt out SigNoz, you may attain out together with your questions in #help channel πŸ‘‡

SigNoz Slack community




Additional Studying

OpenTelemetry Collector – Complete Guide

OpenTelemetry Tracing – things you need to know

Add a Comment

Your email address will not be published. Required fields are marked *