Skip to content

Revert "[Backport 9.0] Update opentelemetry conventions" #253

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

Merged
merged 2 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/sphinx/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@

intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
"requests": ("https://requests.readthedocs.io/en/latest", None),
"requests": ("https://docs.python-requests.org/en/latest", None),
}
5 changes: 1 addition & 4 deletions elastic_transport/_async_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,7 @@ async def perform_request( # type: ignore[override, return]
node: BaseAsyncNode = self.node_pool.get() # type: ignore[assignment]
start_time = self._loop.time()
try:
otel_span.set_node_metadata(
node.host, node.port, node.base_url, target, method
)
otel_span.set_node_metadata(node.host, node.port, node.base_url, target)
resp = await node.perform_request(
method,
target,
Expand Down Expand Up @@ -366,7 +364,6 @@ async def perform_request( # type: ignore[override, return]
# We either got a response we're happy with or
# we've exhausted all of our retries so we return it.
if not retry or attempt >= max_retries:
otel_span.set_db_response(resp.meta.status)
return TransportApiResponse(resp.meta, body)
else:
_logger.warning(
Expand Down
25 changes: 4 additions & 21 deletions elastic_transport/_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,14 @@ def __init__(
self.body_strategy = body_strategy
self.endpoint_id = endpoint_id

if self.otel_span:
self.otel_span.set_attribute("db.system.name", "elasticsearch")
if self.endpoint_id:
self.otel_span.set_attribute("db.operation.name", self.endpoint_id)

def set_node_metadata(
self,
host: str,
port: int,
base_url: str,
target: str,
method: str,
self, host: str, port: int, base_url: str, target: str
) -> None:
if self.otel_span is None:
return

# url.full does not contain auth info which is passed as headers
self.otel_span.set_attribute("url.full", base_url + target)
self.otel_span.set_attribute("http.request.method", method)
self.otel_span.set_attribute("server.address", host)
self.otel_span.set_attribute("server.port", port)

Expand All @@ -77,10 +66,10 @@ def set_elastic_cloud_metadata(self, headers: Mapping[str, str]) -> None:

cluster_name = headers.get("X-Found-Handling-Cluster")
if cluster_name is not None:
self.otel_span.set_attribute("db.namespace", cluster_name)
self.otel_span.set_attribute("db.elasticsearch.cluster.name", cluster_name)
node_name = headers.get("X-Found-Handling-Instance")
if node_name is not None:
self.otel_span.set_attribute("elasticsearch.node.name", node_name)
self.otel_span.set_attribute("db.elasticsearch.node.name", node_name)

def set_db_statement(self, serialized_body: bytes) -> None:
if self.otel_span is None:
Expand All @@ -90,11 +79,5 @@ def set_db_statement(self, serialized_body: bytes) -> None:
return
elif self.body_strategy == "raw" and self.endpoint_id in SEARCH_ENDPOINTS:
self.otel_span.set_attribute(
"db.query.text", serialized_body.decode("utf-8")
"db.statement", serialized_body.decode("utf-8")
)

def set_db_response(self, status_code: int) -> None:
if self.otel_span is None:
return

self.otel_span.set_attribute("db.response.status_code", str(status_code))
5 changes: 1 addition & 4 deletions elastic_transport/_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,7 @@ def perform_request( # type: ignore[return]
node = self.node_pool.get()
start_time = time.time()
try:
otel_span.set_node_metadata(
node.host, node.port, node.base_url, target, method
)
otel_span.set_node_metadata(node.host, node.port, node.base_url, target)
resp = node.perform_request(
method,
target,
Expand Down Expand Up @@ -444,7 +442,6 @@ def perform_request( # type: ignore[return]
# We either got a response we're happy with or
# we've exhausted all of our retries so we return it.
if not retry or attempt >= max_retries:
otel_span.set_db_response(resp.meta.status)
return TransportApiResponse(resp.meta, body)
else:
_logger.warning(
Expand Down
16 changes: 3 additions & 13 deletions tests/test_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def test_no_span():
9200,
"http://localhost:9200/",
"_ml/anomaly_detectors/my-job/_open",
"POST",
)
span.set_elastic_cloud_metadata(
{
Expand All @@ -66,30 +65,23 @@ def test_detailed_span():
9200,
"http://localhost:9200/",
"_ml/anomaly_detectors/my-job/_open",
"POST",
)
span.set_elastic_cloud_metadata(
{
"X-Found-Handling-Cluster": "e9106fc68e3044f0b1475b04bf4ffd5f",
"X-Found-Handling-Instance": "instance-0000000001",
}
)
span.set_db_response(202)

spans = memory_exporter.get_finished_spans()
assert len(spans) == 1
assert spans[0].name == "ml.open_job"
assert spans[0].attributes == {
"db.system.name": "elasticsearch",
"url.full": "http://localhost:9200/_ml/anomaly_detectors/my-job/_open",
"http.request.method": "POST",
"server.address": "localhost",
"server.port": 9200,
"db.operation.name": "my-job/_open",
"http.request.method": "POST",
"db.namespace": "e9106fc68e3044f0b1475b04bf4ffd5f",
"elasticsearch.node.name": "instance-0000000001",
"db.response.status_code": "202",
"db.elasticsearch.cluster.name": "e9106fc68e3044f0b1475b04bf4ffd5f",
"db.elasticsearch.node.name": "instance-0000000001",
}


Expand All @@ -103,7 +95,5 @@ def test_db_statement():
assert len(spans) == 1
assert spans[0].name == "search"
assert spans[0].attributes == {
"db.system.name": "elasticsearch",
"db.operation.name": "search",
"db.query.text": '{"query":{"match_all":{}}}',
"db.statement": '{"query":{"match_all":{}}}',
}