Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aMahanna committed Nov 23, 2023
1 parent 82e0e3d commit 123e6d6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
7 changes: 2 additions & 5 deletions adbpyg_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .abc import Abstract_ADBPyG_Adapter
from .controller import ADBPyG_Controller
from .exceptions import ADBMetagraphError, InvalidADBEdgesError, PyGMetagraphError
from .tracing import TRACING_ENABLED, TracingManager, create_tracer, with_tracing
from .tracing import TRACING_ENABLED, TracingManager, with_tracing
from .typings import (
ADBMap,
ADBMetagraph,
Expand Down Expand Up @@ -113,10 +113,7 @@ def set_tracer(self, tracer: Optional["trace.Tracer"]) -> None:
:raise ImportError: If OpenTelemetry is not installed.
"""
if TRACING_ENABLED:
self.__tracer = tracer or create_tracer("adbpyg-adapter")
TracingManager.set_tracer(self.__tracer)
else:
self.__tracer = None
TracingManager.set_tracer(tracer)

def __set_tracer_attributes(self, **attributes: Any) -> None:
"""Set the OpenTelemetry tracer attributes for the adapter instance.
Expand Down
18 changes: 8 additions & 10 deletions adbpyg_adapter/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from opentelemetry.trace import Tracer

TRACING_ENABLED = True
except ImportError:
except ImportError: # pragma: no cover
TRACING_ENABLED = False


Expand All @@ -35,13 +35,11 @@ def set_tracer(cls, tracer: "Tracer") -> None:
def with_tracing(method: T) -> T:
@wraps(method)
def decorator(*args: Any, **kwargs: Any) -> Any:
tracer = TracingManager.get_tracer()
if tracer := TracingManager.get_tracer():
with tracer.start_as_current_span(method.__name__):
return method(*args, **kwargs)

if tracer is None:
return method(*args, **kwargs)

with tracer.start_as_current_span(method.__name__):
return method(*args, **kwargs)
return method(*args, **kwargs)

return cast(T, decorator)

Expand All @@ -65,18 +63,18 @@ def create_tracer(
:return: A configured tracer instance.
:rtype: opentelemetry.trace.Tracer
"""
if not TRACING_ENABLED:
if not TRACING_ENABLED: # pragma: no cover
m = "OpenTelemetry is not installed. Cannot create tracer. Use `pip install adbpyg-adapter[tracing]`" # noqa: E501
raise RuntimeError(m)

resource = Resource(attributes={SERVICE_NAME: name})
provider = TracerProvider(resource=resource)

if enable_console_tracing:
if enable_console_tracing: # pragma: no cover
console_processor = BatchSpanProcessor(ConsoleSpanExporter())
provider.add_span_processor(console_processor)

for span_exporter in span_exporters:
for span_exporter in span_exporters: # pragma: no cover
provider.add_span_processor(BatchSpanProcessor(span_exporter))

trace.set_tracer_provider(provider)
Expand Down
4 changes: 1 addition & 3 deletions tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,7 @@ def test_pyg_to_arangodb_with_controller() -> None:
data = get_karate_graph()
db.delete_graph(name, drop_collections=True, ignore_missing=True)

ADBPyG_Adapter(db, Custom_ADBPyG_Controller(), tracer=tracer).pyg_to_arangodb(
name, data
)
ADBPyG_Adapter(db, Custom_ADBPyG_Controller()).pyg_to_arangodb(name, data)

for doc in db.collection(f"{name}_N"):
assert "foo" in doc
Expand Down

0 comments on commit 123e6d6

Please sign in to comment.