From f49ac1214d7618e090b40428188e3705bca0ad11 Mon Sep 17 00:00:00 2001 From: Florian Schepers Date: Mon, 3 Jun 2024 10:43:06 +0200 Subject: [PATCH] feat: add file tracer file convertion to new format --- CHANGELOG.md | 2 ++ src/intelligence_layer/core/tracer/file_tracer.py | 8 ++++++++ src/intelligence_layer/core/tracer/tracer.py | 4 +++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f3e481ebc..5261067f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ - All models raise an error during initialization if an incompatible `name` is passed, instead of only when they are used. - Add `aggregation_overviews_to_pandas` function to allow for easier comparison of multiple aggregation overviews - Add `parameter_optimization.ipynb` notebook to demonstrate the optimization of tasks by comparing different parameter combinations. + - Add `convert_file_for_viewing` in the `FileTracer` to convert the trace file format to the new (OpenTelemetry style) format and save as a new file. + - All tracers can now call `submit_to_trace_viewer` to send the trace to the Trace Viewer. ### Fixes - The document index client now correctly URL-encodes document names in its queries. diff --git a/src/intelligence_layer/core/tracer/file_tracer.py b/src/intelligence_layer/core/tracer/file_tracer.py index 5c7813caa..10dbb6d77 100644 --- a/src/intelligence_layer/core/tracer/file_tracer.py +++ b/src/intelligence_layer/core/tracer/file_tracer.py @@ -78,6 +78,14 @@ def traces(self, trace_id: Optional[str] = None) -> InMemoryTracer: ) return self._parse_log(filtered_traces) + def convert_file_for_viewing(self, file_path: Path | str) -> None: + in_memory_tracer = self.traces() + traces = in_memory_tracer.export_for_viewing() + path_to_file = Path(file_path) + with path_to_file.open(mode="w", encoding="utf-8") as file: + for exportedSpan in traces: + file.write(exportedSpan.model_dump_json() + "\n") + class FileSpan(PersistentSpan, FileTracer): """A `Span` created by `FileTracer.span`.""" diff --git a/src/intelligence_layer/core/tracer/tracer.py b/src/intelligence_layer/core/tracer/tracer.py index 8f06ac1ed..e6ea1b02e 100644 --- a/src/intelligence_layer/core/tracer/tracer.py +++ b/src/intelligence_layer/core/tracer/tracer.py @@ -184,7 +184,9 @@ def submit_to_trace_viewer(self) -> bool: "Content-Type": "application/json", "Accept": "application/json", }, - json=ExportedSpanList(self.export_for_viewing()).model_dump(mode="json"), + json=ExportedSpanList(self.export_for_viewing()).model_dump( + mode="json" + ), ) print(res) if res.status_code != 200: