Skip to content

Commit

Permalink
Merge branch 'main' into db-statement-with-sqlcomment
Browse files Browse the repository at this point in the history
  • Loading branch information
lzchen authored Nov 19, 2024
2 parents 9205b23 + a29ad8a commit 44ba166
Show file tree
Hide file tree
Showing 42 changed files with 3,602 additions and 399 deletions.
1 change: 1 addition & 0 deletions .github/component_owners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ components:
- lzchen
- gyliu513
- nirga
- alizenhom
- codefromthecrypt
11 changes: 11 additions & 0 deletions .github/scripts/update-version-patch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash -e

sed -i "/\[stable\]/{n;s/version=.*/version=$1/}" eachdist.ini
sed -i "/\[prerelease\]/{n;s/version=.*/version=$2/}" eachdist.ini

./scripts/eachdist.py update_patch_versions \
--stable_version=$1 \
--unstable_version=$2 \
--stable_version_prev=$3 \
--unstable_version_prev=$4

6 changes: 5 additions & 1 deletion .github/workflows/prepare-patch-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ jobs:
exit 1
fi
stable_version_prev="$stable_major_minor.$((stable_patch))"
unstable_version_prev="0.${unstable_minor}b$((unstable_patch))"
stable_version="$stable_major_minor.$((stable_patch + 1))"
unstable_version="0.${unstable_minor}b$((unstable_patch + 1))"
echo "STABLE_VERSION=$stable_version" >> $GITHUB_ENV
echo "UNSTABLE_VERSION=$unstable_version" >> $GITHUB_ENV
echo "STABLE_VERSION_PREV=$stable_version_prev" >> $GITHUB_ENV
echo "UNSTABLE_VERSION_PREV=$unstable_version_prev" >> $GITHUB_ENV
- name: Update version
run: .github/scripts/update-version.sh $STABLE_VERSION $UNSTABLE_VERSION
run: .github/scripts/update-version-patch.sh $STABLE_VERSION $UNSTABLE_VERSION $STABLE_VERSION_PREV $UNSTABLE_VERSION_PREV

- name: Set up Python 3.9
uses: actions/setup-python@v5
Expand Down
33 changes: 0 additions & 33 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,36 +118,3 @@ jobs:
# the step below is creating a pull request against main
ref: main

- name: Copy change log updates to main
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo "VERSION=${STABLE_VERSION}\/${UNSTABLE_VERSION}" >> $GITHUB_ENV
echo "RELEASE_TAG=$STABLE_VERSION" >> $GITHUB_ENV
./scripts/merge_changelog_to_main.sh
- name: Use CLA approved github bot
run: .github/scripts/use-cla-approved-github-bot.sh

