-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: implement Phase 1 zero-config observability with --observe langfuse flag #1390
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
5 commits
Select commit
Hold shift + click to select a range
1928db8
feat: implement Phase 1 zero-config observability with --observe lang…
praisonai-triage-agent[bot] 53348ae
fix: cache langfuse flow emitter and improve observability diagnostics
Copilot 7de1755
test: add thread-safe observability helper accessors for unit tests
Copilot 19297c0
fix: resolve protocol mismatch and add CLI validation for --observe flag
praisonai-triage-agent[bot] 0085ea5
fix: update langfuse example to use correct TraceEmitter protocol
praisonai-triage-agent[bot] 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,40 @@ | ||
| """ | ||
| Langfuse Integration Example | ||
| Langfuse Integration Example (Updated for TraceSinkProtocol) | ||
|
|
||
| This example shows how to use Langfuse for LLM observability. | ||
| This example shows how to use Langfuse for LLM observability with PraisonAI's native trace infrastructure. | ||
|
|
||
| Setup: | ||
| 1. Sign up at https://langfuse.com/ | ||
| 2. Get your API keys from the project settings | ||
| 3. Set environment variables: | ||
| export LANGFUSE_PUBLIC_KEY=pk-lf-xxx | ||
| export LANGFUSE_SECRET_KEY=sk-lf-xxx | ||
| 4. Install dependencies: | ||
| pip install opentelemetry-sdk opentelemetry-exporter-otlp | ||
| pip install "praisonai[langfuse]" | ||
| export LANGFUSE_PUBLIC_KEY=pk-lf-xxx | ||
| export LANGFUSE_SECRET_KEY=sk-lf-xxx | ||
|
|
||
| Usage: | ||
| python langfuse_example.py | ||
| """ | ||
|
|
||
| import os | ||
| from praisonai_tools.observability import obs | ||
| from praisonaiagents import Agent | ||
|
|
||
| # Initialize Langfuse | ||
| success = obs.init( | ||
| provider="langfuse", | ||
| project_name="praisonai-demo", | ||
| from praisonai.observability import LangfuseSink | ||
| from praisonaiagents.trace.protocol import ( | ||
| TraceEmitter, set_default_emitter | ||
| ) | ||
|
|
||
| if not success: | ||
| print("Failed to initialize Langfuse. Check your API keys.") | ||
| print("Required: LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY") | ||
| exit(1) | ||
|
|
||
| print("Langfuse initialized successfully!") | ||
| print(f"View traces at: https://cloud.langfuse.com/") | ||
| # Initialize Langfuse observability | ||
| sink = LangfuseSink() | ||
| emitter = TraceEmitter(sink=sink, enabled=True) | ||
| set_default_emitter(emitter) | ||
|
|
||
| # Create agent | ||
| # Create and run agent — all traces automatically captured | ||
| agent = Agent( | ||
| name="Coder", | ||
| instructions="You are a helpful coding assistant.", | ||
| model="gpt-4o-mini", | ||
| llm="openai/gpt-4o-mini", | ||
| ) | ||
|
|
||
| # Run with tracing | ||
| with obs.trace("coding-session", user_id="developer-1"): | ||
| response = agent.chat("Write a Python function to calculate fibonacci numbers") | ||
| print(response) | ||
| try: | ||
| result = agent.start("Write a Python function to calculate fibonacci numbers") | ||
| print(result) | ||
| finally: | ||
| # Ensure traces are flushed and resources cleaned up | ||
| sink.flush() | ||
| sink.close() | ||
|
|
||
| print("\nCheck Langfuse dashboard for traces!") |
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,67 @@ | ||
| import sys | ||
| import types | ||
|
|
||
| from praisonai.cli.app import _setup_langfuse_observability | ||
| from praisonai.flow import helpers as flow_helpers | ||
|
|
||
|
|
||
| def test_flow_langfuse_context_observability_reuses_single_emitter(monkeypatch): | ||
| monkeypatch.setenv("PRAISONAI_OBSERVE", "langfuse") | ||
| flow_helpers.reset_langfuse_context_observability_for_tests() | ||
|
|
||
| sink_instances = [] | ||
| set_calls = [] | ||
|
|
||
| class FakeSink: | ||
| def __init__(self): | ||
| sink_instances.append(self) | ||
|
|
||
| class FakeContextTraceEmitter: | ||
| def __init__(self, sink, enabled): | ||
| self.sink = sink | ||
| self.enabled = enabled | ||
|
|
||
| fake_langfuse_module = types.ModuleType("praisonai.observability.langfuse") | ||
| fake_langfuse_module.LangfuseSink = FakeSink | ||
|
|
||
| fake_context_events_module = types.ModuleType("praisonaiagents.trace.context_events") | ||
| fake_context_events_module.ContextTraceEmitter = FakeContextTraceEmitter | ||
| fake_context_events_module.set_context_emitter = set_calls.append | ||
|
|
||
| monkeypatch.setitem(sys.modules, "praisonai.observability.langfuse", fake_langfuse_module) | ||
| monkeypatch.setitem(sys.modules, "praisonaiagents.trace.context_events", fake_context_events_module) | ||
|
|
||
| flow_helpers.setup_langfuse_context_observability() | ||
| flow_helpers.setup_langfuse_context_observability() | ||
|
|
||
| assert len(sink_instances) == 1 | ||
| assert len(set_calls) == 2 | ||
| assert set_calls[0] is set_calls[1] | ||
| assert set_calls[0] is flow_helpers.get_langfuse_context_emitter() | ||
|
|
||
|
|
||
| def test_setup_langfuse_observability_verbose_logs_warning(monkeypatch, capsys): | ||
| class FakeSink: | ||
| def __init__(self): | ||
| raise RuntimeError("boom") | ||
|
|
||
| fake_langfuse_module = types.ModuleType("praisonai.observability.langfuse") | ||
| fake_langfuse_module.LangfuseSink = FakeSink | ||
|
|
||
| fake_protocol_module = types.ModuleType("praisonaiagents.trace.protocol") | ||
| fake_protocol_module.TraceEmitter = object | ||
| fake_protocol_module.set_default_emitter = lambda *_args, **_kwargs: None | ||
|
|
||
| fake_context_events_module = types.ModuleType("praisonaiagents.trace.context_events") | ||
| fake_context_events_module.ContextTraceEmitter = object | ||
| fake_context_events_module.set_context_emitter = lambda *_args, **_kwargs: None | ||
|
|
||
| monkeypatch.setitem(sys.modules, "praisonai.observability.langfuse", fake_langfuse_module) | ||
| monkeypatch.setitem(sys.modules, "praisonaiagents.trace.protocol", fake_protocol_module) | ||
| monkeypatch.setitem(sys.modules, "praisonaiagents.trace.context_events", fake_context_events_module) | ||
|
|
||
| _setup_langfuse_observability(verbose=True) | ||
|
|
||
| captured = capsys.readouterr() | ||
| assert "failed to initialize Langfuse observability" in captured.err | ||
| assert "boom" in captured.err |
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.
A new global field
state.observeis introduced, but the unit-test fixture that resets global CLI state (src/praisonai/tests/unit/cli/test_typer_cli.py) doesn’t reset this field. That can lead to state leaking between CLI tests. Please update the fixture to also restorestate.observeto its default value.