Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ license = "Apache-2.0"
homepage = "https://skywalking.apache.org/"
repository = "https://github.com/apache/skywalking-rust"

[features]
mock = [] # For internal integration testing only, do not use.

[dependencies]
async-stream = "0.3.3"
base64 = "0.13.0"
bytes = "1.1.0"
cfg-if = "1.0.0"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dependency cfg-ifalready exists, here re-import it.

futures-core = "0.3.21"
futures-util = "0.3.21"
prost = "0.10.4"
Expand All @@ -52,6 +56,10 @@ tonic-build = "0.7.2"
[dev-dependencies]
tokio-stream = { version = "0.1.8", features = ["net"] }

[[test]]
name = "trace_context"
required-features = ["mock"]

[[example]]
name = "simple_trace_report"
path = "examples/simple_trace_report.rs"
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,38 @@ context after the span finished.
use skywalking::context::tracer::Tracer;
use skywalking::reporter::grpc::GrpcReporter;
use std::error::Error;
use std::sync::Arc;
use tokio::signal;

async fn handle_request(tracer: Arc<Tracer<GrpcReporter>>) {
async fn handle_request(tracer: Tracer) {
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.
let span = ctx.create_entry_span("op1").unwrap();
// Generate an Entry Span when a request is received.
// An Entry Span is generated only once per context.
Comment thread
jmjoy marked this conversation as resolved.
// 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").unwrap();
let _span2 = ctx.create_exit_span("op2", "remote_peer");

// Something...

ctx.finalize_span(span2);
// Auto close span2 when dropped.
}

ctx.finalize_span(span);
// Auto close span when dropped.
}

tracer.finalize_context(ctx);
// Auto report ctx when dropped.
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let reporter = GrpcReporter::connect("http://0.0.0.0:11800").await?;
let tracer = Arc::new(Tracer::new("service", "instance", reporter));
let tracer = Tracer::new("service", "instance", reporter);

tokio::spawn(handle_request(tracer.clone()));

Expand Down
38 changes: 10 additions & 28 deletions e2e/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,25 @@ use hyper::{Body, Client, Method, Request, Response, Server, StatusCode};
use skywalking::context::propagation::context::SKYWALKING_HTTP_CONTEXT_HEADER_KEY;
use skywalking::context::propagation::decoder::decode_propagation;
use skywalking::context::propagation::encoder::encode_propagation;
use skywalking::context::tracer::Tracer;
use skywalking::context::tracer::{self, Tracer};
use skywalking::reporter::grpc::GrpcReporter;
use std::convert::Infallible;
use std::error::Error;
use std::future::pending;
use std::net::SocketAddr;
use structopt::StructOpt;
use tokio::sync::OnceCell;

static NOT_FOUND_MSG: &str = "not found";
static SUCCESS_MSG: &str = "Success";

static GLOBAL_TRACER: OnceCell<Tracer<GrpcReporter>> = OnceCell::const_new();

fn set_global_tracer(tracer: Tracer<GrpcReporter>) {
if GLOBAL_TRACER.set(tracer).is_err() {
panic!("TRACER has setted")
}
}

fn get_global_tracer() -> &'static Tracer<GrpcReporter> {
GLOBAL_TRACER.get().expect("TRACER haven't setted")
}

async fn handle_ping(
_req: Request<Body>,
client: Client<HttpConnector>,
) -> Result<Response<Body>, Infallible> {
let mut context = get_global_tracer().create_trace_context();
let span = context.create_entry_span("/ping").unwrap();
let mut context = tracer::create_trace_context();
let _span = context.create_entry_span("/ping");
{
let span2 = context.create_exit_span("/pong", "consumer:8082").unwrap();
let _span2 = context.create_exit_span("/pong", "consumer:8082");
let header = encode_propagation(&context, "/pong", "consumer:8082");
let req = Request::builder()
.method(Method::GET)
Expand All @@ -62,10 +49,7 @@ async fn handle_ping(
.unwrap();

client.request(req).await.unwrap();
context.finalize_span(span2);
}
context.finalize_span(span);
get_global_tracer().finalize_context(context);
Ok(Response::new(Body::from("hoge")))
}

Expand Down Expand Up @@ -112,10 +96,8 @@ async fn handle_pong(_req: Request<Body>) -> Result<Response<Body>, Infallible>
.unwrap(),
)
.unwrap();
let mut context = get_global_tracer().create_trace_context_from_propagation(ctx);
let span = context.create_entry_span("/pong").unwrap();
context.finalize_span(span);
get_global_tracer().finalize_context(context);
let mut context = tracer::create_trace_context_from_propagation(ctx);
let _span = context.create_entry_span("/pong");
Ok(Response::new(Body::from("hoge")))
}

