Skip to content

Commit c9bad62

Browse files
authoredJul 3, 2024··
HTTP transition for asgi (#2610)
1 parent a3e457c commit c9bad62

File tree

10 files changed

+1111
-145
lines changed

10 files changed

+1111
-145
lines changed
 

‎CHANGELOG.md

+21-13
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
- `opentelemetry-instrumentation-django` Handle exceptions from request/response hooks
11-
([#2153](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2153))
12-
- `opentelemetry-instrumentation-asyncio` instrumented `asyncio.wait_for` properly raises `asyncio.TimeoutError` as expected
13-
([#2637](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2637))
14-
- `opentelemetry-instrumentation-aws-lambda` Bugfix: AWS Lambda event source key incorrect for SNS in instrumentation library.
15-
([#2612](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2612))
16-
- `opentelemetry-instrumentation-system-metrics` Permit to use psutil 6.0+.
17-
([#2630](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2630))
18-
- `opentelemetry-instrumentation-asgi` Fix generation of `http.target` and `http.url` attributes for ASGI apps
19-
using sub apps
20-
([#2477](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2477))
21-
22-
2310
### Added
2411

2512
- `opentelemetry-instrumentation-pyramid` Record exceptions raised when serving a request
@@ -30,11 +17,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3017
([#2616](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2616))
3118
- `opentelemetry-instrumentation-confluent-kafka` Add support for produce purge
3219
([#2638](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2638))
20+
- `opentelemetry-instrumentation-system-metrics` Permit to use psutil 6.0+.
21+
([#2630](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2630))
3322

3423
### Breaking changes
3524

3625
- `opentelemetry-instrumentation-asgi`, `opentelemetry-instrumentation-fastapi`, `opentelemetry-instrumentation-starlette` Use `tracer` and `meter` of originating components instead of one from `asgi` middleware
3726
([#2580](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2580))
27+
- Populate `{method}` as `HTTP` on `_OTHER` methods from scope
28+
([#2610](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2610))
29+
30+
### Added
31+
32+
- `opentelemetry-instrumentation-asgi` Implement new semantic convention opt-in with stable http semantic conventions
33+
([#2610](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2610))
3834

3935
### Fixed
4036

@@ -50,6 +46,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5046
([#2644](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2644))
5147
- `opentelemetry-instrumentation-confluent-kafka` Confluent Kafka: Ensure consume span is ended when consumer is closed
5248
([#2640](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2640))
49+
- `opentelemetry-instrumentation-asgi` Fix generation of `http.target` and `http.url` attributes for ASGI apps
50+
using sub apps
51+
([#2477](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2477))
52+
- `opentelemetry-instrumentation-aws-lambda` Bugfix: AWS Lambda event source key incorrect for SNS in instrumentation library.
53+
([#2612](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2612))
54+
- `opentelemetry-instrumentation-asyncio` instrumented `asyncio.wait_for` properly raises `asyncio.TimeoutError` as expected
55+
([#2637](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2637))
56+
- `opentelemetry-instrumentation-django` Handle exceptions from request/response hooks
57+
([#2153](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2153))
58+
- `opentelemetry-instrumentation-asgi` Removed `NET_HOST_NAME` AND `NET_HOST_PORT` from active requests count attribute
59+
([#2610](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2610))
60+
5361

5462
## Version 1.25.0/0.46b0 (2024-05-31)
5563

‎instrumentation/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
| [opentelemetry-instrumentation-aiohttp-client](./opentelemetry-instrumentation-aiohttp-client) | aiohttp ~= 3.0 | No | experimental
66
| [opentelemetry-instrumentation-aiohttp-server](./opentelemetry-instrumentation-aiohttp-server) | aiohttp ~= 3.0 | No | experimental
77
| [opentelemetry-instrumentation-aiopg](./opentelemetry-instrumentation-aiopg) | aiopg >= 0.13.0, < 2.0.0 | No | experimental
8-
| [opentelemetry-instrumentation-asgi](./opentelemetry-instrumentation-asgi) | asgiref ~= 3.0 | No | experimental
8+
| [opentelemetry-instrumentation-asgi](./opentelemetry-instrumentation-asgi) | asgiref ~= 3.0 | Yes | migration
99
| [opentelemetry-instrumentation-asyncio](./opentelemetry-instrumentation-asyncio) | asyncio | No | experimental
1010
| [opentelemetry-instrumentation-asyncpg](./opentelemetry-instrumentation-asyncpg) | asyncpg >= 0.12.0 | No | experimental
1111
| [opentelemetry-instrumentation-aws-lambda](./opentelemetry-instrumentation-aws-lambda) | aws_lambda | No | experimental

‎instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

+253-80
Large diffs are not rendered by default.

‎instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/package.py

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414

1515

1616
_instruments = ("asgiref ~= 3.0",)
17+
18+
_supports_metrics = True
19+
20+
_semconv_status = "migration"

‎instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py

+807-20
Large diffs are not rendered by default.

‎instrumentation/opentelemetry-instrumentation-falcon/tests/test_falcon.py

-2
Original file line numberDiff line numberDiff line change
@@ -345,8 +345,6 @@ def test_falcon_metric_values(self):
345345
"http.scheme": "http",
346346
"http.flavor": "1.1",
347347
"http.server_name": "falconframework.org",
348-
"net.host.name": "falconframework.org",
349-
"net.host.port": 80,
350348
}
351349
start = default_timer()
352350
self.client().simulate_get("/hello/756")

‎instrumentation/opentelemetry-instrumentation-flask/tests/test_programmatic.py

-4
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,6 @@ def test_basic_metric_success(self):
584584
"http.scheme": "http",
585585
"http.flavor": "1.1",
586586
"http.server_name": "localhost",
587-
"net.host.name": "localhost",
588-
"net.host.port": 80,
589587
}
590588
self._assert_basic_metric(
591589
expected_duration_attributes,
@@ -627,8 +625,6 @@ def test_basic_metric_nonstandard_http_method_success(self):
627625
"http.scheme": "http",
628626
"http.flavor": "1.1",
629627
"http.server_name": "localhost",
630-
"net.host.name": "localhost",
631-
"net.host.port": 80,
632628
}
633629
self._assert_basic_metric(
634630
expected_duration_attributes,

‎instrumentation/opentelemetry-instrumentation-pyramid/tests/test_automatic.py

-2
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,6 @@ def test_basic_metric_success(self):
224224
"http.scheme": "http",
225225
"http.flavor": "1.1",
226226
"http.server_name": "localhost",
227-
"net.host.name": "localhost",
228-
"net.host.port": 80,
229227
}
230228
metrics_list = self.memory_metrics_reader.get_metrics_data()
231229
for metric in (

‎instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,8 @@ def add_response_attributes(
494494
_set_status(
495495
span,
496496
duration_attrs,
497-
status_code_str,
498497
status_code,
498+
status_code_str,
499499
sem_conv_opt_in_mode,
500500
)
501501

‎opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
from enum import Enum
1818

1919
from opentelemetry.instrumentation.utils import http_status_to_status_code
20+
from opentelemetry.semconv.attributes.client_attributes import (
21+
CLIENT_ADDRESS,
22+
CLIENT_PORT,
23+
)
2024
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
2125
from opentelemetry.semconv.attributes.http_attributes import (
2226
HTTP_REQUEST_METHOD,
@@ -33,11 +37,18 @@
3337
)
3438
from opentelemetry.semconv.attributes.url_attributes import (
3539
URL_FULL,
40+
URL_PATH,
41+
URL_QUERY,
3642
URL_SCHEME,
3743
)
44+
from opentelemetry.semconv.attributes.user_agent_attributes import (
45+
USER_AGENT_ORIGINAL,
46+
)
3847
from opentelemetry.semconv.trace import SpanAttributes
3948
from opentelemetry.trace.status import Status, StatusCode
4049

50+
# These lists represent attributes for metrics that are currently supported
51+
4152
_client_duration_attrs_old = [
4253
SpanAttributes.HTTP_STATUS_CODE,
4354
SpanAttributes.HTTP_HOST,
@@ -85,13 +96,12 @@
8596
SpanAttributes.HTTP_SCHEME,
8697
SpanAttributes.HTTP_FLAVOR,
8798
SpanAttributes.HTTP_SERVER_NAME,
88-
SpanAttributes.NET_HOST_NAME,
89-
SpanAttributes.NET_HOST_PORT,
9099
]
91100

92101
_server_active_requests_count_attrs_new = [
93102
HTTP_REQUEST_METHOD,
94103
URL_SCHEME,
104+
# TODO: Support SERVER_ADDRESS AND SERVER_PORT
95105
]
96106

97107
OTEL_SEMCONV_STABILITY_OPT_IN = "OTEL_SEMCONV_STABILITY_OPT_IN"
@@ -280,38 +290,38 @@ def _set_http_net_host(result, host, sem_conv_opt_in_mode):
280290
if _report_old(sem_conv_opt_in_mode):
281291
set_string_attribute(result, SpanAttributes.NET_HOST_NAME, host)
282292
if _report_new(sem_conv_opt_in_mode):
283-
set_string_attribute(result, SpanAttributes.SERVER_ADDRESS, host)
293+
set_string_attribute(result, SERVER_ADDRESS, host)
284294

285295

286296
def _set_http_net_host_port(result, port, sem_conv_opt_in_mode):
287297
if _report_old(sem_conv_opt_in_mode):
288298
set_int_attribute(result, SpanAttributes.NET_HOST_PORT, port)
289299
if _report_new(sem_conv_opt_in_mode):
290-
set_int_attribute(result, SpanAttributes.SERVER_PORT, port)
300+
set_int_attribute(result, SERVER_PORT, port)
291301

292302

293303
def _set_http_target(result, target, path, query, sem_conv_opt_in_mode):
294304
if _report_old(sem_conv_opt_in_mode):
295305
set_string_attribute(result, SpanAttributes.HTTP_TARGET, target)
296306
if _report_new(sem_conv_opt_in_mode):
297307
if path:
298-
set_string_attribute(result, SpanAttributes.URL_PATH, path)
308+
set_string_attribute(result, URL_PATH, path)
299309
if query:
300-
set_string_attribute(result, SpanAttributes.URL_QUERY, query)
310+
set_string_attribute(result, URL_QUERY, query)
301311

302312

303313
def _set_http_peer_ip(result, ip, sem_conv_opt_in_mode):
304314
if _report_old(sem_conv_opt_in_mode):
305315
set_string_attribute(result, SpanAttributes.NET_PEER_IP, ip)
306316
if _report_new(sem_conv_opt_in_mode):
307-
set_string_attribute(result, SpanAttributes.CLIENT_ADDRESS, ip)
317+
set_string_attribute(result, CLIENT_ADDRESS, ip)
308318

309319

310320
def _set_http_peer_port_server(result, port, sem_conv_opt_in_mode):
311321
if _report_old(sem_conv_opt_in_mode):
312322
set_int_attribute(result, SpanAttributes.NET_PEER_PORT, port)
313323
if _report_new(sem_conv_opt_in_mode):
314-
set_int_attribute(result, SpanAttributes.CLIENT_PORT, port)
324+
set_int_attribute(result, CLIENT_PORT, port)
315325

316326

317327
def _set_http_user_agent(result, user_agent, sem_conv_opt_in_mode):
@@ -320,32 +330,28 @@ def _set_http_user_agent(result, user_agent, sem_conv_opt_in_mode):
320330
result, SpanAttributes.HTTP_USER_AGENT, user_agent
321331
)
322332
if _report_new(sem_conv_opt_in_mode):
323-
set_string_attribute(
324-
result, SpanAttributes.USER_AGENT_ORIGINAL, user_agent
325-
)
333+
set_string_attribute(result, USER_AGENT_ORIGINAL, user_agent)
326334

327335

328336
def _set_http_net_peer_name_server(result, name, sem_conv_opt_in_mode):
329337
if _report_old(sem_conv_opt_in_mode):
330338
set_string_attribute(result, SpanAttributes.NET_PEER_NAME, name)
331339
if _report_new(sem_conv_opt_in_mode):
332-
set_string_attribute(result, SpanAttributes.CLIENT_ADDRESS, name)
340+
set_string_attribute(result, CLIENT_ADDRESS, name)
333341

334342

335343
def _set_http_flavor_version(result, version, sem_conv_opt_in_mode):
336344
if _report_old(sem_conv_opt_in_mode):
337345
set_string_attribute(result, SpanAttributes.HTTP_FLAVOR, version)
338346
if _report_new(sem_conv_opt_in_mode):
339-
set_string_attribute(
340-
result, SpanAttributes.NETWORK_PROTOCOL_VERSION, version
341-
)
347+
set_string_attribute(result, NETWORK_PROTOCOL_VERSION, version)
342348

343349

344350
def _set_status(
345351
span,
346352
metrics_attributes,
347-
status_code_str,
348353
status_code,
354+
status_code_str,
349355
sem_conv_opt_in_mode,
350356
):
351357
if status_code < 0:
@@ -366,12 +372,8 @@ def _set_status(
366372
span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
367373
metrics_attributes[SpanAttributes.HTTP_STATUS_CODE] = status_code
368374
if _report_new(sem_conv_opt_in_mode):
369-
span.set_attribute(
370-
SpanAttributes.HTTP_RESPONSE_STATUS_CODE, status_code
371-
)
372-
metrics_attributes[SpanAttributes.HTTP_RESPONSE_STATUS_CODE] = (
373-
status_code
374-
)
375+
span.set_attribute(HTTP_RESPONSE_STATUS_CODE, status_code)
376+
metrics_attributes[HTTP_RESPONSE_STATUS_CODE] = status_code
375377
if status == StatusCode.ERROR:
376378
span.set_attribute(ERROR_TYPE, status_code_str)
377379
metrics_attributes[ERROR_TYPE] = status_code_str

0 commit comments

Comments
 (0)
Please sign in to comment.