@@ -212,7 +212,11 @@ def from_many(
212212 total = sum (value * weight for _, value, weight in values)
213213 weight = sum (weight for _, _, weight in values)
214214 score_attributes = {name: value for name, value, _ in values}
215- return cls (value = total / weight, step = step, attributes = {** attributes, ** score_attributes})
215+ return cls (
216+ value = total / weight,
217+ step = step,
218+ attributes = {** attributes, ** score_attributes},
219+ )
216220```
217221
218222
@@ -228,13 +232,13 @@ Scorer
228232
229233``` python
230234Scorer(
231- tracer: Tracer,
232235 name: str ,
233236 tags: Sequence[str ],
234237 attributes: dict[str , Any],
235238 func: ScorerCallable[T],
236239 step: int = 0 ,
237240 auto_increment_step: bool = False ,
241+ catch: bool = False ,
238242)
239243```
240244
@@ -254,6 +258,14 @@ auto_increment_step: bool = False
254258
255259Whether to automatically increment the step for each time this scorer is called.
256260
261+ ### catch
262+
263+ ``` python
264+ catch: bool = False
265+ ```
266+
267+ Whether to catch exceptions in the scorer function and return a 0 Metric with error information.
268+
257269### func
258270
259271``` python
@@ -321,17 +333,19 @@ async def __call__(self, object: T) -> Metric:
321333 Returns:
322334 A Metric object.
323335 """
324- from dreadnode.tracing.span import Span
325-
326- with Span(
327- name = self .name,
328- tags = self .tags,
329- attributes = self .attributes,
330- tracer = self .tracer,
331- ):
336+ try :
332337 metric = self .func(object )
333338 if inspect.isawaitable(metric):
334339 metric = await metric
340+ except Exception as exc:
341+ if not self .catch:
342+ raise
343+
344+ warn_at_user_stacklevel(
345+ f " Error executing scorer { self .name!r } for object { object !r } : { exc} " ,
346+ MetricWarning,
347+ )
348+ metric = Metric(value = 0.0 , step = self .step, attributes = {" error" : str (exc)})
335349
336350 if not isinstance (metric, Metric):
337351 metric = Metric(
@@ -373,13 +387,13 @@ def clone(self) -> "Scorer[T]":
373387 A new Scorer.
374388 """
375389 return Scorer(
376- tracer = self .tracer,
377390 name = self .name,
378391 tags = self .tags,
379392 attributes = self .attributes,
380393 func = self .func,
381394 step = self .step,
382395 auto_increment_step = self .auto_increment_step,
396+ catch = self .catch,
383397 )
384398```
385399
@@ -390,11 +404,11 @@ def clone(self) -> "Scorer[T]":
390404
391405``` python
392406from_callable(
393- tracer: Tracer,
394407 func: ScorerCallable[T] | Scorer[T],
395408 * ,
396409 name: str | None = None ,
397410 tags: Sequence[str ] | None = None ,
411+ catch: bool = False ,
398412 ** attributes: Any,
399413) -> Scorer[T]
400414```
@@ -403,9 +417,6 @@ Create a scorer from a callable function.
403417
404418** Parameters:**
405419
406- * ** ` tracer ` **
407- (` Tracer ` )
408- –The tracer to use for reporting metrics.
409420* ** ` func ` **
410421 (` ScorerCallable[T] | Scorer[T] ` )
411422 –The function to call to get the metric.
@@ -419,6 +430,11 @@ Create a scorer from a callable function.
419430 ` None `
420431 )
421432 –A list of tags to attach to the metric.
433+ * ** ` catch ` **
434+ (` bool ` , default:
435+ ` False `
436+ )
437+ –Whether to catch exceptions in the scorer function and return a 0 Metric with error information.
422438* ** ` **attributes ` **
423439 (` Any ` , default:
424440 ` {} `
@@ -435,21 +451,21 @@ Create a scorer from a callable function.
435451@ classmethod
436452def from_callable (
437453 cls ,
438- tracer : Tracer,
439454 func : " ScorerCallable[T] | Scorer[T]" ,
440455 * ,
441456 name : str | None = None ,
442457 tags : t.Sequence[str ] | None = None ,
458+ catch : bool = False ,
443459 ** attributes : t.Any,
444460) -> " Scorer[T]" :
445461 """
446462 Create a scorer from a callable function.
447463
448464 Args:
449- tracer: The tracer to use for reporting metrics.
450465 func: The function to call to get the metric.
451466 name: The name of the scorer, used for reporting metrics.
452467 tags: A list of tags to attach to the metric.
468+ catch: Whether to catch exceptions in the scorer function and return a 0 Metric with error information.
453469 **attributes: A dictionary of attributes to attach to the metric.
454470
455471 Returns:
@@ -470,11 +486,11 @@ def from_callable(
470486 )
471487 name = name or func_name
472488 return cls (
473- tracer = tracer,
474489 name = name,
475490 tags = tags or [],
476491 attributes = attributes or {},
477492 func = func,
493+ catch = catch,
478494 )
479495```
480496
0 commit comments