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 opentelemetry .semconv .trace import SpanAttributes
10+ from pyramid .config import Configurator
511 from pyramid .httpexceptions import HTTPException
612 from pyramid .path import caller_package
713 from pyramid .settings import aslist
814 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
1515
1616 from instana .log import logger
17- from instana .singletons import tracer , agent
17+ from instana .propagators .format import Format
18+ from instana .singletons import agent , tracer
1819 from instana .util .secrets import strip_secrets_from_query
1920 from instana .util .traceutils import extract_custom_headers
20- from instana .propagators .format import Format
2121
2222 if TYPE_CHECKING :
23+ from pyramid .registry import Registry
2324 from pyramid .request import Request
2425 from pyramid .response import Response
25- from pyramid .registry import Registry
2626
2727 class InstanaTweenFactory (object ):
2828 """A factory that provides Instana instrumentation tween for Pyramid apps"""
@@ -32,7 +32,7 @@ def __init__(
3232 ) -> None :
3333 self .handler = handler
3434
35- def __call__ (self , request : "Request" ) -> "Response" :
35+ def __call__ (self , request : "Request" ) -> Optional [ "Response" ] :
3636 ctx = tracer .extract (Format .HTTP_HEADERS , dict (request .headers ))
3737
3838 with tracer .start_as_current_span ("wsgi" , span_context = ctx ) as span :
@@ -57,19 +57,16 @@ def __call__(self, request: "Request") -> "Response":
5757 span .set_attribute (
5858 "http.path_tpl" , request .matched_route .pattern
5959 )
60-
6160 extract_custom_headers (span , response .headers )
62-
6361 tracer .inject (span .context , Format .HTTP_HEADERS , response .headers )
6462 except HTTPException as e :
6563 response = e
6664 logger .debug (
6765 "Pyramid InstanaTweenFactory HTTPException: " , exc_info = True
6866 )
6967 except BaseException as e :
70- span .set_attribute (SpanAttributes . HTTP_STATUS_CODE , 500 )
68+ span .set_attribute ("http.status_code" , 500 )
7169 span .record_exception (e )
72-
7370 logger .debug (
7471 "Pyramid InstanaTweenFactory BaseException: " , exc_info = True
7572 )
@@ -78,12 +75,10 @@ def __call__(self, request: "Request") -> "Response":
7875 span .set_attribute (
7976 SpanAttributes .HTTP_STATUS_CODE , response .status_int
8077 )
81-
82- if 500 <= response .status_int :
83- if response .exception :
78+ if response .status_code >= 500 :
79+ if hasattr (response , "exception" ):
8480 span .record_exception (response .exception )
8581 span .assure_errored ()
86-
8782 return response
8883
8984 INSTANA_TWEEN = __name__ + ".InstanaTweenFactory"
0 commit comments