MCPcopy Index your code
hub / github.com/apache/skywalking-rust

github.com/apache/skywalking-rust @v0.10.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.10.0 ↗ · + Follow
303 symbols 656 edges 42 files 107 documented · 35%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Apache SkyWalking Rust Agent

Sky Walking logo

Twitter Follow

Crates CI

SkyWalking Rust Agent provides observability capability for Rust App and Library, including tracing, metrics, topology map for distributed system and alert. It uses SkyWalking native formats and core concepts to keep best compatibility and performance.

Concepts

All concepts are from the official SkyWalking definitions.

Tracing

Span

Span is an important and common concept in distributed tracing system. Learn Span from Google Dapper Paper. For better performance, we extend the span into 3 kinds.

  1. EntrySpan EntrySpan represents a service provider, also the endpoint of server side. As an APM system, we are targeting the application servers. So almost all the services and MQ-consumer are EntrySpan(s).
  2. LocalSpan LocalSpan represents a normal Java method, which does not relate to remote service, neither a MQ producer/consumer nor a service(e.g. HTTP service) provider/consumer.
  3. ExitSpan ExitSpan represents a client of service or MQ-producer, as named as LeafSpan at early age of SkyWalking. e.g. accessing DB by JDBC, reading Redis/Memcached are cataloged an ExitSpan.

Tag and Log are similar attributes of the span.

  • Tag is a key:value pair to indicate the attribute with a string value.
  • Log is heavier than tag, with one timestamp and multiple key:value pairs. Log represents an event, typically an error happens.

TracingContext

TracingContext is the context of the tracing process. Span should only be created through context, and be archived into the context after the span finished.

Logging

LogRecord

LogRecord is the simple builder for the LogData, which is the Log format of Skywalking.

Metrics

Meter

  • Counter API represents a single monotonically increasing counter which automatically collects data and reports to the backend.
  • Gauge API represents a single numerical value.
  • Histogram API represents a summary sample observations with customized buckets.

Management

Reporting the extra information of the instance.

Report instance properties

The method insert_os_info of skywalking::management::instance::Properties will insert the predefined os info. In addition, you can use insert, update, and remove to customize your instance information.

The predefined os info:

Key Value
hostname The hostname of os.
ipv4 (probably multiple) The ipv4 addresses of network.
language rust
OS Name Linux / Windows / Mac OS X
Process No. The ID of Process.

Keep alive

Keep the instance alive in the backend analysis. Only recommend to do separate keepAlive report when no trace and metrics needs to be reported. Otherwise, it is duplicated.

Example

```rust, no_run use skywalking::{ logging::{logger::Logger, record::{LogRecord, RecordType}}, reporter::grpc::GrpcReporter, trace::tracer::Tracer, metrics::{meter::Counter, metricer::Metricer}, }; use std::error::Error; use tokio::signal;

async fn handle_request(tracer: Tracer, logger: Logger) { let mut ctx = tracer.create_trace_context();

{
    // Generate an Entry Span when a request is received.
    // An Entry Span is generated only once per context.
    // Assign a variable name to guard the span not to be dropped immediately.
    let _span = ctx.create_entry_span("op1");

    // Something...

    {
        // Generates an Exit Span when executing an RPC.
        let span2 = ctx.create_exit_span("op2", "remote_peer");

        // Something...

        // Do logging.
        logger.log(
            LogRecord::new()
                .add_tag("level", "INFO")
                .with_tracing_context(&ctx)
                .with_span(&span2)
                .record_type(RecordType::Text)
                .content("Something...")
        );

        // Auto close span2 when dropped.
    }

    // Auto close span when dropped.
}

// Auto report ctx when dropped.

}

async fn handle_metric(mut metricer: Metricer) { let counter = metricer.register( Counter::new("instance_trace_count") .add_label("region", "us-west") .add_label("az", "az-1"), );

metricer.boot().await;

counter.increment(10.);

}

[tokio::main]

async fn main() -> Result<(), Box> { // Connect to skywalking oap server. let reporter = GrpcReporter::connect("http://0.0.0.0:11800").await?; // Optional authentication, based on backend setting. let reporter = reporter.with_authentication("");

// Spawn the reporting in background, with listening the graceful shutdown signal.
let handle = reporter
    .reporting()
    .await
    .with_graceful_shutdown(async move {
        signal::ctrl_c().await.expect("failed to listen for event");
    })
    .spawn();

let tracer = Tracer::new("service", "instance", reporter.clone());
let logger = Logger::new("service", "instance", reporter.clone());
let metricer = Metricer::new("service", "instance", reporter);

handle_metric(metricer).await;

handle_request(tracer, logger).await;

handle.await?;

Ok(())

}


# Advanced APIs

## Async Span APIs

`Span::prepare_for_async` designed for async use cases.
When tags, logs, and attributes (including end time) of the span need to be set in another
thread or coroutine.

`TracingContext::wait` wait for all `AsyncSpan` finished.

```rust
use skywalking::{
    trace::tracer::Tracer,
    trace::span::HandleSpanObject,
};

