Skip to content

Commit 152daee

Browse files
authored
Update basic-otlp example to show how to set service name (open-telemetry#745)
1 parent 32e5421 commit 152daee

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

examples/basic-otlp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ futures-util = { version = "0.3", default-features = false, features = ["std"] }
99
lazy_static = "1.4"
1010
opentelemetry = { path = "../../opentelemetry", features = ["rt-tokio", "metrics"] }
1111
opentelemetry-otlp = { path = "../../opentelemetry-otlp", features = ["tonic", "metrics"] }
12+
opentelemetry-semantic-conventions = { path = "../../opentelemetry-semantic-conventions" }
1213
serde_json = "1.0"
1314
tokio = { version = "1.0", features = ["full"] }

examples/basic-otlp/README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,47 @@ You should be able to see something similar below with different time and ID in
2020

2121
```
2222
Resource labels:
23-
-> service.name: STRING(unknown_service)
23+
-> service.name: STRING(trace-demo)
2424
InstrumentationLibrarySpans #0
2525
InstrumentationLibrary
2626
Span #0
27-
Trace ID : e60184475358d966b0880ec1cc1cf515
28-
Parent ID : 7b74443b55e404ba
29-
ID : f8e4a037c0fa63bd
27+
Trace ID : 737d9c966e8250475f400776228c0044
28+
Parent ID : ade62a071825f2db
29+
ID : 7aa9ea5f24e0444c
3030
Name : Sub operation...
3131
Kind : SPAN_KIND_INTERNAL
32-
Start time : 2021-11-19 04:07:46.29597 +0000 UTC
33-
End time : 2021-11-19 04:07:46.295997 +0000 UTC
32+
Start time : 2022-02-24 04:59:57.218995 +0000 UTC
33+
End time : 2022-02-24 04:59:57.219022 +0000 UTC
3434
Status code : STATUS_CODE_UNSET
3535
Status message :
3636
Attributes:
3737
-> lemons: STRING(five)
3838
Events:
3939
SpanEvent #0
4040
-> Name: Sub span event
41-
-> Timestamp: 2021-11-19 04:07:46.295982 +0000 UTC
41+
-> Timestamp: 2022-02-24 04:59:57.219012 +0000 UTC
4242
-> DroppedAttributesCount: 0
4343
ResourceSpans #1
4444
Resource labels:
45-
-> service.name: STRING(unknown_service)
45+
-> service.name: STRING(trace-demo)
4646
InstrumentationLibrarySpans #0
4747
InstrumentationLibrary
4848
Span #0
49-
Trace ID : e60184475358d966b0880ec1cc1cf515
49+
Trace ID : 737d9c966e8250475f400776228c0044
5050
Parent ID :
51-
ID : 7b74443b55e404ba
51+
ID : ade62a071825f2db
5252
Name : operation
5353
Kind : SPAN_KIND_INTERNAL
54-
Start time : 2021-11-19 04:07:46.295898 +0000 UTC
55-
End time : 2021-11-19 04:07:46.296015 +0000 UTC
54+
Start time : 2022-02-24 04:59:57.218877 +0000 UTC
55+
End time : 2022-02-24 04:59:57.219043 +0000 UTC
5656
Status code : STATUS_CODE_UNSET
5757
Status message :
5858
Attributes:
5959
-> ex.com/another: STRING(yes)
6060
Events:
6161
SpanEvent #0
6262
-> Name: Nice operation!
63-
-> Timestamp: 2021-11-19 04:07:46.295915 +0000 UTC
63+
-> Timestamp: 2022-02-24 04:59:57.218896 +0000 UTC
6464
-> DroppedAttributesCount: 0
6565
-> Attributes:
6666
-> bogons: INT(100)

examples/basic-otlp/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use futures_util::{Stream, StreamExt as _};
22
use opentelemetry::global::shutdown_tracer_provider;
33
use opentelemetry::sdk::metrics::{selectors, PushController};
4+
use opentelemetry::sdk::Resource;
45
use opentelemetry::trace::TraceError;
56
use opentelemetry::{
67
baggage::BaggageExt,
@@ -21,6 +22,12 @@ fn init_tracer() -> Result<sdktrace::Tracer, TraceError> {
2122
.tonic()
2223
.with_endpoint("http://localhost:4317"),
2324
)
25+
.with_trace_config(
26+
sdktrace::config().with_resource(Resource::new(vec![KeyValue::new(
27+
opentelemetry_semantic_conventions::resource::SERVICE_NAME,
28+
"trace-demo",
29+
)])),
30+
)
2431
.install_batch(opentelemetry::runtime::Tokio)
2532
}
2633

0 commit comments

Comments
 (0)