Skip to content

Commit f349e38

Browse files
authored
asyncpg: Use only the first word from query as a span name (#1324)
1 parent 8e0c8d9 commit f349e38

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

CHANGELOG.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616

1717

18+
- `opentelemetry-instrumentation-asyncpg` Fix high cardinality in the span name
19+
([#1324](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1324))
20+
1821
### Added
1922

2023
- `opentelemetry-instrumentation-grpc` add supports to filter requests to instrument. ([#1241](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1241))
@@ -42,7 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4245
([#1208](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1208))
4346
- `opentelemetry-instrumentation-aiohttp-client` Fix producing additional spans with each newly created ClientSession
4447
- ([#1246](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1246))
45-
- Add _is_openetlemetry_instrumented check in _InstrumentedFastAPI class
48+
- Add _is_opentelemetry_instrumented check in _InstrumentedFastAPI class
4649
([#1313](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1313))
4750
- Fix uninstrumentation of existing app instances in FastAPI
4851
([#1258](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1258))

instrumentation/opentelemetry-instrumentation-asyncpg/src/opentelemetry/instrumentation/asyncpg/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ async def _do_execute(self, func, instance, args, kwargs):
134134
params = getattr(instance, "_params", {})
135135
name = args[0] if args[0] else params.get("database", "postgresql")
136136

137+
try:
138+
name = name.split()[0]
139+
except IndexError:
140+
name = ""
141+
137142
with self._tracer.start_as_current_span(
138143
name, kind=SpanKind.CLIENT
139144
) as span:

tests/opentelemetry-docker-tests/tests/asyncpg/test_asyncpg_functional.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def test_instrumented_execute_method_without_arguments(self, *_, **__):
6262
self.assertEqual(len(spans), 1)
6363
self.assertIs(StatusCode.UNSET, spans[0].status.status_code)
6464
self.check_span(spans[0])
65-
self.assertEqual(spans[0].name, "SELECT 42;")
65+
self.assertEqual(spans[0].name, "SELECT")
6666
self.assertEqual(
6767
spans[0].attributes[SpanAttributes.DB_STATEMENT], "SELECT 42;"
6868
)
@@ -72,6 +72,7 @@ def test_instrumented_fetch_method_without_arguments(self, *_, **__):
7272
spans = self.memory_exporter.get_finished_spans()
7373
self.assertEqual(len(spans), 1)
7474
self.check_span(spans[0])
75+
self.assertEqual(spans[0].name, "SELECT")
7576
self.assertEqual(
7677
spans[0].attributes[SpanAttributes.DB_STATEMENT], "SELECT 42;"
7778
)
@@ -189,7 +190,7 @@ def test_instrumented_execute_method_with_arguments(self, *_, **__):
189190
self.assertIs(StatusCode.UNSET, spans[0].status.status_code)
190191

191192
self.check_span(spans[0])
192-
self.assertEqual(spans[0].name, "SELECT $1;")
193+
self.assertEqual(spans[0].name, "SELECT")
193194
self.assertEqual(
194195
spans[0].attributes[SpanAttributes.DB_STATEMENT], "SELECT $1;"
195196
)
@@ -203,6 +204,7 @@ def test_instrumented_fetch_method_with_arguments(self, *_, **__):
203204
self.assertEqual(len(spans), 1)
204205

205206
self.check_span(spans[0])
207+
self.assertEqual(spans[0].name, "SELECT")
206208
self.assertEqual(
207209
spans[0].attributes[SpanAttributes.DB_STATEMENT], "SELECT $1;"
208210
)

0 commit comments

Comments
 (0)