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

Skip to content

Commit ac3734c

Browse files
committed
lint
1 parent 3eb7dcd commit ac3734c

File tree

9 files changed

+98
-157
lines changed

9 files changed

+98
-157
lines changed

docs/examples/basic_meter/view.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from opentelemetry.sdk.metrics import (
2121
MeterProvider,
2222
UpDownCounter,
23-
ValueRecorder
23+
ValueRecorder,
2424
)
2525
from opentelemetry.sdk.metrics.export.aggregate import (
2626
HistogramAggregator,
@@ -62,29 +62,29 @@
6262
requests_counter,
6363
SumAggregator(),
6464
label_keys=["environment"],
65-
config=ViewConfig.LABEL_KEYS
65+
config=ViewConfig.LABEL_KEYS,
6666
)
6767
counter_view2 = View(
6868
requests_counter,
6969
MinMaxSumCountAggregator(),
7070
label_keys=["os_type"],
71-
config=ViewConfig.LABEL_KEYS
71+
config=ViewConfig.LABEL_KEYS,
7272
)
7373
# This view has ViewConfig set to UNGROUPED, meaning all recorded metrics take
7474
# the labels directly without and consideration for label_keys
7575
counter_view3 = View(
7676
requests_counter,
7777
LastValueAggregator(),
78-
label_keys=["environment"], # is not used due to ViewConfig.UNGROUPED
79-
config=ViewConfig.UNGROUPED
78+
label_keys=["environment"], # is not used due to ViewConfig.UNGROUPED
79+
config=ViewConfig.UNGROUPED,
8080
)
8181
# This view uses the HistogramAggregator which accepts an option config
8282
# parameter to specify the bucket ranges
8383
size_view = View(
8484
requests_size,
85-
HistogramAggregator(config=[20,40,60,80,100]),
86-
label_keys=["environment"], # is not used due to ViewConfig.UNGROUPED
87-
config=ViewConfig.UNGROUPED
85+
HistogramAggregator(config=[20, 40, 60, 80, 100]),
86+
label_keys=["environment"], # is not used due to ViewConfig.UNGROUPED
87+
config=ViewConfig.UNGROUPED,
8888
)
8989

9090
# Register the views to the view manager to use the views. Views MUST be

