11# (c) Copyright IBM Corp. 2021
22# (c) Copyright Instana Inc. 2020
33
4+
45try :
6+ from typing import TYPE_CHECKING , Any , Callable , Dict , Optional , Tuple
7+
8+ import wrapt
9+ from pyramid .config import Configurator
510 from pyramid .httpexceptions import HTTPException
611 from pyramid .path import caller_package
712 from pyramid .settings import aslist
813 from pyramid .tweens import EXCVIEW
9- from pyramid .config import Configurator
10- from typing import TYPE_CHECKING , Dict , Any , Callable , Tuple
11- import wrapt
12-
13- from opentelemetry .semconv .trace import SpanAttributes
14- from opentelemetry .trace import SpanKind
1514
1615 from instana .log import logger
17- from instana .singletons import tracer , agent
16+ from instana .propagators .format import Format
17+ from instana .singletons import agent , tracer
1818 from instana .util .secrets import strip_secrets_from_query
1919 from instana .util .traceutils import extract_custom_headers
20- from instana .propagators .format import Format
2120
2221 if TYPE_CHECKING :
22+ from pyramid .registry import Registry
2323 from pyramid .request import Request
2424 from pyramid .response import Response
25- from pyramid .registry import Registry
2625
2726 class InstanaTweenFactory (object ):
2827 """A factory that provides Instana instrumentation tween for Pyramid apps"""
@@ -32,13 +31,13 @@ def __init__(
3231 ) -> None :
3332 self .handler = handler
3433
35- def __call__ (self , request : "Request" ) -> "Response" :
34+ def __call__ (self , request : "Request" ) -> Optional [ "Response" ] :
3635 ctx = tracer .extract (Format .HTTP_HEADERS , dict (request .headers ))
3736
3837 with tracer .start_as_current_span ("wsgi" , span_context = ctx ) as span :
3938 span .set_attribute ("http.host" , request .host )
40- span .set_attribute (SpanAttributes . HTTP_METHOD , request .method )
41- span .set_attribute (SpanAttributes . HTTP_URL , request .path )
39+ span .set_attribute ("http.method" , request .method )
40+ span .set_attribute ("http.url" , request .path )
4241
4342 extract_custom_headers (span , request .headers )
4443
@@ -57,34 +56,25 @@ def __call__(self, request: "Request") -> "Response":
5756 span .set_attribute (
5857 "http.path_tpl" , request .matched_route .pattern
5958 )
60-
6159 extract_custom_headers (span , response .headers )
62-
6360 tracer .inject (span .context , Format .HTTP_HEADERS , response .headers )
6461 except HTTPException as e :
6562 response = e
6663 logger .debug (
6764 "Pyramid InstanaTweenFactory HTTPException: " , exc_info = True
6865 )
6966 except BaseException as e :
70- span .set_attribute (SpanAttributes . HTTP_STATUS_CODE , 500 )
67+ span .set_attribute ("http.status_code" , 500 )
7168 span .record_exception (e )
72-
7369 logger .debug (
7470 "Pyramid InstanaTweenFactory BaseException: " , exc_info = True
7571 )
7672 finally :
7773 if response :
78- span .set_attribute (
79- SpanAttributes .HTTP_STATUS_CODE , response .status_int
80- )
81-
82- if 500 <= response .status_int :
83- if response .exception :
84- span .record_exception (response .exception )
85- span .assure_errored ()
86-
87- return response
74+ span .set_attribute ("http.status_code" , response .status_code )
75+ if response .status_code >= 400 :
76+ span .record_exception (response )
77+ return response
8878
8979 INSTANA_TWEEN = __name__ + ".InstanaTweenFactory"
9080
0 commit comments