Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
axiomofjoy committed Jan 19, 2025
1 parent 177b7d6 commit 9eacb7e
Show file tree
Hide file tree
Showing 6 changed files with 1,033 additions and 35 deletions.
3 changes: 1 addition & 2 deletions python/openinference-instrumentation/examples/tracer.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter\n",
"from opentelemetry.sdk.resources import Resource\n",
"from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor\n",
"from opentelemetry.trace import Status, StatusCode, set_tracer_provider\n",
"from opentelemetry.trace import Status, StatusCode\n",
"\n",
"from openinference.instrumentation import (\n",
" TracerProvider,\n",
Expand All @@ -40,7 +40,6 @@
"tracer_provider = TracerProvider(resource=resource)\n",
"tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))\n",
"tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))\n",
"set_tracer_provider(tracer_provider)\n",
"tracer = tracer_provider.get_tracer(__name__)"
]
},
Expand Down
2 changes: 2 additions & 0 deletions python/openinference-instrumentation/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ dependencies = [

[project.optional-dependencies]
test = [
"pytest-asyncio",
"pydantic>=2.0.0",
]

[project.urls]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
Status,
StatusCode,
Tracer,
get_tracer,
use_span,
)
from opentelemetry.trace import get_current_span as otel_get_current_span
Expand Down Expand Up @@ -924,17 +923,18 @@ def sync_wrapper(
bound_args = inspect.signature(wrapped).bind(*args, **kwargs)
bound_args.apply_defaults()
arguments = bound_args.arguments

if len(arguments) == 1:
argument = next(iter(arguments.values()))
input_attributes = get_input_value_and_mime_type(value=argument)
else:
input_attributes = get_input_value_and_mime_type(value=arguments)
tool_parameters = safe_json_dumps_io_value(arguments)
input_attributes = get_input_value_and_mime_type(value=arguments)
tool_description: Optional[str] = description
if (
not tool_description
and (docstring := wrapped.__doc__) is not None
and (stripped_docstring := docstring.strip())
):
tool_description = stripped_docstring
tool_attributes = get_tool_attributes(
name=name or wrapped.__name__,
description=description or wrapped.__doc__,
parameters=tool_parameters,
description=tool_description,
parameters={},
)
with tracer.start_as_current_span(
span_name,
Expand Down Expand Up @@ -966,20 +966,18 @@ async def async_wrapper(
bound_args = inspect.signature(wrapped).bind(*args, **kwargs)
bound_args.apply_defaults()
arguments = bound_args.arguments

if len(arguments) == 1:
argument = next(iter(arguments.values()))
input_attributes = get_input_value_and_mime_type(value=argument)
else:
input_attributes = get_input_value_and_mime_type(value=arguments)
tool_parameters = safe_json_dumps_io_value(arguments)
tool_description: Optional[str] = None
if (docstring := wrapped.__doc__) is not None:
tool_description = docstring.strip()
input_attributes = get_input_value_and_mime_type(value=arguments)
tool_description: Optional[str] = description
if (
not tool_description
and (docstring := wrapped.__doc__) is not None
and (stripped_docstring := docstring.strip())
):
tool_description = stripped_docstring
tool_attributes = get_tool_attributes(
name=name or wrapped.__name__,
description=tool_description,
parameters=tool_parameters,
parameters={},
)
with tracer.start_as_current_span(
span_name,
Expand Down
21 changes: 17 additions & 4 deletions python/openinference-instrumentation/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.trace import TracerProvider

from openinference.instrumentation import OITracer, TraceConfig, TracerProvider


@pytest.fixture
Expand All @@ -11,7 +11,20 @@ def in_memory_span_exporter() -> InMemorySpanExporter:


@pytest.fixture
def tracer_provider(in_memory_span_exporter: InMemorySpanExporter) -> TracerProvider:
tracer_provider = trace_sdk.TracerProvider()
def trace_config() -> TraceConfig:
return TraceConfig()


@pytest.fixture
def tracer_provider(
in_memory_span_exporter: InMemorySpanExporter,
trace_config: TraceConfig,
) -> TracerProvider:
tracer_provider = TracerProvider(config=trace_config)
tracer_provider.add_span_processor(SimpleSpanProcessor(in_memory_span_exporter))
return tracer_provider


@pytest.fixture
def tracer(tracer_provider: TracerProvider) -> OITracer:
return tracer_provider.get_tracer(__name__)
17 changes: 9 additions & 8 deletions python/openinference-instrumentation/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,17 @@ def test_settings_from_env_vars_and_code(
hide_input_text: bool,
hide_output_text: bool,
base64_image_max_length: int,
monkeypatch: pytest.MonkeyPatch,
) -> None:
# First part of the test verifies that environment variables are read correctly
os.environ[OPENINFERENCE_HIDE_INPUTS] = str(hide_inputs)
os.environ[OPENINFERENCE_HIDE_OUTPUTS] = str(hide_outputs)
os.environ[OPENINFERENCE_HIDE_INPUT_MESSAGES] = str(hide_input_messages)
os.environ[OPENINFERENCE_HIDE_OUTPUT_MESSAGES] = str(hide_output_messages)
os.environ[OPENINFERENCE_HIDE_INPUT_IMAGES] = str(hide_input_images)
os.environ[OPENINFERENCE_HIDE_INPUT_TEXT] = str(hide_input_text)
os.environ[OPENINFERENCE_HIDE_OUTPUT_TEXT] = str(hide_output_text)
os.environ[OPENINFERENCE_BASE64_IMAGE_MAX_LENGTH] = str(base64_image_max_length)
monkeypatch.setenv(OPENINFERENCE_HIDE_INPUTS, str(hide_inputs))
monkeypatch.setenv(OPENINFERENCE_HIDE_OUTPUTS, str(hide_outputs))
monkeypatch.setenv(OPENINFERENCE_HIDE_INPUT_MESSAGES, str(hide_input_messages))
monkeypatch.setenv(OPENINFERENCE_HIDE_OUTPUT_MESSAGES, str(hide_output_messages))
monkeypatch.setenv(OPENINFERENCE_HIDE_INPUT_IMAGES, str(hide_input_images))
monkeypatch.setenv(OPENINFERENCE_HIDE_INPUT_TEXT, str(hide_input_text))
monkeypatch.setenv(OPENINFERENCE_HIDE_OUTPUT_TEXT, str(hide_output_text))
monkeypatch.setenv(OPENINFERENCE_BASE64_IMAGE_MAX_LENGTH, str(base64_image_max_length))

config = TraceConfig()
assert config.hide_inputs is parse_bool_from_env(OPENINFERENCE_HIDE_INPUTS)
Expand Down
Loading

0 comments on commit 9eacb7e

Please sign in to comment.