From 3882b2288dd7e056c98d992d1fe7b6f4f49171d9 Mon Sep 17 00:00:00 2001 From: Ramji Date: Wed, 10 Jul 2024 13:06:02 -0400 Subject: [PATCH] Update http example with json protocol (#1912) Co-authored-by: Cijo Thomas Co-authored-by: Lalit Kumar Bhasin --- opentelemetry-otlp/Cargo.toml | 1 - .../examples/basic-otlp-http/Cargo.toml | 2 +- .../examples/basic-otlp-http/README.md | 4 ++-- .../examples/basic-otlp-http/src/main.rs | 22 ++++++++++++++----- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/opentelemetry-otlp/Cargo.toml b/opentelemetry-otlp/Cargo.toml index f031791aa6..2a04fa9956 100644 --- a/opentelemetry-otlp/Cargo.toml +++ b/opentelemetry-otlp/Cargo.toml @@ -71,7 +71,6 @@ tls-webpki-roots = ["tls", "tonic/tls-webpki-roots"] # http binary http-proto = ["prost", "opentelemetry-http", "opentelemetry-proto/gen-tonic-messages", "http", "trace", "metrics"] -# http json This does not work today due to known issue. See https://github.com/open-telemetry/opentelemetry-rust/issues/1763. http-json = ["serde_json", "prost", "opentelemetry-http", "opentelemetry-proto/gen-tonic-messages", "opentelemetry-proto/with-serde", "http", "trace", "metrics"] reqwest-blocking-client = ["reqwest/blocking", "opentelemetry-http/reqwest"] reqwest-client = ["reqwest", "opentelemetry-http/reqwest"] diff --git a/opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml b/opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml index 5357988ad5..149a734304 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml +++ b/opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml @@ -16,7 +16,7 @@ once_cell = { workspace = true } opentelemetry = { path = "../../../opentelemetry" } opentelemetry_sdk = { path = "../../../opentelemetry-sdk", features = ["rt-tokio", "metrics", "logs"] } opentelemetry-http = { path = "../../../opentelemetry-http", optional = true } -opentelemetry-otlp = { path = "../..", features = ["http-proto", "reqwest-client", "logs"] } +opentelemetry-otlp = { path = "../..", features = ["http-proto", "http-json", "reqwest-client", "logs"] } opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false} opentelemetry-semantic-conventions = { path = "../../../opentelemetry-semantic-conventions" } diff --git a/opentelemetry-otlp/examples/basic-otlp-http/README.md b/opentelemetry-otlp/examples/basic-otlp-http/README.md index 91394b1560..d70a5534a0 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/README.md +++ b/opentelemetry-otlp/examples/basic-otlp-http/README.md @@ -1,9 +1,9 @@ # Basic OTLP exporter Example This example shows how to setup OpenTelemetry OTLP exporter for logs, metrics -and traces to exports them to the [OpenTelemetry +and traces to export them to the [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector) via OTLP -over HTTP/protobuf. The Collector then sends the data to the appropriate +over selected protocol such as HTTP/protobuf or HTTP/json. The Collector then sends the data to the appropriate backend, in this case, the logging Exporter, which displays data to console. ## Usage diff --git a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs index 3e7c41b48d..610268294e 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs @@ -6,18 +6,18 @@ use opentelemetry::{ Key, KeyValue, }; use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge; +use opentelemetry_otlp::Protocol; use opentelemetry_otlp::{HttpExporterBuilder, WithExportConfig}; use opentelemetry_sdk::trace::{self as sdktrace, Config}; use opentelemetry_sdk::{ logs::{self as sdklogs}, Resource, }; +use std::error::Error; use tracing::info; use tracing_subscriber::prelude::*; use tracing_subscriber::EnvFilter; -use std::error::Error; - #[cfg(feature = "hyper")] mod hyper; @@ -39,14 +39,22 @@ fn init_logs() -> Result opentelemetry_otlp::new_pipeline() .logging() .with_resource(RESOURCE.clone()) - .with_exporter(http_exporter().with_endpoint("http://localhost:4318/v1/logs")) + .with_exporter( + http_exporter() + .with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format + .with_endpoint("http://localhost:4318/v1/logs"), + ) .install_batch(opentelemetry_sdk::runtime::Tokio) } fn init_tracer_provider() -> Result { opentelemetry_otlp::new_pipeline() .tracing() - .with_exporter(http_exporter().with_endpoint("http://localhost:4318/v1/traces")) + .with_exporter( + http_exporter() + .with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format + .with_endpoint("http://localhost:4318/v1/traces"), + ) .with_trace_config(Config::default().with_resource(RESOURCE.clone())) .install_batch(opentelemetry_sdk::runtime::Tokio) } @@ -54,7 +62,11 @@ fn init_tracer_provider() -> Result { fn init_metrics() -> Result { opentelemetry_otlp::new_pipeline() .metrics(opentelemetry_sdk::runtime::Tokio) - .with_exporter(http_exporter().with_endpoint("http://localhost:4318/v1/metrics")) + .with_exporter( + http_exporter() + .with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format + .with_endpoint("http://localhost:4318/v1/metrics"), + ) .with_resource(RESOURCE.clone()) .build() }