diff --git a/automon/integrations/openTelemetryWrapper/client.py b/automon/integrations/openTelemetryWrapper/client.py index 479b2bfc..fb553acd 100644 --- a/automon/integrations/openTelemetryWrapper/client.py +++ b/automon/integrations/openTelemetryWrapper/client.py @@ -1,6 +1,8 @@ import json from opentelemetry.util import types +from opentelemetry.trace import Status, StatusCode + from automon.log import logging from .config import OpenTelemetryConfig @@ -14,28 +16,47 @@ class OpenTelemetryClient(object): def __init__(self): self.config = OpenTelemetryConfig() - def add_event(self, name: str, attributes: types.Attributes = None, **kwargs): - return self.config.current_span.add_event(name=name, attributes=attributes, **kwargs) + async def add_event(self, name: str, attributes: types.Attributes = None, **kwargs): + logger.debug(dict(name=name, attributes=attributes, kwargs=kwargs)) + span = await self.current_span() + return span.add_event(name=name, attributes=attributes, **kwargs) async def clear(self): + logger.debug('clear') return await self.config.clear() + async def current_span(self): + logger.debug('current_span') + return await self.config.current_span() + async def is_ready(self): if await self.config.is_ready(): return True async def get_finished_spans(self): + logger.debug('get_finished_spans') return await self.config.get_finished_spans() async def pop_finished_spans(self): + logger.debug('pop_finished_spans') return await self.config.pop_finished_spans() - def record_exception(self, exception: Exception): - return self.config.current_span.record_exception(exception=exception) - - def start_as_current_span(self, name: str, attributes: types.Attributes = None, **kwargs): + async def record_exception(self, exception: Exception): + logger.error(f'{exception}') + span = await self.current_span() + span.set_status(Status(StatusCode.ERROR)) + return span.record_exception(exception=exception) + + async def start_as_current_span( + self, name: str, + attributes: types.Attributes = None, + **kwargs + ): + logger.debug(dict(name=name, attributes=attributes, kwargs=kwargs)) return self.config.tracer.start_as_current_span( - name=name, attributes=attributes, **kwargs) + name=name, + attributes=attributes, + **kwargs) async def start_consumer(self): """adds spans from memory to queue""" @@ -50,10 +71,14 @@ async def start_producer(self): return async def test(self): - with self.start_as_current_span(name='rootSpan') as trace_root: - with self.start_as_current_span(name='childSpan') as trace_child: - self.add_event('AAAAAAAA') - self.add_event('BBBBBBBB') + with await self.start_as_current_span(name='rootSpan') as trace_root: + await self.add_event('AAAAAAAA') + + with await self.start_as_current_span(name='childSpan') as trace_child: + await self.add_event('AAAAAAAA') + await self.add_event('BBBBBBBB') + + await self.add_event('BBBBBBBB') return True diff --git a/automon/integrations/openTelemetryWrapper/config.py b/automon/integrations/openTelemetryWrapper/config.py index 99462265..e496b5ba 100644 --- a/automon/integrations/openTelemetryWrapper/config.py +++ b/automon/integrations/openTelemetryWrapper/config.py @@ -1,7 +1,6 @@ import asyncio +import opentelemetry -from opentelemetry import trace -from opentelemetry.trace import Status, StatusCode from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import SimpleSpanProcessor from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter @@ -19,25 +18,27 @@ def __init__(self): self.processor = SimpleSpanProcessor(self.memory_processor) self.provider.add_span_processor(self.processor) - trace.set_tracer_provider(self.provider) + opentelemetry.trace.set_tracer_provider(self.provider) - self.tracer = trace.get_tracer(__name__) + self.tracer = opentelemetry.trace.get_tracer(__name__) self.queue_consumer = asyncio.Queue() self.queue_producer = asyncio.Queue() async def clear(self): + logger.debug('clear') return self.memory_processor.clear() - @property - def current_span(self): - return trace.get_current_span() + async def current_span(self): + logger.debug('get_current_span') + return opentelemetry.trace.get_current_span() async def is_ready(self): if self.provider and self.memory_processor and self.processor: return True async def get_finished_spans(self): + logger.debug('get_finished_spans') return self.memory_processor.get_finished_spans() async def pop_finished_spans(self): diff --git a/automon/integrations/openTelemetryWrapper/test/test_memory_trace_client.py b/automon/integrations/openTelemetryWrapper/test/test_memory_trace_client.py index 6de34bce..6fa4b062 100644 --- a/automon/integrations/openTelemetryWrapper/test/test_memory_trace_client.py +++ b/automon/integrations/openTelemetryWrapper/test/test_memory_trace_client.py @@ -9,8 +9,12 @@ class MyTestCase(unittest.TestCase): def test_something(self): self.assertTrue(asyncio.run( - self.client.test()) - ) + self.client.test())) + + spans = asyncio.run( + self.client.get_finished_spans()) + + pass if __name__ == '__main__':