Skip to content

Commit 68feecd

Browse files
committed
apply ellipsis suggestion, test for exceptions
1 parent d6bd36b commit 68feecd

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/lmnr/opentelemetry_lib/decorators/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
)
2121
from lmnr.opentelemetry_lib.tracing import TracerWrapper
2222
from lmnr.opentelemetry_lib.utils.json_encoder import JSONEncoder
23+
from lmnr.sdk.log import get_default_logger
24+
25+
logger = get_default_logger(__name__)
2326

2427

2528
class CustomJSONEncoder(JSONEncoder):
@@ -91,7 +94,8 @@ def _process_input(
9194
span.set_attribute(SPAN_INPUT, "Laminar: input too large to record")
9295
else:
9396
span.set_attribute(SPAN_INPUT, inp)
94-
except TypeError:
97+
except Exception:
98+
logger.debug("Failed to process input, ignoring", exc_info=True)
9599
pass
96100

97101

@@ -117,7 +121,8 @@ def _process_output(
117121
span.set_attribute(SPAN_OUTPUT, "Laminar: output too large to record")
118122
else:
119123
span.set_attribute(SPAN_OUTPUT, output)
120-
except TypeError:
124+
except Exception:
125+
logger.debug("Failed to process output, ignoring", exc_info=True)
121126
pass
122127

123128

tests/test_observe.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,24 @@ def observed_foo(x):
299299
assert json.loads(spans[0].attributes["lmnr.span.input"]) == {"x": 2}
300300

301301

302+
def test_observe_input_formatter_exception(exporter: InMemorySpanExporter):
303+
def input_formatter(x):
304+
raise ValueError("test")
305+
306+
@observe(input_formatter=input_formatter)
307+
def observed_foo(x):
308+
return x
309+
310+
result = observed_foo(1)
311+
spans = exporter.get_finished_spans()
312+
assert result == 1
313+
assert len(spans) == 1
314+
assert spans[0].name == "observed_foo"
315+
assert spans[0].attributes["lmnr.span.instrumentation_source"] == "python"
316+
assert spans[0].attributes["lmnr.span.path"] == ("observed_foo",)
317+
assert "lmnr.span.input" not in spans[0].attributes
318+
319+
302320
def test_observe_input_formatter_with_kwargs(exporter: InMemorySpanExporter):
303321
def input_formatter(x, **kwargs):
304322
return {"x": x + 1, "custom-A": f"{kwargs.get('a')}--"}
@@ -369,6 +387,21 @@ def observed_foo(x):
369387
assert json.loads(spans[0].attributes["lmnr.span.output"]) == {"x": 2}
370388

371389

390+
def test_observe_output_formatter_exception(exporter: InMemorySpanExporter):
391+
def output_formatter(x):
392+
raise ValueError("test")
393+
394+
@observe(output_formatter=output_formatter)
395+
def observed_foo(x):
396+
return x
397+
398+
result = observed_foo(1)
399+
spans = exporter.get_finished_spans()
400+
assert result == 1
401+
assert len(spans) == 1
402+
assert "lmnr.span.output" not in spans[0].attributes
403+
404+
372405
@pytest.mark.asyncio
373406
async def test_observe_output_formatter_async(exporter: InMemorySpanExporter):
374407
def output_formatter(x):

0 commit comments

Comments
 (0)