Skip to content

Commit

Permalink
discussion: if we're goiong to instrument yaml, let's log byte size
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaqq committed Jan 31, 2025
1 parent f7b57d2 commit 1ac5fb9
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ops/_private/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@
@tracer.start_as_current_span('ops.yaml.safe_load') # type: ignore
def safe_load(stream: Union[str, TextIO]) -> Any:
"""Same as yaml.safe_load, but use fast C loader if available."""
if not isinstance(stream, TextIO):
opentelemetry.trace.get_current_span().set_attribute("len", len(stream))
opentelemetry.trace.get_current_span().set_attribute("stream", isinstance(stream, TextIO))
return yaml.load(stream, Loader=_safe_loader) # noqa: S506


@tracer.start_as_current_span('ops.yaml.safe_dump') # type: ignore
def safe_dump(data: Any, stream: Optional[TextIO] = None) -> str:
"""Same as yaml.safe_dump, but use fast C dumper if available."""
return yaml.dump(data, stream=stream, Dumper=_safe_dumper) # type: ignore
rv = yaml.dump(data, stream=stream, Dumper=_safe_dumper)
if stream is None:
assert rv is not None
opentelemetry.trace.get_current_span().set_attribute("len", len(rv))
opentelemetry.trace.get_current_span().set_attribute("stream", stream is not None)
return rv # type: ignore

0 comments on commit 1ac5fb9

Please sign in to comment.