@@ -20,6 +20,8 @@ internal sealed class DiagnosticsMiddleware : OwinMiddleware
2020 private static readonly Func < IOwinRequest , string , IEnumerable < string > > OwinRequestHeaderValuesGetter
2121 = ( request , name ) => request . Headers . GetValues ( name ) ;
2222
23+ private static readonly RequestDataHelper RequestDataHelper = new ( configureByHttpKnownMethodsEnvironmentalVariable : false ) ;
24+
2325 /// <summary>
2426 /// Initializes a new instance of the <see cref="DiagnosticsMiddleware"/> class.
2527 /// </summary>
@@ -84,39 +86,26 @@ private static void BeginRequest(IOwinContext owinContext)
8486 {
8587 var request = owinContext . Request ;
8688
87- /*
88- * Note: Display name is intentionally set to a low cardinality
89- * value because OWIN does not expose any kind of
90- * route/template. See:
91- * https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#name
92- */
93- activity . DisplayName = request . Method switch
94- {
95- "GET" => "HTTP GET" ,
96- "POST" => "HTTP POST" ,
97- "PUT" => "HTTP PUT" ,
98- "DELETE" => "HTTP DELETE" ,
99- _ => $ "HTTP { request . Method } ",
100- } ;
89+ // Note: Display name is intentionally set to a low cardinality
90+ // value because OWIN does not expose any kind of
91+ // route/template. See:
92+ // https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#name
93+ RequestDataHelper . SetActivityDisplayName ( activity , request . Method ) ;
10194
10295 if ( activity . IsAllDataRequested )
10396 {
104- if ( request . Uri . Port == 80 || request . Uri . Port == 443 )
105- {
106- activity . SetTag ( SemanticConventions . AttributeHttpHost , request . Uri . Host ) ;
107- }
108- else
109- {
110- activity . SetTag ( SemanticConventions . AttributeHttpHost , request . Uri . Host + ":" + request . Uri . Port ) ;
111- }
97+ RequestDataHelper . SetHttpMethodTag ( activity , request . Method ) ;
98+ activity . SetTag ( SemanticConventions . AttributeServerAddress , request . Uri . Host ) ;
99+ activity . SetTag ( SemanticConventions . AttributeServerPort , request . Uri . Port ) ;
100+ activity . SetTag ( SemanticConventions . AttributeNetworkProtocolVersion , request . Protocol ) ;
112101
113- activity . SetTag ( SemanticConventions . AttributeHttpMethod , request . Method ) ;
114- activity . SetTag ( SemanticConventions . AttributeHttpTarget , request . Uri . AbsolutePath ) ;
115- activity . SetTag ( SemanticConventions . AttributeHttpUrl , GetUriTagValueFromRequestUri ( request . Uri , OwinInstrumentationActivitySource . Options . DisableUrlQueryRedaction ) ) ;
102+ activity . SetTag ( SemanticConventions . AttributeUrlPath , request . Uri . AbsolutePath ) ;
103+ activity . SetTag ( SemanticConventions . AttributeUrlQuery , request . Query ) ;
104+ activity . SetTag ( SemanticConventions . AttributeUrlScheme , owinContext . Request . Scheme ) ;
116105
117106 if ( request . Headers . TryGetValue ( "User-Agent" , out string [ ] userAgent ) && userAgent . Length > 0 )
118107 {
119- activity . SetTag ( SemanticConventions . AttributeHttpUserAgent , userAgent [ 0 ] ) ;
108+ activity . SetTag ( SemanticConventions . AttributeUserAgentOriginal , userAgent [ 0 ] ) ;
120109 }
121110
122111 try
@@ -163,7 +152,7 @@ private static void RequestEnd(IOwinContext owinContext, Exception? exception, l
163152 {
164153 activity . SetStatus ( Status . Error ) ;
165154
166- if ( OwinInstrumentationActivitySource . Options != null && OwinInstrumentationActivitySource . Options . RecordException )
155+ if ( OwinInstrumentationActivitySource . Options ? . RecordException == true )
167156 {
168157 activity . RecordException ( exception ) ;
169158 }
@@ -173,7 +162,7 @@ private static void RequestEnd(IOwinContext owinContext, Exception? exception, l
173162 activity . SetStatus ( SpanHelper . ResolveActivityStatusForHttpStatusCode ( activity . Kind , response . StatusCode ) ) ;
174163 }
175164
176- activity . SetTag ( SemanticConventions . AttributeHttpStatusCode , response . StatusCode ) ;
165+ activity . SetTag ( SemanticConventions . AttributeHttpResponseStatusCode , response . StatusCode ) ;
177166
178167 try
179168 {
0 commit comments