-
Notifications
You must be signed in to change notification settings - Fork 19
Add support for crewAI's serper tool #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/langtrace_python_sdk/instrumentation/crewai_tools/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from .instrumentation import CrewaiToolsInstrumentation | ||
|
|
||
| __all__ = ["CrewaiToolsInstrumentation"] |
49 changes: 49 additions & 0 deletions
49
src/langtrace_python_sdk/instrumentation/crewai_tools/instrumentation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """ | ||
| Copyright (c) 2025 Scale3 Labs | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| """ | ||
|
|
||
| from typing import Collection | ||
|
|
||
| from importlib_metadata import version as v | ||
| from opentelemetry.instrumentation.instrumentor import BaseInstrumentor | ||
| from opentelemetry.trace import get_tracer | ||
| from wrapt import wrap_function_wrapper as _W | ||
|
|
||
| from .patch import patch_run | ||
|
|
||
|
|
||
| class CrewaiToolsInstrumentation(BaseInstrumentor): | ||
| """ | ||
| The CrewAIInstrumentation class represents the CrewAI instrumentation""" | ||
|
|
||
| def instrumentation_dependencies(self) -> Collection[str]: | ||
| return ["crewai-tools >= 0.32.0"] | ||
|
|
||
| def _instrument(self, **kwargs): | ||
| tracer_provider = kwargs.get("tracer_provider") | ||
| tracer = get_tracer(__name__, "", tracer_provider) | ||
| version = v("crewai-tools") | ||
| try: | ||
| _W( | ||
| "crewai_tools.tools.serper_dev_tool.serper_dev_tool", | ||
| "SerperDevTool._run", | ||
| patch_run("SerperDevTool._run", version, tracer), | ||
| ) | ||
| # pylint: disable=broad-except | ||
| except Exception: | ||
| pass | ||
|
|
||
| def _uninstrument(self, **kwargs): | ||
| pass |
64 changes: 64 additions & 0 deletions
64
src/langtrace_python_sdk/instrumentation/crewai_tools/patch.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,64 @@ | ||||||||||
| import json | ||||||||||
|
|
||||||||||
| from importlib_metadata import version as v | ||||||||||
| from langtrace.trace_attributes import FrameworkSpanAttributes | ||||||||||
| from opentelemetry import baggage | ||||||||||
| from opentelemetry.trace import SpanKind, Tracer | ||||||||||
| from opentelemetry.trace.status import Status, StatusCode | ||||||||||
|
|
||||||||||
| from langtrace_python_sdk.constants import LANGTRACE_SDK_NAME | ||||||||||
| from langtrace_python_sdk.constants.instrumentation.common import ( | ||||||||||
| LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY, SERVICE_PROVIDERS) | ||||||||||
| from langtrace_python_sdk.utils import set_span_attribute | ||||||||||
| from langtrace_python_sdk.utils.llm import get_span_name, set_span_attributes | ||||||||||
| from langtrace_python_sdk.utils.misc import serialize_args, serialize_kwargs | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def patch_run(operation_name, version, tracer: Tracer): | ||||||||||
| def traced_method(wrapped, instance, args, kwargs): | ||||||||||
| service_provider = SERVICE_PROVIDERS["CREWAI"] | ||||||||||
| extra_attributes = baggage.get_baggage(LANGTRACE_ADDITIONAL_SPAN_ATTRIBUTES_KEY) | ||||||||||
| span_attributes = { | ||||||||||
| "langtrace.sdk.name": "langtrace-python-sdk", | ||||||||||
| "langtrace.service.name": service_provider, | ||||||||||
| "langtrace.service.type": "framework", | ||||||||||
| "langtrace.service.version": version, | ||||||||||
| "langtrace.version": v(LANGTRACE_SDK_NAME), | ||||||||||
| **(extra_attributes if extra_attributes is not None else {}), | ||||||||||
| } | ||||||||||
|
|
||||||||||
| inputs = {} | ||||||||||
| if len(args) > 0: | ||||||||||
| inputs["args"] = serialize_args(*args) | ||||||||||
| if len(kwargs) > 0: | ||||||||||
| inputs["kwargs"] = serialize_kwargs(**kwargs) | ||||||||||
| span_attributes["crewai_tools.tools.serper_dev_tool.inputs"] = json.dumps(inputs) | ||||||||||
|
|
||||||||||
| attributes = FrameworkSpanAttributes(**span_attributes) | ||||||||||
|
|
||||||||||
| with tracer.start_as_current_span( | ||||||||||
| get_span_name(operation_name), kind=SpanKind.CLIENT | ||||||||||
| ) as span: | ||||||||||
|
|
||||||||||
| try: | ||||||||||
| set_span_attributes(span, attributes) | ||||||||||
| result = wrapped(*args, **kwargs) | ||||||||||
| if result is not None and len(result) > 0: | ||||||||||
| set_span_attribute( | ||||||||||
| span, "crewai_tools.tools.serper_dev_tool.outputs", str(result) | ||||||||||
| ) | ||||||||||
| if result: | ||||||||||
| span.set_status(Status(StatusCode.OK)) | ||||||||||
| return result | ||||||||||
|
|
||||||||||
| except Exception as err: | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
for future ref, should use helper method |
||||||||||
| # Record the exception in the span | ||||||||||
| span.record_exception(err) | ||||||||||
|
|
||||||||||
| # Set the span status to indicate an error | ||||||||||
| span.set_status(Status(StatusCode.ERROR, str(err))) | ||||||||||
|
|
||||||||||
| # Reraise the exception to ensure it's not swallowed | ||||||||||
| raise | ||||||||||
|
|
||||||||||
| return traced_method | ||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| __version__ = "3.3.31" | ||
| __version__ = "3.4.0" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for future reference should use helper methods from
llm.pyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops just saw this. will update. thanks