Skip to content

Update opentelemetry conventions #2999

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 4 commits into from
Jul 25, 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
6 changes: 3 additions & 3 deletions elasticsearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@

# Ensure that a compatible version of elastic-transport is installed.
_version_groups = tuple(int(x) for x in re.search(r"^(\d+)\.(\d+)\.(\d+)", _elastic_transport_version).groups()) # type: ignore[union-attr]
if _version_groups < (8, 0, 0) or _version_groups > (9, 0, 0):
if _version_groups < (9, 1, 0) or _version_groups > (10, 0, 0):
raise ImportError(
"An incompatible version of elastic-transport is installed. Must be between "
"v8.0.0 and v9.0.0. Install the correct version with the following command: "
"$ python -m pip install 'elastic-transport>=8, <9'"
"v9.1.0 and v10.0.0. Install the correct version with the following command: "
"$ python -m pip install 'elastic-transport>=9.1, <10'"
)

_version_groups = re.search(r"^(\d+)\.(\d+)\.(\d+)", __versionstr__).groups() # type: ignore[assignment, union-attr]
Expand Down
10 changes: 5 additions & 5 deletions elasticsearch/_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ def span(
span_name = endpoint_id or method
with self.tracer.start_as_current_span(span_name) as otel_span:
otel_span.set_attribute("http.request.method", method)
otel_span.set_attribute("db.system", "elasticsearch")
otel_span.set_attribute("db.system.name", "elasticsearch")
if endpoint_id is not None:
otel_span.set_attribute("db.operation", endpoint_id)
otel_span.set_attribute("db.operation.name", endpoint_id)
for key, value in path_parts.items():
otel_span.set_attribute(f"db.elasticsearch.path_parts.{key}", value)
otel_span.set_attribute(f"db.operation.parameter.{key}", value)

yield OpenTelemetrySpan(
otel_span,
Expand All @@ -94,8 +94,8 @@ def helpers_span(self, span_name: str) -> Generator[OpenTelemetrySpan]:
return

with self.tracer.start_as_current_span(span_name) as otel_span:
otel_span.set_attribute("db.system", "elasticsearch")
otel_span.set_attribute("db.operation", span_name)
otel_span.set_attribute("db.system.name", "elasticsearch")
otel_span.set_attribute("db.operation.name", span_name)
# Without a request method, Elastic APM does not display the traces
otel_span.set_attribute("http.request.method", "null")
yield OpenTelemetrySpan(otel_span)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ keywords = [
]
dynamic = ["version"]
dependencies = [
"elastic-transport>=8.15.1,<9",
"elastic-transport>=9.1.0,<10",
"python-dateutil",
"typing-extensions",
]
Expand Down
12 changes: 6 additions & 6 deletions test_elasticsearch/test_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_minimal_span():
assert spans[0].name == "GET"
assert spans[0].attributes == {
"http.request.method": "GET",
"db.system": "elasticsearch",
"db.system.name": "elasticsearch",
}


Expand All @@ -92,11 +92,11 @@ def test_detailed_span():
assert spans[0].name == "ml.open_job"
assert spans[0].attributes == {
"http.request.method": "GET",
"db.system": "elasticsearch",
"db.operation": "ml.open_job",
"db.elasticsearch.path_parts.job_id": "my-job",
"db.elasticsearch.cluster.name": "e9106fc68e3044f0b1475b04bf4ffd5f",
"db.elasticsearch.node.name": "instance-0000000001",
"db.system.name": "elasticsearch",
"db.operation.name": "ml.open_job",
"db.operation.parameter.job_id": "my-job",
"db.namespace": "e9106fc68e3044f0b1475b04bf4ffd5f",
"elasticsearch.node.name": "instance-0000000001",
}


Expand Down
16 changes: 8 additions & 8 deletions test_elasticsearch/test_server/test_otel.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def test_otel_end_to_end(sync_client):
assert spans[0].name == "search"
expected_attributes = {
"http.request.method": "POST",
"db.system": "elasticsearch",
"db.operation": "search",
"db.elasticsearch.path_parts.index": "logs-*",
"db.system.name": "elasticsearch",
"db.operation.name": "search",
"db.operation.parameter.index": "logs-*",
}
# Assert expected atttributes are here, but allow other attributes too
# to make this test robust to elastic-transport changes
Expand Down Expand Up @@ -89,8 +89,8 @@ def test_otel_bulk(sync_client, elasticsearch_url, bulk_helper_name):
parent_span = spans.pop()
assert parent_span.name == f"helpers.{bulk_helper_name}"
assert parent_span.attributes == {
"db.system": "elasticsearch",
"db.operation": f"helpers.{bulk_helper_name}",
"db.system.name": "elasticsearch",
"db.operation.name": f"helpers.{bulk_helper_name}",
"http.request.method": "null",
}

Expand All @@ -99,9 +99,9 @@ def test_otel_bulk(sync_client, elasticsearch_url, bulk_helper_name):
assert span.name == "bulk"
expected_attributes = {
"http.request.method": "PUT",
"db.system": "elasticsearch",
"db.operation": "bulk",
"db.elasticsearch.path_parts.index": "test-index",
"db.system.name": "elasticsearch",
"db.operation.name": "bulk",
"db.operation.parameter.index": "test-index",
}
# Assert expected atttributes are here, but allow other attributes too
# to make this test robust to elastic-transport changes
Expand Down