From 8ee0ef0b5085fa599c084a75ad0af9d8b19909bd Mon Sep 17 00:00:00 2001 From: Ramajeyam Gopalraj Date: Sun, 7 Jul 2024 22:43:52 -0400 Subject: [PATCH 1/3] Update http example --- opentelemetry-otlp/CHANGELOG.md | 2 +- 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 ++++++++++++++----- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/opentelemetry-otlp/CHANGELOG.md b/opentelemetry-otlp/CHANGELOG.md index 5295ae9ee0..b509df939d 100644 --- a/opentelemetry-otlp/CHANGELOG.md +++ b/opentelemetry-otlp/CHANGELOG.md @@ -20,7 +20,7 @@ now use `.with_resource(RESOURCE::default())` to configure Resource when using previous release. - **Breaking** [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) The OTLP logs exporter now overrides the [InstrumentationScope::name](https://github.com/open-telemetry/opentelemetry-proto/blob/b3060d2104df364136d75a35779e6bd48bac449a/opentelemetry/proto/common/v1/common.proto#L73) field with the `target` from `LogRecord`, if target is populated. - Groups batch of `LogRecord` and `Span` by their resource and instrumentation scope before exporting, for better efficiency [#1873](https://github.com/open-telemetry/opentelemetry-rust/pull/1873). - +- Updated basic-otlp-http example to make use of `protocol` setting when building the exporter ## v0.16.0 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..402465428a 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) + .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) + .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) + .with_endpoint("http://localhost:4318/v1/metrics"), + ) .with_resource(RESOURCE.clone()) .build() } From c8571accb5ff0ce3d8149dbbe0d0aad2f0329422 Mon Sep 17 00:00:00 2001 From: Ramajeyam Gopalraj Date: Tue, 9 Jul 2024 14:12:57 -0400 Subject: [PATCH 2/3] address code review comments --- opentelemetry-otlp/CHANGELOG.md | 1 - opentelemetry-otlp/examples/basic-otlp-http/src/main.rs | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/opentelemetry-otlp/CHANGELOG.md b/opentelemetry-otlp/CHANGELOG.md index b509df939d..0951711fcd 100644 --- a/opentelemetry-otlp/CHANGELOG.md +++ b/opentelemetry-otlp/CHANGELOG.md @@ -20,7 +20,6 @@ now use `.with_resource(RESOURCE::default())` to configure Resource when using previous release. - **Breaking** [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) The OTLP logs exporter now overrides the [InstrumentationScope::name](https://github.com/open-telemetry/opentelemetry-proto/blob/b3060d2104df364136d75a35779e6bd48bac449a/opentelemetry/proto/common/v1/common.proto#L73) field with the `target` from `LogRecord`, if target is populated. - Groups batch of `LogRecord` and `Span` by their resource and instrumentation scope before exporting, for better efficiency [#1873](https://github.com/open-telemetry/opentelemetry-rust/pull/1873). -- Updated basic-otlp-http example to make use of `protocol` setting when building the exporter ## v0.16.0 diff --git a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs index 402465428a..610268294e 100644 --- a/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs +++ b/opentelemetry-otlp/examples/basic-otlp-http/src/main.rs @@ -41,7 +41,7 @@ fn init_logs() -> Result .with_resource(RESOURCE.clone()) .with_exporter( http_exporter() - .with_protocol(Protocol::HttpBinary) + .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) @@ -52,7 +52,7 @@ fn init_tracer_provider() -> Result { .tracing() .with_exporter( http_exporter() - .with_protocol(Protocol::HttpBinary) + .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())) @@ -64,7 +64,7 @@ fn init_metrics() -> Result Date: Tue, 9 Jul 2024 14:16:51 -0400 Subject: [PATCH 3/3] revert changelog --- opentelemetry-otlp/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/opentelemetry-otlp/CHANGELOG.md b/opentelemetry-otlp/CHANGELOG.md index 0951711fcd..5295ae9ee0 100644 --- a/opentelemetry-otlp/CHANGELOG.md +++ b/opentelemetry-otlp/CHANGELOG.md @@ -21,6 +21,7 @@ now use `.with_resource(RESOURCE::default())` to configure Resource when using - **Breaking** [1869](https://github.com/open-telemetry/opentelemetry-rust/pull/1869) The OTLP logs exporter now overrides the [InstrumentationScope::name](https://github.com/open-telemetry/opentelemetry-proto/blob/b3060d2104df364136d75a35779e6bd48bac449a/opentelemetry/proto/common/v1/common.proto#L73) field with the `target` from `LogRecord`, if target is populated. - Groups batch of `LogRecord` and `Span` by their resource and instrumentation scope before exporting, for better efficiency [#1873](https://github.com/open-telemetry/opentelemetry-rust/pull/1873). + ## v0.16.0 ### Fixed