Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/opentelemetry/sdk/metrics/_internal/view.py: 56%
41 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:35 +0000
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.
16from fnmatch import fnmatch
17from logging import getLogger
18from typing import Optional, Set, Type
20# FIXME import from typing when support for 3.7 is removed
21from typing_extensions import final
23from opentelemetry.metrics import Instrument
24from opentelemetry.sdk.metrics._internal.aggregation import (
25 Aggregation,
26 DefaultAggregation,
27)
29_logger = getLogger(__name__)
32class View:
33 """
34 A `View` configuration parameters can be used for the following
35 purposes:
37 1. Match instruments: When an instrument matches a view, measurements
38 received by that instrument will be processed.
39 2. Customize metric streams: A metric stream is identified by a match
40 between a view and an instrument and a set of attributes. The metric
41 stream can be customized by certain attributes of the corresponding view.
43 The attributes documented next serve one of the previous two purposes.
45 Args:
46 instrument_type: This is an instrument matching attribute: the class the
47 instrument must be to match the view.
49 instrument_name: This is an instrument matching attribute: the name the
50 instrument must have to match the view. Wild card characters are supported. Wild
51 card characters should not be used with this attribute if the view has also a
52 ``name`` defined.
54 meter_name: This is an instrument matching attribute: the name the
55 instrument meter must have to match the view.
57 meter_version: This is an instrument matching attribute: the version
58 the instrument meter must have to match the view.
60 meter_schema_url: This is an instrument matching attribute: the schema
61 URL the instrument meter must have to match the view.
63 name: This is a metric stream customizing attribute: the name of the
64 metric stream. If `None`, the name of the instrument will be used.
66 description: This is a metric stream customizing attribute: the
67 description of the metric stream. If `None`, the description of the instrument will
68 be used.
70 attribute_keys: This is a metric stream customizing attribute: this is
71 a set of attribute keys. If not `None` then only the measurement attributes that
72 are in ``attribute_keys`` will be used to identify the metric stream.
74 aggregation: This is a metric stream customizing attribute: the
75 aggregation instance to use when data is aggregated for the
76 corresponding metrics stream. If `None` an instance of
77 `DefaultAggregation` will be used.
79 This class is not intended to be subclassed by the user.
80 """
82 _default_aggregation = DefaultAggregation()
84 def __init__(
85 self,
86 instrument_type: Optional[Type[Instrument]] = None,
87 instrument_name: Optional[str] = None,
88 meter_name: Optional[str] = None,
89 meter_version: Optional[str] = None,
90 meter_schema_url: Optional[str] = None,
91 name: Optional[str] = None,
92 description: Optional[str] = None,
93 attribute_keys: Optional[Set[str]] = None,
94 aggregation: Optional[Aggregation] = None,
95 ):
96 if (
97 instrument_type
98 is instrument_name
99 is meter_name
100 is meter_version
101 is meter_schema_url
102 is None
103 ):
104 raise Exception(
105 "Some instrument selection "
106 f"criteria must be provided for View {name}"
107 )
109 if (
110 name is not None
111 and instrument_name is not None
112 and ("*" in instrument_name or "?" in instrument_name)
113 ):
115 raise Exception(
116 f"View {name} declared with wildcard "
117 "characters in instrument_name"
118 )
120 # _name, _description, _aggregation and _attribute_keys will be
121 # accessed when instantiating a _ViewInstrumentMatch.
122 self._name = name
123 self._instrument_type = instrument_type
124 self._instrument_name = instrument_name
125 self._meter_name = meter_name
126 self._meter_version = meter_version
127 self._meter_schema_url = meter_schema_url
129 self._description = description
130 self._attribute_keys = attribute_keys
131 self._aggregation = aggregation or self._default_aggregation
133 # pylint: disable=too-many-return-statements
134 # pylint: disable=too-many-branches
135 @final
136 def _match(self, instrument: Instrument) -> bool:
138 if self._instrument_type is not None:
139 if not isinstance(instrument, self._instrument_type):
140 return False
142 if self._instrument_name is not None:
143 if not fnmatch(instrument.name, self._instrument_name):
144 return False
146 if self._meter_name is not None:
147 if instrument.instrumentation_scope.name != self._meter_name:
148 return False
150 if self._meter_version is not None:
151 if instrument.instrumentation_scope.version != self._meter_version:
152 return False
154 if self._meter_schema_url is not None:
155 if (
156 instrument.instrumentation_scope.schema_url
157 != self._meter_schema_url
158 ):
159 return False
161 return True