Skip to content

Commit 6ae0f47

Browse files
emdnetoxrmx
andauthored
aiohttp-server: fix HTTP error inconsistencies (#4175)
* aiohttp-server: fix HTTP error inconsistencies Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> * fix failing test Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> * changelog and fix pylint Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> * changelog and fix pylint Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> --------- Signed-off-by: emdneto <9735060+emdneto@users.noreply.github.com> Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
1 parent e381a36 commit 6ae0f47

File tree

3 files changed

+400
-31
lines changed

3 files changed

+400
-31
lines changed

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6666
([#4001](https://github.com/open-telemetry/opentelemetry-python-contrib/issues/4001))
6767
- `opentelemetry-instrumentation-psycopg`: Fix `instrument_connection` method to use `_new_cursor_async_factory` on async connections.
6868
([#3956](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3956))
69-
- `opentelemetry-instrumentation-dbapi`: Replace SpanAttributes with semconv constants where applicable
69+
- `opentelemetry-instrumentation-dbapi`: Replace SpanAttributes with semconv constants where applicable
7070
([#4058](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4058))
71-
- `opentelemetry-instrumentation-django`: Replace SpanAttributes with semconv constants where applicable
71+
- `opentelemetry-instrumentation-django`: Replace SpanAttributes with semconv constants where applicable
7272
([#4059](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4059))
73-
- `opentelemetry-instrumentation-botocore`: Replace SpanAttributes with semconv constants where applicable
73+
- `opentelemetry-instrumentation-botocore`: Replace SpanAttributes with semconv constants where applicable
7474
([#4063](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4063))
7575
- `opentelemetry-instrumentation-celery`: Replace SpanAttributes with semconv constants where applicable
7676
([#4056](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4056))
@@ -102,8 +102,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
102102
([#3729](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3729))
103103
- `opentelemetry-instrumentation-confluent-kafka`: Fix incorrect number of argument to `_inner_wrap_close`
104104
([#3922](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3922))
105-
- `opentelemetry-instrumentation-urllib3`: fix multiple arguments error
105+
- `opentelemetry-instrumentation-urllib3`: fix multiple arguments error
106106
([#4144](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4144))
107+
- `opentelemetry-instrumentation-aiohttp-server`: fix HTTP error inconsistencies
108+
([#4175](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/4175))
107109

108110
### Breaking changes
109111

instrumentation/opentelemetry-instrumentation-aiohttp-server/src/opentelemetry/instrumentation/aiohttp_server/__init__.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,10 @@ async def hello(request):
199199
HTTP_SERVER_NAME,
200200
HTTP_URL,
201201
)
202-
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
203202
from opentelemetry.semconv.metrics import MetricInstruments
204203
from opentelemetry.semconv.metrics.http_metrics import (
205204
HTTP_SERVER_REQUEST_DURATION,
206205
)
207-
from opentelemetry.trace.status import Status, StatusCode
208206
from opentelemetry.util.http import (
209207
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SANITIZE_FIELDS,
210208
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
@@ -389,25 +387,21 @@ def set_status_code(
389387
if duration_attrs is None:
390388
duration_attrs = {}
391389

390+
status_code_str = str(status_code)
391+
392392
try:
393393
status_code_int = int(status_code)
394-
status_code_str = str(status_code)
395394
except ValueError:
396-
span.set_status(
397-
Status(
398-
StatusCode.ERROR,
399-
"Non-integer HTTP status: " + repr(status_code),
400-
)
401-
)
402-
else:
403-
_set_status(
404-
span,
405-
duration_attrs,
406-
status_code_int,
407-
status_code_str,
408-
server_span=True,
409-
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
410-
)
395+
status_code_int = -1
396+
397+
_set_status(
398+
span,
399+
duration_attrs,
400+
status_code_int,
401+
status_code_str,
402+
server_span=True,
403+
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
404+
)
411405

412406

413407
class AiohttpGetter(Getter):
@@ -469,6 +463,8 @@ async def _middleware(request, handler):
469463
context=extract(request, getter=getter),
470464
kind=trace.SpanKind.SERVER,
471465
attributes=request_attrs,
466+
set_status_on_exception=False,
467+
record_exception=False,
472468
) as span:
473469
if span.is_recording():
474470
span.set_attributes(
@@ -489,18 +485,32 @@ async def _middleware(request, handler):
489485
collect_response_headers_attributes(resp)
490486
)
491487
span.set_attributes(response_headers_attributes)
488+
except web.HTTPServerError as ex:
489+
set_status_code(
490+
span,
491+
ex.status_code,
492+
request_attrs,
493+
_sem_conv_opt_in_mode,
494+
)
495+
span.record_exception(ex)
496+
raise
492497
except web.HTTPException as ex:
493-
if _report_new(_sem_conv_opt_in_mode):
494-
request_attrs[ERROR_TYPE] = type(ex).__qualname__
495-
if span.is_recording():
496-
span.set_attribute(ERROR_TYPE, type(ex).__qualname__)
497498
set_status_code(
498499
span,
499500
ex.status_code,
500501
request_attrs,
501502
_sem_conv_opt_in_mode,
502503
)
503504
raise
505+
except Exception as ex:
506+
set_status_code(
507+
span,
508+
type(ex).__qualname__,
509+
request_attrs,
510+
_sem_conv_opt_in_mode,
511+
)
512+
span.record_exception(ex)
513+
raise
504514
finally:
505515
duration_s = default_timer() - start
506516
if duration_histogram_old:

0 commit comments

Comments
 (0)