diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ae8cde7..0b725b4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.5 + rev: v0.8.1 hooks: - id: ruff - id: ruff-format diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/pyproject.toml b/instrumentation/elastic-opentelemetry-instrumentation-openai/pyproject.toml index a601cbf..82a89f0 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/pyproject.toml +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/pyproject.toml @@ -65,12 +65,3 @@ where = ["src"] [tool.setuptools.dynamic] version = {attr = "opentelemetry.instrumentation.openai.version.__version__"} - -[tool.ruff] -target-version = "py38" -line-length = 120 - -[tool.ruff.lint.isort] -known-third-party = [ - "opentelemetry", -] diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/__init__.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/__init__.py index 0f0feb3..9c42de4 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/__init__.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/__init__.py @@ -19,11 +19,8 @@ from timeit import default_timer from typing import Collection -from wrapt import register_post_import_hook, wrap_function_wrapper - from opentelemetry._events import get_event_logger from opentelemetry.instrumentation.instrumentor import BaseInstrumentor -from opentelemetry.instrumentation.utils import unwrap from opentelemetry.instrumentation.openai.environment_variables import ( OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, ) @@ -31,27 +28,28 @@ _get_embeddings_span_attributes_from_wrapper, _get_event_attributes, _get_span_attributes_from_wrapper, - _record_token_usage_metrics, _record_operation_duration_metric, - _send_log_events_from_messages, + _record_token_usage_metrics, _send_log_events_from_choices, - _set_span_attributes_from_response, + _send_log_events_from_messages, _set_embeddings_span_attributes_from_response, + _set_span_attributes_from_response, _span_name_from_span_attributes, ) from opentelemetry.instrumentation.openai.package import _instruments from opentelemetry.instrumentation.openai.version import __version__ from opentelemetry.instrumentation.openai.wrappers import StreamWrapper +from opentelemetry.instrumentation.utils import unwrap from opentelemetry.metrics import get_meter from opentelemetry.semconv._incubating.metrics.gen_ai_metrics import ( - create_gen_ai_client_token_usage, create_gen_ai_client_operation_duration, + create_gen_ai_client_token_usage, ) - from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE from opentelemetry.semconv.schemas import Schemas from opentelemetry.trace import SpanKind, get_tracer from opentelemetry.trace.status import StatusCode +from wrapt import register_post_import_hook, wrap_function_wrapper EVENT_GEN_AI_CONTENT_PROMPT = "gen_ai.content.prompt" EVENT_GEN_AI_CONTENT_COMPLETION = "gen_ai.content.completion" diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/helpers.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/helpers.py index 6aa3f03..f35ea35 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/helpers.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/helpers.py @@ -19,8 +19,6 @@ from typing import TYPE_CHECKING from opentelemetry._events import Event, EventLogger -from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE -from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import ( GEN_AI_OPERATION_NAME, GEN_AI_REQUEST_FREQUENCY_PENALTY, @@ -30,14 +28,16 @@ GEN_AI_REQUEST_STOP_SEQUENCES, GEN_AI_REQUEST_TEMPERATURE, GEN_AI_REQUEST_TOP_P, - GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_FINISH_REASONS, + GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_MODEL, GEN_AI_SYSTEM, GEN_AI_TOKEN_TYPE, GEN_AI_USAGE_INPUT_TOKENS, GEN_AI_USAGE_OUTPUT_TOKENS, ) +from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE +from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT try: from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import GEN_AI_REQUEST_ENCODING_FORMATS diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/wrappers.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/wrappers.py index 7d3d7c5..5ef5e8d 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/wrappers.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/src/opentelemetry/instrumentation/openai/wrappers.py @@ -18,10 +18,10 @@ from opentelemetry._events import EventLogger from opentelemetry.instrumentation.openai.helpers import ( - _record_token_usage_metrics, _record_operation_duration_metric, - _set_span_attributes_from_response, + _record_token_usage_metrics, _send_log_events_from_stream_choices, + _set_span_attributes_from_response, ) from opentelemetry.metrics import Histogram from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py index 6cc4c0f..66f7d7d 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/conftest.py @@ -15,8 +15,8 @@ # limitations under the License. import json -import re import os +import re from typing import Sequence, Union from urllib.parse import parse_qs, urlparse @@ -24,8 +24,8 @@ import pytest import yaml from opentelemetry import metrics, trace -from opentelemetry._logs import set_logger_provider from opentelemetry._events import set_event_logger_provider +from opentelemetry._logs import set_logger_provider from opentelemetry.instrumentation.openai import OpenAIInstrumentor from opentelemetry.metrics import Histogram from opentelemetry.sdk._events import EventLoggerProvider diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py index dac91ea..57883e8 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_chat_completions.py @@ -25,7 +25,6 @@ from opentelemetry._events import Event from opentelemetry._logs import LogRecord from opentelemetry.instrumentation.openai import OpenAIInstrumentor -from opentelemetry.trace import SpanKind, StatusCode from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import ( GEN_AI_OPERATION_NAME, GEN_AI_REQUEST_FREQUENCY_PENALTY, @@ -35,15 +34,16 @@ GEN_AI_REQUEST_STOP_SEQUENCES, GEN_AI_REQUEST_TEMPERATURE, GEN_AI_REQUEST_TOP_P, - GEN_AI_SYSTEM, + GEN_AI_RESPONSE_FINISH_REASONS, GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_MODEL, - GEN_AI_RESPONSE_FINISH_REASONS, + GEN_AI_SYSTEM, GEN_AI_USAGE_INPUT_TOKENS, GEN_AI_USAGE_OUTPUT_TOKENS, ) from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT +from opentelemetry.trace import SpanKind, StatusCode from .conftest import ( assert_error_operation_duration_metric, diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_embeddings.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_embeddings.py index 4c7ae7f..74d21f8 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_embeddings.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/test_embeddings.py @@ -19,16 +19,16 @@ import openai import pytest from opentelemetry.instrumentation.openai.helpers import GEN_AI_REQUEST_ENCODING_FORMATS -from opentelemetry.trace import SpanKind, StatusCode from opentelemetry.semconv._incubating.attributes.gen_ai_attributes import ( GEN_AI_OPERATION_NAME, GEN_AI_REQUEST_MODEL, - GEN_AI_SYSTEM, GEN_AI_RESPONSE_MODEL, + GEN_AI_SYSTEM, GEN_AI_USAGE_INPUT_TOKENS, ) from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE from opentelemetry.semconv.attributes.server_attributes import SERVER_ADDRESS, SERVER_PORT +from opentelemetry.trace import SpanKind, StatusCode from .conftest import ( assert_error_operation_duration_metric, @@ -37,7 +37,6 @@ ) from .utils import MOCK_POSITIVE_FLOAT, get_sorted_metrics - test_basic_test_data = [ ("openai_provider_embeddings", "text-embedding-3-small", 4, 0.2263190783560276), ("azure_provider_embeddings", "ada", 4, 0.0017870571464300156), diff --git a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/utils.py b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/utils.py index 02cf46c..fb5b121 100644 --- a/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/utils.py +++ b/instrumentation/elastic-opentelemetry-instrumentation-openai/tests/utils.py @@ -18,9 +18,9 @@ from opentelemetry.sdk._logs._internal import LogData from opentelemetry.sdk.metrics._internal.point import Metric from opentelemetry.sdk.metrics.export import ( - InMemoryMetricReader, DataPointT, HistogramDataPoint, + InMemoryMetricReader, NumberDataPoint, ) from opentelemetry.util.types import AttributeValue diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..eaa3d7e --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,24 @@ +[tool.ruff] +target-version = "py38" +line-length = 120 + +[lint.isort] +known-first-party = [ + "opentelemetry", +] + +[tool.ruff.lint] +# https://docs.astral.sh/ruff/linter/#rule-selection +select = [ + "I", # isort + "F", # pyflakes + "E", # pycodestyle errors + "W", # pycodestyle warnings + "PLC", # pylint convention + "PLE", # pylint error + "Q", # flake8-quotes + "A", # flake8-builtins +] +ignore = [ + "E501", # line-too-long +]