Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 870
Annotate few functions and methods#705
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,23 @@ | ||
| from threading import Lock | ||
| import time | ||
| import types | ||
| from typing import ( | ||
| Any, Callable, Dict, Optional, Sequence, Tuple, Type, TypeVar, | ||
| ) | ||
| from . import values # retain this import style for testability | ||
| from .context_managers import ExceptionCounter, InprogressTracker, Timer | ||
| from .metrics_core import ( | ||
| Metric, METRIC_LABEL_NAME_RE, METRIC_NAME_RE, | ||
| RESERVED_METRIC_LABEL_NAME_RE, | ||
| ) | ||
| from .registry import REGISTRY | ||
| from .registry import CollectorRegistry, REGISTRY | ||
| from .samples import Exemplar | ||
| from .utils import floatToGoString, INF | ||
| T = TypeVar('T', bound='MetricWrapperBase') | ||
csmarchbanks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| F = TypeVar("F", bound=Callable[..., Any]) | ||
| def _build_full_name(metric_type, name, namespace, subsystem, unit): | ||
| full_name = '' | ||
| @@ -56,8 +62,8 @@ def _validate_exemplar(exemplar): | ||
| class MetricWrapperBase: | ||
| _type = None | ||
| _reserved_labelnames = () | ||
| _type: Optional[str] = None | ||
| _reserved_labelnames: Sequence[str] = () | ||
| def _is_observable(self): | ||
| # Whether this metric is observable, i.e. | ||
| @@ -94,20 +100,20 @@ def __repr__(self): | ||
| metric_type = type(self) | ||
| return f"{metric_type.__module__}.{metric_type.__name__}({self._name})" | ||
| def __init__(self, | ||
| name, | ||
| documentation, | ||
| labelnames=(), | ||
| namespace='', | ||
| subsystem='', | ||
| unit='', | ||
| registry=REGISTRY, | ||
| _labelvalues=None, | ||
| ): | ||
| def __init__(self: T, | ||
| name: str, | ||
| documentation: str, | ||
| labelnames: Sequence[str]=(), | ||
| namespace: str='', | ||
| subsystem: str='', | ||
| unit: str='', | ||
| registry: CollectorRegistry=REGISTRY, | ||
| _labelvalues: Optional[Sequence[str]]=None, | ||
| ) -> None: | ||
| self._name = _build_full_name(self._type, name, namespace, subsystem, unit) | ||
| self._labelnames = _validate_labelnames(self, labelnames) | ||
| self._labelvalues = tuple(_labelvalues or ()) | ||
| self._kwargs = {} | ||
| self._kwargs: Dict[str, Any] = {} | ||
| self._documentation = documentation | ||
| self._unit = unit | ||
| @@ -117,7 +123,7 @@ def __init__(self, | ||
| if self._is_parent(): | ||
| # Prepare the fields needed for child metrics. | ||
| self._lock = Lock() | ||
| self._metrics = {} | ||
| self._metrics: Dict[Sequence[str], T] = {} | ||
| if self._is_observable(): | ||
| self._metric_init() | ||
| @@ -127,7 +133,7 @@ def __init__(self, | ||
| if registry: | ||
| registry.register(self) | ||
| def labels(self, *labelvalues, **labelkwargs): | ||
| def labels(self: T, *labelvalues: str, **labelkwargs: str) -> T: | ||
| """Return the child for the given labelset. | ||
| All metrics can have labels, allowing grouping of related time series. | ||
| @@ -193,7 +199,7 @@ def remove(self, *labelvalues): | ||
| with self._lock: | ||
| del self._metrics[labelvalues] | ||
| def clear(self): | ||
| def clear(self) -> None: | ||
| """Remove all labelsets from the metric""" | ||
| with self._lock: | ||
| self._metrics = {} | ||
| @@ -212,7 +218,7 @@ def _multi_samples(self): | ||
| for suffix, sample_labels, value, timestamp, exemplar in metric._samples(): | ||
| yield (suffix, dict(series_labels + list(sample_labels.items())), value, timestamp, exemplar) | ||
| def _child_samples(self): # pragma: no cover | ||
| def _child_samples(self) -> Sequence[Tuple[str, Dict[str, str], float]]: # pragma: no cover | ||
| raise NotImplementedError('_child_samples() must be implemented by %r' % self) | ||
| def _metric_init(self): # pragma: no cover | ||
| @@ -258,12 +264,12 @@ def f(): | ||
| """ | ||
| _type = 'counter' | ||
| def _metric_init(self): | ||
| def _metric_init(self) -> None: | ||
| self._value = values.ValueClass(self._type, self._name, self._name + '_total', self._labelnames, | ||
| self._labelvalues) | ||
| self._created = time.time() | ||
| def inc(self, amount=1, exemplar=None): | ||
| def inc(self, amount: float=1, exemplar: Optional[Dict[str, str]]=None) -> None: | ||
| """Increment counter by the given amount.""" | ||
| self._raise_if_not_observable() | ||
| if amount < 0: | ||
| @@ -273,7 +279,7 @@ def inc(self, amount=1, exemplar=None): | ||
| _validate_exemplar(exemplar) | ||
| self._value.set_exemplar(Exemplar(exemplar, amount, time.time())) | ||
| def count_exceptions(self, exception=Exception): | ||
| def count_exceptions(self, exception: Type[BaseException]=Exception) -> ExceptionCounter: | ||
| """Count exceptions in a block of code or function. | ||
| Can be used as a function decorator or context manager. | ||
| @@ -667,15 +673,15 @@ class Enum(MetricWrapperBase): | ||
| _type = 'stateset' | ||
| def __init__(self, | ||
| name, | ||
| documentation, | ||
| labelnames=(), | ||
| namespace='', | ||
| subsystem='', | ||
| unit='', | ||
| registry=REGISTRY, | ||
| _labelvalues=None, | ||
| states=None, | ||
| name: str, | ||
| documentation: str, | ||
| labelnames: Sequence[str]=(), | ||
| namespace: str='', | ||
| subsystem: str='', | ||
| unit: str='', | ||
| registry: CollectorRegistry=REGISTRY, | ||
| _labelvalues: Optional[Sequence[str]]=None, | ||
| states: Optional[Sequence[str]]=None, | ||
| ): | ||
| super().__init__( | ||
| name=name, | ||
| @@ -693,11 +699,11 @@ def __init__(self, | ||
| raise ValueError(f'No states provided for Enum metric: {name}') | ||
| self._kwargs['states'] = self._states = states | ||
| def _metric_init(self): | ||
| def _metric_init(self) -> None: | ||
| self._value = 0 | ||
| self._lock = Lock() | ||
| def state(self, state): | ||
| def state(self, state: str) -> None: | ||
| """Set enum metric state.""" | ||
| self._raise_if_not_observable() | ||
| with self._lock: | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.