Skip to content

Commit

Permalink
opentelemetry-sdk: speed up exemplars a bit
Browse files Browse the repository at this point in the history
Use a sparse dict to allocate ExemplarsBucket on demand instead of
preallocating all of them in FixedSizeExemplarReservoirABC.

Make the following return around 2X more loops for both trace_based and
always_off exemplars filter:

.tox/benchmark-opentelemetry-sdk/bin/pytest opentelemetry-sdk/benchmarks/metrics/ -k 'test_histogram_record_1000[7]'
  • Loading branch information
xrmx committed Nov 8, 2024
1 parent c4fa8d7 commit 66ae24e
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@
# limitations under the License.

from abc import ABC, abstractmethod
from collections import defaultdict
from random import randrange
from typing import Any, Callable, Dict, List, Optional, Sequence, Union
from typing import (
Any,
Callable,
Dict,
List,
Mapping,
Optional,
Sequence,
Union,
)

from opentelemetry import trace
from opentelemetry.context import Context
Expand Down Expand Up @@ -155,9 +165,9 @@ class FixedSizeExemplarReservoirABC(ExemplarReservoir):
def __init__(self, size: int, **kwargs) -> None:
super().__init__(**kwargs)
self._size: int = size
self._reservoir_storage: List[ExemplarBucket] = [
ExemplarBucket() for _ in range(self._size)
]
self._reservoir_storage: Mapping[int, ExemplarBucket] = defaultdict(
ExemplarBucket
)

def collect(self, point_attributes: Attributes) -> List[Exemplar]:
"""Returns accumulated Exemplars and also resets the reservoir for the next
Expand All @@ -171,15 +181,16 @@ def collect(self, point_attributes: Attributes) -> List[Exemplar]:
exemplars contain the attributes that were filtered out by the aggregator,
but recorded alongside the original measurement.
"""
exemplars = filter(
lambda e: e is not None,
map(
lambda bucket: bucket.collect(point_attributes),
self._reservoir_storage,
),
)
exemplars = [
e
for e in (
bucket.collect(point_attributes)
for _, bucket in sorted(self._reservoir_storage.items())
)
if e is not None
]
self._reset()
return [*exemplars]
return exemplars

def offer(
self,
Expand Down

0 comments on commit 66ae24e

Please sign in to comment.