@@ -23,6 +23,11 @@ use mas_tower::{
23
23
TraceLayer , TraceService ,
24
24
} ;
25
25
use opentelemetry:: KeyValue ;
26
+ use opentelemetry_semantic_conventions:: trace:: {
27
+ CLIENT_ADDRESS , CLIENT_PORT , HTTP_REQUEST_BODY_SIZE , HTTP_REQUEST_METHOD ,
28
+ HTTP_RESPONSE_BODY_SIZE , HTTP_RESPONSE_STATUS_CODE , NETWORK_PROTOCOL_NAME , NETWORK_TRANSPORT ,
29
+ NETWORK_TYPE , SERVER_ADDRESS , SERVER_PORT , URL_FULL , USER_AGENT_ORIGINAL ,
30
+ } ;
26
31
use tower:: {
27
32
limit:: { ConcurrencyLimit , GlobalConcurrencyLimitLayer } ,
28
33
Layer ,
@@ -69,27 +74,25 @@ impl<B> MakeSpan<Request<B>> for MakeSpanForRequest {
69
74
. typed_get :: < UserAgent > ( )
70
75
. map ( tracing:: field:: display) ;
71
76
let content_length = headers. typed_get ( ) . map ( |ContentLength ( len) | len) ;
72
- let net_sock_peer_name = request. uri ( ) . host ( ) ;
73
77
let category = self . category . unwrap_or ( "UNSET" ) ;
74
78
75
79
tracing:: info_span!(
76
80
"http.client.request" ,
77
81
"otel.kind" = "client" ,
78
82
"otel.status_code" = tracing:: field:: Empty ,
79
- "http.method" = %request. method( ) ,
80
- "http.url" = %request. uri( ) ,
81
- "http.status_code" = tracing:: field:: Empty ,
82
- "http.host" = host,
83
- "http.request_content_length" = content_length,
84
- "http.response_content_length" = tracing:: field:: Empty ,
85
- "net.transport" = "ip_tcp" ,
86
- "net.sock.family" = tracing:: field:: Empty ,
87
- "net.sock.peer.name" = net_sock_peer_name,
88
- "net.sock.peer.addr" = tracing:: field:: Empty ,
89
- "net.sock.peer.port" = tracing:: field:: Empty ,
90
- "net.sock.host.addr" = tracing:: field:: Empty ,
91
- "net.sock.host.port" = tracing:: field:: Empty ,
92
- "user_agent.original" = user_agent,
83
+ { HTTP_REQUEST_METHOD } = %request. method( ) ,
84
+ { URL_FULL } = %request. uri( ) ,
85
+ { HTTP_RESPONSE_STATUS_CODE } = tracing:: field:: Empty ,
86
+ { SERVER_ADDRESS } = host,
87
+ { HTTP_REQUEST_BODY_SIZE } = content_length,
88
+ { HTTP_RESPONSE_BODY_SIZE } = tracing:: field:: Empty ,
89
+ { NETWORK_TRANSPORT } = "tcp" ,
90
+ { NETWORK_TYPE } = tracing:: field:: Empty ,
91
+ { SERVER_ADDRESS } = tracing:: field:: Empty ,
92
+ { SERVER_PORT } = tracing:: field:: Empty ,
93
+ { CLIENT_ADDRESS } = tracing:: field:: Empty ,
94
+ { CLIENT_PORT } = tracing:: field:: Empty ,
95
+ { USER_AGENT_ORIGINAL } = user_agent,
93
96
"rust.error" = tracing:: field:: Empty ,
94
97
"mas.category" = category,
95
98
)
@@ -102,22 +105,22 @@ pub struct EnrichSpanOnResponse;
102
105
impl < B > EnrichSpan < Response < B > > for EnrichSpanOnResponse {
103
106
fn enrich_span ( & self , span : & Span , response : & Response < B > ) {
104
107
span. record ( "otel.status_code" , "OK" ) ;
105
- span. record ( "http.status_code" , response. status ( ) . as_u16 ( ) ) ;
108
+ span. record ( HTTP_RESPONSE_STATUS_CODE , response. status ( ) . as_u16 ( ) ) ;
106
109
107
110
if let Some ( ContentLength ( content_length) ) = response. headers ( ) . typed_get ( ) {
108
- span. record ( "http.response_content_length" , content_length) ;
111
+ span. record ( HTTP_RESPONSE_BODY_SIZE , content_length) ;
109
112
}
110
113
111
114
if let Some ( http_info) = response. extensions ( ) . get :: < HttpInfo > ( ) {
112
115
let local = http_info. local_addr ( ) ;
113
116
let remote = http_info. remote_addr ( ) ;
114
117
115
- let family = if local. is_ipv4 ( ) { "inet " } else { "inet6 " } ;
116
- span. record ( "net.sock.family" , family) ;
117
- span. record ( "net.sock.peer.addr" , remote. ip ( ) . to_string ( ) ) ;
118
- span. record ( "net.sock.peer.port" , remote. port ( ) ) ;
119
- span. record ( "net.sock.host.addr" , local. ip ( ) . to_string ( ) ) ;
120
- span. record ( "net.sock.host.port" , local. port ( ) ) ;
118
+ let family = if local. is_ipv4 ( ) { "ipv4 " } else { "ipv6 " } ;
119
+ span. record ( NETWORK_TYPE , family) ;
120
+ span. record ( CLIENT_ADDRESS , remote. ip ( ) . to_string ( ) ) ;
121
+ span. record ( CLIENT_PORT , remote. port ( ) ) ;
122
+ span. record ( SERVER_ADDRESS , local. ip ( ) . to_string ( ) ) ;
123
+ span. record ( SERVER_PORT , local. port ( ) ) ;
121
124
} else {
122
125
tracing:: warn!( "No HttpInfo injected in response extensions" ) ;
123
126
}
@@ -149,8 +152,8 @@ where
149
152
type Iter < ' a > = std:: array:: IntoIter < KeyValue , 3 > ;
150
153
fn attributes < ' a > ( & ' a self , t : & ' a Request < B > ) -> Self :: Iter < ' a > {
151
154
[
152
- KeyValue :: new ( "http.request.method" , t. method ( ) . as_str ( ) . to_owned ( ) ) ,
153
- KeyValue :: new ( "network.protocol.name" , "http" ) ,
155
+ KeyValue :: new ( HTTP_REQUEST_METHOD , t. method ( ) . as_str ( ) . to_owned ( ) ) ,
156
+ KeyValue :: new ( NETWORK_PROTOCOL_NAME , "http" ) ,
154
157
KeyValue :: new ( "mas.category" , self . category . unwrap_or ( "UNSET" ) ) ,
155
158
]
156
159
. into_iter ( )
@@ -167,7 +170,7 @@ where
167
170
type Iter < ' a > = std:: iter:: Once < KeyValue > ;
168
171
fn attributes < ' a > ( & ' a self , t : & ' a Response < B > ) -> Self :: Iter < ' a > {
169
172
std:: iter:: once ( KeyValue :: new (
170
- "http.response.status_code" ,
173
+ HTTP_RESPONSE_STATUS_CODE ,
171
174
i64:: from ( t. status ( ) . as_u16 ( ) ) ,
172
175
) )
173
176
}
0 commit comments