Skip to content

Commit

Permalink
ZO-1629: Work around NonRecordingSpan opentelemetry bug
Browse files Browse the repository at this point in the history
AFAICT otel violates its own API here, should use get_span_context() instead:
  File "opentelemetry/instrumentation/utils.py", line 160, in _generate_opentelemetry_traceparent
    _span_id = trace.format_span_id(span.context.span_id)
  AttributeError: 'NonRecordingSpan' object has no attribute 'context'

I'm backporting open-telemetry/opentelemetry-python-contrib#1200
which sidesteps the issue by using propagate to generate the traceparent header
instead of doing it manually.
  • Loading branch information
wosc committed Jul 28, 2022
1 parent f614eae commit 6d81b45
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions core/docs/changelog/ZO-1629.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ZO-1629: Work around NonRecordingSpan opentelemetry bug
16 changes: 14 additions & 2 deletions core/src/zeit/connector/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from google.cloud.storage.retry import DEFAULT_RETRY
from io import BytesIO, StringIO
from logging import getLogger
from opentelemetry.trace.propagation import tracecontext
from operator import itemgetter
from sqlalchemy import Boolean, TIMESTAMP, Unicode, UnicodeText
from sqlalchemy import Column, ForeignKey, select
Expand All @@ -18,6 +19,7 @@
from zeit.connector.resource import CachedResource
import collections
import opentelemetry.instrumentation.sqlalchemy
import opentelemetry.instrumentation.utils
import os
import os.path
import sqlalchemy
Expand Down Expand Up @@ -66,8 +68,7 @@ def __init__(self, dsn, storage_project, storage_bucket,
self.session = sqlalchemy.orm.scoped_session(
sqlalchemy.orm.sessionmaker(bind=self.engine, future=True))
zope.sqlalchemy.register(self.session)
EngineTracer(zeit.cms.tracing.default_tracer(),
self.engine, enable_commenter=True)
EngineTracer(zeit.cms.tracing.default_tracer(), self.engine)
self.gcs_client = storage.Client(project=storage_project)
self.bucket = self.gcs_client.bucket(storage_bucket)

Expand Down Expand Up @@ -448,4 +449,15 @@ def _before_cur_exec(
for k, v in params.items():
p.write('%s=%r\n' % (k, str(v)[:100]))
context._otel_span.set_attribute('db.parameters', p.getvalue())
# XXX We bypass enable_commenter in superclass, until
# opentelemetry-python-contrib#1200 is released.
statement += self._generate_comment()
return statement, params

propagator = tracecontext.TraceContextTextMapPropagator()

def _generate_comment(self):
meta = {}
self.propagator.inject(meta)
return opentelemetry.instrumentation.utils._generate_sql_comment(
**meta)

0 comments on commit 6d81b45

Please sign in to comment.