Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metrics API RFC 0009 #140

Merged
merged 51 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
6ca4274
Create functions
lzchen Jul 29, 2019
b23cec1
fix lint
lzchen Jul 31, 2019
981eece
Fix lint
lzchen Jul 31, 2019
8ea9709
fix typing
lzchen Jul 31, 2019
00b4f11
Remove options, constructors, seperate labels
lzchen Aug 6, 2019
34c87ce
Consistent naming for float and int
lzchen Aug 6, 2019
df8ae34
Abstract time series
lzchen Aug 6, 2019
a2561ac
Use ABC
lzchen Aug 6, 2019
1ece493
Fix typo
lzchen Aug 6, 2019
ce9268a
Fix docs
lzchen Aug 6, 2019
f5f9f01
seperate measure classes
lzchen Aug 8, 2019
74a1815
Add examples
lzchen Aug 8, 2019
0a0b8ee
fix lint
lzchen Aug 8, 2019
555bf50
Update to RFC 0003
lzchen Aug 14, 2019
d6b1113
Add spancontext, measurebatch
lzchen Aug 14, 2019
c819109
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Aug 15, 2019
18cfc71
Fix docs
lzchen Aug 15, 2019
f646555
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Aug 19, 2019
a44cb47
Fix comments
lzchen Aug 22, 2019
94eaad9
fix lint
lzchen Aug 22, 2019
fc251b2
fix lint
lzchen Aug 22, 2019
262f312
fix lint
lzchen Aug 22, 2019
66c0a56
skip examples
lzchen Aug 22, 2019
e2c4a7e
white space
lzchen Aug 22, 2019
2fb7646
fix spacing
lzchen Aug 22, 2019
eb711cb
fix imports
lzchen Aug 22, 2019
baa3a32
fix imports
lzchen Aug 22, 2019
5c30a9c
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Sep 3, 2019
211b20c
LabelValues to str
lzchen Sep 3, 2019
bffe040
Black formatting
lzchen Sep 3, 2019
0759b9a
fix isort
lzchen Sep 3, 2019
44d62f8
Remove aggregation
lzchen Sep 12, 2019
c5ab2df
Fix names
lzchen Sep 12, 2019
50d2de5
Remove aggregation from docs
lzchen Sep 12, 2019
d79bc7d
Fix lint
lzchen Sep 12, 2019
7fb7cdf
metric changes
lzchen Sep 13, 2019
79322f5
Typing
lzchen Sep 13, 2019
db242f0
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Sep 13, 2019
f9c7cc3
Fix lint
lzchen Sep 13, 2019
4465929
Fix lint
lzchen Sep 13, 2019
695e596
Add space
lzchen Sep 13, 2019
ee43e39
Fix lint
lzchen Sep 13, 2019
e59f7c4
fix comments
lzchen Sep 16, 2019
9946028
handle, recordbatch
lzchen Sep 16, 2019
e1a3101
docs
lzchen Sep 16, 2019
47d5209
Update recordbatch
lzchen Sep 16, 2019
2553b89
black
lzchen Sep 16, 2019
15c2894
Merge branch 'master' of https://github.com/open-telemetry/openteleme…
lzchen Sep 19, 2019
ebcc567
Fix typo
lzchen Sep 19, 2019
41ee12c
remove ValueType
lzchen Sep 19, 2019
62862a2
fix lint
lzchen Sep 19, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/opentelemetry.metrics.handle.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
opentelemetry.metrics.handle module
==========================================

.. automodule:: opentelemetry.metrics.handle

2 changes: 1 addition & 1 deletion docs/opentelemetry.metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Submodules

.. toctree::

opentelemetry.metrics.time_series
opentelemetry.metrics.handle

Module contents
---------------
Expand Down
5 changes: 0 additions & 5 deletions docs/opentelemetry.metrics.time_series.rst

This file was deleted.

97 changes: 48 additions & 49 deletions opentelemetry-api/src/opentelemetry/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
"""
from abc import ABC, abstractmethod
from enum import Enum
from typing import List, Union
from typing import Dict, List, Tuple, Type, Union

from opentelemetry.metrics.time_series import (
CounterTimeSeries,
GaugeTimeSeries,
MeasureTimeSeries,
from opentelemetry.metrics.handle import (
CounterHandle,
GaugeHandle,
MeasureHandle,
)
from opentelemetry.trace import SpanContext

Expand All @@ -47,19 +46,30 @@ class Meter:
for the exported metric are deferred.
"""

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