async fn handle(tracer: Tracer) {
    let mut ctx = tracer.create_trace_context();

    {
        let span = ctx.create_entry_span("op1");

        // Create AsyncSpan and drop span.
        // Internally, span will occupy the position of finalized span stack.
        let mut async_span = span.prepare_for_async();

        // Start async route, catch async_span with `move` keyword.
        tokio::spawn(async move {

            async_span.add_tag("foo", "bar");

            // Something...

            // async_span will drop here, submit modifications to finalized spans stack.
        });
    }

    // Wait for all `AsyncSpan` finished.
    ctx.wait();
}

Advanced Reporter

The advanced report provides an alternative way to submit the agent collected data to the backend.

kafka reporter

The Kafka reporter plugin support report traces, metrics, logs, instance properties to Kafka cluster.

Make sure the feature kafka-reporter is enabled.

#[cfg(feature = "kafka-reporter")]
mod example {
    use skywalking::reporter::Report;
    use skywalking::reporter::kafka::{KafkaReportBuilder, KafkaReporter, ClientConfig};

    async fn do_something(reporter: &impl Report) {
        // ....
    }

    async fn foo() {
        let mut client_config = ClientConfig::new();
        client_config
            .set("bootstrap.servers", "broker:9092")
            .set("message.timeout.ms", "6000");

        let (reporter, reporting) = KafkaReportBuilder::new(client_config).build().await.unwrap();
        let handle = reporting.spawn();

        do_something(&reporter);

        handle.await.unwrap();
    }
}

How to compile?

If you have skywalking-(VERSION).crate, you can unpack it with the way as follows:

tar -xvzf skywalking-(VERSION).crate

Using cargo build generates a library. If you'd like to verify the behavior, we recommend to use cargo run --example simple_trace_report which outputs executable, then run it.

NOTICE

This crate automatically generates protobuf related code, which requires protoc before compile.

Please choose one of the ways to install protoc.

  1. Using your OS package manager.

For Debian-base system:

shell sudo apt install protobuf-compiler

For MacOS:

shell brew install protobuf

  1. Auto compile protoc in the crate build script, just by adding the feature vendored in the Cargo.toml:

shell cargo add skywalking --features vendored

  1. Build from source. If protc isn't install inside $PATH, the env value PROTOC should be set.

shell PROTOC=/the/path/of/protoc

For details, please refer to prost-build:sourcing-protoc.

Release

The SkyWalking committer(PMC included) could follow this doc to release an official version.

License

Apache 2.0

Extension points exported contracts — how you extend this code

Report (Interface)
Report provide non-blocking report method for trace, log and metric object. [12 implementers]
src/reporter/mod.rs
Transform (Interface)
Transform to [MeterData]. [3 implementers]
src/metrics/meter.rs
HandleSpanObject (Interface)
[HandleSpanObject] contains methods to handle [SpanObject]. [2 implementers]
src/trace/span.rs
AssertSerialize (Interface)
(no doc) [2 implementers]
src/proto/v3/mod.rs
CollectItemProduce (Interface)
Special purpose, used for user-defined production operations. Generally, it does not need to be handled. [3 implementers]
src/reporter/mod.rs
AssertSend (Interface)
(no doc) [2 implementers]
src/trace/span.rs
AssertDeserialize (Interface)
(no doc) [2 implementers]
src/proto/v3/mod.rs
CollectItemConsume (Interface)
(no doc) [3 implementers]
src/reporter/mod.rs

Core symbols most depended-on inside this repo

clone
called by 72
src/reporter/grpc.rs
create_trace_context
called by 14
src/trace/tracer.rs
set
called by 13
src/reporter/kafka.rs
add_label
called by 11
src/metrics/meter.rs
create_entry_span
called by 10
src/trace/trace_context.rs
get
called by 9
src/metrics/meter.rs
decode_propagation
called by 9
src/trace/propagation/decoder.rs
create_exit_span
called by 8
src/trace/trace_context.rs

Shape

Method 171
Function 59
Class 54
Interface 10
Enum 9

Languages

Rust99%
Python1%

Modules by API surface

src/trace/trace_context.rs44 symbols
src/reporter/grpc.rs32 symbols
src/reporter/kafka.rs29 symbols
src/metrics/meter.rs18 symbols
src/trace/span.rs15 symbols
src/trace/tracer.rs14 symbols
src/logging/record.rs14 symbols
tests/trace_context.rs12 symbols
src/management/manager.rs12 symbols
e2e/src/main.rs11 symbols
src/metrics/metricer.rs10 symbols
tests/management.rs9 symbols

For agents

$ claude mcp add skywalking-rust \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page