Skip to content

Commit 3882b22

Browse files
ramgdevcijothomaslalitb
authored
Update http example with json protocol (#1912)
Co-authored-by: Cijo Thomas <cijo.thomas@gmail.com> Co-authored-by: Lalit Kumar Bhasin <labhas@microsoft.com>
1 parent 224289e commit 3882b22

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

opentelemetry-otlp/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ tls-webpki-roots = ["tls", "tonic/tls-webpki-roots"]
7171

7272
# http binary
7373
http-proto = ["prost", "opentelemetry-http", "opentelemetry-proto/gen-tonic-messages", "http", "trace", "metrics"]
74-
# http json This does not work today due to known issue. See https://github.com/open-telemetry/opentelemetry-rust/issues/1763.
7574
http-json = ["serde_json", "prost", "opentelemetry-http", "opentelemetry-proto/gen-tonic-messages", "opentelemetry-proto/with-serde", "http", "trace", "metrics"]
7675
reqwest-blocking-client = ["reqwest/blocking", "opentelemetry-http/reqwest"]
7776
reqwest-client = ["reqwest", "opentelemetry-http/reqwest"]

opentelemetry-otlp/examples/basic-otlp-http/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ once_cell = { workspace = true }
1616
opentelemetry = { path = "../../../opentelemetry" }
1717
opentelemetry_sdk = { path = "../../../opentelemetry-sdk", features = ["rt-tokio", "metrics", "logs"] }
1818
opentelemetry-http = { path = "../../../opentelemetry-http", optional = true }
19-
opentelemetry-otlp = { path = "../..", features = ["http-proto", "reqwest-client", "logs"] }
19+
opentelemetry-otlp = { path = "../..", features = ["http-proto", "http-json", "reqwest-client", "logs"] }
2020
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false}
2121
opentelemetry-semantic-conventions = { path = "../../../opentelemetry-semantic-conventions" }
2222

opentelemetry-otlp/examples/basic-otlp-http/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Basic OTLP exporter Example
22

33
This example shows how to setup OpenTelemetry OTLP exporter for logs, metrics
4-
and traces to exports them to the [OpenTelemetry
4+
and traces to export them to the [OpenTelemetry
55
Collector](https://github.com/open-telemetry/opentelemetry-collector) via OTLP
6-
over HTTP/protobuf. The Collector then sends the data to the appropriate
6+
over selected protocol such as HTTP/protobuf or HTTP/json. The Collector then sends the data to the appropriate
77
backend, in this case, the logging Exporter, which displays data to console.
88

99
## Usage

opentelemetry-otlp/examples/basic-otlp-http/src/main.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ use opentelemetry::{
66
Key, KeyValue,
77
};
88
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
9+
use opentelemetry_otlp::Protocol;
910
use opentelemetry_otlp::{HttpExporterBuilder, WithExportConfig};
1011
use opentelemetry_sdk::trace::{self as sdktrace, Config};
1112
use opentelemetry_sdk::{
1213
logs::{self as sdklogs},
1314
Resource,
1415
};
16+
use std::error::Error;
1517
use tracing::info;
1618
use tracing_subscriber::prelude::*;
1719
use tracing_subscriber::EnvFilter;
1820

19-
use std::error::Error;
20-
2121
#[cfg(feature = "hyper")]
2222
mod hyper;
2323

@@ -39,22 +39,34 @@ fn init_logs() -> Result<sdklogs::LoggerProvider, opentelemetry::logs::LogError>
3939
opentelemetry_otlp::new_pipeline()
4040
.logging()
4141
.with_resource(RESOURCE.clone())
42-
.with_exporter(http_exporter().with_endpoint("http://localhost:4318/v1/logs"))
42+
.with_exporter(
43+
http_exporter()
44+
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
45+
.with_endpoint("http://localhost:4318/v1/logs"),
46+
)
4347
.install_batch(opentelemetry_sdk::runtime::Tokio)
4448
}
4549

4650
fn init_tracer_provider() -> Result<sdktrace::TracerProvider, TraceError> {
4751
opentelemetry_otlp::new_pipeline()
4852
.tracing()
49-
.with_exporter(http_exporter().with_endpoint("http://localhost:4318/v1/traces"))
53+
.with_exporter(
54+
http_exporter()
55+
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
56+
.with_endpoint("http://localhost:4318/v1/traces"),
57+
)
5058
.with_trace_config(Config::default().with_resource(RESOURCE.clone()))
5159
.install_batch(opentelemetry_sdk::runtime::Tokio)
5260
}
5361

5462
fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricsError> {
5563
opentelemetry_otlp::new_pipeline()
5664
.metrics(opentelemetry_sdk::runtime::Tokio)
57-
.with_exporter(http_exporter().with_endpoint("http://localhost:4318/v1/metrics"))
65+
.with_exporter(
66+
http_exporter()
67+
.with_protocol(Protocol::HttpBinary) //can be changed to `Protocol::HttpJson` to export in JSON format
68+
.with_endpoint("http://localhost:4318/v1/metrics"),
69+
)
5870
.with_resource(RESOURCE.clone())
5971
.build()
6072
}

0 commit comments

Comments
 (0)