Skip to content
Permalink
Browse files
fix: use debug logging level for OpenTelemetry message (#442)
* fix: use debug logging level for OpenTelemetry message

* only warn at span creation time

* add unit test for skipping warning

* refactor: rename _warned_telemetry to indicate private and mutable
  • Loading branch information
tswast committed Jan 11, 2021
1 parent fb3ad76 commit 7ea6b7c2469d2415192cfdacc379e38e49d24775
Showing with 24 additions and 7 deletions.
  1. +12 −7 google/cloud/bigquery/opentelemetry_tracing.py
  2. +12 −0 tests/unit/test_opentelemetry_tracing.py
@@ -23,16 +23,11 @@
from opentelemetry.trace.status import Status

HAS_OPENTELEMETRY = True
_warned_telemetry = True

except ImportError:
logger.info(
"This service is instrumented using OpenTelemetry. "
"OpenTelemetry could not be imported; please "
"add opentelemetry-api and opentelemetry-instrumentation "
"packages in order to get BigQuery Tracing data."
)

HAS_OPENTELEMETRY = False
_warned_telemetry = False

_default_attributes = {
"db.system": "BigQuery"
@@ -64,8 +59,18 @@ def create_span(name, attributes=None, client=None, job_ref=None):
Raised if a span could not be yielded or issue with call to
OpenTelemetry.
"""
global _warned_telemetry
final_attributes = _get_final_span_attributes(attributes, client, job_ref)
if not HAS_OPENTELEMETRY:
if not _warned_telemetry:
logger.debug(
"This service is instrumented using OpenTelemetry. "
"OpenTelemetry could not be imported; please "
"add opentelemetry-api and opentelemetry-instrumentation "
"packages in order to get BigQuery Tracing data."
)
_warned_telemetry = True

yield None
return
tracer = trace.get_tracer(__name__)
@@ -52,8 +52,20 @@ def setup():
def test_opentelemetry_not_installed(setup, monkeypatch):
monkeypatch.setitem(sys.modules, "opentelemetry", None)
importlib.reload(opentelemetry_tracing)
assert not opentelemetry_tracing._warned_telemetry
with opentelemetry_tracing.create_span("No-op for opentelemetry") as span:
assert span is None
assert opentelemetry_tracing._warned_telemetry


@pytest.mark.skipif(opentelemetry is None, reason="Require `opentelemetry`")
def test_opentelemetry_not_installed_doesnt_warn(setup, monkeypatch):
monkeypatch.setitem(sys.modules, "opentelemetry", None)
importlib.reload(opentelemetry_tracing)
opentelemetry_tracing._warned_telemetry = True
with opentelemetry_tracing.create_span("No-op for opentelemetry") as span:
assert span is None
assert opentelemetry_tracing._warned_telemetry


@pytest.mark.skipif(opentelemetry is None, reason="Require `opentelemetry`")

0 comments on commit 7ea6b7c

Please sign in to comment.