feat: export trace to a local file for quick debugging#1542
Open
llin60 wants to merge 1 commit intolangfuse:mainfrom
Open
feat: export trace to a local file for quick debugging#1542llin60 wants to merge 1 commit intolangfuse:mainfrom
llin60 wants to merge 1 commit intolangfuse:mainfrom
Conversation
| 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.
ReadableSpan objects don't have a to_json() method - this will fail at runtime. Consider using the existing span_formatter function from langfuse._client.utils or manually serialize the span attributes like it does.
| self.filename = filename | ||
|
|
||
| def export(self, spans): | ||
| with open(self.filename, "w") as f: |
Contributor
There was a problem hiding this comment.
file opened in write mode ("w") which overwrites all previous spans each time export() is called - batch exports will lose data
Suggested change
| with open(self.filename, "w") as f: | |
| with open(self.filename, "a") as f: |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Hi Langfuse team,
Thanks for this awesome SDK. I have been using it to test an application with agents involved. However, since the application is under rapid development phase, I made some changes to the exporter so that I can do quick local debugging without setting up the whole Langfuse services. I figured that these changes might be useful to someone else, so I submitted this PR.
This PR introduces an option to export OpenTelemetry traces to a local file, intended to improve the local development and debugging workflow.
Advantages:
This feature is opt-in and does not alter the default behavior of the exporter.
To enable local file export, set the LANGFUSE_OTEL_TRACES_EXPORT_LOCAL_FILEPATH environment variable. Ensure that the LANGFUSE_OTEL_TRACES_EXPORT_PATH variable is unset to avoid conflicts, which also protects the default exporter behavior. The local exporter is only effective when the LANGFUSE_OTEL_TRACES_EXPORT_LOCAL_FILEPATH is set AND the LANGFUSE_OTEL_TRACES_EXPORT_PATH is unset.
Important
Adds local file export option for OpenTelemetry traces using a new environment variable and
FileSpanExporterclass.LANGFUSE_OTEL_TRACES_EXPORT_LOCAL_FILEPATHinenvironment_variables.py.FileSpanExporterinspan_processor.pyto handle file-based trace export.LANGFUSE_OTEL_TRACES_EXPORT_LOCAL_FILEPATHis set andLANGFUSE_OTEL_TRACES_EXPORT_PATHis unset.FileSpanExporterinspan_processor.pywrites spans to a specified file in JSON format.LANGFUSE_OTEL_TRACES_EXPORT_LOCAL_FILEPATHto specify local file path for trace export inenvironment_variables.py.This description was created by
for 38937af. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Summary
This PR adds local file export for OpenTelemetry traces to support offline debugging. While the feature idea is valuable, the implementation has critical runtime errors that must be fixed before merging.
Critical Issues:
ReadableSpan.to_json()method doesn't exist - will cause immediate runtime failureSuggested fixes:
span_formatterutility or manually serialize span attributesConfidence Score: 0/5
FileSpanExportercalls a non-existent method (span.to_json()) which will immediately fail when the local export path is used. Additionally, the file overwrite issue will cause data loss. These are not edge cases but fundamental issues in the core functionality.Important Files Changed
LANGFUSE_OTEL_TRACES_EXPORT_LOCAL_FILEPATHwith documentationFileSpanExporterclass with critical bugs: calls non-existentto_json()method and overwrites file on each exportFlowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[Initialize LangfuseSpanProcessor] --> B{Check Environment Variables} B --> C{Is LANGFUSE_OTEL_TRACES_EXPORT_LOCAL_FILEPATH set?} C -->|Yes| D{Is LANGFUSE_OTEL_TRACES_EXPORT_PATH unset?} C -->|No| E[Use OTLPSpanExporter] D -->|Yes| F[Use FileSpanExporter] D -->|No| E F --> G[Export spans to local file] E --> H[Export spans to remote endpoint] G --> I[Write JSON to file per batch] H --> J[Send OTLP format via HTTP]Last reviewed commit: 38937af
(2/5) Greptile learns from your feedback when you react with thumbs up/down!