Skip to content

Commit

Permalink
HH-194786 add client_response_hook and get status code from context
Browse files Browse the repository at this point in the history
  • Loading branch information
rogoza-igor committed Oct 11, 2023
1 parent 7240a07 commit cc8b867
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
18 changes: 14 additions & 4 deletions frontik/integrations/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING
from urllib.parse import urlparse

from http_client import client_request_context
from http_client import client_request_context, response_status_code_context
from http_client.options import options as http_client_options
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
Expand Down Expand Up @@ -71,9 +71,7 @@ def initialize_app(self, app: FrontikApplication) -> Future | None:
provider.add_span_processor(BatchSpanProcessor(otlp_exporter))
trace.set_tracer_provider(provider)

self.aiohttp_instrumentor.instrument(
request_hook=_client_request_hook,
)
self.aiohttp_instrumentor.instrument(request_hook=_client_request_hook, response_hook=_client_response_hook)

self.tornado_instrumentor.instrument(
server_request_hook=_server_request_hook,
Expand Down Expand Up @@ -121,6 +119,18 @@ def _client_request_hook(span: Span, params: aiohttp.TraceRequestStartParams) ->
return


def _client_response_hook(
span: Span,
params: aiohttp.TraceRequestEndParams | aiohttp.TraceRequestExceptionParams,
) -> None:
if not span or not span.is_recording():
return
response_status_code: int = response_status_code_context.get(None)
if response_status_code is None:
return
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, response_status_code)


def get_netloc(url: str) -> str:
parts = urlparse(url)
if parts.scheme not in ('http', 'https', ''):
Expand Down
16 changes: 13 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jinja2 = '3.1.2'
lxml = '4.9.2'
pydantic = '>=1.10.5,<2'
tornado = '6.3.2'
http-client = {git = 'https://github.com/hhru/balancing-http-client.git', tag = '2.1.3'}
http-client = {git = 'https://github.com/hhru/balancing-http-client.git', branch = 'HH-194888'}
python-consul2-hh = {git = 'https://github.com/hhru/python-consul2', tag = 'v0.2.9'}
opentelemetry-sdk = '1.17.0'
opentelemetry-api = '1.17.0'
Expand Down

0 comments on commit cc8b867

Please sign in to comment.