diff --git a/CHANGELOG.md b/CHANGELOG.md index a44feefffe..b754fccc41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +### Fixed + +- `opentelemetry-instrumentation` Fix client address is set to server address in new semconv + ([#3354](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3354)) + ## Version 1.31.0/0.52b0 (2025-03-12) ### Added @@ -36,7 +41,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Loosen `opentelemetry-instrumentation-starlette[instruments]` specifier ([#3304](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3304)) - ### Fixed - `opentelemetry-instrumentation-redis` Add missing entry in doc string for `def _instrument` @@ -112,7 +116,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Breaking changes - `opentelemetry-exporter-prometheus-remote-write` updated protobuf required version from 4.21 to 5.26 and regenerated protobufs - ([#3219](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3219)) + ([#3219](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3219)) - `opentelemetry-instrumentation-sqlalchemy` including sqlcomment in `db.statement` span attribute value is now opt-in ([#3112](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3112)) - `opentelemetry-instrumentation-dbapi` including sqlcomment in `db.statement` span attribute value is now opt-in diff --git a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py index 6fcccf84ec..594a0b7df3 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_asgi_middleware.py @@ -52,7 +52,10 @@ from opentelemetry.semconv.attributes.network_attributes import ( NETWORK_PROTOCOL_VERSION, ) -from opentelemetry.semconv.attributes.server_attributes import SERVER_PORT +from opentelemetry.semconv.attributes.server_attributes import ( + SERVER_ADDRESS, + SERVER_PORT, +) from opentelemetry.semconv.attributes.url_attributes import ( URL_PATH, URL_QUERY, @@ -401,6 +404,7 @@ def validate_outputs( "attributes": { HTTP_REQUEST_METHOD: "GET", URL_SCHEME: "http", + SERVER_ADDRESS: "127.0.0.1", SERVER_PORT: 80, NETWORK_PROTOCOL_VERSION: "1.0", URL_PATH: "/", @@ -436,6 +440,7 @@ def validate_outputs( "attributes": { HTTP_REQUEST_METHOD: "GET", URL_SCHEME: "http", + SERVER_ADDRESS: "127.0.0.1", SERVER_PORT: 80, NETWORK_PROTOCOL_VERSION: "1.0", URL_PATH: "/", @@ -706,7 +711,7 @@ async def test_behavior_with_scope_server_as_none_new_semconv(self): def update_expected_server(expected): expected[3]["attributes"].update( { - CLIENT_ADDRESS: "0.0.0.0", + SERVER_ADDRESS: "0.0.0.0", SERVER_PORT: 80, } ) @@ -733,7 +738,7 @@ def update_expected_server(expected): SpanAttributes.HTTP_HOST: "0.0.0.0", SpanAttributes.NET_HOST_PORT: 80, SpanAttributes.HTTP_URL: "http://0.0.0.0/", - CLIENT_ADDRESS: "0.0.0.0", + SERVER_ADDRESS: "0.0.0.0", SERVER_PORT: 80, } ) @@ -948,7 +953,7 @@ async def test_websocket(self): SpanAttributes.HTTP_HOST: self.scope["server"][0], SpanAttributes.HTTP_FLAVOR: self.scope["http_version"], SpanAttributes.HTTP_TARGET: self.scope["path"], - SpanAttributes.HTTP_URL: f'{self.scope["scheme"]}://{self.scope["server"][0]}{self.scope["path"]}', + SpanAttributes.HTTP_URL: f"{self.scope['scheme']}://{self.scope['server'][0]}{self.scope['path']}", SpanAttributes.NET_PEER_IP: self.scope["client"][0], SpanAttributes.NET_PEER_PORT: self.scope["client"][1], SpanAttributes.HTTP_STATUS_CODE: 200, @@ -1018,6 +1023,7 @@ async def test_websocket_new_semconv(self): "kind": trace_api.SpanKind.SERVER, "attributes": { URL_SCHEME: self.scope["scheme"], + SERVER_ADDRESS: self.scope["server"][0], SERVER_PORT: self.scope["server"][1], NETWORK_PROTOCOL_VERSION: self.scope["http_version"], URL_PATH: self.scope["path"], @@ -1096,12 +1102,13 @@ async def test_websocket_both_semconv(self): SpanAttributes.HTTP_HOST: self.scope["server"][0], SpanAttributes.HTTP_FLAVOR: self.scope["http_version"], SpanAttributes.HTTP_TARGET: self.scope["path"], - SpanAttributes.HTTP_URL: f'{self.scope["scheme"]}://{self.scope["server"][0]}{self.scope["path"]}', + SpanAttributes.HTTP_URL: f"{self.scope['scheme']}://{self.scope['server'][0]}{self.scope['path']}", SpanAttributes.NET_PEER_IP: self.scope["client"][0], SpanAttributes.NET_PEER_PORT: self.scope["client"][1], SpanAttributes.HTTP_STATUS_CODE: 200, SpanAttributes.HTTP_METHOD: self.scope["method"], URL_SCHEME: self.scope["scheme"], + SERVER_ADDRESS: self.scope["server"][0], SERVER_PORT: self.scope["server"][1], NETWORK_PROTOCOL_VERSION: self.scope["http_version"], URL_PATH: self.scope["path"], @@ -1661,6 +1668,7 @@ def test_request_attributes_new_semconv(self): HTTP_REQUEST_METHOD: "GET", URL_PATH: "/", URL_QUERY: "foo=bar", + SERVER_ADDRESS: "127.0.0.1", SERVER_PORT: 80, URL_SCHEME: "http", NETWORK_PROTOCOL_VERSION: "1.0", @@ -1696,6 +1704,7 @@ def test_request_attributes_both_semconv(self): HTTP_REQUEST_METHOD: "GET", URL_PATH: "/", URL_QUERY: "foo=bar", + SERVER_ADDRESS: "127.0.0.1", SERVER_PORT: 80, URL_SCHEME: "http", NETWORK_PROTOCOL_VERSION: "1.0", diff --git a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py index 091c876535..f75ca114a4 100644 --- a/opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py +++ b/opentelemetry-instrumentation/src/opentelemetry/instrumentation/_semconv.py @@ -354,7 +354,8 @@ def _set_http_host_server(result, host, sem_conv_opt_in_mode): if _report_old(sem_conv_opt_in_mode): set_string_attribute(result, SpanAttributes.HTTP_HOST, host) if _report_new(sem_conv_opt_in_mode): - set_string_attribute(result, CLIENT_ADDRESS, host) + if not result.get(SERVER_ADDRESS): + set_string_attribute(result, SERVER_ADDRESS, host) # net.peer.ip -> net.sock.peer.addr