@@ -25,14 +25,8 @@ use opentelemetry::{
2525 Context ,
2626 trace:: { SpanKind , TraceContextExt } ,
2727} ;
28- use opentelemetry_semantic_conventions:: {
29- attribute:: { OTEL_STATUS_CODE , OTEL_STATUS_DESCRIPTION } ,
30- trace:: {
31- CLIENT_ADDRESS , CLIENT_PORT , HTTP_REQUEST_HEADER , HTTP_REQUEST_METHOD ,
32- HTTP_RESPONSE_STATUS_CODE , HTTP_ROUTE , SERVER_ADDRESS , SERVER_PORT , URL_PATH , URL_QUERY ,
33- URL_SCHEME , USER_AGENT_ORIGINAL ,
34- } ,
35- } ;
28+ use opentelemetry_semantic_conventions as semconv;
29+ use opentelemetry_semantic_conventions:: trace:: HTTP_REQUEST_HEADER ;
3630use pin_project:: pin_project;
3731use snafu:: { ResultExt , Snafu } ;
3832use tower:: { Layer , Service } ;
@@ -416,21 +410,21 @@ impl SpanExt for Span {
416410 "HTTP request" ,
417411 { OTEL_NAME } = span_name,
418412 { OTEL_KIND } = ?SpanKind :: Server ,
419- { OTEL_STATUS_CODE } = Empty ,
413+ { semconv :: attribute :: OTEL_STATUS_CODE } = Empty ,
420414 // The current tracing-opentelemetry version still uses the old semantic convention
421415 // See https://github.com/tokio-rs/tracing-opentelemetry/pull/209
422- { OTEL_STATUS_DESCRIPTION } = Empty ,
423- { HTTP_REQUEST_METHOD } = http_method,
424- { HTTP_RESPONSE_STATUS_CODE } = Empty ,
425- { HTTP_ROUTE } = Empty ,
426- { URL_PATH } = url. path( ) ,
427- { URL_QUERY } = url. query( ) ,
428- { URL_SCHEME } = url. scheme_str( ) . unwrap_or_default( ) ,
429- { USER_AGENT_ORIGINAL } = Empty ,
430- { SERVER_ADDRESS } = Empty ,
431- { SERVER_PORT } = Empty ,
432- { CLIENT_ADDRESS } = Empty ,
433- { CLIENT_PORT } = Empty ,
416+ { semconv :: attribute :: OTEL_STATUS_DESCRIPTION } = Empty ,
417+ { semconv :: trace :: HTTP_REQUEST_METHOD } = http_method,
418+ { semconv :: trace :: HTTP_RESPONSE_STATUS_CODE } = Empty ,
419+ { semconv :: trace :: HTTP_ROUTE } = Empty ,
420+ { semconv :: trace :: URL_PATH } = url. path( ) ,
421+ { semconv :: trace :: URL_QUERY } = url. query( ) ,
422+ { semconv :: trace :: URL_SCHEME } = url. scheme_str( ) . unwrap_or_default( ) ,
423+ { semconv :: trace :: USER_AGENT_ORIGINAL } = Empty ,
424+ { semconv :: trace :: SERVER_ADDRESS } = Empty ,
425+ { semconv :: trace :: SERVER_PORT } = Empty ,
426+ { semconv :: trace :: CLIENT_ADDRESS } = Empty ,
427+ { semconv :: trace :: CLIENT_PORT } = Empty ,
434428 // TODO (@Techassi): Add network.protocol.version
435429 ) ;
436430
@@ -475,7 +469,7 @@ impl SpanExt for Span {
475469 }
476470
477471 if let Some ( user_agent) = req. user_agent ( ) {
478- span. record ( USER_AGENT_ORIGINAL , user_agent) ;
472+ span. record ( semconv :: trace :: USER_AGENT_ORIGINAL , user_agent) ;
479473 }
480474
481475 // Setting server.address and server.port
@@ -485,21 +479,27 @@ impl SpanExt for Span {
485479 // NOTE (@Techassi): We cast to i64, because otherwise the field
486480 // will NOT be recorded as a number but as a string. This is likely
487481 // an issue in the tracing-opentelemetry crate.
488- span. record ( SERVER_ADDRESS , host)
489- . record ( SERVER_PORT , port as i64 ) ;
482+ span. record ( semconv :: trace :: SERVER_ADDRESS , host)
483+ . record ( semconv :: trace :: SERVER_PORT , port as i64 ) ;
490484 }
491485
492486 // Setting fields according to the HTTP server semantic conventions
493487 // See https://opentelemetry.io/docs/specs/semconv/http/http-spans/#http-server-semantic-conventions
494488
495489 if let Some ( client_socket_address) = req. client_socket_address ( ) {
496- span. record ( CLIENT_ADDRESS , client_socket_address. ip ( ) . to_string ( ) ) ;
490+ span. record (
491+ semconv:: trace:: CLIENT_ADDRESS ,
492+ client_socket_address. ip ( ) . to_string ( ) ,
493+ ) ;
497494
498495 if opt_in {
499496 // NOTE (@Techassi): We cast to i64, because otherwise the field
500497 // will NOT be recorded as a number but as a string. This is
501498 // likely an issue in the tracing-opentelemetry crate.
502- span. record ( CLIENT_PORT , client_socket_address. port ( ) as i64 ) ;
499+ span. record (
500+ semconv:: trace:: CLIENT_PORT ,
501+ client_socket_address. port ( ) as i64 ,
502+ ) ;
503503 }
504504 }
505505
@@ -513,7 +513,7 @@ impl SpanExt for Span {
513513 // See: https://github.com/tokio-rs/tracing/issues/1343
514514
515515 if let Some ( http_route) = req. matched_path ( ) {
516- span. record ( HTTP_ROUTE , http_route. as_str ( ) ) ;
516+ span. record ( semconv :: trace :: HTTP_ROUTE , http_route. as_str ( ) ) ;
517517 }
518518
519519 span
@@ -542,15 +542,18 @@ impl SpanExt for Span {
542542 // NOTE (@Techassi): We cast to i64, because otherwise the field will
543543 // NOT be recorded as a number but as a string. This is likely an issue
544544 // in the tracing-opentelemetry crate.
545- self . record ( HTTP_RESPONSE_STATUS_CODE , status_code. as_u16 ( ) as i64 ) ;
545+ self . record (
546+ semconv:: trace:: HTTP_RESPONSE_STATUS_CODE ,
547+ status_code. as_u16 ( ) as i64 ,
548+ ) ;
546549
547550 // Only set the span status to "Error" when we encountered an server
548551 // error. See:
549552 //
550553 // - https://opentelemetry.io/docs/specs/semconv/http/http-spans/#status
551554 // - https://github.com/open-telemetry/opentelemetry-specification/blob/v1.26.0/specification/trace/api.md#set-status
552555 if status_code. is_server_error ( ) {
553- self . record ( OTEL_STATUS_CODE , "Error" ) ;
556+ self . record ( semconv :: attribute :: OTEL_STATUS_CODE , "Error" ) ;
554557 // NOTE (@Techassi): Can we add a status_description here as well?
555558 }
556559
@@ -564,8 +567,11 @@ impl SpanExt for Span {
564567 {
565568 // NOTE (@Techassi): This field might get renamed: https://github.com/tokio-rs/tracing-opentelemetry/issues/115
566569 // NOTE (@Techassi): It got renamed, a fixed version of tracing-opentelemetry is not available yet
567- self . record ( OTEL_STATUS_CODE , "Error" )
568- . record ( OTEL_STATUS_DESCRIPTION , error. to_string ( ) ) ;
570+ self . record ( semconv:: attribute:: OTEL_STATUS_CODE , "Error" )
571+ . record (
572+ semconv:: attribute:: OTEL_STATUS_DESCRIPTION ,
573+ error. to_string ( ) ,
574+ ) ;
569575 }
570576}
571577
0 commit comments