1919
2020import base64
2121import datetime
22+ import inspect
2223import logging
2324import os
2425import secrets
3233from google .auth import environment_vars
3334from google .cloud .exceptions import NotFound
3435
35- from google .cloud .storage ._opentelemetry_tracing import (
36- _is_bucket_metadata_disabled ,
37- )
38- from google .cloud .storage ._opentelemetry_tracing import (
39- create_trace_span as _base_create_trace_span ,
40- )
36+ from google .cloud .storage import _opentelemetry_tracing
4137from google .cloud .storage .constants import _DEFAULT_TIMEOUT
4238from google .cloud .storage .retry import (
4339 DEFAULT_RETRY ,
4440 DEFAULT_RETRY_IF_METAGENERATION_SPECIFIED ,
4541)
4642
43+ _base_create_trace_span = None
44+
4745_logger = logging .getLogger (__name__ )
4846
4947STORAGE_EMULATOR_ENV_VAR = "STORAGE_EMULATOR_HOST" # Despite name, includes scheme.
@@ -172,7 +170,8 @@ def _prepare_base_cm(self):
172170 and client
173171 and hasattr (client , "_bucket_metadata_cache" )
174172 and client ._bucket_metadata_cache
175- and not _is_bucket_metadata_disabled ()
173+ and _opentelemetry_tracing ._is_otel_traces_enabled ()
174+ and not _opentelemetry_tracing ._is_bucket_metadata_disabled ()
176175 ):
177176 try :
178177 if name in (
@@ -202,7 +201,12 @@ def _prepare_base_cm(self):
202201 if "client" not in kwargs and client :
203202 kwargs ["client" ] = client
204203
205- self ._base_cm = _base_create_trace_span (name , attributes = span_attrs , ** kwargs )
204+ create_span_fn = (
205+ _base_create_trace_span
206+ if _base_create_trace_span is not None
207+ else _opentelemetry_tracing .create_trace_span
208+ )
209+ self ._base_cm = create_span_fn (name , attributes = span_attrs , ** kwargs )
206210 return self ._base_cm
207211
208212 def _handle_not_found (self ):
@@ -212,6 +216,7 @@ def _handle_not_found(self):
212216 and self .client
213217 and hasattr (self .client , "_bucket_metadata_cache" )
214218 and self .client ._bucket_metadata_cache
219+ and _opentelemetry_tracing ._is_otel_traces_enabled ()
215220 ):
216221 try :
217222 self .client ._bucket_metadata_cache .check_and_evict (self .bucket_name )
@@ -235,15 +240,21 @@ def __exit__(self, exc_type, exc_val, exc_tb):
235240
236241 async def __aenter__ (self ):
237242 self ._prepare_base_cm ()
238- return await self ._base_cm .__aenter__ ()
243+ if hasattr (self ._base_cm , "__aenter__" ):
244+ res = self ._base_cm .__aenter__ ()
245+ return await res if inspect .isawaitable (res ) else res
246+ return self ._base_cm .__enter__ ()
239247
240248 async def __aexit__ (self , exc_type , exc_val , exc_tb ):
241249 if exc_val is not None and isinstance (
242250 exc_val , (NotFound , api_exceptions .NotFound )
243251 ):
244252 self ._handle_not_found ()
245253 if self ._base_cm is not None :
246- return await self ._base_cm .__aexit__ (exc_type , exc_val , exc_tb )
254+ if hasattr (self ._base_cm , "__aexit__" ):
255+ res = self ._base_cm .__aexit__ (exc_type , exc_val , exc_tb )
256+ return await res if inspect .isawaitable (res ) else res
257+ return self ._base_cm .__exit__ (exc_type , exc_val , exc_tb )
247258 return False
248259
249260
0 commit comments