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+ from opentelemetry .semconv .trace import SpanAttributes
8+
9+ import wrapt
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,11 +32,11 @@ 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 :
39- span .set_attribute ("http.host" , request .host )
39+ span .set_attribute (SpanAttributes . HTTP_HOST , request .host )
4040 span .set_attribute (SpanAttributes .HTTP_METHOD , request .method )
4141 span .set_attribute (SpanAttributes .HTTP_URL , request .path )
4242
@@ -57,9 +57,7 @@ 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
@@ -69,7 +67,6 @@ def __call__(self, request: "Request") -> "Response":
6967 except BaseException as e :
7068 span .set_attribute (SpanAttributes .HTTP_STATUS_CODE , 500 )
7169 span .record_exception (e )
72-
7370 logger .debug (
7471 "Pyramid InstanaTweenFactory BaseException: " , exc_info = True
7572 )
@@ -83,7 +80,6 @@ def __call__(self, request: "Request") -> "Response":
8380 if response .exception :
8481 span .record_exception (response .exception )
8582 span .assure_errored ()
86-
8783 return response
8884
8985 INSTANA_TWEEN = __name__ + ".InstanaTweenFactory"
0 commit comments