- name: Create pull request against main
env:
# not using secrets.GITHUB_TOKEN since pull requests from that token do not run workflows
GITHUB_TOKEN: ${{ secrets.OPENTELEMETRYBOT_GITHUB_TOKEN }}
run: |
message="Copy change log updates from $GITHUB_REF_NAME"
body="Copy log updates from \`$GITHUB_REF_NAME\`."
branch="opentelemetrybot/copy-change-log-updates-from-${GITHUB_REF_NAME//\//-}"
if [[ -z $PRIOR_VERSION_WHEN_PATCH ]]; then
if git diff --quiet; then
echo there are no updates needed to the change log on main, not creating pull request
exit 0 # success
fi
fi
git commit -a -m "$message"
git push origin HEAD:$branch
gh pr create --title "$message" \
--body "$body" \
--head $branch \
--base main
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `opentelemetry-instrumentation-sqlalchemy` Update unit tests to run with SQLALchemy 2
([#2976](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2976))
- Add `opentelemetry-instrumentation-openai-v2` to `opentelemetry-bootstrap`
([#2996](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2996))

### Fixed

- `opentelemetry-instrumentation-httpx`: instrument_client is a static method again
([#3003](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3003))

### Breaking changes

- `opentelemetry-instrumentation-sqlalchemy` teach instruments version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Support for `AsyncOpenAI/AsyncCompletions` ([#2984](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2984))

## Version 2.0b0 (2024-11-08)

- Use generic `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` environment variable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [

[project.optional-dependencies]
instruments = [
"openai >= 1.0.0",
"openai >= 1.26.0",
]

[project.entry-points.opentelemetry_instrumentor]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from opentelemetry.semconv.schemas import Schemas
from opentelemetry.trace import get_tracer

from .patch import chat_completions_create
from .patch import async_chat_completions_create, chat_completions_create


class OpenAIInstrumentor(BaseInstrumentor):
Expand Down Expand Up @@ -84,7 +84,16 @@ def _instrument(self, **kwargs):
),
)

wrap_function_wrapper(
module="openai.resources.chat.completions",
name="AsyncCompletions.create",
wrapper=async_chat_completions_create(
tracer, event_logger, is_content_enabled()
),
)

def _uninstrument(self, **kwargs):
import openai # pylint: disable=import-outside-toplevel

unwrap(openai.resources.chat.completions.Completions, "create")
unwrap(openai.resources.chat.completions.AsyncCompletions, "create")
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
from opentelemetry.semconv._incubating.attributes import (
gen_ai_attributes as GenAIAttributes,
)
from opentelemetry.semconv.attributes import (
error_attributes as ErrorAttributes,
)
from opentelemetry.trace import Span, SpanKind, Tracer
from opentelemetry.trace.status import Status, StatusCode

from .utils import (
choice_to_event,
get_llm_request_attributes,
handle_span_exception,
is_streaming,
message_to_event,
set_span_attribute,
Expand Down Expand Up @@ -72,12 +69,49 @@ def traced_method(wrapped, instance, args, kwargs):
return result

except Exception as error:
span.set_status(Status(StatusCode.ERROR, str(error)))
handle_span_exception(span, error)
raise

return traced_method


def async_chat_completions_create(
tracer: Tracer, event_logger: EventLogger, capture_content: bool
):
"""Wrap the `create` method of the `AsyncChatCompletion` class to trace it."""

async def traced_method(wrapped, instance, args, kwargs):
span_attributes = {**get_llm_request_attributes(kwargs, instance)}

span_name = f"{span_attributes[GenAIAttributes.GEN_AI_OPERATION_NAME]} {span_attributes[GenAIAttributes.GEN_AI_REQUEST_MODEL]}"
with tracer.start_as_current_span(
name=span_name,
kind=SpanKind.CLIENT,
attributes=span_attributes,
end_on_exit=False,
) as span:
if span.is_recording():
for message in kwargs.get("messages", []):
event_logger.emit(
message_to_event(message, capture_content)
)

try:
result = await wrapped(*args, **kwargs)
if is_streaming(kwargs):
return StreamWrapper(
result, span, event_logger, capture_content
)

if span.is_recording():
span.set_attribute(
ErrorAttributes.ERROR_TYPE, type(error).__qualname__
_set_response_attributes(
span, result, event_logger, capture_content
)
span.end()
return result

except Exception as error:
handle_span_exception(span, error)
raise

return traced_method
Expand Down Expand Up @@ -286,10 +320,19 @@ def __enter__(self):
def __exit__(self, exc_type, exc_val, exc_tb):
try:
if exc_type is not None:
self.span.set_status(Status(StatusCode.ERROR, str(exc_val)))
self.span.set_attribute(
ErrorAttributes.ERROR_TYPE, exc_type.__qualname__
)
handle_span_exception(self.span, exc_val)
finally:
self.cleanup()
return False # Propagate the exception

async def __aenter__(self):
self.setup()
return self

async def __aexit__(self, exc_type, exc_val, exc_tb):
try:
if exc_type is not None:
handle_span_exception(self.span, exc_val)
finally:
self.cleanup()
return False # Propagate the exception
Expand All @@ -301,6 +344,9 @@ def close(self):
def __iter__(self):
return self

def __aiter__(self):
return self

def __next__(self):
try:
chunk = next(self.stream)
Expand All @@ -310,10 +356,20 @@ def __next__(self):
self.cleanup()
raise
except Exception as error:
self.span.set_status(Status(StatusCode.ERROR, str(error)))
self.span.set_attribute(
ErrorAttributes.ERROR_TYPE, type(error).__qualname__
)
handle_span_exception(self.span, error)
self.cleanup()
raise

async def __anext__(self):
try:
chunk = await self.stream.__anext__()
self.process_chunk(chunk)
return chunk
except StopAsyncIteration:
self.cleanup()
raise
except Exception as error:
handle_span_exception(self.span, error)
self.cleanup()
raise

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
from opentelemetry.semconv._incubating.attributes import (
server_attributes as ServerAttributes,
)
from opentelemetry.semconv.attributes import (
error_attributes as ErrorAttributes,
)
from opentelemetry.trace.status import Status, StatusCode

OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = (
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"
Expand Down Expand Up @@ -138,9 +142,11 @@ def choice_to_event(choice, capture_content):

if choice.message:
message = {
"role": choice.message.role
if choice.message and choice.message.role
else None
"role": (
choice.message.role
if choice.message and choice.message.role
else None
)
}
tool_calls = extract_tool_calls(choice.message, capture_content)
if tool_calls:
Expand Down Expand Up @@ -210,3 +216,12 @@ def get_llm_request_attributes(

# filter out None values
return {k: v for k, v in attributes.items() if v is not None}


def handle_span_exception(span, error):
span.set_status(Status(StatusCode.ERROR, str(error)))
if span.is_recording():
span.set_attribute(
ErrorAttributes.ERROR_TYPE, type(error).__qualname__
)
span.end()
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ importlib-metadata==6.11.0
packaging==24.0
pytest==7.4.4
pytest-vcr==1.0.2
pytest-asyncio==0.21.0
wrapt==1.16.0
opentelemetry-api==1.28 # when updating, also update in pyproject.toml
opentelemetry-sdk==1.28 # when updating, also update in pyproject.toml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ importlib-metadata==6.11.0
packaging==24.0
pytest==7.4.4
pytest-vcr==1.0.2
pytest-asyncio==0.21.0
wrapt==1.16.0
# test with the latest version of opentelemetry-api, sdk, and semantic conventions

Expand Down
Loading

0 comments on commit 44ba166

Please sign in to comment.