We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Missing ABC for Metric
[Missing ABC](https://github.com/open-telemetry/opentelemetry-python/blob/master/opentelemetry-api/src/opentelemetry/metrics/__init__.py#L90) for `Metric` seems strange to me.
 I guess in the PR #311 and Issue #66 settle to add ABC for all interfaces.

Maybe it should look like:
```python

class Metric(abc.ABC):
    """...
    """

    @abc.abstractmethod
    def get_handle(self, label_set: LabelSet) -> "object":
        """...
        """


```
Missing ABC for Metric
[Missing ABC](https://github.com/open-telemetry/opentelemetry-python/blob/master/opentelemetry-api/src/opentelemetry/metrics/__init__.py#L90) for `Metric` seems strange to me.
 I guess in the PR #311 and Issue #66 settle to add ABC for all interfaces.

Maybe it should look like:
```python

class Metric(abc.ABC):
    """...
    """

    @abc.abstractmethod
    def get_handle(self, label_set: LabelSet) -> "object":
        """...
        """


```

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `opentelemetry-api/src/opentelemetry/metrics/__init__.py`
Content:
```
1 # Copyright 2019, 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 The OpenTelemetry metrics API describes the classes used to report raw
17 measurements, as well as metrics with known aggregation and labels.
18 
19 The `Meter` class is used to construct `Metric` s to record raw statistics
20 as well as metrics with predefined aggregation.
21 
22 See the `metrics api`_ spec for terminology and context clarification.
23 
24 .. _metrics api:
25     https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/api-metrics.md
26 
27 
28 """
29 import abc
30 from typing import Callable, Dict, Optional, Sequence, Tuple, Type, TypeVar
31 
32 from opentelemetry.util import loader
33 
34 ValueT = TypeVar("ValueT", int, float)
35 
36 
37 class DefaultMetricHandle:
38     """The default MetricHandle.
39 
40     Used when no MetricHandle implementation is available.
41     """
42 
43 
44 class CounterHandle:
45     def add(self, value: ValueT) -> None:
46         """Increases the value of the handle by ``value``.
47 
48         Args:
49             value: The value to record to the handle.
50         """
51 
52 
53 class GaugeHandle:
54     def set(self, value: ValueT) -> None:
55         """Sets the current value of the handle to ``value``.
56 
57         Args:
58             value: The value to record to the handle.
59         """
60 
61 
62 class MeasureHandle:
63     def record(self, value: ValueT) -> None:
64         """Records the given ``value`` to this handle.
65 
66         Args:
67             value: The value to record to the handle.
68         """
69 
70 
71 class LabelSet(abc.ABC):
72     """A canonicalized set of labels useful for preaggregation
73 
74     Re-usable LabelSet objects provide a potential optimization for scenarios
75     where handles might not be effective. For example, if the LabelSet will be
76     re-used but only used once per metrics, handles do not offer any
77     optimization. It may best to pre-compute a canonicalized LabelSet once and
78     re-use it with the direct calling convention. LabelSets are immutable and
79     should be opaque in implementation.
80     """
81 
82 
83 class DefaultLabelSet(LabelSet):
84     """The default LabelSet.
85 
86     Used when no LabelSet implementation is available.
87     """
88 
89 
90 class Metric:
91     """Base class for various types of metrics.
92 
93     Metric class that inherit from this class are specialized with the type of
94     handle that the metric holds.
95     """
96 
97     def get_handle(self, label_set: LabelSet) -> "object":
98         """Gets a handle, used for repeated-use of metrics instruments.
99 
100         Handles are useful to reduce the cost of repeatedly recording a metric
101         with a pre-defined set of label values. All metric kinds (counter,
102         gauge, measure) support declaring a set of required label keys. The
103         values corresponding to these keys should be specified in every handle.
104         "Unspecified" label values, in cases where a handle is requested but
105         a value was not provided are permitted.
106 
107         Args:
108             label_set: `LabelSet` to associate with the returned handle.
109         """
110 
111 
112 class DefaultMetric(Metric):
113     """The default Metric used when no Metric implementation is available."""
114 
115     def get_handle(self, label_set: LabelSet) -> "DefaultMetricHandle":
116         """Gets a `DefaultMetricHandle`.
117 
118         Args:
119             label_set: `LabelSet` to associate with the returned handle.
120         """
121         return DefaultMetricHandle()
122 
123 
124 class Counter(Metric):
125     """A counter type metric that expresses the computation of a sum."""
126 
127     def get_handle(self, label_set: LabelSet) -> "CounterHandle":
128         """Gets a `CounterHandle`."""
129         return CounterHandle()
130 
131     def add(self, label_set: LabelSet, value: ValueT) -> None:
132         """Increases the value of the counter by ``value``.
133 
134         Args:
135             label_set: `LabelSet` to associate with the returned handle.
136             value: The value to add to the counter metric.
137         """
138 
139 
140 class Gauge(Metric):
141     """A gauge type metric that expresses a pre-calculated value.
142 
143     Gauge metrics have a value that is either ``Set`` by explicit
144     instrumentation or observed through a callback. This kind of metric
145     should be used when the metric cannot be expressed as a sum or because
146     the measurement interval is arbitrary.
147     """
148 
149     def get_handle(self, label_set: LabelSet) -> "GaugeHandle":
150         """Gets a `GaugeHandle`."""
151         return GaugeHandle()
152 
153     def set(self, label_set: LabelSet, value: ValueT) -> None:
154         """Sets the value of the gauge to ``value``.
155 
156         Args:
157             label_set: `LabelSet` to associate with the returned handle.
158             value: The value to set the gauge metric to.
159         """
160 
161 
162 class Measure(Metric):
163     """A measure type metric that represent raw stats that are recorded.
164 
165     Measure metrics represent raw statistics that are recorded. By
166     default, measure metrics can accept both positive and negatives.
167     Negative inputs will be discarded when monotonic is True.
168     """
169 
170     def get_handle(self, label_set: LabelSet) -> "MeasureHandle":
171         """Gets a `MeasureHandle` with a float value."""
172         return MeasureHandle()
173 
174     def record(self, label_set: LabelSet, value: ValueT) -> None:
175         """Records the ``value`` to the measure.
176 
177         Args:
178             label_set: `LabelSet` to associate with the returned handle.
179             value: The value to record to this measure metric.
180         """
181 
182 
183 MetricT = TypeVar("MetricT", Counter, Gauge, Measure)
184 
185 
186 # pylint: disable=unused-argument
187 class Meter(abc.ABC):
188     """An interface to allow the recording of metrics.
189 
190     `Metric` s are used for recording pre-defined aggregation (gauge and
191     counter), or raw values (measure) in which the aggregation and labels
192     for the exported metric are deferred.
193     """
194 
195     @abc.abstractmethod
196     def record_batch(
197         self,
198         label_set: LabelSet,
199         record_tuples: Sequence[Tuple["Metric", ValueT]],
200     ) -> None:
201         """Atomically records a batch of `Metric` and value pairs.
202 
203         Allows the functionality of acting upon multiple metrics with
204         a single API call. Implementations should find metric and handles that
205         match the key-value pairs in the label tuples.
206 
207         Args:
208             label_set: The `LabelSet` associated with all measurements in
209                 the batch. A measurement is a tuple, representing the `Metric`
210                 being recorded and the corresponding value to record.
211             record_tuples: A sequence of pairs of `Metric` s and the
212                 corresponding value to record for that metric.
213         """
214 
215     @abc.abstractmethod
216     def create_metric(
217         self,
218         name: str,
219         description: str,
220         unit: str,
221         value_type: Type[ValueT],
222         metric_type: Type[MetricT],
223         label_keys: Sequence[str] = (),
224         enabled: bool = True,
225         monotonic: bool = False,
226     ) -> "Metric":
227         """Creates a ``metric_kind`` metric with type ``value_type``.
228 
229         Args:
230             name: The name of the metric.
231             description: Human-readable description of the metric.
232             unit: Unit of the metric values.
233             value_type: The type of values being recorded by the metric.
234             metric_type: The type of metric being created.
235             label_keys: The keys for the labels with dynamic values.
236             enabled: Whether to report the metric by default.
237             monotonic: Whether to only allow non-negative values.
238 
239         Returns: A new ``metric_type`` metric with values of ``value_type``.
240         """
241 
242     @abc.abstractmethod
243     def get_label_set(self, labels: Dict[str, str]) -> "LabelSet":
244         """Gets a `LabelSet` with the given labels.
245 
246         Args:
247             labels: A dictionary representing label key to label value pairs.
248 
249         Returns: A `LabelSet` object canonicalized using the given input.
250         """
251 
252 
253 class DefaultMeter(Meter):
254     """The default Meter used when no Meter implementation is available."""
255 
256     def record_batch(
257         self,
258         label_set: LabelSet,
259         record_tuples: Sequence[Tuple["Metric", ValueT]],
260     ) -> None:
261         pass
262 
263     def create_metric(
264         self,
265         name: str,
266         description: str,
267         unit: str,
268         value_type: Type[ValueT],
269         metric_type: Type[MetricT],
270         label_keys: Sequence[str] = (),
271         enabled: bool = True,
272         monotonic: bool = False,
273     ) -> "Metric":
274         # pylint: disable=no-self-use
275         return DefaultMetric()
276 
277     def get_label_set(self, labels: Dict[str, str]) -> "LabelSet":
278         # pylint: disable=no-self-use
279         return DefaultLabelSet()
280 
281 
282 # Once https://github.com/python/mypy/issues/7092 is resolved,
283 # the following type definition should be replaced with
284 # from opentelemetry.util.loader import ImplementationFactory
285 ImplementationFactory = Callable[[Type[Meter]], Optional[Meter]]
286 
287 _METER = None
288 _METER_FACTORY = None
289 
290 
291 def meter() -> Meter:
292     """Gets the current global :class:`~.Meter` object.
293 
294     If there isn't one set yet, a default will be loaded.
295     """
296     global _METER, _METER_FACTORY  # pylint:disable=global-statement
297 
298     if _METER is None:
299         # pylint:disable=protected-access
300         _METER = loader._load_impl(DefaultMeter, _METER_FACTORY)
301         del _METER_FACTORY
302 
303     return _METER
304 
305 
306 def set_preferred_meter_implementation(factory: ImplementationFactory) -> None:
307     """Set the factory to be used to create the meter.
308 
309     See :mod:`opentelemetry.util.loader` for details.
310 
311     This function may not be called after a meter is already loaded.
312 
313     Args:
314         factory: Callback that should create a new :class:`Meter` instance.
315     """
316     global _METER, _METER_FACTORY  # pylint:disable=global-statement
317 
318     if _METER:
319         raise RuntimeError("Meter already loaded.")
320 
321     _METER_FACTORY = factory
322
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/opentelemetry-api/src/opentelemetry/metrics/__init__.py b/opentelemetry-api/src/opentelemetry/metrics/__init__.py
--- a/opentelemetry-api/src/opentelemetry/metrics/__init__.py
+++ b/opentelemetry-api/src/opentelemetry/metrics/__init__.py
@@ -87,13 +87,14 @@
     """
 
 
-class Metric:
+class Metric(abc.ABC):
     """Base class for various types of metrics.
 
     Metric class that inherit from this class are specialized with the type of
     handle that the metric holds.
     """
 
+    @abc.abstractmethod
     def get_handle(self, label_set: LabelSet) -> "object":
         """Gets a handle, used for repeated-use of metrics instruments.

{"golden_diff": "diff --git a/opentelemetry-api/src/opentelemetry/metrics/__init__.py b/opentelemetry-api/src/opentelemetry/metrics/__init__.py\n--- a/opentelemetry-api/src/opentelemetry/metrics/__init__.py\n+++ b/opentelemetry-api/src/opentelemetry/metrics/__init__.py\n@@ -87,13 +87,14 @@\n     \"\"\"\n \n \n-class Metric:\n+class Metric(abc.ABC):\n     \"\"\"Base class for various types of metrics.\n \n     Metric class that inherit from this class are specialized with the type of\n     handle that the metric holds.\n     \"\"\"\n \n+    @abc.abstractmethod\n     def get_handle(self, label_set: LabelSet) -> \"object\":\n         \"\"\"Gets a handle, used for repeated-use of metrics instruments.\n", "issue": "Missing ABC for Metric\n[Missing ABC](https://github.com/open-telemetry/opentelemetry-python/blob/master/opentelemetry-api/src/opentelemetry/metrics/__init__.py#L90) for `Metric` seems strange to me.\r\n I guess in the PR #311 and Issue #66 settle to add ABC for all interfaces.\r\n\r\nMaybe it should look like:\r\n```python\r\n\r\nclass Metric(abc.ABC):\r\n    \"\"\"...\r\n    \"\"\"\r\n\r\n    @abc.abstractmethod\r\n    def get_handle(self, label_set: LabelSet) -> \"object\":\r\n        \"\"\"...\r\n        \"\"\"\r\n\r\n\r\n```\nMissing ABC for Metric\n[Missing ABC](https://github.com/open-telemetry/opentelemetry-python/blob/master/opentelemetry-api/src/opentelemetry/metrics/__init__.py#L90) for `Metric` seems strange to me.\r\n I guess in the PR #311 and Issue #66 settle to add ABC for all interfaces.\r\n\r\nMaybe it should look like:\r\n```python\r\n\r\nclass Metric(abc.ABC):\r\n    \"\"\"...\r\n    \"\"\"\r\n\r\n    @abc.abstractmethod\r\n    def get_handle(self, label_set: LabelSet) -> \"object\":\r\n        \"\"\"...\r\n        \"\"\"\r\n\r\n\r\n```\n", "before_files": [{"content": "# Copyright 2019, OpenTelemetry Authors\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"\nThe OpenTelemetry metrics API describes the classes used to report raw\nmeasurements, as well as metrics with known aggregation and labels.\n\nThe `Meter` class is used to construct `Metric` s to record raw statistics\nas well as metrics with predefined aggregation.\n\nSee the `metrics api`_ spec for terminology and context clarification.\n\n.. _metrics api:\n    https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/api-metrics.md\n\n\n\"\"\"\nimport abc\nfrom typing import Callable, Dict, Optional, Sequence, Tuple, Type, TypeVar\n\nfrom opentelemetry.util import loader\n\nValueT = TypeVar(\"ValueT\", int, float)\n\n\nclass DefaultMetricHandle:\n    \"\"\"The default MetricHandle.\n\n    Used when no MetricHandle implementation is available.\n    \"\"\"\n\n\nclass CounterHandle:\n    def add(self, value: ValueT) -> None:\n        \"\"\"Increases the value of the handle by ``value``.\n\n        Args:\n            value: The value to record to the handle.\n        \"\"\"\n\n\nclass GaugeHandle:\n    def set(self, value: ValueT) -> None:\n        \"\"\"Sets the current value of the handle to ``value``.\n\n        Args:\n            value: The value to record to the handle.\n        \"\"\"\n\n\nclass MeasureHandle:\n    def record(self, value: ValueT) -> None:\n        \"\"\"Records the given ``value`` to this handle.\n\n        Args:\n            value: The value to record to the handle.\n        \"\"\"\n\n\nclass LabelSet(abc.ABC):\n    \"\"\"A canonicalized set of labels useful for preaggregation\n\n    Re-usable LabelSet objects provide a potential optimization for scenarios\n    where handles might not be effective. For example, if the LabelSet will be\n    re-used but only used once per metrics, handles do not offer any\n    optimization. It may best to pre-compute a canonicalized LabelSet once and\n    re-use it with the direct calling convention. LabelSets are immutable and\n    should be opaque in implementation.\n    \"\"\"\n\n\nclass DefaultLabelSet(LabelSet):\n    \"\"\"The default LabelSet.\n\n    Used when no LabelSet implementation is available.\n    \"\"\"\n\n\nclass Metric:\n    \"\"\"Base class for various types of metrics.\n\n    Metric class that inherit from this class are specialized with the type of\n    handle that the metric holds.\n    \"\"\"\n\n    def get_handle(self, label_set: LabelSet) -> \"object\":\n        \"\"\"Gets a handle, used for repeated-use of metrics instruments.\n\n        Handles are useful to reduce the cost of repeatedly recording a metric\n        with a pre-defined set of label values. All metric kinds (counter,\n        gauge, measure) support declaring a set of required label keys. The\n        values corresponding to these keys should be specified in every handle.\n        \"Unspecified\" label values, in cases where a handle is requested but\n        a value was not provided are permitted.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n        \"\"\"\n\n\nclass DefaultMetric(Metric):\n    \"\"\"The default Metric used when no Metric implementation is available.\"\"\"\n\n    def get_handle(self, label_set: LabelSet) -> \"DefaultMetricHandle\":\n        \"\"\"Gets a `DefaultMetricHandle`.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n        \"\"\"\n        return DefaultMetricHandle()\n\n\nclass Counter(Metric):\n    \"\"\"A counter type metric that expresses the computation of a sum.\"\"\"\n\n    def get_handle(self, label_set: LabelSet) -> \"CounterHandle\":\n        \"\"\"Gets a `CounterHandle`.\"\"\"\n        return CounterHandle()\n\n    def add(self, label_set: LabelSet, value: ValueT) -> None:\n        \"\"\"Increases the value of the counter by ``value``.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n            value: The value to add to the counter metric.\n        \"\"\"\n\n\nclass Gauge(Metric):\n    \"\"\"A gauge type metric that expresses a pre-calculated value.\n\n    Gauge metrics have a value that is either ``Set`` by explicit\n    instrumentation or observed through a callback. This kind of metric\n    should be used when the metric cannot be expressed as a sum or because\n    the measurement interval is arbitrary.\n    \"\"\"\n\n    def get_handle(self, label_set: LabelSet) -> \"GaugeHandle\":\n        \"\"\"Gets a `GaugeHandle`.\"\"\"\n        return GaugeHandle()\n\n    def set(self, label_set: LabelSet, value: ValueT) -> None:\n        \"\"\"Sets the value of the gauge to ``value``.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n            value: The value to set the gauge metric to.\n        \"\"\"\n\n\nclass Measure(Metric):\n    \"\"\"A measure type metric that represent raw stats that are recorded.\n\n    Measure metrics represent raw statistics that are recorded. By\n    default, measure metrics can accept both positive and negatives.\n    Negative inputs will be discarded when monotonic is True.\n    \"\"\"\n\n    def get_handle(self, label_set: LabelSet) -> \"MeasureHandle\":\n        \"\"\"Gets a `MeasureHandle` with a float value.\"\"\"\n        return MeasureHandle()\n\n    def record(self, label_set: LabelSet, value: ValueT) -> None:\n        \"\"\"Records the ``value`` to the measure.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n            value: The value to record to this measure metric.\n        \"\"\"\n\n\nMetricT = TypeVar(\"MetricT\", Counter, Gauge, Measure)\n\n\n# pylint: disable=unused-argument\nclass Meter(abc.ABC):\n    \"\"\"An interface to allow the recording of metrics.\n\n    `Metric` s are used for recording pre-defined aggregation (gauge and\n    counter), or raw values (measure) in which the aggregation and labels\n    for the exported metric are deferred.\n    \"\"\"\n\n    @abc.abstractmethod\n    def record_batch(\n        self,\n        label_set: LabelSet,\n        record_tuples: Sequence[Tuple[\"Metric\", ValueT]],\n    ) -> None:\n        \"\"\"Atomically records a batch of `Metric` and value pairs.\n\n        Allows the functionality of acting upon multiple metrics with\n        a single API call. Implementations should find metric and handles that\n        match the key-value pairs in the label tuples.\n\n        Args:\n            label_set: The `LabelSet` associated with all measurements in\n                the batch. A measurement is a tuple, representing the `Metric`\n                being recorded and the corresponding value to record.\n            record_tuples: A sequence of pairs of `Metric` s and the\n                corresponding value to record for that metric.\n        \"\"\"\n\n    @abc.abstractmethod\n    def create_metric(\n        self,\n        name: str,\n        description: str,\n        unit: str,\n        value_type: Type[ValueT],\n        metric_type: Type[MetricT],\n        label_keys: Sequence[str] = (),\n        enabled: bool = True,\n        monotonic: bool = False,\n    ) -> \"Metric\":\n        \"\"\"Creates a ``metric_kind`` metric with type ``value_type``.\n\n        Args:\n            name: The name of the metric.\n            description: Human-readable description of the metric.\n            unit: Unit of the metric values.\n            value_type: The type of values being recorded by the metric.\n            metric_type: The type of metric being created.\n            label_keys: The keys for the labels with dynamic values.\n            enabled: Whether to report the metric by default.\n            monotonic: Whether to only allow non-negative values.\n\n        Returns: A new ``metric_type`` metric with values of ``value_type``.\n        \"\"\"\n\n    @abc.abstractmethod\n    def get_label_set(self, labels: Dict[str, str]) -> \"LabelSet\":\n        \"\"\"Gets a `LabelSet` with the given labels.\n\n        Args:\n            labels: A dictionary representing label key to label value pairs.\n\n        Returns: A `LabelSet` object canonicalized using the given input.\n        \"\"\"\n\n\nclass DefaultMeter(Meter):\n    \"\"\"The default Meter used when no Meter implementation is available.\"\"\"\n\n    def record_batch(\n        self,\n        label_set: LabelSet,\n        record_tuples: Sequence[Tuple[\"Metric\", ValueT]],\n    ) -> None:\n        pass\n\n    def create_metric(\n        self,\n        name: str,\n        description: str,\n        unit: str,\n        value_type: Type[ValueT],\n        metric_type: Type[MetricT],\n        label_keys: Sequence[str] = (),\n        enabled: bool = True,\n        monotonic: bool = False,\n    ) -> \"Metric\":\n        # pylint: disable=no-self-use\n        return DefaultMetric()\n\n    def get_label_set(self, labels: Dict[str, str]) -> \"LabelSet\":\n        # pylint: disable=no-self-use\n        return DefaultLabelSet()\n\n\n# Once https://github.com/python/mypy/issues/7092 is resolved,\n# the following type definition should be replaced with\n# from opentelemetry.util.loader import ImplementationFactory\nImplementationFactory = Callable[[Type[Meter]], Optional[Meter]]\n\n_METER = None\n_METER_FACTORY = None\n\n\ndef meter() -> Meter:\n    \"\"\"Gets the current global :class:`~.Meter` object.\n\n    If there isn't one set yet, a default will be loaded.\n    \"\"\"\n    global _METER, _METER_FACTORY  # pylint:disable=global-statement\n\n    if _METER is None:\n        # pylint:disable=protected-access\n        _METER = loader._load_impl(DefaultMeter, _METER_FACTORY)\n        del _METER_FACTORY\n\n    return _METER\n\n\ndef set_preferred_meter_implementation(factory: ImplementationFactory) -> None:\n    \"\"\"Set the factory to be used to create the meter.\n\n    See :mod:`opentelemetry.util.loader` for details.\n\n    This function may not be called after a meter is already loaded.\n\n    Args:\n        factory: Callback that should create a new :class:`Meter` instance.\n    \"\"\"\n    global _METER, _METER_FACTORY  # pylint:disable=global-statement\n\n    if _METER:\n        raise RuntimeError(\"Meter already loaded.\")\n\n    _METER_FACTORY = factory\n", "path": "opentelemetry-api/src/opentelemetry/metrics/__init__.py"}], "after_files": [{"content": "# Copyright 2019, OpenTelemetry Authors\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n\"\"\"\nThe OpenTelemetry metrics API describes the classes used to report raw\nmeasurements, as well as metrics with known aggregation and labels.\n\nThe `Meter` class is used to construct `Metric` s to record raw statistics\nas well as metrics with predefined aggregation.\n\nSee the `metrics api`_ spec for terminology and context clarification.\n\n.. _metrics api:\n    https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/api-metrics.md\n\n\n\"\"\"\nimport abc\nfrom typing import Callable, Dict, Optional, Sequence, Tuple, Type, TypeVar\n\nfrom opentelemetry.util import loader\n\nValueT = TypeVar(\"ValueT\", int, float)\n\n\nclass DefaultMetricHandle:\n    \"\"\"The default MetricHandle.\n\n    Used when no MetricHandle implementation is available.\n    \"\"\"\n\n\nclass CounterHandle:\n    def add(self, value: ValueT) -> None:\n        \"\"\"Increases the value of the handle by ``value``.\n\n        Args:\n            value: The value to record to the handle.\n        \"\"\"\n\n\nclass GaugeHandle:\n    def set(self, value: ValueT) -> None:\n        \"\"\"Sets the current value of the handle to ``value``.\n\n        Args:\n            value: The value to record to the handle.\n        \"\"\"\n\n\nclass MeasureHandle:\n    def record(self, value: ValueT) -> None:\n        \"\"\"Records the given ``value`` to this handle.\n\n        Args:\n            value: The value to record to the handle.\n        \"\"\"\n\n\nclass LabelSet(abc.ABC):\n    \"\"\"A canonicalized set of labels useful for preaggregation\n\n    Re-usable LabelSet objects provide a potential optimization for scenarios\n    where handles might not be effective. For example, if the LabelSet will be\n    re-used but only used once per metrics, handles do not offer any\n    optimization. It may best to pre-compute a canonicalized LabelSet once and\n    re-use it with the direct calling convention. LabelSets are immutable and\n    should be opaque in implementation.\n    \"\"\"\n\n\nclass DefaultLabelSet(LabelSet):\n    \"\"\"The default LabelSet.\n\n    Used when no LabelSet implementation is available.\n    \"\"\"\n\n\nclass Metric(abc.ABC):\n    \"\"\"Base class for various types of metrics.\n\n    Metric class that inherit from this class are specialized with the type of\n    handle that the metric holds.\n    \"\"\"\n\n    @abc.abstractmethod\n    def get_handle(self, label_set: LabelSet) -> \"object\":\n        \"\"\"Gets a handle, used for repeated-use of metrics instruments.\n\n        Handles are useful to reduce the cost of repeatedly recording a metric\n        with a pre-defined set of label values. All metric kinds (counter,\n        gauge, measure) support declaring a set of required label keys. The\n        values corresponding to these keys should be specified in every handle.\n        \"Unspecified\" label values, in cases where a handle is requested but\n        a value was not provided are permitted.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n        \"\"\"\n\n\nclass DefaultMetric(Metric):\n    \"\"\"The default Metric used when no Metric implementation is available.\"\"\"\n\n    def get_handle(self, label_set: LabelSet) -> \"DefaultMetricHandle\":\n        \"\"\"Gets a `DefaultMetricHandle`.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n        \"\"\"\n        return DefaultMetricHandle()\n\n\nclass Counter(Metric):\n    \"\"\"A counter type metric that expresses the computation of a sum.\"\"\"\n\n    def get_handle(self, label_set: LabelSet) -> \"CounterHandle\":\n        \"\"\"Gets a `CounterHandle`.\"\"\"\n        return CounterHandle()\n\n    def add(self, label_set: LabelSet, value: ValueT) -> None:\n        \"\"\"Increases the value of the counter by ``value``.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n            value: The value to add to the counter metric.\n        \"\"\"\n\n\nclass Gauge(Metric):\n    \"\"\"A gauge type metric that expresses a pre-calculated value.\n\n    Gauge metrics have a value that is either ``Set`` by explicit\n    instrumentation or observed through a callback. This kind of metric\n    should be used when the metric cannot be expressed as a sum or because\n    the measurement interval is arbitrary.\n    \"\"\"\n\n    def get_handle(self, label_set: LabelSet) -> \"GaugeHandle\":\n        \"\"\"Gets a `GaugeHandle`.\"\"\"\n        return GaugeHandle()\n\n    def set(self, label_set: LabelSet, value: ValueT) -> None:\n        \"\"\"Sets the value of the gauge to ``value``.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n            value: The value to set the gauge metric to.\n        \"\"\"\n\n\nclass Measure(Metric):\n    \"\"\"A measure type metric that represent raw stats that are recorded.\n\n    Measure metrics represent raw statistics that are recorded. By\n    default, measure metrics can accept both positive and negatives.\n    Negative inputs will be discarded when monotonic is True.\n    \"\"\"\n\n    def get_handle(self, label_set: LabelSet) -> \"MeasureHandle\":\n        \"\"\"Gets a `MeasureHandle` with a float value.\"\"\"\n        return MeasureHandle()\n\n    def record(self, label_set: LabelSet, value: ValueT) -> None:\n        \"\"\"Records the ``value`` to the measure.\n\n        Args:\n            label_set: `LabelSet` to associate with the returned handle.\n            value: The value to record to this measure metric.\n        \"\"\"\n\n\nMetricT = TypeVar(\"MetricT\", Counter, Gauge, Measure)\n\n\n# pylint: disable=unused-argument\nclass Meter(abc.ABC):\n    \"\"\"An interface to allow the recording of metrics.\n\n    `Metric` s are used for recording pre-defined aggregation (gauge and\n    counter), or raw values (measure) in which the aggregation and labels\n    for the exported metric are deferred.\n    \"\"\"\n\n    @abc.abstractmethod\n    def record_batch(\n        self,\n        label_set: LabelSet,\n        record_tuples: Sequence[Tuple[\"Metric\", ValueT]],\n    ) -> None:\n        \"\"\"Atomically records a batch of `Metric` and value pairs.\n\n        Allows the functionality of acting upon multiple metrics with\n        a single API call. Implementations should find metric and handles that\n        match the key-value pairs in the label tuples.\n\n        Args:\n            label_set: The `LabelSet` associated with all measurements in\n                the batch. A measurement is a tuple, representing the `Metric`\n                being recorded and the corresponding value to record.\n            record_tuples: A sequence of pairs of `Metric` s and the\n                corresponding value to record for that metric.\n        \"\"\"\n\n    @abc.abstractmethod\n    def create_metric(\n        self,\n        name: str,\n        description: str,\n        unit: str,\n        value_type: Type[ValueT],\n        metric_type: Type[MetricT],\n        label_keys: Sequence[str] = (),\n        enabled: bool = True,\n        monotonic: bool = False,\n    ) -> \"Metric\":\n        \"\"\"Creates a ``metric_kind`` metric with type ``value_type``.\n\n        Args:\n            name: The name of the metric.\n            description: Human-readable description of the metric.\n            unit: Unit of the metric values.\n            value_type: The type of values being recorded by the metric.\n            metric_type: The type of metric being created.\n            label_keys: The keys for the labels with dynamic values.\n            enabled: Whether to report the metric by default.\n            monotonic: Whether to only allow non-negative values.\n\n        Returns: A new ``metric_type`` metric with values of ``value_type``.\n        \"\"\"\n\n    @abc.abstractmethod\n    def get_label_set(self, labels: Dict[str, str]) -> \"LabelSet\":\n        \"\"\"Gets a `LabelSet` with the given labels.\n\n        Args:\n            labels: A dictionary representing label key to label value pairs.\n\n        Returns: A `LabelSet` object canonicalized using the given input.\n        \"\"\"\n\n\nclass DefaultMeter(Meter):\n    \"\"\"The default Meter used when no Meter implementation is available.\"\"\"\n\n    def record_batch(\n        self,\n        label_set: LabelSet,\n        record_tuples: Sequence[Tuple[\"Metric\", ValueT]],\n    ) -> None:\n        pass\n\n    def create_metric(\n        self,\n        name: str,\n        description: str,\n        unit: str,\n        value_type: Type[ValueT],\n        metric_type: Type[MetricT],\n        label_keys: Sequence[str] = (),\n        enabled: bool = True,\n        monotonic: bool = False,\n    ) -> \"Metric\":\n        # pylint: disable=no-self-use\n        return DefaultMetric()\n\n    def get_label_set(self, labels: Dict[str, str]) -> \"LabelSet\":\n        # pylint: disable=no-self-use\n        return DefaultLabelSet()\n\n\n# Once https://github.com/python/mypy/issues/7092 is resolved,\n# the following type definition should be replaced with\n# from opentelemetry.util.loader import ImplementationFactory\nImplementationFactory = Callable[[Type[Meter]], Optional[Meter]]\n\n_METER = None\n_METER_FACTORY = None\n\n\ndef meter() -> Meter:\n    \"\"\"Gets the current global :class:`~.Meter` object.\n\n    If there isn't one set yet, a default will be loaded.\n    \"\"\"\n    global _METER, _METER_FACTORY  # pylint:disable=global-statement\n\n    if _METER is None:\n        # pylint:disable=protected-access\n        _METER = loader._load_impl(DefaultMeter, _METER_FACTORY)\n        del _METER_FACTORY\n\n    return _METER\n\n\ndef set_preferred_meter_implementation(factory: ImplementationFactory) -> None:\n    \"\"\"Set the factory to be used to create the meter.\n\n    See :mod:`opentelemetry.util.loader` for details.\n\n    This function may not be called after a meter is already loaded.\n\n    Args:\n        factory: Callback that should create a new :class:`Meter` instance.\n    \"\"\"\n    global _METER, _METER_FACTORY  # pylint:disable=global-statement\n\n    if _METER:\n        raise RuntimeError(\"Meter already loaded.\")\n\n    _METER_FACTORY = factory\n", "path": "opentelemetry-api/src/opentelemetry/metrics/__init__.py"}]}