def create_counter(
name: str,
description: str,
unit: str,
value_type: "ValueType",
value_type: Union[Type[float], Type[int]],
is_bidirectional: bool = False,
label_keys: List[str] = None,
span_context: SpanContext = None,
Expand Down Expand Up @@ -91,7 +101,7 @@ def create_gauge(
name: str,
description: str,
unit: str,
value_type: "ValueType",
value_type: Union[Type[float], Type[int]],
is_unidirectional: bool = False,
label_keys: List[str] = None,
span_context: SpanContext = None,
Expand Down Expand Up @@ -122,7 +132,7 @@ def create_measure(
name: str,
description: str,
unit: str,
value_type: "ValueType",
value_type: Union[Type[float], Type[int]],
is_non_negative: bool = False,
label_keys: List[str] = None,
span_context: SpanContext = None,
Expand Down Expand Up @@ -157,82 +167,71 @@ class Metric(ABC):
"""

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

def remove_time_series(self, label_values: List[str]) -> None:
"""Removes the timeseries from the Metric, if present.
def remove_handle(self, label_values: List[str]) -> None:
"""Removes the handle from the Metric, if present.
The timeseries with matching label values will be removed.
The handle with matching label values will be removed.
args:
label_values: The list of label values to match against.
"""

def clear(self) -> None:
"""Removes all timeseries from the `Metric`."""
"""Removes all handles from the `Metric`."""


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

def get_or_create_time_series(
self, label_values: List[str]
) -> "CounterTimeSeries":
"""Gets a `CounterTimeSeries` with a float value."""
def get_handle(self, label_values: List[str]) -> "CounterHandle":
"""Gets a `CounterHandle` with a float value."""


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

def get_or_create_time_series(
self, label_values: List[str]
) -> "CounterTimeSeries":
"""Gets a `CounterTimeSeries` with an int value."""
def get_handle(self, label_values: List[str]) -> "CounterHandle":
"""Gets a `CounterHandle` with an int value."""


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

def get_or_create_time_series(
self, label_values: List[str]
) -> "GaugeTimeSeries":
"""Gets a `GaugeTimeSeries` with a float value."""
def get_handle(self, label_values: List[str]) -> "GaugeHandle":
"""Gets a `GaugeHandle` with a float value."""


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

def get_or_create_time_series(
self, label_values: List[str]
) -> "GaugeTimeSeries":
"""Gets a `GaugeTimeSeries` with an int value."""
def get_handle(self, label_values: List[str]) -> "GaugeHandle":
"""Gets a `GaugeHandle` with an int value."""


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

def get_or_create_time_series(
self, label_values: List[str]
) -> "MeasureTimeSeries":
"""Gets a `MeasureTimeSeries` with a float value."""
def get_handle(self, label_values: List[str]) -> "MeasureHandle":
"""Gets a `MeasureHandle` with a float value."""


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

def get_or_create_time_series(
self, label_values: List[str]
) -> "MeasureTimeSeries":
"""Gets a `MeasureTimeSeries` with an int value."""
def get_handle(self, label_values: List[str]) -> "MeasureHandle":
"""Gets a `MeasureHandle` with an int value."""
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from opentelemetry import metrics

METER = metrics.Meter()
COUNTER = METER.create_int_counter(
COUNTER = METER.create_counter(
"sum numbers",
"sum numbers over time",
"number",
Expand All @@ -25,8 +25,8 @@
)

# Metrics sent to some exporter
METRIC_TESTING = COUNTER.get_or_create_time_series("Testing")
METRIC_STAGING = COUNTER.get_or_create_time_series("Staging")
METRIC_TESTING = COUNTER.get_handle("Testing")
METRIC_STAGING = COUNTER.get_handle("Staging")

for i in range(100):
METRIC_STAGING.add(i)
7 changes: 5 additions & 2 deletions opentelemetry-api/src/opentelemetry/metrics/examples/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
)

# Metrics sent to some exporter
MEASURE_METRIC_TESTING = MEASURE.get_or_create_time_series("Testing")
MEASURE_METRIC_STAGING = MEASURE.get_or_create_time_series("Staging")
MEASURE_METRIC_TESTING = MEASURE.get_handle("Testing")
MEASURE_METRIC_STAGING = MEASURE.get_handle("Staging")

# record individual measures
STATISTIC = 100
MEASURE_METRIC_STAGING.record(STATISTIC)

# Batch recording
METER.record_batch([tuple(MEASURE_METRIC_STAGING, STATISTIC)])
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
import typing


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


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


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