Skip to content

Commit

Permalink
ZO-4712: Move testing support for tracing into main code, so we can r…
Browse files Browse the repository at this point in the history
…euse it e.g. from zeit.storage
  • Loading branch information
wosc committed Mar 19, 2024
1 parent 2bf1d95 commit 83e7e89
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 41 deletions.
39 changes: 0 additions & 39 deletions core/src/zeit/cms/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import unittest
import xml.sax.saxutils

from opentelemetry.sdk.trace.export import SimpleSpanProcessor
import celery.contrib.testing.app
import celery.contrib.testing.worker
import celery_longterm_scheduler
Expand All @@ -30,8 +29,6 @@
import lxml.cssselect
import lxml.etree
import lxml.html
import opentelemetry.sdk.trace
import opentelemetry.sdk.trace.export.in_memory_span_exporter as otel_export
import plone.testing
import plone.testing.zca
import plone.testing.zodb
Expand Down Expand Up @@ -1218,39 +1215,3 @@ def xmltotext(xml):
xml = copy.deepcopy(xml)
lxml.etree.indent(xml)
return lxml.etree.tostring(xml, encoding=str)


class Trace:
def __init__(self, exporter):
self._exporter = exporter

@property
def spans(self):
return self._exporter.get_finished_spans()

def __getitem__(self, name):
for span in self.spans:
if span.name == name:
return span
raise KeyError(name)

@staticmethod
def provider():
provider = opentelemetry.sdk.trace.TracerProvider()
exporter = otel_export.InMemorySpanExporter()
provider.add_span_processor(SimpleSpanProcessor(exporter))
return provider, exporter


@contextlib.contextmanager
def captrace():
provider, exporter = Trace.provider()
set_tracer_provider(provider)
yield Trace(exporter)
exporter.clear()
set_tracer_provider(None)


def set_tracer_provider(provider):
opentelemetry.trace._TRACER_PROVIDER_SET_ONCE._done = False # sigh
opentelemetry.trace.set_tracer_provider(provider)
2 changes: 1 addition & 1 deletion core/src/zeit/cms/tests/test_tracing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import opentelemetry.trace

from zeit.cms.testing import set_tracer_provider
from zeit.cms.tracing import _testing_set_tracer_provider as set_tracer_provider
import zeit.cms.testing
import zeit.cms.tracing

Expand Down
43 changes: 42 additions & 1 deletion core/src/zeit/cms/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import Tracer, TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor, ConsoleSpanExporter
from opentelemetry.sdk.trace.export import (
BatchSpanProcessor,
ConsoleSpanExporter,
SimpleSpanProcessor,
)
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
except ImportError:
TracerProvider = object
Expand Down Expand Up @@ -199,3 +204,39 @@ def anonymize(value):
else:
iv = os.urandom(16)
return Fernet(key)._encrypt_from_parts(value.encode('utf-8'), ts, iv).decode('ascii')


class TestTrace:
def __init__(self, exporter):
self._exporter = exporter

@property
def spans(self):
return self._exporter.get_finished_spans()

def __getitem__(self, name):
for span in self.spans:
if span.name == name:
return span
raise KeyError(name)

@staticmethod
def provider():
provider = TracerProvider()
exporter = InMemorySpanExporter()
provider.add_span_processor(SimpleSpanProcessor(exporter))
return provider, exporter


@contextlib.contextmanager
def captrace():
provider, exporter = TestTrace.provider()
_testing_set_tracer_provider(provider)
yield TestTrace(exporter)
exporter.clear()
_testing_set_tracer_provider(None)


def _testing_set_tracer_provider(provider):
opentelemetry.trace._TRACER_PROVIDER_SET_ONCE._done = False # sigh
opentelemetry.trace.set_tracer_provider(provider)

0 comments on commit 83e7e89

Please sign in to comment.