Skip to content

Commit d2d893f

Browse files
authored
Upgrade ruff to 0.16 (open-telemetry#5471)
* Upgrade ruff * Change ruff stuff
1 parent 6706ae4 commit d2d893f

11 files changed

Lines changed: 124 additions & 50 deletions

File tree

.pre-commit-config.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
33
# Ruff version.
4-
rev: v0.14.1
4+
rev: v0.16.0
55
hooks:
66
# Run the linter.
77
- id: ruff
88
args: ["--fix", "--show-fixes"]
99
# Run the formatter.
1010
- id: ruff-format
11+
- id: ruff-format
12+
name: ruff-format (markdown & towncrier)
13+
types_or: [python, pyi, jupyter, markdown, text]
14+
files: \.(md|added|changed|deprecated|removed|fixed)$
15+
args: ["--preview"]
1116
- repo: https://github.com/astral-sh/uv-pre-commit
1217
# uv version.
1318
rev: 0.6.0

CHANGELOG.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -545,24 +545,33 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
545545
```python
546546
# Before
547547
from opentelemetry.sdk._logs import LogData
548-
def export(self, batch: Sequence[LogData]) -> LogRecordExportResult:
549-
...
548+
549+
550+
def export(self, batch: Sequence[LogData]) -> LogRecordExportResult: ...
551+
550552

551553
# After
552554
from opentelemetry.sdk._logs import ReadableLogRecord
553-
def export(self, batch: Sequence[ReadableLogRecord]) -> LogRecordExportResult:
554-
...
555+
556+
557+
def export(
558+
self, batch: Sequence[ReadableLogRecord]
559+
) -> LogRecordExportResult: ...
555560
```
556561

557562
- **For Log Processors:** Use `ReadWriteLogRecord` for processing, `ReadableLogRecord` for exporting
558563
```python
559564
# Before
560565
from opentelemetry.sdk._logs import LogData
561-
def on_emit(self, log_data: LogData):
562-
...
566+
567+
568+
def on_emit(self, log_data: LogData): ...
569+
563570

564571
# After
565572
from opentelemetry.sdk._logs import ReadWriteLogRecord, ReadableLogRecord
573+
574+
566575
def on_emit(self, log_record: ReadWriteLogRecord):
567576
# Convert to ReadableLogRecord before exporting
568577
readable = ReadableLogRecord(

dev-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ asgiref==3.7.2
1717
psutil==7.2.2
1818
GitPython==3.1.52
1919
pre-commit==3.7.0
20-
ruff==0.14.1
20+
ruff==0.16.0

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

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -332,73 +332,120 @@ def create_observable_counter(
332332
For example, an observable counter could be used to report system CPU
333333
time periodically. Here is a basic implementation::
334334
335-
def cpu_time_callback(options: CallbackOptions) -> Iterable[Observation]:
335+
def cpu_time_callback(
336+
options: CallbackOptions,
337+
) -> Iterable[Observation]:
336338
observations = []
337339
with open("/proc/stat") as procstat:
338340
procstat.readline() # skip the first line
339341
for line in procstat:
340-
if not line.startswith("cpu"): break
342+
if not line.startswith("cpu"):
343+
break
341344
cpu, *states = line.split()
342-
observations.append(Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
343-
observations.append(Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
344-
observations.append(Observation(int(states[2]) // 100, {"cpu": cpu, "state": "system"}))
345+
observations.append(
346+
Observation(
347+
int(states[0]) // 100,
348+
{"cpu": cpu, "state": "user"},
349+
)
350+
)
351+
observations.append(
352+
Observation(
353+
int(states[1]) // 100,
354+
{"cpu": cpu, "state": "nice"},
355+
)
356+
)
357+
observations.append(
358+
Observation(
359+
int(states[2]) // 100,
360+
{"cpu": cpu, "state": "system"},
361+
)
362+
)
345363
# ... other states
346364
return observations
347365
366+
348367
meter.create_observable_counter(
349368
"system.cpu.time",
350369
callbacks=[cpu_time_callback],
351370
unit="s",
352-
description="CPU time"
371+
description="CPU time",
353372
)
354373
355374
To reduce memory usage, you can use generator callbacks instead of
356375
building the full list::
357376
358-
def cpu_time_callback(options: CallbackOptions) -> Iterable[Observation]:
377+
def cpu_time_callback(
378+
options: CallbackOptions,
379+
) -> Iterable[Observation]:
359380
with open("/proc/stat") as procstat:
360381
procstat.readline() # skip the first line
361382
for line in procstat:
362-
if not line.startswith("cpu"): break
383+
if not line.startswith("cpu"):
384+
break
363385
cpu, *states = line.split()
364-
yield Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"})
365-
yield Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"})
386+
yield Observation(
387+
int(states[0]) // 100,
388+
{"cpu": cpu, "state": "user"},
389+
)
390+
yield Observation(
391+
int(states[1]) // 100,
392+
{"cpu": cpu, "state": "nice"},
393+
)
366394
# ... other states
367395
368396
Alternatively, you can pass a sequence of generators directly instead of a sequence of
369397
callbacks, which each should return iterables of :class:`~opentelemetry.metrics.Observation`::
370398
371-
def cpu_time_callback(states_to_include: set[str]) -> Iterable[Iterable[Observation]]:
399+
def cpu_time_callback(
400+
states_to_include: set[str],
401+
) -> Iterable[Iterable[Observation]]:
372402
# accept options sent in from OpenTelemetry
373403
options = yield
374404
while True:
375405
observations = []
376406
with open("/proc/stat") as procstat:
377407
procstat.readline() # skip the first line
378408
for line in procstat:
379-
if not line.startswith("cpu"): break
409+
if not line.startswith("cpu"):
410+
break
380411
cpu, *states = line.split()
381412
if "user" in states_to_include:
382-
observations.append(Observation(int(states[0]) // 100, {"cpu": cpu, "state": "user"}))
413+
observations.append(
414+
Observation(
415+
int(states[0]) // 100,
416+
{"cpu": cpu, "state": "user"},
417+
)
418+
)
383419
if "nice" in states_to_include:
384-
observations.append(Observation(int(states[1]) // 100, {"cpu": cpu, "state": "nice"}))
420+
observations.append(
421+
Observation(
422+
int(states[1]) // 100,
423+
{"cpu": cpu, "state": "nice"},
424+
)
425+
)
385426
# ... other states
386427
# yield the observations and receive the options for next iteration
387428
options = yield observations
388429
430+
389431
meter.create_observable_counter(
390432
"system.cpu.time",
391433
callbacks=[cpu_time_callback({"user", "system"})],
392434
unit="s",
393-
description="CPU time"
435+
description="CPU time",
394436
)
395437
396438
The :class:`~opentelemetry.metrics.CallbackOptions` contain a timeout which the
397439
callback should respect. For example if the callback does asynchronous work, like
398440
making HTTP requests, it should respect the timeout::
399441
400-
def scrape_http_callback(options: CallbackOptions) -> Iterable[Observation]:
401-
r = requests.get('http://scrapethis.com', timeout=options.timeout_millis / 10**3)
442+
def scrape_http_callback(
443+
options: CallbackOptions,
444+
) -> Iterable[Observation]:
445+
r = requests.get(
446+
"http://scrapethis.com",
447+
timeout=options.timeout_millis / 10**3,
448+
)
402449
for value in r.json():
403450
yield Observation(value)
404451

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,24 @@
3232
def get_header_from_flask_request(request, key):
3333
return request.headers.get_all(key)
3434
35-
def set_header_into_requests_request(request: requests.Request,
36-
key: str, value: str):
35+
36+
def set_header_into_requests_request(
37+
request: requests.Request, key: str, value: str
38+
):
3739
request.headers[key] = value
3840
41+
3942
def example_route():
4043
context = PROPAGATOR.extract(
41-
get_header_from_flask_request,
42-
flask.request
44+
get_header_from_flask_request, flask.request
4345
)
4446
request_to_downstream = requests.Request(
4547
"GET", "http://httpbin.org/get"
4648
)
4749
PROPAGATOR.inject(
4850
set_header_into_requests_request,
4951
request_to_downstream,
50-
context=context
52+
context=context,
5153
)
5254
session = requests.Session()
5355
session.send(request_to_downstream.prepare())

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,8 @@ def start_as_current_span(
355355
with tracer.start_as_current_span("two") as child:
356356
child.add_event("child's event")
357357
trace.get_current_span() # returns child
358-
trace.get_current_span() # returns parent
359-
trace.get_current_span() # returns previously active span
358+
trace.get_current_span() # returns parent
359+
trace.get_current_span() # returns previously active span
360360
361361
This is a convenience method for creating spans attached to the
362362
tracer's context. Applications that need more control over the span
@@ -374,8 +374,8 @@ def start_as_current_span(
374374
This can also be used as a decorator::
375375
376376
@tracer.start_as_current_span("name")
377-
def function():
378-
...
377+
def function(): ...
378+
379379
380380
function()
381381

opentelemetry-configuration/src/opentelemetry/configuration/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@
1313
programmatic use:
1414
1515
>>> from opentelemetry.configuration import (
16-
... load_config_file, configure_sdk,
16+
... load_config_file,
17+
... configure_sdk,
1718
... )
1819
>>> config = load_config_file("otel-config.yaml")
1920
>>> configure_sdk(config)
2021
2122
Construct a configuration programmatically and apply it:
2223
2324
>>> from opentelemetry.configuration import (
24-
... OpenTelemetryConfiguration, configure_sdk,
25+
... OpenTelemetryConfiguration,
26+
... configure_sdk,
2527
... )
2628
>>> configure_sdk(OpenTelemetryConfiguration(file_format="1.0-rc.1"))
2729

opentelemetry-configuration/src/opentelemetry/configuration/_sdk.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def configure_sdk(config: OpenTelemetryConfiguration) -> None:
8585
8686
Example:
8787
>>> from opentelemetry.configuration.file import (
88-
... load_config_file, configure_sdk,
88+
... load_config_file,
89+
... configure_sdk,
8990
... )
9091
>>> config = load_config_file("otel-config.yaml")
9192
>>> configure_sdk(config)

opentelemetry-configuration/src/opentelemetry/configuration/file/_env_substitution.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ def substitute_env_vars(text: str) -> str:
2929
Text with environment variables substituted.
3030
3131
Examples:
32-
>>> os.environ['SERVICE_NAME'] = 'my-service'
33-
>>> substitute_env_vars('name: ${SERVICE_NAME}')
32+
>>> os.environ["SERVICE_NAME"] = "my-service"
33+
>>> substitute_env_vars("name: ${SERVICE_NAME}")
3434
'name: my-service'
35-
>>> substitute_env_vars('name: ${MISSING:-default}')
35+
>>> substitute_env_vars("name: ${MISSING:-default}")
3636
'name: default'
37-
>>> substitute_env_vars('price: $$100')
37+
>>> substitute_env_vars("price: $$100")
3838
'price: $100'
3939
"""
4040
# Pattern matches $$ (escape sequence) or ${VAR_NAME} / ${VAR_NAME:-default_value}

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,23 @@
2525
2626
trace.set_tracer_provider(
2727
TracerProvider(
28-
resource=Resource.create({
29-
"service.name": "shoppingcart",
30-
"service.instance.id": "instance-12",
31-
}),
28+
resource=Resource.create(
29+
{
30+
"service.name": "shoppingcart",
31+
"service.instance.id": "instance-12",
32+
}
33+
),
3234
),
3335
)
3436
print(trace.get_tracer_provider().resource.attributes)
3537
36-
{'telemetry.sdk.language': 'python',
37-
'telemetry.sdk.name': 'opentelemetry',
38-
'telemetry.sdk.version': '0.13.dev0',
39-
'service.name': 'shoppingcart',
40-
'service.instance.id': 'instance-12'}
38+
{
39+
"telemetry.sdk.language": "python",
40+
"telemetry.sdk.name": "opentelemetry",
41+
"telemetry.sdk.version": "0.13.dev0",
42+
"service.name": "shoppingcart",
43+
"service.instance.id": "instance-12",
44+
}
4145
4246
Note that the OpenTelemetry project documents certain `"standard attributes"
4347
<https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/README.md>`_

0 commit comments

Comments
 (0)