Skip to content

Commit 4bc2d8c

Browse files
authored
Show working example for OTLP Http (#1756)
1 parent d2668e6 commit 4bc2d8c

File tree

4 files changed

+70
-151
lines changed

4 files changed

+70
-151
lines changed

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ publish = false
99
once_cell = { workspace = true }
1010
opentelemetry = { path = "../../../opentelemetry" }
1111
opentelemetry_sdk = { path = "../../../opentelemetry-sdk", features = ["rt-tokio", "metrics", "logs"] }
12-
opentelemetry-otlp = { path = "../..", features = ["http-proto", "http-json", "reqwest-client", "logs"] }
12+
opentelemetry-otlp = { path = "../..", features = ["http-proto", "reqwest-client", "logs"] }
1313
opentelemetry-appender-tracing = { path = "../../../opentelemetry-appender-tracing", default-features = false}
1414
opentelemetry-semantic-conventions = { path = "../../../opentelemetry-semantic-conventions" }
1515

1616
tokio = { workspace = true, features = ["full"] }
17-
tracing-subscriber = { workspace = true, features = ["registry", "std"] }
18-
tracing = { workspace = true, features = ["std"] }
19-
tracing-core = { workspace = true }
17+
opentelemetry-appender-log = { path = "../../../opentelemetry-appender-log", default-features = false}
18+
log = { workspace = true }

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

+57-135
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

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

99
## Usage
1010

@@ -47,208 +47,130 @@ You should be able to see something similar below with different time and ID in
4747
### Span
4848

4949
```text
50-
2023-09-08T21:50:35.884Z info ResourceSpans #0
50+
...
51+
2024-05-14T02:15:56.827Z info ResourceSpans #0
5152
Resource SchemaURL:
5253
Resource attributes:
53-
-> service.name: Str(basic-otlp-tracing-example)
54+
-> service.name: Str(basic-otlp-example)
5455
ScopeSpans #0
5556
ScopeSpans SchemaURL:
56-
InstrumentationScope ex.com/basic
57+
InstrumentationScope basic
58+
InstrumentationScope attributes:
59+
-> scope-key: Str(scope-value)
5760
Span #0
58-
Trace ID : f8e7ea4dcab43689cea14f708309d682
59-
Parent ID : 8b560e2e7238eab5
60-
ID : 9e36b48dc07b32fe
61+
Trace ID : 4467894e2d8d0c4165df1218160bc260
62+
Parent ID : 589ea953b6ec03a9
63+
ID : b2aa3c3a9c21e0d0
6164
Name : Sub operation...
6265
Kind : Internal
63-
Start time : 2023-09-08 21:50:35.872800345 +0000 UTC
64-
End time : 2023-09-08 21:50:35.87282574 +0000 UTC
66+
Start time : 2024-05-14 02:15:56.824239163 +0000 UTC
67+
End time : 2024-05-14 02:15:56.824244315 +0000 UTC
6568
Status code : Unset
6669
Status message :
6770
Attributes:
68-
-> lemons: Str(five)
71+
-> another.key: Str(yes)
6972
Events:
7073
SpanEvent #0
7174
-> Name: Sub span event
72-
-> Timestamp: 2023-09-08 21:50:35.872808684 +0000 UTC
75+
-> Timestamp: 2024-05-14 02:15:56.82424188 +0000 UTC
7376
-> DroppedAttributesCount: 0
7477
ResourceSpans #1
7578
Resource SchemaURL:
7679
Resource attributes:
77-
-> service.name: Str(basic-otlp-tracing-example)
80+
-> service.name: Str(basic-otlp-example)
7881
ScopeSpans #0
7982
ScopeSpans SchemaURL:
80-
InstrumentationScope ex.com/basic
83+
InstrumentationScope basic
84+
InstrumentationScope attributes:
85+
-> scope-key: Str(scope-value)
8186
Span #0
82-
Trace ID : f8e7ea4dcab43689cea14f708309d682
87+
Trace ID : 4467894e2d8d0c4165df1218160bc260
8388
Parent ID :
84-
ID : 8b560e2e7238eab5
85-
Name : operation
89+
ID : 589ea953b6ec03a9
90+
Name : Main operation
8691
Kind : Internal
87-
Start time : 2023-09-08 21:50:35.872735497 +0000 UTC
88-
End time : 2023-09-08 21:50:35.872832026 +0000 UTC
92+
Start time : 2024-05-14 02:15:56.824194899 +0000 UTC
93+
End time : 2024-05-14 02:15:56.824251136 +0000 UTC
8994
Status code : Unset
9095
Status message :
9196
Attributes:
92-
-> ex.com/another: Str(yes)
97+
-> another.key: Str(yes)
9398
Events:
9499
SpanEvent #0
95100
-> Name: Nice operation!
96-
-> Timestamp: 2023-09-08 21:50:35.872750123 +0000 UTC
101+
-> Timestamp: 2024-05-14 02:15:56.824201397 +0000 UTC
97102
-> DroppedAttributesCount: 0
98103
-> Attributes::
99104
-> bogons: Int(100)
100105
{"kind": "exporter", "data_type": "traces", "name": "logging"}
106+
...
101107
```
102108

103109
### Metric
104110

105111
```text
106-
2023-09-08T19:14:12.522Z info ResourceMetrics #0
112+
...
113+
2024-05-14T02:15:56.827Z info ResourceMetrics #0
107114
Resource SchemaURL:
108115
Resource attributes:
109-
-> service.name: Str(basic-otlp-metrics-example)
116+
-> service.name: Str(basic-otlp-example)
110117
ScopeMetrics #0
111-
ScopeMetrics SchemaURL:
112-
InstrumentationScope ex.com/basic
118+
ScopeMetrics SchemaURL: schema_url
119+
InstrumentationScope basic v1.0
120+
InstrumentationScope attributes:
121+
-> scope-key: Str(scope-value)
113122
Metric #0
114123
Descriptor:
115-
-> Name: ex.com.one
116-
-> Description: A gauge set to 1.0
117-
-> Unit:
118-
-> DataType: Gauge
119-
NumberDataPoints #0
120-
Data point attributes:
121-
-> A: Str(1)
122-
-> B: Str(2)
123-
-> C: Str(3)
124-
-> lemons: Int(10)
125-
StartTimestamp: 1970-01-01 00:00:00 +0000 UTC
126-
Timestamp: 2023-09-08 19:14:12.468030127 +0000 UTC
127-
Value: 1.000000
128-
Metric #1
129-
Descriptor:
130-
-> Name: ex.com.two
131-
-> Description:
132-
-> Unit:
133-
-> DataType: Histogram
124+
-> Name: test_counter
125+
-> Description: a simple counter for demo purposes.
126+
-> Unit: my_unit
127+
-> DataType: Sum
128+
-> IsMonotonic: true
134129
-> AggregationTemporality: Cumulative
135-
HistogramDataPoints #0
130+
NumberDataPoints #0
136131
Data point attributes:
137-
-> A: Str(1)
138-
-> B: Str(2)
139-
-> C: Str(3)
140-
-> lemons: Int(10)
141-
StartTimestamp: 2023-09-08 19:14:12.466896812 +0000 UTC
142-
Timestamp: 2023-09-08 19:14:12.468052807 +0000 UTC
143-
Count: 1
144-
Sum: 5.500000
145-
Min: 5.500000
146-
Max: 5.500000
147-
ExplicitBounds #0: 0.000000
148-
ExplicitBounds #1: 5.000000
149-
ExplicitBounds #2: 10.000000
150-
ExplicitBounds #3: 25.000000
151-
ExplicitBounds #4: 50.000000
152-
ExplicitBounds #5: 75.000000
153-
ExplicitBounds #6: 100.000000
154-
ExplicitBounds #7: 250.000000
155-
ExplicitBounds #8: 500.000000
156-
ExplicitBounds #9: 750.000000
157-
ExplicitBounds #10: 1000.000000
158-
ExplicitBounds #11: 2500.000000
159-
ExplicitBounds #12: 5000.000000
160-
ExplicitBounds #13: 7500.000000
161-
ExplicitBounds #14: 10000.000000
162-
Buckets #0, Count: 0
163-
Buckets #1, Count: 0
164-
Buckets #2, Count: 1
165-
Buckets #3, Count: 0
166-
Buckets #4, Count: 0
167-
Buckets #5, Count: 0
168-
Buckets #6, Count: 0
169-
Buckets #7, Count: 0
170-
Buckets #8, Count: 0
171-
Buckets #9, Count: 0
172-
Buckets #10, Count: 0
173-
Buckets #11, Count: 0
174-
Buckets #12, Count: 0
175-
Buckets #13, Count: 0
176-
Buckets #14, Count: 0
177-
Buckets #15, Count: 0
178-
HistogramDataPoints #1
179-
StartTimestamp: 2023-09-08 19:14:12.466896812 +0000 UTC
180-
Timestamp: 2023-09-08 19:14:12.468052807 +0000 UTC
181-
Count: 1
182-
Sum: 1.300000
183-
Min: 1.300000
184-
Max: 1.300000
185-
ExplicitBounds #0: 0.000000
186-
ExplicitBounds #1: 5.000000
187-
ExplicitBounds #2: 10.000000
188-
ExplicitBounds #3: 25.000000
189-
ExplicitBounds #4: 50.000000
190-
ExplicitBounds #5: 75.000000
191-
ExplicitBounds #6: 100.000000
192-
ExplicitBounds #7: 250.000000
193-
ExplicitBounds #8: 500.000000
194-
ExplicitBounds #9: 750.000000
195-
ExplicitBounds #10: 1000.000000
196-
ExplicitBounds #11: 2500.000000
197-
ExplicitBounds #12: 5000.000000
198-
ExplicitBounds #13: 7500.000000
199-
ExplicitBounds #14: 10000.000000
200-
Buckets #0, Count: 0
201-
Buckets #1, Count: 1
202-
Buckets #2, Count: 0
203-
Buckets #3, Count: 0
204-
Buckets #4, Count: 0
205-
Buckets #5, Count: 0
206-
Buckets #6, Count: 0
207-
Buckets #7, Count: 0
208-
Buckets #8, Count: 0
209-
Buckets #9, Count: 0
210-
Buckets #10, Count: 0
211-
Buckets #11, Count: 0
212-
Buckets #12, Count: 0
213-
Buckets #13, Count: 0
214-
Buckets #14, Count: 0
215-
Buckets #15, Count: 0
216-
{"kind": "exporter", "data_type": "metrics", "name": "logging"}
132+
-> test_key: Str(test_value)
133+
StartTimestamp: 2024-05-14 02:15:56.824127393 +0000 UTC
134+
Timestamp: 2024-05-14 02:15:56.825354918 +0000 UTC
135+
Value: 11
136+
...
217137
```
218138

219139
### Logs
220140

221141
```text
222-
2023-09-08T21:50:35.884Z info ResourceLog #0
142+
...
143+
2024-05-14T02:15:56.828Z info ResourceLog #0
223144
Resource SchemaURL:
224145
Resource attributes:
225-
-> service.name: Str(basic-otlp-logging-example)
146+
-> service.name: Str(basic-otlp-example)
226147
ScopeLogs #0
227148
ScopeLogs SchemaURL:
228-
InstrumentationScope opentelemetry-log-appender 0.1.0
149+
InstrumentationScope opentelemetry-log-appender 0.3.0
229150
LogRecord #0
230-
ObservedTimestamp: 2023-09-08 21:50:35.872759168 +0000 UTC
151+
ObservedTimestamp: 2024-05-14 02:15:56.824218088 +0000 UTC
231152
Timestamp: 1970-01-01 00:00:00 +0000 UTC
232153
SeverityText: INFO
233154
SeverityNumber: Info(9)
234155
Body: Str(hello from banana. My price is 2.99. I am also inside a Span!)
235-
Trace ID: f8e7ea4dcab43689cea14f708309d682
236-
Span ID: 8b560e2e7238eab5
156+
Trace ID: 4467894e2d8d0c4165df1218160bc260
157+
Span ID: 589ea953b6ec03a9
237158
Flags: 1
238159
ResourceLog #1
239160
Resource SchemaURL:
240161
Resource attributes:
241-
-> service.name: Str(basic-otlp-logging-example)
162+
-> service.name: Str(basic-otlp-example)
242163
ScopeLogs #0
243164
ScopeLogs SchemaURL:
244-
InstrumentationScope opentelemetry-log-appender 0.1.0
165+
InstrumentationScope opentelemetry-log-appender 0.3.0
245166
LogRecord #0
246-
ObservedTimestamp: 2023-09-08 21:50:35.872833713 +0000 UTC
167+
ObservedTimestamp: 2024-05-14 02:15:56.824254268 +0000 UTC
247168
Timestamp: 1970-01-01 00:00:00 +0000 UTC
248169
SeverityText: INFO
249170
SeverityNumber: Info(9)
250171
Body: Str(hello from apple. My price is 1.99)
251172
Trace ID:
252173
Span ID:
253174
Flags: 0
175+
...
254176
```

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
use log::{info, Level};
12
use once_cell::sync::Lazy;
23
use opentelemetry::{
34
global,
45
metrics::{MetricsError, Unit},
56
trace::{TraceContextExt, TraceError, Tracer, TracerProvider as _},
67
Key, KeyValue,
78
};
8-
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
9+
use opentelemetry_appender_log::OpenTelemetryLogBridge;
910
use opentelemetry_otlp::WithExportConfig;
1011
use opentelemetry_sdk::trace as sdktrace;
1112
use opentelemetry_sdk::{
@@ -14,8 +15,6 @@ use opentelemetry_sdk::{
1415
};
1516

1617
use std::error::Error;
17-
use tracing::info;
18-
use tracing_subscriber::prelude::*;
1918

2019
static RESOURCE: Lazy<Resource> = Lazy::new(|| {
2120
Resource::new(vec![KeyValue::new(
@@ -49,16 +48,12 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
4948
}
5049

5150
fn init_metrics() -> Result<opentelemetry_sdk::metrics::SdkMeterProvider, MetricsError> {
52-
let export_config = opentelemetry_otlp::ExportConfig {
53-
endpoint: "http://localhost:4318/v1/metrics".to_string(),
54-
..opentelemetry_otlp::ExportConfig::default()
55-
};
5651
let provider = opentelemetry_otlp::new_pipeline()
5752
.metrics(opentelemetry_sdk::runtime::Tokio)
5853
.with_exporter(
5954
opentelemetry_otlp::new_exporter()
6055
.http()
61-
.with_export_config(export_config),
56+
.with_endpoint("http://localhost:4318/v1/metrics"),
6257
)
6358
.with_resource(RESOURCE.clone())
6459
.build();
@@ -91,8 +86,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
9186
// provider on their own. Dropping logger providers will disable log
9287
// emitting.
9388
let logger_provider = init_logs().unwrap();
94-
let layer = OpenTelemetryTracingBridge::new(&logger_provider);
95-
tracing_subscriber::registry().with(layer).init();
89+
90+
// Create a new OpenTelemetryLogBridge using the above LoggerProvider.
91+
let otel_log_appender = OpenTelemetryLogBridge::new(&logger_provider);
92+
log::set_boxed_logger(Box::new(otel_log_appender)).unwrap();
93+
log::set_max_level(Level::Info.to_level_filter());
9694

9795
let common_scope_attributes = vec![KeyValue::new("scope-key", "scope-value")];
9896
let tracer = global::tracer_provider()

opentelemetry-otlp/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! order to support open-source telemetry data formats (e.g. Jaeger,
88
//! Prometheus, etc.) sending to multiple open-source or commercial back-ends.
99
//!
10-
//! Currently, this crate only support sending tracing data or metrics in OTLP
10+
//! Currently, this crate only support sending telemetry in OTLP
1111
//! via grpc and http (in binary format). Supports for other format and protocol
1212
//! will be added in the future. The details of what's currently offering in this
1313
//! crate can be found in this doc.
@@ -18,7 +18,7 @@
1818
//! you want to send data to:
1919
//!
2020
//! ```shell
21-
//! $ docker run -p 4317:4317 otel/opentelemetry-collector-dev:latest
21+
//! $ docker run -p 4317:4317 otel/opentelemetry-collector:latest
2222
//! ```
2323
//!
2424
//! Then install a new pipeline with the recommended defaults to start exporting

0 commit comments

Comments
 (0)