Skip to content

Commit

Permalink
opentelemetry: add event. add exception. add contextmanager from client.
Browse files Browse the repository at this point in the history
 update datadog
  • Loading branch information
naisanzaa committed Apr 3, 2024
1 parent 57bb53c commit 03f5c91
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
27 changes: 24 additions & 3 deletions automon/integrations/openTelemetryWrapper/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json

from opentelemetry.util import types
from automon.log import logging

from .config import OpenTelemetryConfig
Expand All @@ -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()

Expand All @@ -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:
Expand All @@ -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())
Expand All @@ -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
11 changes: 9 additions & 2 deletions automon/integrations/openTelemetryWrapper/config.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 03f5c91

Please sign in to comment.