Skip to content

Commit

Permalink
adding otlp http transport, using proto binary (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
danrusei committed Apr 20, 2021
1 parent 241dff9 commit 7e88009
Show file tree
Hide file tree
Showing 15 changed files with 713 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"examples/basic",
"examples/basic-otlp",
"examples/basic-otlp-with-selector",
"examples/basic-otlp-http",
"examples/datadog",
"examples/external-otlp-tonic-tokio",
"examples/grpc",
Expand Down
12 changes: 12 additions & 0 deletions examples/basic-otlp-http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "basic-otlp-http"
version = "0.1.0"
authors = ["rdan <dan.rusei@gmail.com>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
opentelemetry = { path = "../../opentelemetry", features = ["rt-tokio", "metrics", "serialize"] }
opentelemetry-otlp = { path = "../../opentelemetry-otlp", features = ["http-proto", "reqwest-client"] }
tokio = { version = "1.0", features = ["full"] }
6 changes: 6 additions & 0 deletions examples/basic-otlp-http/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM rust:1.51
COPY . /usr/src/basic-otlp-http/
WORKDIR /usr/src/basic-otlp-http/
RUN cargo build --release
RUN cargo install --path .
CMD ["/usr/local/cargo/bin/basic-otlp-http"]
24 changes: 24 additions & 0 deletions examples/basic-otlp-http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
* The application send data directly to a Collector (port 55681)
* Run the application locally, to run as a docker container you have to change the relative paths from the `Cargo.toml`
* The Collector then sends the data to the appropriate backend, in this case JAEGER

This demo uses `docker-compose` and by default runs against the `otel/opentelemetry-collector-dev:latest` image.

```shell
docker-compose up
or
docker-compose up -d
```

In another terminal run the application `cargo run`

Use the browser to see the trace:
- Jaeger at http://0.0.0.0:16686

Tear it down:

```shell
docker-compose down
```


37 changes: 37 additions & 0 deletions examples/basic-otlp-http/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
version: "2"
services:

# Jaeger
jaeger-all-in-one:
image: jaegertracing/all-in-one:latest
ports:
- "16686:16686"
- "14268"
- "14250"

# Collector
otel-collector:
image: otel/opentelemetry-collector-dev:latest
command: ["--config=/etc/otel-collector-config.yaml", "${OTELCOL_ARGS}"]
volumes:
- ./otel-collector-config.yaml:/etc/otel-collector-config.yaml
ports:
- "1888:1888" # pprof extension
- "13133:13133" # health_check extension
- "4317" # OTLP gRPC receiver
- "55681:55681" # OTLP HTTP receiver
- "55670:55679" # zpages extension
depends_on:
- jaeger-all-in-one


# metrics-rust:
# build:
# dockerfile: $PWD/Dockerfile
# context: ./basic-otlp-http
# environment:
# - OTLP_TONIC_ENDPOINT=otel-collector:4317
# depends_on:
# - otel-collector


35 changes: 35 additions & 0 deletions examples/basic-otlp-http/otel-collector-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
receivers:
otlp:
protocols:
http:
grpc:

exporters:
logging:
loglevel: debug

jaeger:
endpoint: jaeger-all-in-one:14250
insecure: true

processors:
batch:

extensions:
health_check:
pprof:
endpoint: :1888
zpages:
endpoint: :55679

service:
extensions: [pprof, zpages, health_check]
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [logging, jaeger]
metrics:
receivers: [otlp]
processors: [batch]
exporters: [logging]
48 changes: 48 additions & 0 deletions examples/basic-otlp-http/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use opentelemetry::trace::TraceError;
use opentelemetry::{global, sdk::trace as sdktrace};
use opentelemetry::{
trace::{TraceContextExt, Tracer},
Key,
};
use std::error::Error;
use std::time::Duration;

fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
opentelemetry_otlp::new_pipeline()
.with_endpoint("http://localhost:55681/v1/traces")
.with_http()
.install_batch(opentelemetry::runtime::Tokio)
}

const LEMONS_KEY: Key = Key::from_static_str("ex.com/lemons");
const ANOTHER_KEY: Key = Key::from_static_str("ex.com/another");

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _ = init_tracer()?;

let tracer = global::tracer("ex.com/basic");

tracer.in_span("operation", |cx| {
let span = cx.span();
span.add_event(
"Nice operation!".to_string(),
vec![Key::new("bogons").i64(100)],
);
span.set_attribute(ANOTHER_KEY.string("yes"));

tracer.in_span("Sub operation...", |cx| {
let span = cx.span();
span.set_attribute(LEMONS_KEY.string("five"));

span.add_event("Sub span event".to_string(), vec![]);
});
});

// wait for 1 minutes so that we could see metrics being pushed via OTLP every 10 seconds.
tokio::time::sleep(Duration::from_secs(60)).await;

global::shutdown_tracer_provider();

Ok(())
}
11 changes: 11 additions & 0 deletions opentelemetry-otlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ protobuf = { version = "2.18", optional = true }
thiserror = "1.0"
tonic = { version = "0.4", optional = true }
tokio = { version = "1.0", features = ["full"], optional = true }
opentelemetry-http = { version = "0.2", path = "../opentelemetry-http", optional = true }
reqwest = { version = "0.11", optional = true, default-features = false }
surf = { version = "2.0", optional = true }
http = "0.2"

