def test_hooks(self): def request_hook(span: Span, params: aiohttp.TraceRequestStartParams): span.update_name(f"{params.method} - {params.url.path}") def response_hook( span: Span, params: typing.Union[ aiohttp.TraceRequestEndParams, aiohttp.TraceRequestExceptionParams, ], ): span.set_attribute("response_hook_attr", "value") AioHttpClientInstrumentor().uninstrument() AioHttpClientInstrumentor().instrument( request_hook=request_hook, response_hook=response_hook ) url = "/test-path" run_with_test_server( self.get_default_request(url), url, self.default_handler ) span = self.assert_spans(1) self.assertEqual("GET - /test-path", span.name) self.assertIn("response_hook_attr", span.attributes) self.assertEqual(span.attributes["response_hook_attr"], "value")
def test_uninstrument(self): AioHttpClientInstrumentor().uninstrument() run_with_test_server(self.get_default_request(), self.URL, self.default_handler) self.assert_spans(0) AioHttpClientInstrumentor().instrument() run_with_test_server(self.get_default_request(), self.URL, self.default_handler) self.assert_spans(1)
def test_span_name(self): def span_name_callback(params: aiohttp.TraceRequestStartParams) -> str: return "{} - {}".format(params.method, params.url.path) AioHttpClientInstrumentor().uninstrument() AioHttpClientInstrumentor().instrument(span_name=span_name_callback) url = "/test-path" run_with_test_server(self.get_default_request(url), url, self.default_handler) span = self.assert_spans(1) self.assertEqual("GET - /test-path", span.name)
def test_instrument_with_custom_trace_config(self): AioHttpClientInstrumentor().uninstrument() AioHttpClientInstrumentor().instrument( trace_configs=[aiohttp_client.create_trace_config()] ) self.assert_spans(0) run_with_test_server( self.get_default_request(), self.URL, self.default_handler ) self.assert_spans(2)
def test_url_filter(self): def strip_query_params(url: yarl.URL) -> str: return str(url.with_query(None)) AioHttpClientInstrumentor().uninstrument() AioHttpClientInstrumentor().instrument(url_filter=strip_query_params) url = "/test-path?query=params" host, port = run_with_test_server(self.get_default_request(url), url, self.default_handler) span = self.assert_spans(1) self.assertEqual( "http://{}:{}/test-path".format(host, port), span.attributes["http.url"], )
def create_app(): app = FastAPI( debug=setting.DEBUG, title=setting.TITLE, description=setting.DESCRIPTION, ) # thread local just flask like g app.add_middleware(GlobalsMiddleware) # 注册 db models setup_db_models(app) # 初始化路由 setup_routers(app) # 初始化静态资源路径 setup_static_files(app) # 初始化全局 middleware setup_middleware(app) # 初始化全局 middleware setup_logging(app) # 初始化 sentry if setting.SENTRY_DSN: setup_sentry(app) # Enable instrumentation AioHttpClientInstrumentor().instrument() return app
def setup_instrumentation(app): settings: TracingSettings = _get_settings() _TRACE_PROVIDER = TracerProvider( resource=Resource.create({"service.name": settings.jaeger_service}) ) trace.set_tracer_provider(_TRACE_PROVIDER) if settings.jaeger_hostname: # pragma: no cover _JAEGER_EXPORTER = JaegerExporter( agent_host_name=settings.jaeger_hostname, agent_port=settings.jaeger_port, ) _TRACE_PROVIDER.add_span_processor(BatchSpanProcessor(_JAEGER_EXPORTER)) AioHttpClientInstrumentor().instrument() RequestsInstrumentor().instrument() # Register logging middleware app.middleware("http")(_log_requests_middleware) app.middleware("http")(_bind_logger_tracecontext_middleware) FastAPIInstrumentor.instrument_app(app) return app
async def uninstrument_request(server: aiohttp.test_utils.TestServer): client = aiohttp.test_utils.TestClient(server) AioHttpClientInstrumentor().uninstrument_session(client.session) async with client as session: await session.get(self.URL)
def tearDown(self): super().tearDown() AioHttpClientInstrumentor().uninstrument()
def setUp(self): super().setUp() AioHttpClientInstrumentor().instrument()