This commit is contained in:
Timothy Jaeryang Baek
2026-03-17 17:58:01 -05:00
parent fcf7208352
commit de3317e26b
220 changed files with 17200 additions and 22836 deletions
@@ -38,7 +38,7 @@ def requests_hook(span: Span, request: PreparedRequest):
Http Request Hook
"""
span.update_name(f"{request.method} {request.url}")
span.update_name(f'{request.method} {request.url}')
span.set_attributes(
attributes={
SpanAttributes.HTTP_URL: request.url,
@@ -70,8 +70,8 @@ def redis_request_hook(span: Span, instance: Union[Redis | RedisCluster], args,
# - redis.cluster.RedisCluster
# Instead of checking the type, we check if the instance has a nodes_manager attribute.
try:
db = ""
if hasattr(instance, "nodes_manager"):
db = ''
if hasattr(instance, 'nodes_manager'):
default_node = instance.nodes_manager.default_node
if not default_node:
return
@@ -79,17 +79,17 @@ def redis_request_hook(span: Span, instance: Union[Redis | RedisCluster], args,
port = default_node.port
else:
connection_kwargs: dict = instance.connection_pool.connection_kwargs
host = connection_kwargs.get("host")
port = connection_kwargs.get("port")
db = connection_kwargs.get("db")
host = connection_kwargs.get('host')
port = connection_kwargs.get('port')
db = connection_kwargs.get('db')
span.set_attributes(
{
SpanAttributes.DB_INSTANCE: f"{host}/{db}",
SpanAttributes.DB_NAME: f"{host}/{db}",
SpanAttributes.DB_INSTANCE: f'{host}/{db}',
SpanAttributes.DB_NAME: f'{host}/{db}',
SpanAttributes.DB_TYPE: SPAN_REDIS_TYPE,
SpanAttributes.DB_PORT: port,
SpanAttributes.DB_IP: host,
SpanAttributes.DB_STATEMENT: " ".join([str(i) for i in args]),
SpanAttributes.DB_STATEMENT: ' '.join([str(i) for i in args]),
SpanAttributes.DB_OPERATION: str(args[0]),
}
)
@@ -102,7 +102,7 @@ def httpx_request_hook(span: Span, request: RequestInfo):
HTTPX Request Hook
"""
span.update_name(f"{request.method.decode()} {str(request.url)}")
span.update_name(f'{request.method.decode()} {str(request.url)}')
span.set_attributes(
attributes={
SpanAttributes.HTTP_URL: str(request.url),
@@ -117,11 +117,7 @@ def httpx_response_hook(span: Span, request: RequestInfo, response: ResponseInfo
"""
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, response.status_code)
span.set_status(
StatusCode.ERROR
if response.status_code >= status.HTTP_400_BAD_REQUEST
else StatusCode.OK
)
span.set_status(StatusCode.ERROR if response.status_code >= status.HTTP_400_BAD_REQUEST else StatusCode.OK)
async def httpx_async_request_hook(span: Span, request: RequestInfo):
@@ -132,9 +128,7 @@ async def httpx_async_request_hook(span: Span, request: RequestInfo):
httpx_request_hook(span, request)
async def httpx_async_response_hook(
span: Span, request: RequestInfo, response: ResponseInfo
):
async def httpx_async_response_hook(span: Span, request: RequestInfo, response: ResponseInfo):
"""
Async Response Hook
"""
@@ -147,7 +141,7 @@ def aiohttp_request_hook(span: Span, request: TraceRequestStartParams):
Aiohttp Request Hook
"""
span.update_name(f"{request.method} {str(request.url)}")
span.update_name(f'{request.method} {str(request.url)}')
span.set_attributes(
attributes={
SpanAttributes.HTTP_URL: str(request.url),
@@ -156,20 +150,14 @@ def aiohttp_request_hook(span: Span, request: TraceRequestStartParams):
)
def aiohttp_response_hook(
span: Span, response: Union[TraceRequestExceptionParams, TraceRequestEndParams]
):
def aiohttp_response_hook(span: Span, response: Union[TraceRequestExceptionParams, TraceRequestEndParams]):
"""
Aiohttp Response Hook
"""
if isinstance(response, TraceRequestEndParams):
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, response.response.status)
span.set_status(
StatusCode.ERROR
if response.response.status >= status.HTTP_400_BAD_REQUEST
else StatusCode.OK
)
span.set_status(StatusCode.ERROR if response.response.status >= status.HTTP_400_BAD_REQUEST else StatusCode.OK)
elif isinstance(response, TraceRequestExceptionParams):
span.set_status(StatusCode.ERROR)
span.set_attribute(SpanAttributes.ERROR_MESSAGE, str(response.exception))
@@ -191,9 +179,7 @@ class Instrumentor(BaseInstrumentor):
instrument_fastapi(app=self.app)
SQLAlchemyInstrumentor().instrument(engine=self.db_engine)
RedisInstrumentor().instrument(request_hook=redis_request_hook)
RequestsInstrumentor().instrument(
request_hook=requests_hook, response_hook=response_hook
)
RequestsInstrumentor().instrument(request_hook=requests_hook, response_hook=response_hook)
LoggingInstrumentor().instrument()
HTTPXClientInstrumentor().instrument(
request_hook=httpx_request_hook,
@@ -208,7 +194,7 @@ class Instrumentor(BaseInstrumentor):
SystemMetricsInstrumentor().instrument()
def _uninstrument(self, **kwargs):
if getattr(self, "instrumentors", None) is None:
if getattr(self, 'instrumentors', None) is None:
return
for instrumentor in self.instrumentors:
instrumentor.uninstrument()