@@ -64,21 +64,129 @@ fn init_logs() -> opentelemetry_sdk::logs::LoggerProvider {
64
64
65
65
#[ cfg( feature = "trace" ) ]
66
66
fn emit_span ( ) {
67
- let tracer = global:: tracer ( "stdout-test" ) ;
68
- let mut span = tracer. start ( "test_span" ) ;
69
- span. set_attribute ( KeyValue :: new ( "test_key" , "test_value" ) ) ;
67
+ use opentelemetry:: trace:: {
68
+ SpanContext , SpanId , TraceFlags , TraceId , TraceState , TracerProvider ,
69
+ } ;
70
+
71
+ let tracer = global:: tracer_provider ( )
72
+ . tracer_builder ( "stdout-example" )
73
+ . with_version ( "v1" )
74
+ . with_schema_url ( "schema_url" )
75
+ . with_attributes ( [ KeyValue :: new ( "scope_key" , "scope_value" ) ] )
76
+ . build ( ) ;
77
+ let mut span = tracer. start ( "example-span" ) ;
78
+ span. set_attribute ( KeyValue :: new ( "attribute_key1" , "attribute_value1" ) ) ;
79
+ span. set_attribute ( KeyValue :: new ( "attribute_key2" , "attribute_value2" ) ) ;
70
80
span. add_event (
71
- "test_event" ,
72
- vec ! [ KeyValue :: new( "test_event_key" , "test_event_value" ) ] ,
81
+ "example-event-name" ,
82
+ vec ! [ KeyValue :: new( "event_attribute1" , "event_value1" ) ] ,
83
+ ) ;
84
+ span. add_link (
85
+ SpanContext :: new (
86
+ TraceId :: from_hex ( "58406520a006649127e371903a2de979" ) . expect ( "invalid" ) ,
87
+ SpanId :: from_hex ( "b6d7d7f6d7d6d7f6" ) . expect ( "invalid" ) ,
88
+ TraceFlags :: default ( ) ,
89
+ false ,
90
+ TraceState :: NONE ,
91
+ ) ,
92
+ vec ! [
93
+ KeyValue :: new( "link_attribute1" , "link_value1" ) ,
94
+ KeyValue :: new( "link_attribute2" , "link_value2" ) ,
95
+ ] ,
96
+ ) ;
97
+
98
+ span. add_link (
99
+ SpanContext :: new (
100
+ TraceId :: from_hex ( "23401120a001249127e371903f2de971" ) . expect ( "invalid" ) ,
101
+ SpanId :: from_hex ( "cd37d765d743d7f6" ) . expect ( "invalid" ) ,
102
+ TraceFlags :: default ( ) ,
103
+ false ,
104
+ TraceState :: NONE ,
105
+ ) ,
106
+ vec ! [
107
+ KeyValue :: new( "link_attribute1" , "link_value1" ) ,
108
+ KeyValue :: new( "link_attribute2" , "link_value2" ) ,
109
+ ] ,
73
110
) ;
74
111
span. end ( ) ;
75
112
}
76
113
77
114
#[ cfg( feature = "metrics" ) ]
78
115
fn emit_metrics ( ) {
79
- let meter = global:: meter ( "stdout-test" ) ;
80
- let c = meter. u64_counter ( "test_counter" ) . init ( ) ;
81
- c. add ( 1 , & [ KeyValue :: new ( "test_key" , "test_value" ) ] ) ;
116
+ let meter = global:: meter ( "stdout-example" ) ;
117
+ let c = meter. u64_counter ( "example_counter" ) . init ( ) ;
118
+ c. add (
119
+ 1 ,
120
+ & [
121
+ KeyValue :: new ( "name" , "apple" ) ,
122
+ KeyValue :: new ( "color" , "green" ) ,
123
+ ] ,
124
+ ) ;
125
+ c. add (
126
+ 1 ,
127
+ & [
128
+ KeyValue :: new ( "name" , "apple" ) ,
129
+ KeyValue :: new ( "color" , "green" ) ,
130
+ ] ,
131
+ ) ;
132
+ c. add (
133
+ 2 ,
134
+ & [
135
+ KeyValue :: new ( "name" , "apple" ) ,
136
+ KeyValue :: new ( "color" , "red" ) ,
137
+ ] ,
138
+ ) ;
139
+ c. add (
140
+ 1 ,
141
+ & [
142
+ KeyValue :: new ( "name" , "banana" ) ,
143
+ KeyValue :: new ( "color" , "yellow" ) ,
144
+ ] ,
145
+ ) ;
146
+ c. add (
147
+ 11 ,
148
+ & [
149
+ KeyValue :: new ( "name" , "banana" ) ,
150
+ KeyValue :: new ( "color" , "yellow" ) ,
151
+ ] ,
152
+ ) ;
153
+
154
+ let h = meter. f64_histogram ( "example_histogram" ) . init ( ) ;
155
+ h. record (
156
+ 1.0 ,
157
+ & [
158
+ KeyValue :: new ( "name" , "apple" ) ,
159
+ KeyValue :: new ( "color" , "green" ) ,
160
+ ] ,
161
+ ) ;
162
+ h. record (
163
+ 1.0 ,
164
+ & [
165
+ KeyValue :: new ( "name" , "apple" ) ,
166
+ KeyValue :: new ( "color" , "green" ) ,
167
+ ] ,
168
+ ) ;
169
+ h. record (
170
+ 2.0 ,
171
+ & [
172
+ KeyValue :: new ( "name" , "apple" ) ,
173
+ KeyValue :: new ( "color" , "red" ) ,
174
+ ] ,
175
+ ) ;
176
+ h. record (
177
+ 1.0 ,
178
+ & [
179
+ KeyValue :: new ( "name" , "banana" ) ,
180
+ KeyValue :: new ( "color" , "yellow" ) ,
181
+ ] ,
182
+ ) ;
183
+ h. record (
184
+ 11.0 ,
185
+ & [
186
+ KeyValue :: new ( "name" , "banana" ) ,
187
+ KeyValue :: new ( "color" , "yellow" ) ,
188
+ ] ,
189
+ ) ;
82
190
}
83
191
84
192
#[ cfg( feature = "logs" ) ]
@@ -101,17 +209,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
101
209
#[ cfg( feature = "logs" ) ]
102
210
emit_log ( ) ;
103
211
104
- println ! (
105
- "======================================================================================"
106
- ) ;
107
-
108
212
#[ cfg( feature = "trace" ) ]
109
213
emit_span ( ) ;
110
214
111
- println ! (
112
- "======================================================================================"
113
- ) ;
114
-
115
215
#[ cfg( feature = "metrics" ) ]
116
216
emit_metrics ( ) ;
117
217
0 commit comments