Skip to content

Commit

Permalink
Merge branch 'main' into tommycpp/span_limit
Browse files Browse the repository at this point in the history
  • Loading branch information
jtescher authored Apr 21, 2021
2 parents be8636c + 7e88009 commit 90df68a
Show file tree
Hide file tree
Showing 27 changed files with 770 additions and 356 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(())
}
7 changes: 5 additions & 2 deletions examples/basic-otlp-with-selector/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Basic OpenTelemetry Example
# Basic OTLP exporter Example

This example shows basic span and metric usage, and exports to the [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector) via OTLP with a custom metric aggregator selector.
This example shows how to configure OTLP metrics exporter to use custom aggregator selectors and custom export kind selectors.

## Prerequisite
You should first start a `opentelemetry-collector` on localhost using the default configuration.

11 changes: 11 additions & 0 deletions examples/basic-otlp-with-selector/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use futures::stream::Stream;
use futures::StreamExt;
use opentelemetry::global::shutdown_tracer_provider;
use opentelemetry::sdk::export::metrics::{ExportKind, ExportKindFor};
use opentelemetry::sdk::{
export::metrics::{Aggregator, AggregatorSelector},
metrics::{aggregators, PushController},
Expand Down Expand Up @@ -50,6 +51,15 @@ impl AggregatorSelector for CustomAggregator {
}
}

#[derive(Debug, Clone)]
struct CustomExportKindFor();

impl ExportKindFor for CustomExportKindFor {
fn export_kind_for(&self, _descriptor: &Descriptor) -> ExportKind {
ExportKind::Delta
}
}

fn init_meter() -> metrics::Result<PushController> {
let export_config = ExporterConfig {
endpoint: "http://localhost:4317".to_string(),
Expand All @@ -58,6 +68,7 @@ fn init_meter() -> metrics::Result<PushController> {
};
opentelemetry_otlp::new_metrics_pipeline(tokio::spawn, delayed_interval)
.with_export_config(export_config)
.with_export_kind(CustomExportKindFor())
.with_aggregator_selector(CustomAggregator())
.build()
}
Expand Down
4 changes: 3 additions & 1 deletion examples/basic-otlp/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Basic OpenTelemetry Example
# Basic OTLP exporter Example

This example shows basic span and metric usage, and exports to the [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector) via OTLP.

## Prerequisite
You should first start a `opentelemetry-collector` on localhost using the default configuration.
9 changes: 4 additions & 5 deletions examples/basic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use opentelemetry::sdk::{metrics::PushController, trace as sdktrace};
use opentelemetry::trace::TraceError;
use opentelemetry::{
baggage::BaggageExt,
metrics::{self, MetricsError, ObserverResult},
metrics::{MetricsError, ObserverResult},
trace::{TraceContextExt, Tracer},
Context, Key, KeyValue,
};
Expand All @@ -27,15 +27,14 @@ fn delayed_interval(duration: Duration) -> impl Stream<Item = tokio::time::Insta
opentelemetry::util::tokio_interval_stream(duration).skip(1)
}

fn init_meter() -> metrics::Result<PushController> {
fn init_meter() -> PushController {
opentelemetry::sdk::export::metrics::stdout(tokio::spawn, delayed_interval)
.with_quantiles(vec![0.5, 0.9, 0.99])
.with_formatter(|batch| {
serde_json::to_value(batch)
.map(|value| value.to_string())
.map_err(|err| MetricsError::Other(err.to_string()))
})
.try_init()
.init()
}

const FOO_KEY: Key = Key::from_static_str("ex.com/foo");
Expand All @@ -55,7 +54,7 @@ lazy_static::lazy_static! {
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let _tracer = init_tracer()?;
let _started = init_meter()?;
let _started = init_meter();

let tracer = global::tracer("ex.com/basic");
let meter = global::meter("ex.com/basic");
Expand Down
4 changes: 1 addition & 3 deletions opentelemetry-jaeger/src/exporter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,8 @@ fn links_to_references(links: sdk::trace::EvictedQueue<Link>) -> Option<Vec<jaeg
let trace_id_high = (trace_id >> 64) as i64;
let trace_id_low = trace_id as i64;

// TODO: properly set the reference type when specs are defined
// see https://github.com/open-telemetry/opentelemetry-specification/issues/65
jaeger::SpanRef::new(
jaeger::SpanRefType::ChildOf,
jaeger::SpanRefType::FollowsFrom,
trace_id_low,
trace_id_high,
span_context.span_id().to_u64() as i64,
Expand Down
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");
}
}
Loading

0 comments on commit 90df68a

Please sign in to comment.