@@ -81,14 +81,28 @@ fn compute_driver_rpc_operation(path: &str) -> (&'static str, &'static str) {
8181 }
8282}
8383
84- /// Build a tracer provider for the configured OTLP/gRPC endpoint.
84+ /// Build a tracer provider for the configured OTLP/gRPC endpoint and gateway .
8585#[ must_use]
86- pub fn provider_for ( endpoint : Option < & str > ) -> ( Option < SdkTracerProvider > , Option < SetupError > ) {
87- openshell_otel:: provider_for ( endpoint. map ( |endpoint| OtlpTraceConfig {
88- endpoint,
89- service_name : ServiceName :: Fixed ( SERVICE_NAME ) ,
90- service_version : Some ( openshell_core:: VERSION ) ,
91- resource_attributes : Vec :: new ( ) ,
86+ pub fn provider_for (
87+ endpoint : Option < & str > ,
88+ gateway_name : Option < & str > ,
89+ ) -> ( Option < SdkTracerProvider > , Option < SetupError > ) {
90+ openshell_otel:: provider_for ( endpoint. map ( |endpoint| {
91+ OtlpTraceConfig {
92+ endpoint,
93+ service_name : ServiceName :: Fixed ( SERVICE_NAME ) ,
94+ service_version : Some ( openshell_core:: VERSION ) ,
95+ resource_attributes : gateway_name
96+ . map ( str:: trim)
97+ . filter ( |name| !name. is_empty ( ) )
98+ . map ( |name| {
99+ vec ! [ opentelemetry:: KeyValue :: new(
100+ "openshell.gateway.name" ,
101+ name. to_string( ) ,
102+ ) ]
103+ } )
104+ . unwrap_or_default ( ) ,
105+ }
92106 } ) )
93107}
94108
@@ -115,6 +129,7 @@ mod tests {
115129 struct Received {
116130 spans : Vec < Span > ,
117131 service_names : Vec < String > ,
132+ gateway_names : Vec < String > ,
118133 }
119134
120135 #[ derive( Clone ) ]
@@ -133,18 +148,19 @@ mod tests {
133148 let mut received = self . received . lock ( ) . unwrap ( ) ;
134149 for resource_span in request. into_inner ( ) . resource_spans {
135150 if let Some ( resource) = resource_span. resource {
136- received. service_names . extend (
137- resource
138- . attributes
139- . into_iter ( )
140- . filter ( |attribute| attribute. key == "service.name" )
141- . filter_map ( |attribute| attribute. value )
142- . filter_map ( |value| value. value )
143- . filter_map ( |value| match value {
144- opentelemetry_proto:: tonic:: common:: v1:: any_value:: Value :: StringValue ( value) => Some ( value) ,
145- _ => None ,
146- } ) ,
147- ) ;
151+ for attribute in resource. attributes {
152+ let Some ( value) = attribute. value . and_then ( |value| value. value ) else {
153+ continue ;
154+ } ;
155+ let opentelemetry_proto:: tonic:: common:: v1:: any_value:: Value :: StringValue ( value) = value else {
156+ continue ;
157+ } ;
158+ match attribute. key . as_str ( ) {
159+ "service.name" => received. service_names . push ( value) ,
160+ "openshell.gateway.name" => received. gateway_names . push ( value) ,
161+ _ => { }
162+ }
163+ }
148164 }
149165 for scope_span in resource_span. scope_spans {
150166 received. spans . extend ( scope_span. spans ) ;
@@ -204,7 +220,7 @@ mod tests {
204220 }
205221
206222 #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
207- async fn vm_driver_spans_reach_otlp_collector_with_distinct_service_name ( ) {
223+ async fn vm_driver_spans_reach_otlp_collector_with_resource_identity ( ) {
208224 let received = Arc :: new ( Mutex :: new ( Received :: default ( ) ) ) ;
209225 let exported = Arc :: new ( tokio:: sync:: Notify :: new ( ) ) ;
210226 let listener = tokio:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
@@ -226,7 +242,10 @@ mod tests {
226242 . await
227243 } ) ;
228244
229- let ( provider, error) = super :: provider_for ( Some ( & format ! ( "http://{address}" ) ) ) ;
245+ let ( provider, error) = super :: provider_for (
246+ Some ( & format ! ( "http://{address}" ) ) ,
247+ Some ( "production-us-west" ) ,
248+ ) ;
230249 assert ! ( error. is_none( ) , "valid OTLP endpoint should configure" ) ;
231250 let provider = provider. expect ( "provider" ) ;
232251 let subscriber = tracing_subscriber:: registry ( ) . with ( super :: layer ( & provider) ) ;
@@ -265,5 +284,6 @@ mod tests {
265284 "VM spans should use a distinct service name, got {:?}" ,
266285 received. service_names
267286 ) ;
287+ assert_eq ! ( received. gateway_names, [ "production-us-west" ] ) ;
268288 }
269289}
0 commit comments