ext/opentelemetry-ext-prometheus/tests/test_prometheus_exporter.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ def setUp(self):
3232
set_meter_provider(metrics.MeterProvider())
3333
self._meter = get_meter_provider().get_meter(__name__)
3434
self._test_metric = self._meter.create_metric(
35-
"testname",
36-
"testdesc",
37-
"unit",
38-
int,
39-
metrics.Counter,
35+
"testname", "testdesc", "unit", int, metrics.Counter,
4036
)
4137
labels = {"environment": "staging"}
4238
self._labels_key = metrics.get_dict_as_key(labels)
@@ -77,11 +73,7 @@ def test_export(self):
7773
def test_counter_to_prometheus(self):
7874
meter = get_meter_provider().get_meter(__name__)
7975
metric = meter.create_metric(
80-
"test@name",
81-
"testdesc",
82-
"unit",
83-
int,
84-
metrics.Counter,
76+
"test@name", "testdesc", "unit", int, metrics.Counter,
8577
)
8678
labels = {"environment@": "staging", "os": "Windows"}
8779
key_labels = metrics.get_dict_as_key(labels)
@@ -144,10 +136,5 @@ def __init__(
144136
enabled: bool = True,
145137
):
146138
super().__init__(
147-
name,
148-
description,
149-
unit,
150-
value_type,
151-
meter,
152-
enabled=enabled,
139+
name, description, unit, value_type, meter, enabled=enabled,
153140
)

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

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class BaseBoundInstrument:
4949
"""
5050

5151
def __init__(
52-
self,
53-
labels: Tuple[Tuple[str, str]],
54-
metric: metrics_api.MetricT,
52+
self, labels: Tuple[Tuple[str, str]], metric: metrics_api.MetricT,
5553
):
5654
self._labels = labels
5755
self._metric = metric
58-
self.view_datas = metric.meter.view_manager.generate_view_datas(metric, labels)
56+
self.view_datas = metric.meter.view_manager.generate_view_datas(
57+
metric, labels
58+
)
5959
self._view_datas_lock = threading.Lock()
6060
self._ref_count = 0
6161
self._ref_count_lock = threading.Lock()
@@ -66,7 +66,7 @@ def _validate_update(self, value: metrics_api.ValueT) -> bool:
6666
if not isinstance(value, self._metric.value_type):
6767
logger.warning(
6868
"Invalid value passed for %s.",
69-
self._metric.value_type.__name__
69+
self._metric.value_type.__name__,
7070
)
7171
return False
7272
return True
@@ -165,10 +165,7 @@ def bind(self, labels: Dict[str, str]) -> BaseBoundInstrument:
165165
with self.bound_instruments_lock:
166166
bound_instrument = self.bound_instruments.get(key)
167167
if bound_instrument is None:
168-
bound_instrument = self.BOUND_INSTR_TYPE(
169-
key,
170-
self,
171-
)
168+
bound_instrument = self.BOUND_INSTR_TYPE(key, self,)
172169
self.bound_instruments[key] = bound_instrument
173170
bound_instrument.increase_ref_count()
174171
return bound_instrument
@@ -380,9 +377,14 @@ def _collect_metrics(self) -> None:
380377
continue
381378
to_remove = []
382379
with metric.bound_instruments_lock:
383-
for labels, bound_instrument in metric.bound_instruments.items():
380+
for (
381+
labels,
382+
bound_instrument,
383+
) in metric.bound_instruments.items():
384384
for view_data in bound_instrument.view_datas:
385-
record = Record(metric, view_data.labels, view_data.aggregator)
385+
record = Record(
386+
metric, view_data.labels, view_data.aggregator
387+
)
386388
self.batcher.process(record)
387389

388390
if bound_instrument.ref_count() == 0:
@@ -428,12 +430,7 @@ def create_metric(
428430
"""See `opentelemetry.metrics.Meter.create_metric`."""
429431
# Ignore type b/c of mypy bug in addition to missing annotations
430432
metric = metric_type( # type: ignore
431-
name,
432-
description,
433-
unit,
434-
value_type,
435-
self,
436-
enabled=enabled,
433+
name, description, unit, value_type, self, enabled=enabled,
437434
)
438435
with self.metrics_lock:
439436
self.metrics.add(metric)
@@ -451,13 +448,7 @@ def register_observer(
451448
enabled: bool = True,
452449
) -> metrics_api.Observer:
453450
ob = observer_type(
454-
callback,
455-
name,
456-
description,
457-
unit,
458-
value_type,
459-
label_keys,
460-
enabled,
451+
callback, name, description, unit, value_type, label_keys, enabled,
461452
)
462453
with self.observers_lock:
463454
self.observers.add(ob)

opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/aggregate.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def __init__(self, config=None):
142142
if boundaries and self._validate_boundaries(boundaries):
143143
self._boundaries = boundaries
144144
else:
145-
self._boundaries = (10,20,30,40,50,60,70,80,90,100)
145+
self._boundaries = (10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
146146
self.current = OrderedDict([(bb, 0) for bb in self._boundaries])
147147
self.checkpoint = OrderedDict([(bb, 0) for bb in self._boundaries])
148148
self.current[">"] = 0
@@ -152,8 +152,10 @@ def _validate_boundaries(self, boundaries):
152152
if not boundaries:
153153
logger.warning("Bounds is empty. Using default.")
154154
return False
155-
if not all(boundaries[ii] < boundaries[ii + 1]
156-
for ii in range(len(boundaries) - 1)):
155+
if not all(
156+
boundaries[ii] < boundaries[ii + 1]
157+
for ii in range(len(boundaries) - 1)
158+
):
157159
logger.warning(
158160
"Bounds must be sorted in increasing order. Using default."
159161
)
@@ -174,7 +176,7 @@ def update(self, value):
174176
if self.current is None:
175177
self.current = [0 for ii in range(len(self._boundaries) + 1)]
176178
# greater than max value
177-
if value >= self._boundaries[len(self._boundaries)-1]:
179+
if value >= self._boundaries[len(self._boundaries) - 1]:
178180
self.current[">"] += 1
179181
else:
180182
for bb in self._boundaries:
@@ -278,6 +280,6 @@ def verify_type(this, other):
278280
logger.warning(
279281
"Error in merging %s with %s.",
280282
this.__class__.__name__,
281-
other.__class__.__name__
283+
other.__class__.__name__,
282284
)
283285
return False

opentelemetry-sdk/src/opentelemetry/sdk/metrics/export/batcher.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ def checkpoint_set(self) -> Sequence[MetricRecord]:
4040
data in all of the aggregators in this batcher.
4141
"""
4242
metric_records = []
43-
for (instrument, aggregator_type, labels), aggregator in self._batch_map.items():
43+
for (
44+
(instrument, aggregator_type, labels),
45+
aggregator,
46+
) in self._batch_map.items():
4447
metric_records.append(MetricRecord(instrument, labels, aggregator))
4548
return metric_records
4649

