Repository: googleapis/python-bigquery

Issue:
Too noise logging about telemetry
Hello,

In the Apache Airflow project, we use the BigQuery library, but recently we've started to see annoying log message when the library is loaded. It is enough that the library is loaded and there is an message every time. 

In my opinion, this message should be of a lower level (DEBUG) so that it is not displayed much less often or is displayed only when the client is initialized. 
```
import logging
logging.basicConfig(level=logging.INFO)

from google.cloud import bigquery
```
Output: 
```
INFO:google.cloud.bigquery.opentelemetry_tracing: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.
```

Related issue: https://github.com/apache/airflow/issues/13131

CC: @tswast 


Discussion:
@tswast this problem is in part related to the implementation of your AIP. https://cwiki.apache.org/confluence/display/AIRFLOW/AIP-8+Split+Providers+into+Separate+Packages+for+Airflow+2.0
Now we need to load a few more classes to be able to read the attributes of some classes, which unfortunately leads to the BigQuery client being loaded as well.

Resolving patch:
diff --git a/google/cloud/bigquery/opentelemetry_tracing.py b/google/cloud/bigquery/opentelemetry_tracing.py
--- a/google/cloud/bigquery/opentelemetry_tracing.py
+++ b/google/cloud/bigquery/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__)


Test patch:
diff --git a/tests/unit/test_opentelemetry_tracing.py b/tests/unit/test_opentelemetry_tracing.py
--- a/tests/unit/test_opentelemetry_tracing.py
+++ b/tests/unit/test_opentelemetry_tracing.py
@@ -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`")