Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix http.host and net.peer.ip new http semconv mapping #2814

Merged
merged 12 commits into from
Aug 23, 2024
Prev Previous commit
Next Next commit
server
lzchen committed Aug 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit ee107ab42fdc8ae831ce01a0477c93e5b0c28c50
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-tornado` Handle http client exception and record exception info into span
([#2563](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2563))
- `opentelemetry-instrumentation` fix `http.host` new http semantic convention mapping to depend on `kind` of span
([2783](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2783))
([2814](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2814))

## Version 1.26.0/0.47b0 (2024-07-23)

Original file line number Diff line number Diff line change
@@ -218,7 +218,7 @@ def client_response_hook(span: Span, scope: dict[str, Any], message: dict[str, A
_set_http_host_server,
_set_http_method,
_set_http_net_host_port,
_set_http_peer_ip,
_set_http_peer_ip_server,
_set_http_peer_port_server,
_set_http_scheme,
_set_http_target,
@@ -380,7 +380,7 @@ def collect_request_attributes(
_set_http_user_agent(result, http_user_agent[0], sem_conv_opt_in_mode)

if "client" in scope and scope["client"] is not None:
_set_http_peer_ip(result, scope.get("client")[0], sem_conv_opt_in_mode)
_set_http_peer_ip_server(result, scope.get("client")[0], sem_conv_opt_in_mode)
_set_http_peer_port_server(
result, scope.get("client")[1], sem_conv_opt_in_mode
)
Original file line number Diff line number Diff line change
@@ -1086,7 +1086,6 @@ def test_websocket_both_semconv(self):
SpanAttributes.HTTP_METHOD: self.scope["method"],
URL_SCHEME: self.scope["scheme"],
SERVER_PORT: self.scope["server"][1],
SERVER_ADDRESS: self.scope["server"][0],
NETWORK_PROTOCOL_VERSION: self.scope["http_version"],
URL_PATH: self.scope["path"],
CLIENT_ADDRESS: self.scope["client"][0],
@@ -1629,7 +1628,6 @@ def test_request_attributes_new_semconv(self):
attrs,
{
HTTP_REQUEST_METHOD: "GET",
SERVER_ADDRESS: "127.0.0.1",
URL_PATH: "/",
URL_QUERY: "foo=bar",
SERVER_PORT: 80,
@@ -1665,7 +1663,6 @@ def test_request_attributes_both_semconv(self):
SpanAttributes.NET_PEER_IP: "127.0.0.1",
SpanAttributes.NET_PEER_PORT: 32767,
HTTP_REQUEST_METHOD: "GET",
SERVER_ADDRESS: "127.0.0.1",
URL_PATH: "/",
URL_QUERY: "foo=bar",
SERVER_PORT: 80,
@@ -1690,7 +1687,7 @@ def test_query_string_new_semconv(self):
_HTTPStabilityMode.HTTP,
)
self.assertEqual(attrs[URL_SCHEME], "http")
self.assertEqual(attrs[SERVER_ADDRESS], "127.0.0.1")
self.assertEqual(attrs[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(attrs[URL_PATH], "/")
self.assertEqual(attrs[URL_QUERY], "foo=bar")

@@ -1704,7 +1701,7 @@ def test_query_string_both_semconv(self):
attrs[SpanAttributes.HTTP_URL], "http://127.0.0.1/?foo=bar"
)
self.assertEqual(attrs[URL_SCHEME], "http")
self.assertEqual(attrs[SERVER_ADDRESS], "127.0.0.1")
self.assertEqual(attrs[CLIENT_ADDRESS], "127.0.0.1")
self.assertEqual(attrs[URL_PATH], "/")
self.assertEqual(attrs[URL_QUERY], "foo=bar")

Original file line number Diff line number Diff line change
@@ -491,7 +491,9 @@ def _set_metric_attributes(
sem_conv_opt_in_mode: _HTTPStabilityMode = _HTTPStabilityMode.DEFAULT,
) -> None:

_set_http_host_client(metric_attributes, instance.host, sem_conv_opt_in_mode)
_set_http_host_client(
metric_attributes, instance.host, sem_conv_opt_in_mode
)
_set_http_scheme(metric_attributes, instance.scheme, sem_conv_opt_in_mode)
_set_http_method(
metric_attributes,
Original file line number Diff line number Diff line change
@@ -231,7 +231,7 @@ def response_hook(span: Span, environ: WSGIEnvironment, status: str, response_he
_set_http_net_host,
_set_http_net_host_port,
_set_http_net_peer_name_server,
_set_http_peer_ip,
_set_http_peer_ip_server,
_set_http_peer_port_server,
_set_http_scheme,
_set_http_target,
@@ -360,7 +360,7 @@ def collect_request_attributes(

remote_addr = environ.get("REMOTE_ADDR")
if remote_addr:
_set_http_peer_ip(result, remote_addr, sem_conv_opt_in_mode)
_set_http_peer_ip_server(result, remote_addr, sem_conv_opt_in_mode)

peer_port = environ.get("REMOTE_PORT")
if peer_port:
Original file line number Diff line number Diff line change
@@ -254,6 +254,7 @@ def _set_http_scheme(result, scheme, sem_conv_opt_in_mode):

# Client


def _set_http_host_client(result, host, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.HTTP_HOST, host)
@@ -284,6 +285,7 @@ def _set_http_network_protocol_version(result, version, sem_conv_opt_in_mode):

# Server


def _set_http_net_host(result, host, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.NET_HOST_NAME, host)
@@ -320,7 +322,7 @@ def _set_http_host_server(result, host, sem_conv_opt_in_mode):
# net.sock.peer.addr -> client.socket.address for server spans (TODO) AND client.address
# https://github.com/open-telemetry/semantic-conventions/blob/v1.21.0/CHANGELOG.md#v1210-2023-07-13
# https://github.com/open-telemetry/semantic-conventions/blob/main/docs/non-normative/http-migration.md#common-attributes-across-http-client-and-server-spans
def _set_http_peer_ip(result, ip, sem_conv_opt_in_mode):
def _set_http_peer_ip_server(result, ip, sem_conv_opt_in_mode):
if _report_old(sem_conv_opt_in_mode):
set_string_attribute(result, SpanAttributes.NET_PEER_IP, ip)
if _report_new(sem_conv_opt_in_mode):