-
Notifications
You must be signed in to change notification settings - Fork 242
feat: export trace to a local file for quick debugging #1542
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
+33
−6
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
|
|
||
| import base64 | ||
| import os | ||
| import json | ||
| from typing import Callable, Dict, List, Optional | ||
|
|
||
| from opentelemetry import context as context_api | ||
|
|
@@ -21,18 +22,32 @@ | |
| from opentelemetry.sdk.trace import ReadableSpan, Span | ||
| from opentelemetry.sdk.trace.export import BatchSpanProcessor | ||
| from opentelemetry.trace import format_span_id | ||
| from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult | ||
|
|
||
| from langfuse._client.environment_variables import ( | ||
| LANGFUSE_FLUSH_AT, | ||
| LANGFUSE_FLUSH_INTERVAL, | ||
| LANGFUSE_OTEL_TRACES_EXPORT_PATH, | ||
| LANGFUSE_OTEL_TRACES_EXPORT_LOCAL_FILEPATH, | ||
| ) | ||
| from langfuse._client.propagation import _get_propagated_attributes_from_context | ||
| from langfuse._client.span_filter import is_default_export_span, is_langfuse_span | ||
| from langfuse._client.utils import span_formatter | ||
| from langfuse.logger import langfuse_logger | ||
| from langfuse.version import __version__ as langfuse_version | ||
|
|
||
| class FileSpanExporter(SpanExporter): | ||
| def __init__(self, filename): | ||
| self.filename = filename | ||
|
|
||
| def export(self, spans): | ||
| with open(self.filename, "w") as f: | ||
| for span in spans: | ||
| f.write(json.dumps(span.to_json()) + "\n") | ||
|
Contributor
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.
|
||
| return SpanExportResult.SUCCESS | ||
|
|
||
| def shutdown(self): | ||
| pass | ||
|
|
||
| class LangfuseSpanProcessor(BatchSpanProcessor): | ||
| """OpenTelemetry span processor that exports spans to the Langfuse API. | ||
|
|
@@ -98,18 +113,21 @@ def __init__( | |
| headers = {**default_headers, **(additional_headers or {})} | ||
|
|
||
| traces_export_path = os.environ.get(LANGFUSE_OTEL_TRACES_EXPORT_PATH, None) | ||
|
|
||
| traces_export_local_filepath = os.environ.get(LANGFUSE_OTEL_TRACES_EXPORT_LOCAL_FILEPATH, None) | ||
| endpoint = ( | ||
| f"{base_url}/{traces_export_path}" | ||
| if traces_export_path | ||
| else f"{base_url}/api/public/otel/v1/traces" | ||
| ) | ||
|
|
||
| langfuse_span_exporter = OTLPSpanExporter( | ||
| endpoint=endpoint, | ||
| headers=headers, | ||
| timeout=timeout, | ||
| ) | ||
| if traces_export_local_filepath and not traces_export_path: | ||
| langfuse_span_exporter = FileSpanExporter(traces_export_local_filepath) | ||
| else: | ||
| langfuse_span_exporter = OTLPSpanExporter( | ||
| endpoint=endpoint, | ||
| headers=headers, | ||
| timeout=timeout, | ||
| ) | ||
|
|
||
| super().__init__( | ||
| span_exporter=langfuse_span_exporter, | ||
|
|
||
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.
file opened in write mode (
"w") which overwrites all previous spans each timeexport()is called - batch exports will lose data