Skip to content

Commit 00183a6

Browse files
authored
Update ruff configuration (elastic#39)
Centralize it and add more checks to match opentelemetry upstream one.
1 parent b00139d commit 00183a6

File tree

10 files changed

+44
-32
lines changed

10 files changed

+44
-32
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.6.5
3+
rev: v0.8.1
44
hooks:
55
- id: ruff
66
- id: ruff-format

instrumentation/elastic-opentelemetry-instrumentation-openai/pyproject.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,3 @@ where = ["src"]
6565

6666
[tool.setuptools.dynamic]
6767
version = {attr = "opentelemetry.instrumentation.openai.version.__version__"}
68-
69-
[tool.ruff]
70-
target-version = "py38"
71-
line-length = 120
72-
73-
[tool.ruff.lint.isort]
74-
known-third-party = [
75-
"opentelemetry",
76-
]

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/__init__.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,37 @@
1919
from timeit import default_timer
2020
from typing import Collection
2121

22-
from wrapt import register_post_import_hook, wrap_function_wrapper
23-
2422
from opentelemetry._events import get_event_logger
2523
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
26-
from opentelemetry.instrumentation.utils import unwrap
2724
from opentelemetry.instrumentation.openai.environment_variables import (
2825
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT,
2926
)
3027
from opentelemetry.instrumentation.openai.helpers import (
3128
_get_embeddings_span_attributes_from_wrapper,
3229
_get_event_attributes,
3330
_get_span_attributes_from_wrapper,
34-
_record_token_usage_metrics,
3531
_record_operation_duration_metric,
36-
_send_log_events_from_messages,
32+
_record_token_usage_metrics,
3733
_send_log_events_from_choices,
38-
_set_span_attributes_from_response,
34+
_send_log_events_from_messages,
3935
_set_embeddings_span_attributes_from_response,
36+
_set_span_attributes_from_response,
4037
_span_name_from_span_attributes,
4138
)
4239
from opentelemetry.instrumentation.openai.package import _instruments
4340
from opentelemetry.instrumentation.openai.version import __version__
4441
from opentelemetry.instrumentation.openai.wrappers import StreamWrapper
42+
from opentelemetry.instrumentation.utils import unwrap
4543
from opentelemetry.metrics import get_meter
4644
from opentelemetry.semconv._incubating.metrics.gen_ai_metrics import (
47-
create_gen_ai_client_token_usage,
4845
create_gen_ai_client_operation_duration,
46+
create_gen_ai_client_token_usage,
4947
)
50-
5148
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
5249
from opentelemetry.semconv.schemas import Schemas
5350
from opentelemetry.trace import SpanKind, get_tracer
5451
from opentelemetry.trace.status import StatusCode
52+
from wrapt import register_post_import_hook, wrap_function_wrapper
5553

5654
EVENT_GEN_AI_CONTENT_PROMPT = "gen_ai.content.prompt"
5755
EVENT_GEN_AI_CONTENT_COMPLETION = "gen_ai.content.completion"

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/helpers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
from typing import TYPE_CHECKING
2020

2121
from opentelemetry._events import Event, EventLogger
22-
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
23-
from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT
2422
from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import (
2523
GEN_AI_OPERATION_NAME,
2624
GEN_AI_REQUEST_FREQUENCY_PENALTY,
@@ -30,14 +28,16 @@
3028
GEN_AI_REQUEST_STOP_SEQUENCES,
3129
GEN_AI_REQUEST_TEMPERATURE,
3230
GEN_AI_REQUEST_TOP_P,
33-
GEN_AI_RESPONSE_ID,
3431
GEN_AI_RESPONSE_FINISH_REASONS,
32+
GEN_AI_RESPONSE_ID,
3533
GEN_AI_RESPONSE_MODEL,
3634
GEN_AI_SYSTEM,
3735
GEN_AI_TOKEN_TYPE,
3836
GEN_AI_USAGE_INPUT_TOKENS,
3937
GEN_AI_USAGE_OUTPUT_TOKENS,
4038
)
39+
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
40+
from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT
4141

4242
try:
4343
from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_REQUEST_ENCODING_FORMATS

instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/wrappers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919
from opentelemetry._events import EventLogger
2020
from opentelemetry.instrumentation.openai.helpers import (
21-
_record_token_usage_metrics,
2221
_record_operation_duration_metric,
23-
_set_span_attributes_from_response,
22+
_record_token_usage_metrics,
2423
_send_log_events_from_stream_choices,
24+
_set_span_attributes_from_response,
2525
)
2626
from opentelemetry.metrics import Histogram
2727
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE

instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
# limitations under the License.
1616

1717
import json
18-
import re
1918
import os
19+
import re
2020
from typing import Sequence, Union
2121
from urllib.parse import parse_qs, urlparse
2222

2323
import openai
2424
import pytest
2525
import yaml
2626
from opentelemetry import metrics, trace
27-
from opentelemetry._logs import set_logger_provider
2827
from opentelemetry._events import set_event_logger_provider
28+
from opentelemetry._logs import set_logger_provider
2929
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
3030
from opentelemetry.metrics import Histogram
3131
from opentelemetry.sdk._events import EventLoggerProvider

instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from opentelemetry._events import Event
2626
from opentelemetry._logs import LogRecord
2727
from opentelemetry.instrumentation.openai import OpenAIInstrumentor
28-
from opentelemetry.trace import SpanKind, StatusCode
2928
from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import (
3029
GEN_AI_OPERATION_NAME,
3130
GEN_AI_REQUEST_FREQUENCY_PENALTY,
@@ -35,15 +34,16 @@
3534
GEN_AI_REQUEST_STOP_SEQUENCES,
3635
GEN_AI_REQUEST_TEMPERATURE,
3736
GEN_AI_REQUEST_TOP_P,
38-
GEN_AI_SYSTEM,
37+
GEN_AI_RESPONSE_FINISH_REASONS,
3938
GEN_AI_RESPONSE_ID,
4039
GEN_AI_RESPONSE_MODEL,
41-
GEN_AI_RESPONSE_FINISH_REASONS,
40+
GEN_AI_SYSTEM,
4241
GEN_AI_USAGE_INPUT_TOKENS,
4342
GEN_AI_USAGE_OUTPUT_TOKENS,
4443
)
4544
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
4645
from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT
46+
from opentelemetry.trace import SpanKind, StatusCode
4747

4848
from .conftest import (
4949
assert_error_operation_duration_metric,

instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_embeddings.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@
1919
import openai
2020
import pytest
2121
from opentelemetry.instrumentation.openai.helpers import GEN_AI_REQUEST_ENCODING_FORMATS
22-
from opentelemetry.trace import SpanKind, StatusCode
2322
from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import (
2423
GEN_AI_OPERATION_NAME,
2524
GEN_AI_REQUEST_MODEL,
26-
GEN_AI_SYSTEM,
2725
GEN_AI_RESPONSE_MODEL,
26+
GEN_AI_SYSTEM,
2827
GEN_AI_USAGE_INPUT_TOKENS,
2928
)
3029
from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
3130
from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT
31+
from opentelemetry.trace import SpanKind, StatusCode
3232

3333
from .conftest import (
3434
assert_error_operation_duration_metric,
@@ -37,7 +37,6 @@
3737
)
3838
from .utils import MOCK_POSITIVE_FLOAT, get_sorted_metrics
3939

40-
4140
test_basic_test_data = [
4241
("openai_provider_embeddings", "text-embedding-3-small", 4, 0.2263190783560276),
4342
("azure_provider_embeddings", "ada", 4, 0.0017870571464300156),

instrumentation/elastic-opentelemetry-instrumentation-openai/tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
from opentelemetry.sdk._logs._internal import LogData
1919
from opentelemetry.sdk.metrics._internal.point import Metric
2020
from opentelemetry.sdk.metrics.export import (
21-
InMemoryMetricReader,
2221
DataPointT,
2322
HistogramDataPoint,
23+
InMemoryMetricReader,
2424
NumberDataPoint,
2525
)
2626
from opentelemetry.util.types import AttributeValue

pyproject.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[tool.ruff]
2+
target-version = "py38"
3+
line-length = 120
4+
5+
[lint.isort]
6+
known-first-party = [
7+
"opentelemetry",
8+
]
9+
10+
[tool.ruff.lint]
11+
# https://docs.astral.sh/ruff/linter/#rule-selection
12+
select = [
13+
"I", # isort
14+
"F", # pyflakes
15+
"E", # pycodestyle errors
16+
"W", # pycodestyle warnings
17+
"PLC", # pylint convention
18+
"PLE", # pylint error
19+
"Q", # flake8-quotes
20+
"A", # flake8-builtins
21+
]
22+
ignore = [
23+
"E501", # line-too-long
24+
]

0 commit comments

Comments
 (0)