Skip to content

Commit 4aa8ca6

Browse files
authored
Fix lost db context in elasticsearch instrumentation + Elastic Cloud (#1878)
* Fix instance information overriding db context in elasticsearch * CHANGELOG
1 parent 4940236 commit 4aa8ca6

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

CHANGELOG.asciidoc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ endif::[]
3838
3939
* Add support for grpc aio server interceptor {pull}1870[#1870]
4040
41-
//[float]
42-
//===== Bug fixes
43-
//
41+
[float]
42+
===== Bug fixes
43+
44+
* Fix a bug in the Elasticsearch client instrumentation which was causing loss of database context (including statement) when interacting with Elastic Cloud {pull}1878[#1878]
4445
4546
4647
[[release-notes-6.x]]

elasticapm/instrumentation/packages/asyncio/elasticsearch.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,14 @@ async def call(self, module, method, wrapped, instance, args, kwargs):
6363
result = await wrapped(*args, **kwargs)
6464
if hasattr(result, "meta"): # elasticsearch-py 8.x+
6565
status_code = result.meta.status
66+
cluster = result.meta.headers.get("x-found-handling-cluster")
6667
else:
6768
status_code = result[0]
69+
cluster = result[1].get("x-found-handling-cluster")
6870

69-
span.context["http"] = {"status_code": status_code}
71+
span.update_context("http", {"status_code": status_code})
72+
if cluster:
73+
span.update_context("db", {"instance": cluster})
7074

7175
return result
7276

elasticapm/instrumentation/packages/elasticsearch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ def call(self, module, method, wrapped, instance, args, kwargs):
7575
else:
7676
status_code = result[0]
7777
cluster = result[1].get("x-found-handling-cluster")
78-
span.context["http"] = {"status_code": status_code}
78+
span.update_context("http", {"status_code": status_code})
7979
if cluster:
80-
span.context["db"] = {"instance": cluster}
80+
span.update_context("db", {"instance": cluster})
8181

8282
return result
8383

0 commit comments

Comments
 (0)