Skip to content

Commit

Permalink
Tweak sdk tests so they run faster (#3513)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmcollins authored Nov 6, 2023
1 parent 2a8d4ed commit a74e82b
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions opentelemetry-sdk/tests/trace/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
OTEL_BSP_MAX_QUEUE_SIZE,
OTEL_BSP_SCHEDULE_DELAY,
)
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import export
from opentelemetry.sdk.trace.export import logger
from opentelemetry.sdk.trace.export.in_memory_span_exporter import (
Expand Down Expand Up @@ -151,7 +152,7 @@ def test_simple_span_processor_not_sampled(self):
self.assertListEqual([], spans_names_list)


def _create_start_and_end_span(name, span_processor):
def _create_start_and_end_span(name, span_processor, resource):
span = trace._Span(
name,
trace_api.SpanContext(
Expand All @@ -161,6 +162,7 @@ def _create_start_and_end_span(name, span_processor):
trace_flags=trace_api.TraceFlags(trace_api.TraceFlags.SAMPLED),
),
span_processor=span_processor,
resource=resource,
)
span.start()
span.end()
Expand Down Expand Up @@ -245,8 +247,9 @@ def test_shutdown(self):

span_names = ["xxx", "bar", "foo"]

resource = Resource.create({})
for name in span_names:
_create_start_and_end_span(name, span_processor)
_create_start_and_end_span(name, span_processor, resource)

span_processor.shutdown()
self.assertTrue(my_exporter.is_shutdown)
Expand All @@ -264,15 +267,16 @@ def test_flush(self):
span_names0 = ["xxx", "bar", "foo"]
span_names1 = ["yyy", "baz", "fox"]

resource = Resource.create({})
for name in span_names0:
_create_start_and_end_span(name, span_processor)
_create_start_and_end_span(name, span_processor, resource)

self.assertTrue(span_processor.force_flush())
self.assertListEqual(span_names0, spans_names_list)

# create some more spans to check that span processor still works
for name in span_names1:
_create_start_and_end_span(name, span_processor)
_create_start_and_end_span(name, span_processor, resource)

self.assertTrue(span_processor.force_flush())
self.assertListEqual(span_names0 + span_names1, spans_names_list)
Expand All @@ -298,10 +302,12 @@ def test_flush_from_multiple_threads(self):
my_exporter, max_queue_size=512, max_export_batch_size=128
)

resource = Resource.create({})

def create_spans_and_flush(tno: int):
for span_idx in range(num_spans):
_create_start_and_end_span(
f"Span {tno}-{span_idx}", span_processor
f"Span {tno}-{span_idx}", span_processor, resource
)
self.assertTrue(span_processor.force_flush())

Expand All @@ -323,7 +329,8 @@ def test_flush_timeout(self):
)
span_processor = export.BatchSpanProcessor(my_exporter)

_create_start_and_end_span("foo", span_processor)
resource = Resource.create({})
_create_start_and_end_span("foo", span_processor, resource)

# check that the timeout is not meet
with self.assertLogs(level=WARNING):
Expand All @@ -341,8 +348,9 @@ def test_batch_span_processor_lossless(self):
my_exporter, max_queue_size=512, max_export_batch_size=128
)

resource = Resource.create({})
for _ in range(512):
_create_start_and_end_span("foo", span_processor)
_create_start_and_end_span("foo", span_processor, resource)

time.sleep(1)
self.assertTrue(span_processor.force_flush())
Expand All @@ -363,9 +371,10 @@ def test_batch_span_processor_many_spans(self):
schedule_delay_millis=100,
)

resource = Resource.create({})
for _ in range(4):
for _ in range(256):
_create_start_and_end_span("foo", span_processor)
_create_start_and_end_span("foo", span_processor, resource)

time.sleep(0.1) # give some time for the exporter to upload spans

Expand Down Expand Up @@ -467,7 +476,8 @@ def test_batch_span_processor_scheduled_delay(self):
)

# create single span
_create_start_and_end_span("foo", span_processor)
resource = Resource.create({})
_create_start_and_end_span("foo", span_processor, resource)

self.assertTrue(export_event.wait(2))
export_time = time.time()
Expand Down Expand Up @@ -497,7 +507,8 @@ def test_batch_span_processor_reset_timeout(self):
)

with mock.patch.object(span_processor.condition, "wait") as mock_wait:
_create_start_and_end_span("foo", span_processor)
resource = Resource.create({})
_create_start_and_end_span("foo", span_processor, resource)
self.assertTrue(export_event.wait(2))

# give some time for exporter to loop
Expand Down

0 comments on commit a74e82b

Please sign in to comment.