Skip to content

Commit d51a0a7

Browse files
fix: db.params OpenTelemetry integration issue (#346)
When db.params are not all of the same type OpenTelemetry will log a warning. Prevent this by stringifying values. Also adds a SQLALCHEMY_SPANNER_TRACE_HIDE_QUERY_PARAMETERS env var to prevent logging db.params to OpenTelemetry. Co-authored-by: Knut Olav Løite <koloite@gmail.com>
1 parent b7e5fac commit d51a0a7

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

packages/sqlalchemy-spanner/google/cloud/sqlalchemy_spanner/_opentelemetry_tracing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
"""Manages OpenTelemetry trace creation and handling"""
1616

17+
import collections
18+
import os
19+
1720
from contextlib import contextmanager
1821

1922
from google.api_core.exceptions import GoogleAPICallError
@@ -46,6 +49,16 @@ def trace_call(name, extra_attributes=None):
4649
}
4750

4851
if extra_attributes:
52+
if os.environ.get("SQLALCHEMY_SPANNER_TRACE_HIDE_QUERY_PARAMETERS"):
53+
extra_attributes.pop("db.params", None)
54+
55+
# Stringify "db.params" sequence values before sending to OpenTelemetry,
56+
# otherwise OpenTelemetry may log a Warning if types differ.
57+
if isinstance(extra_attributes, dict):
58+
for k, v in extra_attributes.items():
59+
if k == "db.params" and isinstance(v, collections.abc.Sequence):
60+
extra_attributes[k] = [str(e) for e in v]
61+
4962
attributes.update(extra_attributes)
5063

5164
with tracer.start_as_current_span(

0 commit comments

Comments
 (0)