@@ -70,6 +73,8 @@ def process(self, record) -> None:
7073
if self.stateful:
7174
# if stateful batcher, create a copy of the aggregator and update
7275
# it with the current checkpointed value for long-term storage
73-
aggregator = record.aggregator.__class__(config=record.aggregator.config)
76+
aggregator = record.aggregator.__class__(
77+
config=record.aggregator.config
78+
)
7479
aggregator.merge(record.aggregator)
7580
self._batch_map[key] = aggregator

opentelemetry-sdk/src/opentelemetry/sdk/metrics/view.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838

3939

4040
class ViewData:
41-
4241
def __init__(self, labels: Tuple[Tuple[str, str]], aggregator: Aggregator):
4342
self.labels = labels
4443
self.aggregator = aggregator
@@ -48,14 +47,13 @@ def record(self, value: ValueT):
4847

4948
# Uniqueness is based on labels and aggregator type
5049
def __hash__(self):
51-
return hash(
52-
(self.labels,
53-
self.aggregator.__class__)
54-
)
50+
return hash((self.labels, self.aggregator.__class__))
5551

5652
def __eq__(self, other):
57-
return self.labels == other.labels and \
58-
self.aggregator.__class__ == other.aggregator.__class__
53+
return (
54+
self.labels == other.labels
55+
and self.aggregator.__class__ == other.aggregator.__class__
56+
)
5957

6058

6159
class ViewConfig:
@@ -66,8 +64,8 @@ class ViewConfig:
6664

6765

6866
class View:
69-
70-
def __init__(self,
67+
def __init__(
68+
self,
7169
metric: InstrumentT,
7270
aggregator: Aggregator,
7371
label_keys: Sequence[str] = None,
@@ -83,23 +81,21 @@ def __init__(self,
8381
# Uniqueness is based on metric, aggregator type, ordered label keys and ViewConfig
8482
def __hash__(self):
8583
return hash(
86-
(self.metric,
87-
self.aggregator,
88-
tuple(self.label_keys),
89-
self.config)
84+
(self.metric, self.aggregator, tuple(self.label_keys), self.config)
9085
)
9186

9287
def __eq__(self, other):
93-
return self.metric == other.metric and \
94-
self.aggregator.__class__ == other.aggregator.__class__ and \
95-
self.label_keys == other.label_keys and \
96-
self.config == other.config
88+
return (
89+
self.metric == other.metric
90+
and self.aggregator.__class__ == other.aggregator.__class__
91+
and self.label_keys == other.label_keys
92+
and self.config == other.config
93+
)
9794

9895

9996
class ViewManager:
100-
10197
def __init__(self):
102-
self.views = defaultdict(set) # Map[Metric, Set]
98+
self.views = defaultdict(set) # Map[Metric, Set]
10399
self._view_lock = threading.Lock()
104100

105101
def register_view(self, view):
@@ -139,7 +135,9 @@ def generate_view_datas(self, metric, labels):
139135
updated_labels = labels
140136
# ViewData that is duplicate (same labels and aggregator) will be
141137
# aggregated together as one
142-
view_datas.add(ViewData(tuple(updated_labels), view.aggregator))
138+
view_datas.add(
139+
ViewData(tuple(updated_labels), view.aggregator)
140+
)
143141
return view_datas
144142

145143

@@ -159,4 +157,4 @@ def get_default_aggregator(instrument: InstrumentT) -> Aggregator:
159157
if issubclass(instrument_type, ValueObserver):
160158
return ValueObserverAggregator()
161159
logger.warning("No default aggregator configured for: %s", instrument_type)
162-
return SumAggregator()
160+
return SumAggregator()

0 commit comments

Comments
 (0)