Skip to content

Commit af17965

Browse files
y-youngemdnetolzchen
authored
Fix client address is set to server address in new semconv (#3354)
* Fix client address is set to server address * fix: asgi tests * docs: update CHANGELOG * docs: update CHANGELOG * refactor: only populate server address if missing --------- Co-authored-by: Emídio Neto <[email protected]> Co-authored-by: Leighton Chen <[email protected]>
1 parent 76e614f commit af17965

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
1212
## Unreleased
1313

14+
### Fixed
15+
16+
- `opentelemetry-instrumentation` Fix client address is set to server address in new semconv
17+
([#3354](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3354))
18+
1419
## Version 1.31.0/0.52b0 (2025-03-12)
1520

1621
### Added
@@ -36,7 +41,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3641
- Loosen `opentelemetry-instrumentation-starlette[instruments]` specifier
3742
([#3304](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3304))
3843

39-
4044
### Fixed
4145

4246
- `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
112116
### Breaking changes
113117

114118
- `opentelemetry-exporter-prometheus-remote-write` updated protobuf required version from 4.21 to 5.26 and regenerated protobufs
115-
([#3219](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3219))
119+
([#3219](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3219))
116120
- `opentelemetry-instrumentation-sqlalchemy` including sqlcomment in `db.statement` span attribute value is now opt-in
117121
([#3112](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3112))
118122
- `opentelemetry-instrumentation-dbapi` including sqlcomment in `db.statement` span attribute value is now opt-in

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@
5252
from opentelemetry.semconv.attributes.network_attributes import (
5353
NETWORK_PROTOCOL_VERSION,
5454
)
55-
from opentelemetry.semconv.attributes.server_attributes import SERVER_PORT
55+
from opentelemetry.semconv.attributes.server_attributes import (
56+
SERVER_ADDRESS,
57+
SERVER_PORT,
58+
)
5659
from opentelemetry.semconv.attributes.url_attributes import (
5760
URL_PATH,
5861
URL_QUERY,
@@ -401,6 +404,7 @@ def validate_outputs(
401404
"attributes": {
402405
HTTP_REQUEST_METHOD: "GET",
403406
URL_SCHEME: "http",
407+
SERVER_ADDRESS: "127.0.0.1",
404408
SERVER_PORT: 80,
405409
NETWORK_PROTOCOL_VERSION: "1.0",
406410
URL_PATH: "/",
@@ -436,6 +440,7 @@ def validate_outputs(
436440
"attributes": {
437441
HTTP_REQUEST_METHOD: "GET",
438442
URL_SCHEME: "http",
443+
SERVER_ADDRESS: "127.0.0.1",
439444
SERVER_PORT: 80,
440445
NETWORK_PROTOCOL_VERSION: "1.0",
441446
URL_PATH: "/",
@@ -706,7 +711,7 @@ async def test_behavior_with_scope_server_as_none_new_semconv(self):
706711
def update_expected_server(expected):
707712
expected[3]["attributes"].update(
708713
{
709-
CLIENT_ADDRESS: "0.0.0.0",
714+
SERVER_ADDRESS: "0.0.0.0",
710715
SERVER_PORT: 80,
711716
}
712717
)
@@ -733,7 +738,7 @@ def update_expected_server(expected):
733738
SpanAttributes.HTTP_HOST: "0.0.0.0",
734739
SpanAttributes.NET_HOST_PORT: 80,
735740
SpanAttributes.HTTP_URL: "http://0.0.0.0/",
736-
CLIENT_ADDRESS: "0.0.0.0",
741+
SERVER_ADDRESS: "0.0.0.0",
737742
SERVER_PORT: 80,
738743
}
739744
)
@@ -948,7 +953,7 @@ async def test_websocket(self):
948953
SpanAttributes.HTTP_HOST: self.scope["server"][0],
949954
SpanAttributes.HTTP_FLAVOR: self.scope["http_version"],
950955
SpanAttributes.HTTP_TARGET: self.scope["path"],
951-
SpanAttributes.HTTP_URL: f'{self.scope["scheme"]}://{self.scope["server"][0]}{self.scope["path"]}',
956+
SpanAttributes.HTTP_URL: f"{self.scope['scheme']}://{self.scope['server'][0]}{self.scope['path']}",
952957
SpanAttributes.NET_PEER_IP: self.scope["client"][0],
953958
SpanAttributes.NET_PEER_PORT: self.scope["client"][1],
954959
SpanAttributes.HTTP_STATUS_CODE: 200,
@@ -1018,6 +1023,7 @@ async def test_websocket_new_semconv(self):
10181023
"kind": trace_api.SpanKind.SERVER,
10191024
"attributes": {
10201025
URL_SCHEME: self.scope["scheme"],
1026+
SERVER_ADDRESS: self.scope["server"][0],
10211027
SERVER_PORT: self.scope["server"][1],
10221028
NETWORK_PROTOCOL_VERSION: self.scope["http_version"],
10231029
URL_PATH: self.scope["path"],
@@ -1096,12 +1102,13 @@ async def test_websocket_both_semconv(self):
10961102
SpanAttributes.HTTP_HOST: self.scope["server"][0],
10971103
SpanAttributes.HTTP_FLAVOR: self.scope["http_version"],
10981104
SpanAttributes.HTTP_TARGET: self.scope["path"],
1099-
SpanAttributes.HTTP_URL: f'{self.scope["scheme"]}://{self.scope["server"][0]}{self.scope["path"]}',
1105+
SpanAttributes.HTTP_URL: f"{self.scope['scheme']}://{self.scope['server'][0]}{self.scope['path']}",
11001106
SpanAttributes.NET_PEER_IP: self.scope["client"][0],
11011107
SpanAttributes.NET_PEER_PORT: self.scope["client"][1],
11021108
SpanAttributes.HTTP_STATUS_CODE: 200,
11031109
SpanAttributes.HTTP_METHOD: self.scope["method"],
11041110
URL_SCHEME: self.scope["scheme"],
1111+
SERVER_ADDRESS: self.scope["server"][0],
11051112
SERVER_PORT: self.scope["server"][1],
11061113
NETWORK_PROTOCOL_VERSION: self.scope["http_version"],
11071114
URL_PATH: self.scope["path"],
@@ -1661,6 +1668,7 @@ def test_request_attributes_new_semconv(self):
16611668
HTTP_REQUEST_METHOD: "GET",
16621669
URL_PATH: "/",
16631670
URL_QUERY: "foo=bar",
1671+
SERVER_ADDRESS: "127.0.0.1",
16641672
SERVER_PORT: 80,
16651673
URL_SCHEME: "http",
16661674
NETWORK_PROTOCOL_VERSION: "1.0",
@@ -1696,6 +1704,7 @@ def test_request_attributes_both_semconv(self):
16961704
HTTP_REQUEST_METHOD: "GET",
16971705
URL_PATH: "/",
16981706
URL_QUERY: "foo=bar",
1707+
SERVER_ADDRESS: "127.0.0.1",
16991708
SERVER_PORT: 80,
17001709
URL_SCHEME: "http",
17011710
NETWORK_PROTOCOL_VERSION: "1.0",

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,8 @@ def _set_http_host_server(result, host, sem_conv_opt_in_mode):
354354
if _report_old(sem_conv_opt_in_mode):
355355
set_string_attribute(result, SpanAttributes.HTTP_HOST, host)
356356
if _report_new(sem_conv_opt_in_mode):
357-
set_string_attribute(result, CLIENT_ADDRESS, host)
357+
if not result.get(SERVER_ADDRESS):
358+
set_string_attribute(result, SERVER_ADDRESS, host)
358359

359360

360361
# net.peer.ip -> net.sock.peer.addr

0 commit comments

Comments
 (0)