Skip to content

Commit

Permalink
fix: use debug logging level for OpenTelemetry message (#442)
Browse files Browse the repository at this point in the history
* 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 authored Jan 11, 2021
1 parent fb3ad76 commit 7ea6b7c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
19 changes: 12 additions & 7 deletions google/cloud/bigquery/opentelemetry_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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__)
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/test_opentelemetry_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`")
Expand Down

0 comments on commit 7ea6b7c

Please sign in to comment.