[dev-dependencies]
chrono = "0.4"
Expand All @@ -64,5 +68,12 @@ openssl = ["grpcio/openssl"]
openssl-vendored = ["grpcio/openssl-vendored"]
integration-testing = ["tonic", "tonic-build", "prost", "tokio/full", "opentelemetry/trace"]

http-proto = ["prost-build", "prost", "opentelemetry-http"]
reqwest-blocking-client = ["reqwest/blocking", "opentelemetry-http/reqwest"]
reqwest-client = ["reqwest", "opentelemetry-http/reqwest"]
reqwest-rustls = ["reqwest", "reqwest/rustls-tls-native-roots"]
surf-client = ["surf", "opentelemetry-http/surf"]

[build-dependencies]
tonic-build = { version = "0.4", optional = true }
prost-build = {version = "0.7", optional = true }
35 changes: 35 additions & 0 deletions opentelemetry-otlp/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@
//
// Grpc related files used by grpcio are maintained at src/proto/grpcio. tests/grpc_build.rs makes
// sure they are up to date.
#[cfg(any(feature = "tonic", feature = "http-proto"))]
use std::path::PathBuf;

fn main() {
#[cfg(feature = "tonic")]
{
let out_dir = PathBuf::from(
std::env::var("OUT_DIR").expect("OUT_DIR should be set by cargo but can't find"),
)
.join("tonic");
std::fs::create_dir_all(out_dir.clone()).expect("cannot create output dir");
tonic_build::configure()
.build_server(std::env::var_os("CARGO_FEATURE_INTEGRATION_TESTING").is_some())
.build_client(true)
.format(false)
.out_dir(out_dir)
.compile(
&[
"src/proto/opentelemetry-proto/opentelemetry/proto/common/v1/common.proto",
Expand All @@ -22,4 +32,29 @@ fn main() {
&["src/proto/opentelemetry-proto"],
)
.expect("Error generating protobuf");
}

#[cfg(feature = "http-proto")]
{
let out_dir = PathBuf::from(
std::env::var("OUT_DIR").expect("OUT_DIR should be set by cargo but can't find"),
)
.join("prost");
std::fs::create_dir_all(out_dir.clone()).expect("cannot create output dir");
prost_build::Config::new()
.out_dir(out_dir)
.compile_protos(
&[
"src/proto/opentelemetry-proto/opentelemetry/proto/common/v1/common.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/resource/v1/resource.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/trace/v1/trace.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/trace/v1/trace_config.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/collector/trace/v1/trace_service.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/metrics/v1/metrics.proto",
"src/proto/opentelemetry-proto/opentelemetry/proto/collector/metrics/v1/metrics_service.proto",
],
&["src/proto/opentelemetry-proto"],
)
.expect("Error generating protobuf");
}
}

0 comments on commit 7e88009

Please sign in to comment.