Before you can view the trace data of your application, you must use a client to report your application data to Managed Service for OpenTelemetry. This topic describes how to use the Rust agent of SkyWalking to report Rust application data.
Prerequisites
Protobuf is installed.
macOS
brew install protobuf
Debian-based OS
sudo apt install protobuf-compiler
Background information
SkyWalking is a popular application performance monitoring (APM) service developed in China. SkyWalking is designed for microservices applications, cloud-native applications, and containerized applications in Docker, Kubernetes, and Mesos. SkyWalking is also a distributed tracing system.
skywalking-rust is the official Rust agent repository of SkyWalking. You can use skywalking-rust to monitor Rust applications. You must use skywalking-rust to manually instrument Rust applications.
Sample code
For more information about the sample code repository, see skywalking-demo at GitHub.
The sample code implements a simple HTTP request based on the hyper framework for Rust, and uses skywalking-rust to manually instrument an application to report data to Managed Service for OpenTelemetry.
Use SkyWalking to manually instrument a Rust application
Add the SkyWalking dependency to a Rust project.
NoteIn this example, SkyWalking 0.8.0 is used.
Method 1: Add the dependency to the Cargo.toml file
# Add the dependency to [dependency]. skywalking = { version = "0.8.0", features = ["vendored"] }
Method 2: Run a command on the terminal
cargo add skywalking --features vendored
Import the SkyWalking module to the source code.
# Import the SkyWalking module to the source code to be instrumented. use skywalking::{reporter::grpc::GrpcReporter, trace::tracer::Tracer};
Manually instrument the application.
// Use EntrySpan, LocalSpan, and ExitSpan to manually add instrumentation. You can use these three types of spans to implement end-to-end tracing analysis. // EntrySpan: the entry span. The server uses an entry span to obtain the tracing analysis context from an HTTP request. // LocalSpan: the local span. You can use a local span to instrument an application in the same process. // ExitSpan: the exit span. The client uses an exit span to inject the tracing analysis context into the HTTP request. // The following sample code provides an example on how to add instrumentation for cross-process traces: // client.rs: let mut ctx = tracer.create_trace_context(); { do something... let span = ctx.create_exit_span("operation1", "remote_peer"); } // server.rs: let mut ctx = tracer.create_trace_context(); { let span = ctx.create_entry_span("operation1"); do something... }
Configure an endpoint and a token.
For more information about how to obtain an endpoint and an authentication token, see Prerequisites.
// <endpoint> specifies the endpoint of the collector. <token> specifies the authentication token of the collector. <service_name> specifies the name of your application. let endpoint = "<endpoint>"; let token = "<token>"; let service_name = "<service_name>"; let instance_name = "<instance_name>"; let reporter = GrpcReporter::connect(endpoint).await?; let reporter = reporter.with_authentication(token); let tracer = Tracer::new(service_name, instance_name, reporter.clone());
Restart the application.
FAQ
Problem description: An error message is returned when you build a Rush project, as shown in the following figure.
Cause: This may be caused by the lack of Protobuf. For more information about how to install Protobuf, see Prerequisites.