|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from unittest.mock import patch |
| 16 | + |
| 17 | +from opentelemetry.exporter.otlp.trace_exporter import OTLPSpanExporter |
| 18 | +from opentelemetry.sdk.trace import TracerProvider, sampling |
| 19 | +from opentelemetry.sdk.trace.export import ( |
| 20 | + BatchExportSpanProcessor, |
| 21 | + SimpleExportSpanProcessor, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def get_tracer_with_processor(span_processor_class): |
| 26 | + span_processor = span_processor_class(OTLPSpanExporter()) |
| 27 | + tracer = TracerProvider( |
| 28 | + active_span_processor=span_processor, sampler=sampling.DEFAULT_ON, |
| 29 | + ).get_tracer("pipeline_benchmark_tracer") |
| 30 | + return tracer |
| 31 | + |
| 32 | + |
| 33 | +class MockTraceServiceStub(object): |
| 34 | + def __init__(self, channel): |
| 35 | + self.Export = lambda *args, **kwargs: None |
| 36 | + |
| 37 | + |
| 38 | +@patch( |
| 39 | + "opentelemetry.exporter.otlp.trace_exporter.OTLPSpanExporter._stub", |
| 40 | + new=MockTraceServiceStub, |
| 41 | +) |
| 42 | +def test_simple_span_processor(benchmark): |
| 43 | + tracer = get_tracer_with_processor(SimpleExportSpanProcessor) |
| 44 | + |
| 45 | + def create_spans_to_be_exported(): |
| 46 | + span = tracer.start_span("benchmarkedSpan",) |
| 47 | + for i in range(10): |
| 48 | + span.set_attribute( |
| 49 | + "benchmarkAttribute_{}".format(i), |
| 50 | + "benchmarkAttrValue_{}".format(i), |
| 51 | + ) |
| 52 | + span.end() |
| 53 | + |
| 54 | + benchmark(create_spans_to_be_exported) |
| 55 | + |
| 56 | + |
| 57 | +@patch( |
| 58 | + "opentelemetry.exporter.otlp.trace_exporter.OTLPSpanExporter._stub", |
| 59 | + new=MockTraceServiceStub, |
| 60 | +) |
| 61 | +def test_batch_span_processor(benchmark): |
| 62 | + """Runs benchmark tests using BatchExportSpanProcessor. |
| 63 | +
|
| 64 | + One particular call by pytest-benchmark will be much more expensive since |
| 65 | + the batch export thread will activate and consume a lot of CPU to process |
| 66 | + all the spans. For this reason, focus on the average measurement. Do not |
| 67 | + focus on the min/max measurements which will be misleading. |
| 68 | + """ |
| 69 | + tracer = get_tracer_with_processor(BatchExportSpanProcessor) |
| 70 | + |
| 71 | + def create_spans_to_be_exported(): |
| 72 | + span = tracer.start_span("benchmarkedSpan",) |
| 73 | + for i in range(10): |
| 74 | + span.set_attribute( |
| 75 | + "benchmarkAttribute_{}".format(i), |
| 76 | + "benchmarkAttrValue_{}".format(i), |
| 77 | + ) |
| 78 | + span.end() |
| 79 | + |
| 80 | + benchmark(create_spans_to_be_exported) |
0 commit comments