Repository navigation
opentelemetry-sdk: add additional logs benchmarks & make additional perf improvements #5784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
herin049
wants to merge
6
commits into
open-telemetry:main
Choose a base branch
from
herin049:feat/logs-benches
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+489
−40
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
190a23f
opentelemetry-sdk: add additional logs benchmarks & make additional p…
herin049 0763d4f
add changelog fragment
herin049 c802762
address co-pilot comment
herin049 e7e4a98
add memory/gc info to benchmarks + kwargs vs. log record emit
herin049 960d761
update logs classes to use __slots__
herin049 1db15f7
update kwargs processor filtering to default to using current context…
herin049 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `opentelemetry-sdk`: add additional logs benchmarks and improve performance for disabled loggers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
opentelemetry-sdk/benchmarks/logs/test_benchmark_logs_enabled.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| # Copyright The OpenTelemetry Authors | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import gc | ||
| import os | ||
| import tracemalloc | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from opentelemetry._logs import LogRecord, SeverityNumber | ||
| from opentelemetry._logs._internal import ProxyLogger | ||
| from opentelemetry.sdk._logs import LoggerProvider, LogRecordProcessor | ||
| from opentelemetry.sdk._logs._internal import _disable_logger_configurator | ||
| from opentelemetry.sdk.environment_variables import OTEL_SDK_DISABLED | ||
|
|
||
|
|
||
| class _SeverityFilteringProcessor(LogRecordProcessor): | ||
| """Processor that only accepts records at or above ``min_severity``. | ||
|
|
||
| ``on_emit`` does no export work so the benchmarks measure the SDK | ||
| overhead of creating and dispatching the record. | ||
| """ | ||
|
|
||
| def __init__(self, min_severity): | ||
| self._min_severity = min_severity | ||
|
|
||
| def on_emit(self, log_record): | ||
| pass | ||
|
|
||
| def enabled( | ||
| self, | ||
| *, | ||
| context=None, | ||
| instrumentation_scope=None, | ||
| severity_number=None, | ||
| event_name=None, | ||
| ): | ||
| return severity_number is None or severity_number.value >= self._min_severity.value | ||
|
|
||
| def shutdown(self): | ||
| pass | ||
|
|
||
| def force_flush(self, timeout_millis=30000): | ||
| return True | ||
|
|
||
|
|
||
| def _make_disabled_logger(mechanism): | ||
| """Returns a logger for which an INFO record is disabled.""" | ||
| if mechanism == "logger_config": | ||
| provider = LoggerProvider( | ||
| shutdown_on_exit=False, | ||
| _logger_configurator=_disable_logger_configurator, | ||
| ) | ||
| provider.add_log_record_processor(_SeverityFilteringProcessor(SeverityNumber.INFO)) | ||
| return provider.get_logger("bench") | ||
| if mechanism == "processor_filter": | ||
| provider = LoggerProvider(shutdown_on_exit=False) | ||
| provider.add_log_record_processor(_SeverityFilteringProcessor(SeverityNumber.WARN)) | ||
| return provider.get_logger("bench") | ||
| if mechanism == "no_processors": | ||
| return LoggerProvider(shutdown_on_exit=False).get_logger("bench") | ||
| if mechanism == "sdk_disabled": | ||
| with mock.patch.dict(os.environ, {OTEL_SDK_DISABLED: "true"}): | ||
| provider = LoggerProvider(shutdown_on_exit=False) | ||
| return provider.get_logger("bench") | ||
| if mechanism == "api_proxy_noop": | ||
| return ProxyLogger("bench") | ||
| raise ValueError(f"unknown mechanism: {mechanism}") | ||
|
|
||
|
|
||
| def _emit_unguarded(logger, index): | ||
| logger.emit( | ||
| severity_number=SeverityNumber.INFO, | ||
| body=f"benchmark log message {index}", | ||
| attributes={ | ||
| "http.request.method": "GET", | ||
| "http.route": "/users/{id}", | ||
| "http.response.status_code": 200, | ||
| "user.id": index, | ||
| "server.address": "example.com", | ||
| }, | ||
| event_name="test.event", | ||
| ) | ||
|
|
||
|
|
||
| def _emit_guarded(logger, index): | ||
| if logger.enabled(severity_number=SeverityNumber.INFO, event_name="test.event"): | ||
| _emit_unguarded(logger, index) | ||
|
|
||
|
|
||
| def _emit_log_record_unguarded(logger, index): | ||
| logger.emit( | ||
| LogRecord( | ||
|
herin049 marked this conversation as resolved.
|
||
| severity_number=SeverityNumber.INFO, | ||
| body=f"benchmark log message {index}", | ||
| attributes={ | ||
| "http.request.method": "GET", | ||
| "http.route": "/users/{id}", | ||
| "http.response.status_code": 200, | ||
| "user.id": index, | ||
| "server.address": "example.com", | ||
| }, | ||
| event_name="test.event", | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def _emit_log_record_guarded(logger, index): | ||
| if logger.enabled(severity_number=SeverityNumber.INFO, event_name="test.event"): | ||
| logger.emit( | ||
| LogRecord( | ||
| severity_number=SeverityNumber.INFO, | ||
| body=f"benchmark log message {index}", | ||
| attributes={ | ||
| "http.request.method": "GET", | ||
| "http.route": "/users/{id}", | ||
| "http.response.status_code": 200, | ||
| "user.id": index, | ||
| "server.address": "example.com", | ||
| }, | ||
| event_name="test.event", | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| _EMIT_FUNCTIONS = { | ||
| ("kwargs", False): _emit_unguarded, | ||
| ("kwargs", True): _emit_guarded, | ||
| ("log_record", False): _emit_log_record_unguarded, | ||
| ("log_record", True): _emit_log_record_guarded, | ||
| } | ||
|
|
||
|
|
||
| def _measure_allocations(benchmark, emit, logger): | ||
| for index in range(100): | ||
| emit(logger, index) | ||
|
|
||
| peaks = [] | ||
| for index in range(200): | ||
| tracemalloc.start() | ||
| emit(logger, index) | ||
| _, peak = tracemalloc.get_traced_memory() | ||
| tracemalloc.stop() | ||
| peaks.append(peak) | ||
| benchmark.extra_info["mean_alloc_bytes"] = sum(peaks) / len(peaks) | ||
|
|
||
| # Measure gen-0 collections before/after emitting 10,000 log records | ||
| gc.collect() | ||
| collections_before = gc.get_stats()[0]["collections"] | ||
| for index in range(10_000): | ||
| emit(logger, index) | ||
| benchmark.extra_info["gc_gen0_collections_per_10k"] = gc.get_stats()[0]["collections"] - collections_before | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("guarded", [False, True], ids=["unguarded", "guarded"]) | ||
| @pytest.mark.parametrize("style", ["kwargs", "log_record"]) | ||
| @pytest.mark.parametrize( | ||
| "mechanism", | ||
| [ | ||
| "logger_config", | ||
| "processor_filter", | ||
| "no_processors", | ||
| "sdk_disabled", | ||
| "api_proxy_noop", | ||
| ], | ||
| ) | ||
| def test_disabled_emit(benchmark, mechanism, style, guarded): | ||
| benchmark.group = f"disabled-{mechanism}-{style}" | ||
| logger = _make_disabled_logger(mechanism) | ||
| emit = _EMIT_FUNCTIONS[(style, guarded)] | ||
| _measure_allocations(benchmark, emit, logger) | ||
|
|
||
| benchmark(emit, logger, 42) | ||
|
aabmass marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @pytest.mark.parametrize("guarded", [False, True], ids=["unguarded", "guarded"]) | ||
| def test_enabled_emit(benchmark, guarded): | ||
| benchmark.group = "enabled-guard-overhead" | ||
| provider = LoggerProvider(shutdown_on_exit=False) | ||
| provider.add_log_record_processor(_SeverityFilteringProcessor(SeverityNumber.INFO)) | ||
| logger = provider.get_logger("bench") | ||
| emit = _emit_guarded if guarded else _emit_unguarded | ||
|
|
||
| benchmark(emit, logger, 42) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.