|
| 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 | + |
| 16 | +from logging import getLogger |
| 17 | +from threading import Lock |
| 18 | +from typing import Iterable, Set |
| 19 | + |
| 20 | +from opentelemetry.sdk._metrics.aggregation import ( |
| 21 | + _convert_aggregation_temporality, |
| 22 | +) |
| 23 | +from opentelemetry.sdk._metrics.measurement import Measurement |
| 24 | +from opentelemetry.sdk._metrics.point import AggregationTemporality, Metric |
| 25 | +from opentelemetry.sdk.resources import Resource |
| 26 | +from opentelemetry.sdk.util.instrumentation import InstrumentationInfo |
| 27 | + |
| 28 | +_logger = getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +class _ViewInstrumentMatch: |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + name: str, |
| 35 | + unit: str, |
| 36 | + description: str, |
| 37 | + aggregation: type, |
| 38 | + instrumentation_info: InstrumentationInfo, |
| 39 | + resource: Resource, |
| 40 | + attribute_keys: Set[str] = None, |
| 41 | + ): |
| 42 | + self._name = name |
| 43 | + self._unit = unit |
| 44 | + self._description = description |
| 45 | + self._aggregation = aggregation |
| 46 | + self._instrumentation_info = instrumentation_info |
| 47 | + self._resource = resource |
| 48 | + self._attribute_keys = attribute_keys |
| 49 | + self._attributes_aggregation = {} |
| 50 | + self._attributes_previous_point = {} |
| 51 | + self._lock = Lock() |
| 52 | + |
| 53 | + def consume_measurement(self, measurement: Measurement) -> None: |
| 54 | + |
| 55 | + if self._attribute_keys is not None: |
| 56 | + |
| 57 | + attributes = {} |
| 58 | + |
| 59 | + for key, value in measurement.attributes.items(): |
| 60 | + if key in self._attribute_keys: |
| 61 | + attributes[key] = value |
| 62 | + elif measurement.attributes is not None: |
| 63 | + attributes = measurement.attributes |
| 64 | + else: |
| 65 | + attributes = {} |
| 66 | + |
| 67 | + attributes = frozenset(attributes.items()) |
| 68 | + |
| 69 | + if attributes not in self._attributes_aggregation.keys(): |
| 70 | + with self._lock: |
| 71 | + self._attributes_aggregation[attributes] = self._aggregation() |
| 72 | + |
| 73 | + self._attributes_aggregation[attributes].aggregate(measurement) |
| 74 | + |
| 75 | + def collect(self, temporality: int) -> Iterable[Metric]: |
| 76 | + |
| 77 | + with self._lock: |
| 78 | + for ( |
| 79 | + attributes, |
| 80 | + aggregation, |
| 81 | + ) in self._attributes_aggregation.items(): |
| 82 | + |
| 83 | + previous_point = self._attributes_previous_point.get( |
| 84 | + attributes |
| 85 | + ) |
| 86 | + |
| 87 | + current_point = aggregation.collect() |
| 88 | + |
| 89 | + # pylint: disable=assignment-from-none |
| 90 | + self._attributes_previous_point[ |
| 91 | + attributes |
| 92 | + ] = _convert_aggregation_temporality( |
| 93 | + previous_point, |
| 94 | + current_point, |
| 95 | + AggregationTemporality.CUMULATIVE, |
| 96 | + ) |
| 97 | + |
| 98 | + if current_point is not None: |
| 99 | + |
| 100 | + yield Metric( |
| 101 | + attributes=dict(attributes), |
| 102 | + description=self._description, |
| 103 | + instrumentation_info=self._instrumentation_info, |
| 104 | + name=self._name, |
| 105 | + resource=self._resource, |
| 106 | + unit=self._unit, |
| 107 | + point=_convert_aggregation_temporality( |
| 108 | + previous_point, |
| 109 | + current_point, |
| 110 | + temporality, |
| 111 | + ), |
| 112 | + ) |
0 commit comments