Skip to content

Commit

Permalink
Trace module exports (#255)
Browse files Browse the repository at this point in the history
* Flatten trace exports under the trace sub module.
* Clean up api/trace exports to be flattened under api/trace instead of just api.
* Alias sdk::trace and api::trace in examples and doc tests.
  • Loading branch information
Andreas Wiede authored Oct 6, 2020
1 parent 503503c commit b76e3fd
Show file tree
Hide file tree
Showing 58 changed files with 698 additions and 603 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ rand_distr = "0.3.0"
tokio = { version = "0.2", features = ["full"] }

[features]
default = ["metrics", "trace"]
default = ["trace"]
base64_format = ["base64", "binary_propagator"]
trace = ["rand", "pin-project", "async-trait"]
metrics = ["thiserror", "dashmap", "fnv"]
Expand Down
21 changes: 12 additions & 9 deletions benches/trace.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use criterion::{criterion_group, criterion_main, Criterion};
use opentelemetry::{
api::{Key, Span, Tracer, TracerProvider},
sdk,
api::{
trace::{Span, Tracer, TracerProvider},
Key,
},
sdk::trace as sdktrace,
};

fn criterion_benchmark(c: &mut Criterion) {
Expand Down Expand Up @@ -64,13 +67,13 @@ fn criterion_benchmark(c: &mut Criterion) {
});
}

