|
| 1 | +import bm |
| 2 | +import bm.utils as utils |
| 3 | +from opentelemetry.sdk.trace import TracerProvider |
| 4 | +from opentelemetry.trace import SpanContext |
| 5 | +from opentelemetry.trace import get_tracer |
| 6 | +from opentelemetry.trace import set_tracer_provider |
| 7 | +from opentelemetry.trace.status import Status as OtelStatus |
| 8 | +from opentelemetry.trace.status import StatusCode as OtelStatusCode |
| 9 | + |
| 10 | + |
| 11 | +set_tracer_provider(TracerProvider()) |
| 12 | +otel_tracer = get_tracer(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class OtelSdkSpan(bm.Scenario): |
| 16 | +nspans: int |
| 17 | +ntags: int |
| 18 | +ltags: int |
| 19 | +nmetrics: int |
| 20 | +finishspan: bool |
| 21 | +telemetry: bool |
| 22 | +add_event: bool |
| 23 | +add_link: bool |
| 24 | +get_context: bool |
| 25 | +is_recording: bool |
| 26 | +record_exception: bool |
| 27 | +set_status: bool |
| 28 | +update_name: bool |
| 29 | + |
| 30 | +def run(self): |
| 31 | +# run scenario to also set tags on spans |
| 32 | +tags = utils.gen_tags(self) |
| 33 | +settags = len(tags) > 0 |
| 34 | + |
| 35 | +# run scenario to also set metrics on spans |
| 36 | +metrics = utils.gen_metrics(self) |
| 37 | +setmetrics = len(metrics) > 0 |
| 38 | + |
| 39 | +# run scenario to include finishing spans |
| 40 | +# Note - if finishspan is False the span will be gc'd when the SpanAggregrator._traces is reset |
| 41 | +# (ex: tracer.configure(filter) is called) |
| 42 | +finishspan = self.finishspan |
| 43 | +# config._telemetry_enabled = self.telemetry |
| 44 | +add_event = self.add_event |
| 45 | +add_link = self.add_link |
| 46 | +get_context = self.get_context |
| 47 | +is_recording = self.is_recording |
| 48 | +record_exception = self.record_exception |
| 49 | +set_status = self.set_status |
| 50 | +update_name = self.update_name |
| 51 | + |
| 52 | +# Pre-allocate all of the unique strings we'll need, that way the baseline memory overhead |
| 53 | +# is held constant throughout all tests. |
| 54 | +test_attributes = {"key": "value"} |
| 55 | +test_exception = RuntimeError("test_exception") |
| 56 | +test_link_context = SpanContext(trace_id=1, span_id=2, is_remote=False) |
| 57 | +test_status = OtelStatus(OtelStatusCode.ERROR, "something went wrong") |
| 58 | + |
| 59 | +def _(loops): |
| 60 | +for _ in range(loops): |
| 61 | +for i in range(self.nspans): |
| 62 | +s = otel_tracer.start_span("test." + str(i)) |
| 63 | + |
| 64 | +if settags: |
| 65 | +s.set_attributes(tags) |
| 66 | +if setmetrics: |
| 67 | +s.set_attributes(metrics) |
| 68 | +if add_event: |
| 69 | +s.add_event("test.event", test_attributes) |
| 70 | +if add_link: |
| 71 | +s.add_link(test_link_context) |
| 72 | +if get_context: |
| 73 | +_ = s.get_span_context() |
| 74 | +if is_recording: |
| 75 | +_ = s.is_recording() |
| 76 | +if record_exception: |
| 77 | +s.record_exception(test_exception) |
| 78 | +if set_status: |
| 79 | +s.set_status(test_status) |
| 80 | +if update_name: |
| 81 | +s.update_name("test.renamed." + str(i)) |
| 82 | +if finishspan: |
| 83 | +s.end() |
| 84 | + |
| 85 | +yield _ |
0 commit comments