@@ -5,10 +5,10 @@ use opentelemetry::trace::{
5
5
use opentelemetry:: { global, KeyValue } ;
6
6
use opentelemetry_otlp:: Protocol ;
7
7
use opentelemetry_otlp:: WithExportConfig ;
8
- use opentelemetry_sdk:: trace:: { Config , BatchConfigBuilder } ;
8
+ use opentelemetry_sdk:: trace:: { Config , BatchConfigBuilder , Sampler } ;
9
9
use opentelemetry_sdk:: { runtime, Resource } ;
10
10
use std:: ffi:: { CStr , CString } ;
11
- use std:: ptr;
11
+ use std:: { ptr, time } ;
12
12
use std:: ptr:: addr_of;
13
13
use std:: slice;
14
14
use std:: sync:: { Arc , OnceLock } ;
@@ -17,7 +17,7 @@ use tokio::sync::mpsc::{Receiver, Sender};
17
17
18
18
19
19
const TRACEPARENT_HEADER_LEN : u8 = 55 ;
20
-
20
+ const TIMEOUT : time :: Duration = std :: time :: Duration :: from_secs ( 10 ) ;
21
21
22
22
#[ repr( C ) ]
23
23
pub struct nxt_str_t {
@@ -64,6 +64,7 @@ unsafe fn nxt_otel_rs_init(
64
64
log_callback : unsafe extern "C" fn ( * mut i8 ) ,
65
65
endpoint : * const nxt_str_t ,
66
66
protocol : * const nxt_str_t ,
67
+ sample_fraction : f64 ,
67
68
batch_size : f64
68
69
) {
69
70
if endpoint. is_null ( ) ||
@@ -110,6 +111,7 @@ unsafe fn nxt_otel_rs_init(
110
111
ep,
111
112
proto,
112
113
batch_size,
114
+ sample_fraction,
113
115
rx
114
116
) ) ;
115
117
} ,
@@ -132,16 +134,21 @@ async unsafe fn nxt_otel_rs_runtime(
132
134
endpoint : String ,
133
135
proto : Protocol ,
134
136
batch_size : f64 ,
137
+ sample_fraction : f64 ,
135
138
mut rx : Receiver < SpanMessage >
136
139
) {
137
140
let pipeline = opentelemetry_otlp:: new_pipeline ( )
138
141
. tracing ( )
139
- . with_trace_config ( Config :: default ( ) . with_resource (
140
- Resource :: new ( vec ! [ KeyValue :: new(
141
- opentelemetry_semantic_conventions:: resource:: SERVICE_NAME ,
142
- "NGINX Unit" ,
143
- ) ] ) ,
144
- ) )
142
+ . with_trace_config (
143
+ Config :: default ( )
144
+ . with_resource (
145
+ Resource :: new ( vec ! [ KeyValue :: new(
146
+ opentelemetry_semantic_conventions:: resource:: SERVICE_NAME ,
147
+ "NGINX Unit" ,
148
+ ) ] )
149
+ )
150
+ . with_sampler ( Sampler :: TraceIdRatioBased ( sample_fraction) )
151
+ )
145
152
. with_batch_config (
146
153
BatchConfigBuilder :: default ( )
147
154
. with_max_export_batch_size ( batch_size as _ )
@@ -156,15 +163,15 @@ async unsafe fn nxt_otel_rs_runtime(
156
163
. with_http_client ( reqwest:: Client :: new ( ) ) // needed because rustls feature
157
164
. with_endpoint ( endpoint)
158
165
. with_protocol ( proto)
159
- . with_timeout ( std :: time :: Duration :: new ( 10 , 0 ) )
166
+ . with_timeout ( TIMEOUT )
160
167
) . install_batch ( runtime:: Tokio ) ,
161
168
Protocol :: Grpc => pipeline
162
169
. with_exporter (
163
170
opentelemetry_otlp:: new_exporter ( )
164
171
. tonic ( )
165
172
. with_endpoint ( endpoint)
166
173
. with_protocol ( proto)
167
- . with_timeout ( std :: time :: Duration :: new ( 10 , 0 ) )
174
+ . with_timeout ( TIMEOUT )
168
175
) . install_batch ( runtime:: Tokio ) ,
169
176
} ;
170
177
@@ -211,7 +218,7 @@ pub unsafe fn nxt_otel_rs_copy_traceparent(buf: *mut i8, span: *const BoxedSpan)
211
218
( * span) . span_context( ) . trace_flags( ) // 1 char, 2 hex
212
219
) ;
213
220
214
- assert_eq ! ( traceparent. len( ) , TRACEPARENT_HEADER_LEN as _ ) ;
221
+ assert_eq ! ( traceparent. len( ) , TRACEPARENT_HEADER_LEN as usize ) ;
215
222
216
223
ptr:: copy_nonoverlapping (
217
224
traceparent. as_bytes ( ) . as_ptr ( ) ,
@@ -270,8 +277,7 @@ pub unsafe fn nxt_otel_rs_get_or_create_trace(trace_id: *mut i8) -> *mut BoxedSp
270
277
}
271
278
272
279
#[ no_mangle]
273
- #[ tokio:: main]
274
- pub async unsafe fn nxt_otel_rs_send_trace ( trace : * mut BoxedSpan ) {
280
+ pub unsafe fn nxt_otel_rs_send_trace ( trace : * mut BoxedSpan ) {
275
281
// damage nothing on an improper call
276
282
if trace. is_null ( ) {
277
283
eprintln ! ( "trace was null, returning" ) ;
0 commit comments