fn trace_benchmark_group<F: Fn(&sdk::Tracer)>(c: &mut Criterion, name: &str, f: F) {
fn trace_benchmark_group<F: Fn(&sdktrace::Tracer)>(c: &mut Criterion, name: &str, f: F) {
let mut group = c.benchmark_group(name);

group.bench_function("always-sample", |b| {
let always_sample = sdk::TracerProvider::builder()
.with_config(sdk::Config {
default_sampler: Box::new(sdk::Sampler::AlwaysOn),
let always_sample = sdktrace::TracerProvider::builder()
.with_config(sdktrace::Config {
default_sampler: Box::new(sdktrace::Sampler::AlwaysOn),
..Default::default()
})
.build()
Expand All @@ -80,9 +83,9 @@ fn trace_benchmark_group<F: Fn(&sdk::Tracer)>(c: &mut Criterion, name: &str, f:
});

group.bench_function("never-sample", |b| {
let never_sample = sdk::TracerProvider::builder()
.with_config(sdk::Config {
default_sampler: Box::new(sdk::Sampler::AlwaysOff),
let never_sample = sdktrace::TracerProvider::builder()
.with_config(sdktrace::Config {
default_sampler: Box::new(sdktrace::Sampler::AlwaysOff),
..Default::default()
})
.build()
Expand Down
10 changes: 6 additions & 4 deletions examples/actix-http/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use actix_service::Service;
use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer};
use opentelemetry::api::trace::futures::FutureExt;
use opentelemetry::api::{Key, TraceContextExt, Tracer};
use opentelemetry::{global, sdk};
use opentelemetry::api::{
trace::{FutureExt, TraceContextExt, Tracer},
Key,
};
use opentelemetry::{global, sdk::trace as sdktrace};
use std::error::Error;

fn init_tracer() -> Result<(sdk::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
fn init_tracer() -> Result<(sdktrace::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
opentelemetry_jaeger::new_pipeline()
.with_collector_endpoint("http://127.0.0.1:14268/api/traces")
.with_service_name("trace-http-demo")
Expand Down
10 changes: 6 additions & 4 deletions examples/actix-udp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use actix_service::Service;
use actix_web::middleware::Logger;
use actix_web::{web, App, HttpServer};
use opentelemetry::api::trace::futures::FutureExt;
use opentelemetry::api::{Key, TraceContextExt, Tracer};
use opentelemetry::{global, sdk};
use opentelemetry::api::{
trace::{FutureExt, TraceContextExt, Tracer},
Key,
};
use opentelemetry::{global, sdk::trace as sdktrace};
use std::error::Error;

fn init_tracer() -> Result<(sdk::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
fn init_tracer() -> Result<(sdktrace::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
opentelemetry_jaeger::new_pipeline()
.with_agent_endpoint("localhost:6831")
.with_service_name("trace-udp-demo")
Expand Down
10 changes: 7 additions & 3 deletions examples/async/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
//!
//! [`hello_world`]: https://github.com/tokio-rs/tokio/blob/132e9f1da5965530b63554d7a1c59824c3de4e30/tokio/examples/hello_world.rs
use opentelemetry::{
api::{trace::futures::FutureExt, Context, TraceContextExt, Tracer},
global, sdk,
api::{
trace::{FutureExt, TraceContextExt, Tracer},
Context,
},
global,
sdk::trace as sdktrace,
};
use std::{error::Error, io, net::SocketAddr};
use tokio::io::AsyncWriteExt;
Expand Down Expand Up @@ -50,7 +54,7 @@ async fn run(addr: &SocketAddr) -> io::Result<usize> {
write(&mut stream).with_context(cx).await
}

fn init_tracer() -> Result<(sdk::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
fn init_tracer() -> Result<(sdktrace::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
opentelemetry_jaeger::new_pipeline()
.with_service_name("trace-demo")
.install()
Expand Down
18 changes: 13 additions & 5 deletions examples/aws-xray/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use hyper::{body::Body, Client};
use opentelemetry::api::{Context, TraceContextExt, Tracer};
use opentelemetry::{api, exporter::trace::stdout, global, sdk};
use opentelemetry::{
api::{
self,
trace::{TraceContextExt, Tracer},
Context,
},
exporter::trace::stdout,
global,
sdk::trace as sdktrace,
};
use opentelemetry_contrib::{XrayIdGenerator, XrayTraceContextPropagator};

fn init_tracer() -> (sdk::Tracer, stdout::Uninstall) {
fn init_tracer() -> (sdktrace::Tracer, stdout::Uninstall) {
global::set_text_map_propagator(XrayTraceContextPropagator::new());

// Install stdout exporter pipeline to be able to retrieve the collected spans.
// For the demonstration, use `Sampler::AlwaysOn` sampler to sample all traces. In a production
// application, use `Sampler::ParentBased` or `Sampler::TraceIdRatioBased` with a desired ratio.
stdout::new_pipeline()
.with_trace_config(sdk::Config {
default_sampler: Box::new(sdk::Sampler::AlwaysOn),
.with_trace_config(sdktrace::Config {
default_sampler: Box::new(sdktrace::Sampler::AlwaysOn),
id_generator: Box::new(XrayIdGenerator::default()),
..Default::default()
})
Expand Down
11 changes: 6 additions & 5 deletions examples/aws-xray/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use opentelemetry::{
api::{Span, Tracer},
api::trace::{Span, Tracer},
exporter::trace::stdout,
global, sdk,
global,
sdk::trace as sdktrace,
};
use opentelemetry_contrib::{XrayIdGenerator, XrayTraceContextPropagator};
use std::{convert::Infallible, net::SocketAddr};
Expand All @@ -27,15 +28,15 @@ async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
))
}

fn init_tracer() -> (sdk::Tracer, stdout::Uninstall) {
fn init_tracer() -> (sdktrace::Tracer, stdout::Uninstall) {
global::set_text_map_propagator(XrayTraceContextPropagator::new());

// Install stdout exporter pipeline to be able to retrieve the collected spans.
// For the demonstration, use `Sampler::AlwaysOn` sampler to sample all traces. In a production
// application, use `Sampler::ParentBased` or `Sampler::TraceIdRatioBased` with a desired ratio.
stdout::new_pipeline()
.with_trace_config(sdk::Config {
default_sampler: Box::new(sdk::Sampler::AlwaysOn),
.with_trace_config(sdktrace::Config {
default_sampler: Box::new(sdktrace::Sampler::AlwaysOn),
id_generator: Box::new(XrayIdGenerator::default()),
..Default::default()
})
Expand Down
11 changes: 7 additions & 4 deletions examples/basic-otlp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use futures::stream::{Stream, StreamExt};
use opentelemetry::api::metrics::{self, MetricsError, ObserverResult};
use opentelemetry::api::{BaggageExt, Context, Key, KeyValue, TraceContextExt, Tracer};
use opentelemetry::api::{
metrics::{self, MetricsError, ObserverResult},
trace::{TraceContextExt, Tracer},
BaggageExt, Context, Key, KeyValue,
};
use opentelemetry::exporter;
use opentelemetry::sdk::metrics::PushController;
use opentelemetry::{global, sdk};
use opentelemetry::{global, sdk::trace as sdktrace};
use std::error::Error;
use std::time::Duration;

fn init_tracer() -> Result<(sdk::Tracer, opentelemetry_otlp::Uninstall), Box<dyn Error>> {
fn init_tracer() -> Result<(sdktrace::Tracer, opentelemetry_otlp::Uninstall), Box<dyn Error>> {
opentelemetry_otlp::new_pipeline().install()
}

Expand Down
12 changes: 8 additions & 4 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use futures::stream::{Stream, StreamExt};
use opentelemetry::api::metrics::{self, MetricsError, ObserverResult};
use opentelemetry::api::{BaggageExt, Context, Key, KeyValue, TraceContextExt, Tracer};
use opentelemetry::api::{
metrics::{self, MetricsError, ObserverResult},
trace::{TraceContextExt, Tracer},
BaggageExt, Context, Key, KeyValue,
};
use opentelemetry::exporter;
use opentelemetry::global;
use opentelemetry::sdk::metrics::PushController;
use opentelemetry::{global, sdk};
use opentelemetry::sdk::trace as sdktrace;
use std::error::Error;
use std::time::Duration;

fn init_tracer() -> Result<(sdk::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
fn init_tracer() -> Result<(sdktrace::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
opentelemetry_jaeger::new_pipeline()
.with_service_name("trace-demo")
.with_tags(vec![
Expand Down
5 changes: 4 additions & 1 deletion examples/datadog/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use opentelemetry::api::{Key, Span, TraceContextExt, Tracer};
use opentelemetry::api::{
trace::{Span, TraceContextExt, Tracer},
Key,
};
use opentelemetry::global;
use opentelemetry_contrib::datadog::ApiVersion;
use std::thread;
Expand Down
7 changes: 4 additions & 3 deletions examples/grpc/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
use hello_world::greeter_client::GreeterClient;
use hello_world::HelloRequest;
use opentelemetry::api::{
Context, KeyValue, TextMapFormat, TraceContextExt, TraceContextPropagator, Tracer,
trace::{TraceContextExt, TraceContextPropagator, Tracer},
Context, KeyValue, TextMapFormat,
};
use opentelemetry::sdk;
use opentelemetry::sdk::trace as sdktrace;
use std::error::Error;

pub mod hello_world {
tonic::include_proto!("helloworld");
}

fn tracing_init() -> Result<(sdk::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
fn tracing_init() -> Result<(sdktrace::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
opentelemetry_jaeger::new_pipeline()
.with_service_name("grpc-client")
.install()
Expand Down
12 changes: 8 additions & 4 deletions examples/grpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use tonic::{transport::Server, Request, Response, Status};

use hello_world::greeter_server::{Greeter, GreeterServer};
use hello_world::{HelloReply, HelloRequest};
use opentelemetry::api::{self, KeyValue, Span, TextMapFormat, Tracer};
use opentelemetry::api::{
self,
trace::{Span, Tracer},
KeyValue, TextMapFormat,
};
use opentelemetry::global;
use opentelemetry::sdk;
use opentelemetry::sdk::trace as sdktrace;
use std::error::Error;

pub mod hello_world {
Expand All @@ -20,7 +24,7 @@ impl Greeter for MyGreeter {
&self,
request: Request<HelloRequest>, // Accept request of type HelloRequest
) -> Result<Response<HelloReply>, Status> {
let propagator = api::TraceContextPropagator::new();
let propagator = api::trace::TraceContextPropagator::new();
let parent_cx = propagator.extract(request.metadata());
let span = global::tracer("greeter").start_from_context("Processing reply", &parent_cx);
span.set_attribute(KeyValue::new("request", format!("{:?}", request)));
Expand All @@ -34,7 +38,7 @@ impl Greeter for MyGreeter {
}
}

fn tracing_init() -> Result<(sdk::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
fn tracing_init() -> Result<(sdktrace::Tracer, opentelemetry_jaeger::Uninstall), Box<dyn Error>> {
opentelemetry_jaeger::new_pipeline()
.with_service_name("grpc-server")
.install()
Expand Down
15 changes: 9 additions & 6 deletions examples/http/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use hyper::{body::Body, Client};
use opentelemetry::api::{Context, TextMapFormat, TraceContextExt, Tracer};
use opentelemetry::{api, exporter::trace::stdout, global, sdk};
use opentelemetry::api::{
trace::{TraceContextExt, Tracer},
Context, TextMapFormat,
};
use opentelemetry::{api, exporter::trace::stdout, global, sdk::trace as sdktrace};

fn init_tracer() -> (sdk::Tracer, stdout::Uninstall) {
fn init_tracer() -> (sdktrace::Tracer, stdout::Uninstall) {
// Install stdout exporter pipeline to be able to retrieve the collected spans.
// For the demonstration, use `Sampler::AlwaysOn` sampler to sample all traces. In a production
// application, use `Sampler::ParentBased` or `Sampler::TraceIdRatioBased` with a desired ratio.
stdout::new_pipeline()
.with_trace_config(sdk::Config {
default_sampler: Box::new(sdk::Sampler::AlwaysOn),
.with_trace_config(sdktrace::Config {
default_sampler: Box::new(sdktrace::Sampler::AlwaysOn),
..Default::default()
})
.install()
Expand All @@ -19,7 +22,7 @@ async fn main() -> std::result::Result<(), Box<dyn std::error::Error + Send + Sy
let _guard = init_tracer();

let client = Client::new();
let propagator = api::TraceContextPropagator::new();
let propagator = api::trace::TraceContextPropagator::new();
let span = global::tracer("example/client").start("say hello");
let cx = Context::current_with_span(span);

Expand Down
14 changes: 9 additions & 5 deletions examples/http/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use opentelemetry::{
api::{self, Span, TextMapFormat, Tracer},
api::{
self,
trace::{Span, Tracer},
TextMapFormat,
},
exporter::trace::stdout,
global, sdk,
};
use std::{convert::Infallible, net::SocketAddr};

async fn handle(req: Request<Body>) -> Result<Response<Body>, Infallible> {
let propagator = api::TraceContextPropagator::new();
let propagator = api::trace::TraceContextPropagator::new();
let parent_cx = propagator.extract(req.headers());
let span = global::tracer("example/server").start_from_context("hello", &parent_cx);
span.add_event("handling this...".to_string(), Vec::new());

Ok(Response::new("Hello, World!".into()))
}

fn init_tracer() -> (sdk::Tracer, stdout::Uninstall) {
fn init_tracer() -> (sdk::trace::Tracer, stdout::Uninstall) {
// Install stdout exporter pipeline to be able to retrieve the collected spans.

// For the demonstration, use `Sampler::AlwaysOn` sampler to sample all traces. In a production
// application, use `Sampler::ParentBased` or `Sampler::TraceIdRatioBased` with a desired ratio.
stdout::new_pipeline()
.with_trace_config(sdk::Config {
default_sampler: Box::new(sdk::Sampler::AlwaysOn),
.with_trace_config(sdk::trace::Config {
default_sampler: Box::new(sdk::trace::Sampler::AlwaysOn),
..Default::default()
})
.install()
Expand Down
4 changes: 2 additions & 2 deletions examples/stdout.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use opentelemetry::{
api::Tracer,
api::trace::Tracer,
exporter::trace::stdout,
sdk::{trace, Sampler},
sdk::trace::{self, Sampler},
};

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion examples/zipkin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use opentelemetry::api::{Span, Tracer};
use opentelemetry::api::trace::{Span, Tracer};
use opentelemetry::global;
use std::thread;
use std::time::Duration;
Expand Down
Loading

0 comments on commit b76e3fd

Please sign in to comment.