Expand Down Expand Up @@ -158,13 +140,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
let reporter = GrpcReporter::connect("http://collector:19876").await?;

let handle = if opt.mode == "consumer" {
set_global_tracer(Tracer::new("consumer", "node_0", reporter));
let handle = get_global_tracer().reporting(pending());
tracer::set_global_tracer(Tracer::new("consumer", "node_0", reporter));
let handle = tracer::reporting(pending());
run_consumer_service([0, 0, 0, 0]).await;
handle
} else if opt.mode == "producer" {
set_global_tracer(Tracer::new("producer", "node_0", reporter));
let handle = get_global_tracer().reporting(pending());
tracer::set_global_tracer(Tracer::new("producer", "node_0", reporter));
let handle = tracer::reporting(pending());
run_producer_service([0, 0, 0, 0]).await;
handle
} else {
Expand Down
20 changes: 10 additions & 10 deletions examples/simple_trace_report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,38 @@
use skywalking::context::tracer::Tracer;
use skywalking::reporter::grpc::GrpcReporter;
use std::error::Error;
use std::sync::Arc;
use tokio::signal;

async fn handle_request(tracer: Arc<Tracer<GrpcReporter>>) {
async fn handle_request(tracer: Tracer) {
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.
let span = ctx.create_entry_span("op1").unwrap();
// Generate an Entry Span when a request is received.
// An Entry Span is generated only once per context.
// You should assign a variable name to guard the span not 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").unwrap();
let _span2 = ctx.create_exit_span("op2", "remote_peer");

// Something...

ctx.finalize_span(span2);
// Auto close span2 when dropped.
}

ctx.finalize_span(span);
// Auto close span when dropped.
}

tracer.finalize_context(ctx);
// Auto report ctx when dropped.
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let reporter = GrpcReporter::connect("http://0.0.0.0:11800").await?;
let tracer = Arc::new(Tracer::new("service", "instance", reporter));
let tracer = Tracer::new("service", "instance", reporter);

tokio::spawn(handle_request(tracer.clone()));

Expand Down
1 change: 0 additions & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
//

pub mod random_generator;
pub mod time;
3 changes: 2 additions & 1 deletion src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//

pub mod propagation;
pub mod system_time;
pub mod span;
pub(crate) mod system_time;
pub mod trace_context;
pub mod tracer;
21 changes: 10 additions & 11 deletions src/context/propagation/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ use base64::encode;
/// Encode TracingContext to carry current trace info to the destination of RPC call.
/// In general, the output of this function will be packed in `sw8` header in HTTP call.
pub fn encode_propagation(context: &TracingContext, endpoint: &str, address: &str) -> String {
let mut res = String::new();

res += "1-";
res += format!("{}-", encode(context.trace_id.to_string())).as_str();
res += format!("{}-", encode(context.trace_segment_id.to_string())).as_str();
res += format!("{}-", context.peek_active_span_id().unwrap_or(0)).as_str();
res += format!("{}-", encode(context.service.as_str())).as_str();
res += format!("{}-", encode(context.service_instance.as_str())).as_str();
res += format!("{}-", encode(endpoint)).as_str();
res += &encode(address);
res
format!(
"1-{}-{}-{}-{}-{}-{}-{}",
encode(context.trace_id()),
encode(context.trace_segment_id()),
context.peek_active_span_id().unwrap_or(0),
encode(context.service()),
encode(context.service_instance()),
encode(endpoint),
encode(address)
)
}
Loading