|
12 | 12 | # See the License for the specific language governing permissions and
|
13 | 13 | # limitations under the License.
|
14 | 14 |
|
15 |
| -from typing import Iterable |
| 15 | +from threading import RLock |
| 16 | +from typing import Dict, Iterable, List |
16 | 17 |
|
17 |
| -from opentelemetry.sdk._metrics.aggregation import AggregationTemporality |
| 18 | +from opentelemetry._metrics.instrument import Counter, Histogram, Instrument |
| 19 | +from opentelemetry.sdk._metrics._view_instrument_match import ( |
| 20 | + _ViewInstrumentMatch, |
| 21 | +) |
| 22 | +from opentelemetry.sdk._metrics.aggregation import ( |
| 23 | + AggregationTemporality, |
| 24 | + ExplicitBucketHistogramAggregation, |
| 25 | + LastValueAggregation, |
| 26 | + SumAggregation, |
| 27 | +) |
18 | 28 | from opentelemetry.sdk._metrics.measurement import Measurement
|
19 | 29 | from opentelemetry.sdk._metrics.point import Metric
|
20 | 30 | from opentelemetry.sdk._metrics.sdk_configuration import SdkConfiguration
|
| 31 | +from opentelemetry.sdk._metrics.view import View |
21 | 32 |
|
22 | 33 |
|
23 |
| -# TODO: #2378 |
24 | 34 | class MetricReaderStorage:
|
25 | 35 | """The SDK's storage for a given reader"""
|
26 | 36 |
|
27 | 37 | def __init__(self, sdk_config: SdkConfiguration) -> None:
|
28 |
| - pass |
| 38 | + self._lock = RLock() |
| 39 | + self._sdk_config = sdk_config |
| 40 | + self._view_instrument_match: Dict[ |
| 41 | + Instrument, List[_ViewInstrumentMatch] |
| 42 | + ] = {} |
| 43 | + |
| 44 | + def _get_or_init_view_instrument_match( |
| 45 | + self, instrument: Instrument |
| 46 | + ) -> List["_ViewInstrumentMatch"]: |
| 47 | + # Optimistically get the relevant views for the given instrument. Once set for a given |
| 48 | + # instrument, the mapping will never change |
| 49 | + if instrument in self._view_instrument_match: |
| 50 | + return self._view_instrument_match[instrument] |
| 51 | + |
| 52 | + with self._lock: |
| 53 | + # double check if it was set before we held the lock |
| 54 | + if instrument in self._view_instrument_match: |
| 55 | + return self._view_instrument_match[instrument] |
| 56 | + |
| 57 | + # not present, hold the lock and add a new mapping |
| 58 | + matches = [] |
| 59 | + for view in self._sdk_config.views: |
| 60 | + if view.match(instrument): |
| 61 | + # Note: if a view matches multiple instruments, this will create a separate |
| 62 | + # _ViewInstrumentMatch per instrument. If the user's View configuration includes a |
| 63 | + # name, this will cause multiple conflicting output streams. |
| 64 | + matches.append( |
| 65 | + _ViewInstrumentMatch( |
| 66 | + name=view.name or instrument.name, |
| 67 | + resource=self._sdk_config.resource, |
| 68 | + instrumentation_info=None, |
| 69 | + aggregation=view.aggregation, |
| 70 | + unit=instrument.unit, |
| 71 | + description=view.description, |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + # if no view targeted the instrument, use the default |
| 76 | + if not matches: |
| 77 | + # TODO: the logic to select aggregation could be moved |
| 78 | + if isinstance(instrument, Counter): |
| 79 | + agg = SumAggregation(True, AggregationTemporality.DELTA) |
| 80 | + elif isinstance(instrument, Histogram): |
| 81 | + agg = ExplicitBucketHistogramAggregation() |
| 82 | + else: |
| 83 | + agg = LastValueAggregation() |
| 84 | + matches.append( |
| 85 | + _ViewInstrumentMatch( |
| 86 | + resource=self._sdk_config.resource, |
| 87 | + instrumentation_info=None, |
| 88 | + aggregation=agg, |
| 89 | + unit=instrument.unit, |
| 90 | + description=instrument.description, |
| 91 | + name=instrument.name, |
| 92 | + ) |
| 93 | + ) |
| 94 | + self._view_instrument_match[instrument] = matches |
| 95 | + return matches |
29 | 96 |
|
30 | 97 | def consume_measurement(self, measurement: Measurement) -> None:
|
31 |
| - pass |
| 98 | + for matches in self._get_or_init_view_instrument_match( |
| 99 | + measurement.instrument |
| 100 | + ): |
| 101 | + matches.consume_measurement(measurement) |
32 | 102 |
|
33 | 103 | def collect(self, temporality: AggregationTemporality) -> Iterable[Metric]:
|
34 |
| - pass |
| 104 | + # use a list instead of yielding to prevent a slow reader from holding SDK locks |
| 105 | + metrics: List[Metric] = [] |
| 106 | + |
| 107 | + # While holding the lock, new _ViewInstrumentMatch can't be added from another thread (so we are |
| 108 | + # sure we collect all existing view). However, instruments can still send measurements |
| 109 | + # that will make it into the individual aggregations; collection will acquire those |
| 110 | + # locks iteratively to keep locking as fine-grained as possible. One side effect is |
| 111 | + # that end times can be slightly skewed among the metric streams produced by the SDK, |
| 112 | + # but we still align the output timestamps for a single instrument. |
| 113 | + with self._lock: |
| 114 | + for matches in self._view_instrument_match.values(): |
| 115 | + for match in matches: |
| 116 | + metrics.extend(match.collect(temporality)) |
| 117 | + |
| 118 | + return metrics |
| 119 | + |
| 120 | + |
| 121 | +def default_view(instrument: Instrument) -> View: |
| 122 | + # TODO: #2247 |
| 123 | + return View() |
0 commit comments