@@ -80,14 +80,28 @@ fn compute_driver_rpc_operation(path: &str) -> (&'static str, &'static str) {
8080 }
8181}
8282
83- /// Build a tracer provider for the configured OTLP/gRPC endpoint.
83+ /// Build a tracer provider for the configured OTLP/gRPC endpoint and gateway .
8484#[ must_use]
85- pub fn provider_for ( endpoint : Option < & str > ) -> ( Option < SdkTracerProvider > , Option < SetupError > ) {
86- openshell_otel:: provider_for ( endpoint. map ( |endpoint| OtlpTraceConfig {
87- endpoint,
88- service_name : ServiceName :: Fixed ( SERVICE_NAME ) ,
89- service_version : Some ( openshell_core:: VERSION ) ,
90- resource_attributes : Vec :: new ( ) ,
85+ pub fn provider_for (
86+ endpoint : Option < & str > ,
87+ gateway_name : Option < & str > ,
88+ ) -> ( Option < SdkTracerProvider > , Option < SetupError > ) {
89+ openshell_otel:: provider_for ( endpoint. map ( |endpoint| {
90+ OtlpTraceConfig {
91+ endpoint,
92+ service_name : ServiceName :: Fixed ( SERVICE_NAME ) ,
93+ service_version : Some ( openshell_core:: VERSION ) ,
94+ resource_attributes : gateway_name
95+ . map ( str:: trim)
96+ . filter ( |name| !name. is_empty ( ) )
97+ . map ( |name| {
98+ vec ! [ opentelemetry:: KeyValue :: new(
99+ "openshell.gateway.name" ,
100+ name. to_string( ) ,
101+ ) ]
102+ } )
103+ . unwrap_or_default ( ) ,
104+ }
91105 } ) )
92106}
93107
@@ -114,6 +128,7 @@ mod tests {
114128 struct Received {
115129 spans : Vec < Span > ,
116130 service_names : Vec < String > ,
131+ gateway_names : Vec < String > ,
117132 }
118133
119134 #[ derive( Clone ) ]
@@ -132,18 +147,19 @@ mod tests {
132147 let mut received = self . received . lock ( ) . unwrap ( ) ;
133148 for resource_span in request. into_inner ( ) . resource_spans {
134149 if let Some ( resource) = resource_span. resource {
135- received. service_names . extend (
136- resource
137- . attributes
138- . into_iter ( )
139- . filter ( |attribute| attribute. key == "service.name" )
140- . filter_map ( |attribute| attribute. value )
141- . filter_map ( |value| value. value )
142- . filter_map ( |value| match value {
143- opentelemetry_proto:: tonic:: common:: v1:: any_value:: Value :: StringValue ( value) => Some ( value) ,
144- _ => None ,
145- } ) ,
146- ) ;
150+ for attribute in resource. attributes {
151+ let Some ( value) = attribute. value . and_then ( |value| value. value ) else {
152+ continue ;
153+ } ;
154+ let opentelemetry_proto:: tonic:: common:: v1:: any_value:: Value :: StringValue ( value) = value else {
155+ continue ;
156+ } ;
157+ match attribute. key . as_str ( ) {
158+ "service.name" => received. service_names . push ( value) ,
159+ "openshell.gateway.name" => received. gateway_names . push ( value) ,
160+ _ => { }
161+ }
162+ }
147163 }
148164 for scope_span in resource_span. scope_spans {
149165 received. spans . extend ( scope_span. spans ) ;
@@ -202,7 +218,7 @@ mod tests {
202218 }
203219
204220 #[ tokio:: test( flavor = "multi_thread" , worker_threads = 2 ) ]
205- async fn vm_driver_spans_reach_otlp_collector_with_distinct_service_name ( ) {
221+ async fn vm_driver_spans_reach_otlp_collector_with_resource_identity ( ) {
206222 let received = Arc :: new ( Mutex :: new ( Received :: default ( ) ) ) ;
207223 let exported = Arc :: new ( tokio:: sync:: Notify :: new ( ) ) ;
208224 let listener = tokio:: net:: TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
@@ -224,7 +240,10 @@ mod tests {
224240 . await
225241 } ) ;
226242
227- let ( provider, error) = super :: provider_for ( Some ( & format ! ( "http://{address}" ) ) ) ;
243+ let ( provider, error) = super :: provider_for (
244+ Some ( & format ! ( "http://{address}" ) ) ,
245+ Some ( "production-us-west" ) ,
246+ ) ;
228247 assert ! ( error. is_none( ) , "valid OTLP endpoint should configure" ) ;
229248 let provider = provider. expect ( "provider" ) ;
230249 let subscriber = tracing_subscriber:: registry ( ) . with ( super :: layer ( & provider) ) ;
@@ -263,5 +282,6 @@ mod tests {
263282 "VM spans should use a distinct service name, got {:?}" ,
264283 received. service_names
265284 ) ;
285+ assert_eq ! ( received. gateway_names, [ "production-us-west" ] ) ;
266286 }
267287}
0 commit comments