-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat: add basic opentelemetry tracing support #75
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ae3352
feat: add basic opentelemetry tracing support
codefromthecrypt d7596b0
Update tests/cli/test_session_otel.py
codefromthecrypt 5101754
feedback
codefromthecrypt 081b9df
double-end
codefromthecrypt 0796b07
stale dep
codefromthecrypt 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
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
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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import pytest | ||
| from httpx import HTTPStatusError | ||
| from unittest.mock import MagicMock, patch | ||
| from exchange import Message, Text | ||
| from goose.cli.session import Session | ||
| from goose.cli.prompt.goose_prompt_session import GoosePromptSession | ||
| from goose.cli.prompt.user_input import PromptAction, UserInput | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
| from opentelemetry.sdk.trace.export import SimpleSpanProcessor | ||
| from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def memory_exporter(): | ||
| yield InMemorySpanExporter() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def create_session_with_mock_configs(mock_sessions_path, exchange_factory, profile_factory, memory_exporter): | ||
| # Create a tracer with the same memory exporter as test assertions use. We do this for each | ||
| # test to ensure they can run in parallel without interfering with each other. | ||
| tracer_provider = TracerProvider() | ||
| memory_exporter = memory_exporter | ||
| span_processor = SimpleSpanProcessor(memory_exporter) | ||
| tracer_provider.add_span_processor(span_processor) | ||
|
|
||
| with patch("goose.cli.session.build_exchange", return_value=exchange_factory()), patch( | ||
| "goose.cli.session.load_profile", return_value=profile_factory() | ||
| ), patch("goose.cli.session.SessionNotifier") as mock_session_notifier, patch( | ||
| "goose.cli.session.load_provider", return_value="provider" | ||
| ): | ||
| mock_session_notifier.return_value = MagicMock() | ||
|
|
||
| def create_session(session_attributes: dict = {}): | ||
| return Session(**session_attributes, tracer=tracer_provider.get_tracer(__name__)) | ||
|
|
||
| yield create_session | ||
|
|
||
|
|
||
| def test_trace_run(create_session_with_mock_configs, memory_exporter): | ||
| session = create_session_with_mock_configs() | ||
|
|
||
| message = Message(role="user", id="abracadabra", content=[Text("List the files in this directory")]) | ||
|
|
||
| # Call the run function, for a single message which results in an exit. | ||
| with patch.object(Session, "process_first_message", return_value=message), patch.object( | ||
| Session, "reply", return_value=None | ||
| ), patch.object( | ||
| GoosePromptSession, "get_user_input", return_value=UserInput(action=PromptAction.EXIT) | ||
| ), patch.object(Session, "save_session", return_value=None): | ||
| session.run() | ||
|
|
||
| spans = memory_exporter.get_finished_spans() | ||
| assert len(spans) == 1 | ||
| span = spans[0] | ||
| assert span.name == message.role | ||
| assert span.attributes == {"goose.id": message.id, "goose.role": message.role, "goose.text": message.text} | ||
|
|
||
|
|
||
| def test_trace_run_interrupted(create_session_with_mock_configs, memory_exporter): | ||
| session = create_session_with_mock_configs() | ||
|
|
||
| message = Message(role="user", id="abracadabra", content=[Text("List the files in this directory")]) | ||
|
|
||
| # Call the run function, for a single message which results in an interrupt. | ||
| with patch.object(Session, "process_first_message", return_value=message), patch.object( | ||
| Session, "reply", side_effect=KeyboardInterrupt | ||
| ), patch.object( | ||
| GoosePromptSession, "get_user_input", return_value=UserInput(action=PromptAction.EXIT) | ||
| ), patch.object(Session, "save_session", return_value=None): | ||
| session.run() | ||
|
|
||
| spans = memory_exporter.get_finished_spans() | ||
| assert len(spans) == 1 | ||
| span = spans[0] | ||
| assert span.name == message.role | ||
| assert span.attributes == {"goose.id": message.id, "goose.role": message.role, "goose.text": message.text} | ||
| assert span.status.is_ok is False | ||
| assert span.status.description == "KeyboardInterrupt" | ||
|
|
||
|
|
||
| def test_trace_run_error(create_session_with_mock_configs, memory_exporter): | ||
| session = create_session_with_mock_configs() | ||
|
|
||
| message = Message(role="user", id="abracadabra", content=[Text("List the files in this directory")]) | ||
|
|
||
| # Call the run function, for a single message which results in an HTTP error. | ||
| with patch.object(Session, "process_first_message", return_value=message), patch.object( | ||
| Session, "reply", side_effect=HTTPStatusError("HTTP error", request=None, response=None) | ||
| ), patch.object( | ||
| GoosePromptSession, "get_user_input", return_value=UserInput(action=PromptAction.EXIT) | ||
| ), patch.object(Session, "save_session", return_value=None): | ||
| session.run() | ||
|
|
||
| spans = memory_exporter.get_finished_spans() | ||
| assert len(spans) == 1 | ||
| span = spans[0] | ||
| assert span.name == message.role | ||
| assert span.attributes == {"goose.id": message.id, "goose.role": message.role, "goose.text": message.text} | ||
| assert span.status.is_ok is False | ||
| assert span.status.description == "HTTP error" |
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.
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.
This is an interesting case. I don't know if an explicit interrupt would actually be an error for the user, it's expected to fail - a browser comparison would be closing a tab during a request, it would be common for the request to still complete on the server side and be green. Anyways, it's probably correct to set it as an error though not fully confident
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.
added a comment as I don't know yet, either, but maybe after some practice someone will.
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.
hey idea.. maybe we set it to ok, but leave the interrupt?