@@ -88,14 +88,28 @@ fn compute_driver_rpc_operation(path: &str) -> (&'static str, &'static str) {
8888 }
8989}
9090
91- /// Build a tracer provider for the configured OTLP/gRPC endpoint.
91+ /// Build a tracer provider for the configured OTLP/gRPC endpoint and gateway .
9292#[ must_use]
93- pub fn provider_for ( endpoint : Option < & str > ) -> ( Option < SdkTracerProvider > , Option < SetupError > ) {
94- openshell_otel:: provider_for ( endpoint. map ( |endpoint| OtlpTraceConfig {
95- endpoint,
96- service_name : ServiceName :: Fixed ( SERVICE_NAME ) ,
97- service_version : Some ( openshell_core:: VERSION ) ,
98- resource_attributes : Vec :: new ( ) ,
93+ pub fn provider_for (
94+ endpoint : Option < & str > ,
95+ gateway_name : Option < & str > ,
96+ ) -> ( Option < SdkTracerProvider > , Option < SetupError > ) {
97+ openshell_otel:: provider_for ( endpoint. map ( |endpoint| {
98+ OtlpTraceConfig {
99+ endpoint,
100+ service_name : ServiceName :: Fixed ( SERVICE_NAME ) ,
101+ service_version : Some ( openshell_core:: VERSION ) ,
102+ resource_attributes : gateway_name
103+ . map ( str:: trim)
104+ . filter ( |name| !name. is_empty ( ) )
105+ . map ( |name| {
106+ vec ! [ opentelemetry:: KeyValue :: new(
107+ "openshell.gateway.name" ,
108+ name. to_string( ) ,
109+ ) ]
110+ } )
111+ . unwrap_or_default ( ) ,
112+ }
99113 } ) )
100114}
101115
@@ -122,6 +136,7 @@ mod tests {
122136 struct Received {
123137 spans : Vec < Span > ,
124138 service_names : Vec < String > ,
139+ gateway_names : Vec < String > ,
125140 }
126141
127142 #[ derive( Clone ) ]
@@ -140,18 +155,19 @@ mod tests {
140155 let mut received = self . received . lock ( ) . unwrap ( ) ;
141156 for resource_span in request. into_inner ( ) . resource_spans {
142157 if let Some ( resource) = resource_span. resource {
143- received. service_names . extend (
144- resource
145- . attributes
146- . into_iter ( )
147- . filter ( |attribute| attribute. key == "service.name" )
148- . filter_map ( |attribute| attribute. value )
149- . filter_map ( |value| value. value )
150- . filter_map ( |value| match value {
151- opentelemetry_proto:: tonic:: common:: v1:: any_value:: Value :: StringValue ( value) => Some ( value) ,
152- _ => None ,
153- } ) ,
154- ) ;
158+ for attribute in resource. attributes {
159+ let Some ( value) = attribute. value . and_then ( |value| value. value ) else {
160+ continue ;
161+ } ;
162+ let opentelemetry_proto:: tonic:: common:: v1:: any_value:: Value :: StringValue ( value) = value else {
163+ continue ;
164+ } ;
165+ match attribute. key . as_str ( ) {
166+ "service.name" => received. service_names . push ( value) ,
167+ "openshell.gateway.name" => received. gateway_names . push ( value) ,
168+ _ => { }
169+ }
170+ }
155171 }
156172 for scope_span in resource_span. scope_spans {
157173 received. spans . extend ( scope_span. spans ) ;
@@ -211,7 +227,7 @@ mod tests {
211227 }
212228
213229 #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
214- async fn vm_driver_spans_reach_otlp_collector_with_distinct_service_name ( ) {
230+ async fn vm_driver_spans_reach_otlp_collector_with_resource_identity ( ) {
215231 let received = Arc :: new ( Mutex :: new ( Received :: default ( ) ) ) ;
216232 let exported = Arc :: new ( tokio:: sync:: Notify :: new ( ) ) ;
217233 let listener = tokio:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
@@ -233,7 +249,10 @@ mod tests {
233249 . await
234250 } ) ;
235251
236- let ( provider, error) = super :: provider_for ( Some ( & format ! ( "http://{address}" ) ) ) ;
252+ let ( provider, error) = super :: provider_for (
253+ Some ( & format ! ( "http://{address}" ) ) ,
254+ Some ( "production-us-west" ) ,
255+ ) ;
237256 assert ! ( error. is_none( ) , "valid OTLP endpoint should configure" ) ;
238257 let provider = provider. expect ( "provider" ) ;
239258 let subscriber = tracing_subscriber:: registry ( ) . with ( super :: layer ( & provider) ) ;
@@ -272,5 +291,6 @@ mod tests {
272291 "VM spans should use a distinct service name, got {:?}" ,
273292 received. service_names
274293 ) ;
294+ assert_eq ! ( received. gateway_names, [ "production-us-west" ] ) ;
275295 }
276296}
0 commit comments