@@ -4,7 +4,7 @@ use once_cell::sync::Lazy;
4
4
use opentelemetry:: { global, KeyValue } ;
5
5
6
6
#[ cfg( feature = "trace" ) ]
7
- use opentelemetry:: trace:: { Span , Tracer } ;
7
+ use opentelemetry:: trace:: Tracer ;
8
8
9
9
#[ cfg( feature = "metrics" ) ]
10
10
use opentelemetry_sdk:: metrics:: { PeriodicReader , SdkMeterProvider } ;
@@ -60,52 +60,23 @@ fn init_logs() -> opentelemetry_sdk::logs::LoggerProvider {
60
60
61
61
#[ cfg( feature = "trace" ) ]
62
62
fn emit_span ( ) {
63
- use opentelemetry:: {
64
- trace:: { SpanContext , SpanId , TraceFlags , TraceId , TraceState } ,
65
- InstrumentationScope ,
66
- } ;
63
+ use opentelemetry:: { trace:: TraceContextExt , InstrumentationScope } ;
67
64
68
65
let scope = InstrumentationScope :: builder ( "stdout-example" )
69
66
. with_version ( "v1" )
70
67
. with_attributes ( [ KeyValue :: new ( "scope_key" , "scope_value" ) ] )
71
68
. build ( ) ;
72
69
73
70
let tracer = global:: tracer_with_scope ( scope) ;
74
- let mut span = tracer. start ( "example-span" ) ;
75
- span. set_attribute ( KeyValue :: new ( "attribute_key1" , "attribute_value1" ) ) ;
76
- span. set_attribute ( KeyValue :: new ( "attribute_key2" , "attribute_value2" ) ) ;
77
- span. add_event (
78
- "example-event-name" ,
79
- vec ! [ KeyValue :: new( "event_attribute1" , "event_value1" ) ] ,
80
- ) ;
81
- span. add_link (
82
- SpanContext :: new (
83
- TraceId :: from_hex ( "58406520a006649127e371903a2de979" ) . expect ( "invalid" ) ,
84
- SpanId :: from_hex ( "b6d7d7f6d7d6d7f6" ) . expect ( "invalid" ) ,
85
- TraceFlags :: default ( ) ,
86
- false ,
87
- TraceState :: NONE ,
88
- ) ,
89
- vec ! [
90
- KeyValue :: new( "link_attribute1" , "link_value1" ) ,
91
- KeyValue :: new( "link_attribute2" , "link_value2" ) ,
92
- ] ,
93
- ) ;
94
-
95
- span. add_link (
96
- SpanContext :: new (
97
- TraceId :: from_hex ( "23401120a001249127e371903f2de971" ) . expect ( "invalid" ) ,
98
- SpanId :: from_hex ( "cd37d765d743d7f6" ) . expect ( "invalid" ) ,
99
- TraceFlags :: default ( ) ,
100
- false ,
101
- TraceState :: NONE ,
102
- ) ,
103
- vec ! [
104
- KeyValue :: new( "link_attribute1" , "link_value1" ) ,
105
- KeyValue :: new( "link_attribute2" , "link_value2" ) ,
106
- ] ,
107
- ) ;
108
- span. end ( ) ;
71
+ tracer. in_span ( "example-span" , |cx| {
72
+ let span = cx. span ( ) ;
73
+ span. set_attribute ( KeyValue :: new ( "my-attribute" , "my-value" ) ) ;
74
+ span. add_event (
75
+ "example-event-name" ,
76
+ vec ! [ KeyValue :: new( "event_attribute1" , "event_value1" ) ] ,
77
+ ) ;
78
+ emit_log ( ) ;
79
+ } )
109
80
}
110
81
111
82
#[ cfg( feature = "metrics" ) ]
0 commit comments