Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca10173

Browse files
authored
Metrics API RFC 0009 (open-telemetry#140)
* Create functions Comments for Meter More comments Add more comments Fix typos * fix lint * Fix lint * fix typing * Remove options, constructors, seperate labels * Consistent naming for float and int * Abstract time series * Use ABC * Fix typo * Fix docs * seperate measure classes * Add examples * fix lint * Update to RFC 0003 * Add spancontext, measurebatch * Fix docs * Fix comments * fix lint * fix lint * fix lint * skip examples * white space * fix spacing * fix imports * fix imports * LabelValues to str * Black formatting * fix isort * Remove aggregation * Fix names * Remove aggregation from docs * Fix lint * metric changes * Typing * Fix lint * Fix lint * Add space * Fix lint * fix comments * handle, recordbatch * docs * Update recordbatch * black * Fix typo * remove ValueType * fix lint
1 parent 8258927 commit ca10173

File tree

7 files changed

+65
-63
lines changed

7 files changed

+65
-63
lines changed

docs/opentelemetry.metrics.handle.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
opentelemetry.metrics.handle module
2+
==========================================
3+
4+
.. automodule:: opentelemetry.metrics.handle
5+

docs/opentelemetry.metrics.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Submodules
66

77
.. toctree::
88

9-
opentelemetry.metrics.time_series
9+
opentelemetry.metrics.handle
1010

1111
Module contents
1212
---------------

docs/opentelemetry.metrics.time_series.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

opentelemetry-api/src/opentelemetry/metrics/__init__.py

Lines changed: 48 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
2828
"""
2929
from abc import ABC, abstractmethod
30-
from enum import Enum
31-
from typing import List, Union
30+
from typing import Dict, List, Tuple, Type, Union
3231

33-
from opentelemetry.metrics.time_series import (
34-
CounterTimeSeries,
35-
GaugeTimeSeries,
36-
MeasureTimeSeries,
32+
from opentelemetry.metrics.handle import (
33+
CounterHandle,
34+
GaugeHandle,
35+
MeasureHandle,
3736
)
3837
from opentelemetry.trace import SpanContext
3938

@@ -47,19 +46,30 @@ class Meter:
4746
for the exported metric are deferred.
4847
"""
4948

50-
# TODO: RecordBatch
49+
def record_batch(
50+
self,
51+
label_tuples: Dict[str, str],
52+
record_tuples: List[Tuple["Metric", Union[float, int]]],
53+
) -> None:
54+
"""Atomically records a batch of `Metric` and value pairs.
5155
56+
Allows the functionality of acting upon multiple metrics with
57+
a single API call. Implementations should find handles that match
58+
the key-value pairs in the label tuples.
5259
53-
class ValueType(Enum):
54-
FLOAT = 0
55-
INT = 1
60+
Args:
61+
label_tuples: A collection of key value pairs that will be matched
62+
against to record for the metric-handle that has those labels.
63+
record_tuples: A list of pairs of `Metric` s and the
64+
corresponding value to record for that metric.
65+
"""
5666

5767

5868
def create_counter(
5969
name: str,
6070
description: str,
6171
unit: str,
62-
value_type: "ValueType",
72+
value_type: Union[Type[float], Type[int]],
6373
is_bidirectional: bool = False,
6474
label_keys: List[str] = None,
6575
span_context: SpanContext = None,
@@ -91,7 +101,7 @@ def create_gauge(
91101
name: str,
92102
description: str,
93103
unit: str,
94-
value_type: "ValueType",
104+
value_type: Union[Type[float], Type[int]],
95105
is_unidirectional: bool = False,
96106
label_keys: List[str] = None,
97107
span_context: SpanContext = None,
@@ -122,7 +132,7 @@ def create_measure(
122132
name: str,
123133
description: str,
124134
unit: str,
125-
value_type: "ValueType",
135+
value_type: Union[Type[float], Type[int]],
126136
is_non_negative: bool = False,
127137
label_keys: List[str] = None,
128138
span_context: SpanContext = None,
@@ -157,82 +167,71 @@ class Metric(ABC):
157167
"""
158168

159169
@abstractmethod
160-
def get_or_create_time_series(self, label_values: List[str]) -> "object":
161-
"""Gets a timeseries, used for repeated-use of metrics instruments.
170+
def get_handle(self, label_values: List[str]) -> "object":
171+
"""Gets a handle, used for repeated-use of metrics instruments.
162172
163-
If the provided label values are not already associated with this
164-
metric, a new timeseries is returned, otherwise it returns the existing
165-
timeseries with the exact label values. The timeseries returned
166-
contains logic and behaviour specific to the type of metric that
167-
overrides this function.
173+
Handles are useful to reduce the cost of repeatedly recording a metric
174+
with a pre-defined set of label values. All metric kinds (counter,
175+
gauge, measure) support declaring a set of required label keys. The
176+
values corresponding to these keys should be specified in every handle.
177+
"Unspecified" label values, in cases where a handle is requested but
178+
a value was not provided are permitted.
168179
169180
Args:
170181
label_values: A list of label values that will be associated
171-
with the return timeseries.
182+
with the return handle.
172183
"""
173184

174-
def remove_time_series(self, label_values: List[str]) -> None:
175-
"""Removes the timeseries from the Metric, if present.
185+
def remove_handle(self, label_values: List[str]) -> None:
186+
"""Removes the handle from the Metric, if present.
176187
177-
The timeseries with matching label values will be removed.
188+
The handle with matching label values will be removed.
178189
179190
args:
180191
label_values: The list of label values to match against.
181192
"""
182193

183194
def clear(self) -> None:
184-
"""Removes all timeseries from the `Metric`."""
195+
"""Removes all handles from the `Metric`."""
185196

186197

187198
class FloatCounter(Metric):
188199
"""A counter type metric that holds float values."""
189200

190-
def get_or_create_time_series(
191-
self, label_values: List[str]
192-
) -> "CounterTimeSeries":
193-
"""Gets a `CounterTimeSeries` with a float value."""
201+
def get_handle(self, label_values: List[str]) -> "CounterHandle":
202+
"""Gets a `CounterHandle` with a float value."""
194203

195204

196205
class IntCounter(Metric):
197206
"""A counter type metric that holds int values."""
198207

199-
def get_or_create_time_series(
200-
self, label_values: List[str]
201-
) -> "CounterTimeSeries":
202-
"""Gets a `CounterTimeSeries` with an int value."""
208+
def get_handle(self, label_values: List[str]) -> "CounterHandle":
209+
"""Gets a `CounterHandle` with an int value."""
203210

204211

205212
class FloatGauge(Metric):
206213
"""A gauge type metric that holds float values."""
207214

208-
def get_or_create_time_series(
209-
self, label_values: List[str]
210-
) -> "GaugeTimeSeries":
211-
"""Gets a `GaugeTimeSeries` with a float value."""
215+
def get_handle(self, label_values: List[str]) -> "GaugeHandle":
216+
"""Gets a `GaugeHandle` with a float value."""
212217

213218

214219
class IntGauge(Metric):
215220
"""A gauge type metric that holds int values."""
216221

217-
def get_or_create_time_series(
218-
self, label_values: List[str]
219-
) -> "GaugeTimeSeries":
220-
"""Gets a `GaugeTimeSeries` with an int value."""
222+
def get_handle(self, label_values: List[str]) -> "GaugeHandle":
223+
"""Gets a `GaugeHandle` with an int value."""
221224

222225

223226
class FloatMeasure(Metric):
224227
"""A measure type metric that holds float values."""
225228

226-
def get_or_create_time_series(
227-
self, label_values: List[str]
228-
) -> "MeasureTimeSeries":
229-
"""Gets a `MeasureTimeSeries` with a float value."""
229+
def get_handle(self, label_values: List[str]) -> "MeasureHandle":
230+
"""Gets a `MeasureHandle` with a float value."""
230231

231232

232233
class IntMeasure(Metric):
233234
"""A measure type metric that holds int values."""
234235

235-
def get_or_create_time_series(
236-
self, label_values: List[str]
237-
) -> "MeasureTimeSeries":
238-
"""Gets a `MeasureTimeSeries` with an int value."""
236+
def get_handle(self, label_values: List[str]) -> "MeasureHandle":
237+
"""Gets a `MeasureHandle` with an int value."""

opentelemetry-api/src/opentelemetry/metrics/examples/pre_aggregated.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from opentelemetry import metrics
1717

1818
METER = metrics.Meter()
19-
COUNTER = METER.create_int_counter(
19+
COUNTER = METER.create_counter(
2020
"sum numbers",
2121
"sum numbers over time",
2222
"number",
@@ -25,8 +25,8 @@
2525
)
2626

2727
# Metrics sent to some exporter
28-
METRIC_TESTING = COUNTER.get_or_create_time_series("Testing")
29-
METRIC_STAGING = COUNTER.get_or_create_time_series("Staging")
28+
METRIC_TESTING = COUNTER.get_handle("Testing")
29+
METRIC_STAGING = COUNTER.get_handle("Staging")
3030

3131
for i in range(100):
3232
METRIC_STAGING.add(i)

opentelemetry-api/src/opentelemetry/metrics/examples/raw.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
)
2626

2727
# Metrics sent to some exporter
28-
MEASURE_METRIC_TESTING = MEASURE.get_or_create_time_series("Testing")
29-
MEASURE_METRIC_STAGING = MEASURE.get_or_create_time_series("Staging")
28+
MEASURE_METRIC_TESTING = MEASURE.get_handle("Testing")
29+
MEASURE_METRIC_STAGING = MEASURE.get_handle("Staging")
3030

3131
# record individual measures
3232
STATISTIC = 100
3333
MEASURE_METRIC_STAGING.record(STATISTIC)
34+
35+
# Batch recording
36+
METER.record_batch([tuple(MEASURE_METRIC_STAGING, STATISTIC)])

opentelemetry-api/src/opentelemetry/metrics/time_series.py renamed to opentelemetry-api/src/opentelemetry/metrics/handle.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
import typing
1616

1717

18-
class CounterTimeSeries:
18+
class CounterHandle:
1919
def add(self, value: typing.Union[float, int]) -> None:
2020
"""Adds the given value to the current value.
2121
2222
The input value cannot be negative if not bidirectional.
2323
"""
2424

2525

26-
class GaugeTimeSeries:
26+
class GaugeHandle:
2727
def set(self, value: typing.Union[float, int]) -> None:
2828
"""Sets the current value to the given value. Can be negative."""
2929

3030

31-
class MeasureTimeSeries:
31+
class MeasureHandle:
3232
def record(self, value: typing.Union[float, int]) -> None:
3333
"""Records the given value to this measure."""

0 commit comments

Comments
 (0)