diff --git a/automon/integrations/openTelemetryWrapper/client.py b/automon/integrations/openTelemetryWrapper/client.py index 8528ceda..479b2bfc 100644 --- a/automon/integrations/openTelemetryWrapper/client.py +++ b/automon/integrations/openTelemetryWrapper/client.py @@ -1,5 +1,6 @@ import json +from opentelemetry.util import types from automon.log import logging from .config import OpenTelemetryConfig @@ -13,6 +14,9 @@ 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 clear(self): return await self.config.clear() @@ -23,6 +27,16 @@ async def is_ready(self): async def get_finished_spans(self): return await self.config.get_finished_spans() + async def pop_finished_spans(self): + 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): + return self.config.tracer.start_as_current_span( + name=name, attributes=attributes, **kwargs) + async def start_consumer(self): """adds spans from memory to queue""" while True: @@ -35,6 +49,14 @@ async def start_producer(self): pass 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') + + return True + async def to_dict(self): return [ json.loads(span.to_json()) @@ -50,12 +72,11 @@ async def to_datadog(self): hostname = span['context']['trace_id'] service = span['context']['span_id'] - span['datadog'] = dict( + log.append(dict( ddsource=ddsource, ddtags=ddtags, hostname=hostname, service=service, message=message, - ) - log.append(span) + )) return log diff --git a/automon/integrations/openTelemetryWrapper/config.py b/automon/integrations/openTelemetryWrapper/config.py index 1126e16e..99462265 100644 --- a/automon/integrations/openTelemetryWrapper/config.py +++ b/automon/integrations/openTelemetryWrapper/config.py @@ -1,6 +1,7 @@ import asyncio 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 @@ -28,6 +29,10 @@ def __init__(self): async def clear(self): return self.memory_processor.clear() + @property + def current_span(self): + return trace.get_current_span() + async def is_ready(self): if self.provider and self.memory_processor and self.processor: return True @@ -42,8 +47,10 @@ async def pop_finished_spans(self): return spans async def test(self): - with self.tracer.start_as_current_span("rootSpan"): - with self.tracer.start_as_current_span("childSpan"): + with self.tracer.start_as_current_span(name="rootSpan") as trace_root: + with self.tracer.start_as_current_span(name="childSpan") as trace_child: + trace_child.add_event('AAAAAAAA') + trace_child.add_event('BBBBBBBB') print("Hello world!") return True diff --git a/automon/integrations/openTelemetryWrapper/test/test_memory_trace_client.py b/automon/integrations/openTelemetryWrapper/test/test_memory_trace_client.py new file mode 100644 index 00000000..6de34bce --- /dev/null +++ b/automon/integrations/openTelemetryWrapper/test/test_memory_trace_client.py @@ -0,0 +1,17 @@ +import unittest +import asyncio + +from automon.integrations.openTelemetryWrapper import OpenTelemetryClient + + +class MyTestCase(unittest.TestCase): + client = OpenTelemetryClient() + + def test_something(self): + self.assertTrue(asyncio.run( + self.client.test()) + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/automon/integrations/openTelemetryWrapper/test/test_memory_trace.py b/automon/integrations/openTelemetryWrapper/test/test_memory_trace_config.py similarity index 100% rename from automon/integrations/openTelemetryWrapper/test/test_memory_trace.py rename to automon/integrations/openTelemetryWrapper/test/test_memory_trace_config.py