1212import java .util .Optional ;
1313
1414import org .apache .commons .lang3 .ArrayUtils ;
15+ import org .apache .commons .lang3 .reflect .MethodUtils ;
1516import org .jooq .lambda .Seq ;
1617
1718import com .compassion .commons .Utilities ;
@@ -38,6 +39,7 @@ public static MetricPayload with(Object base) {
3839 types = ArrayUtils .isEmpty (types ) ? StandardMetricType .values () : types ;
3940 Arrays .asList (types ).forEach ($ -> mapOf .put ($ , value .get ()));
4041 }
42+
4143 for (var m : base .getClass ().getDeclaredMethods ()) {
4244 var prov = Seq .of (m .getDeclaredAnnotation (MetricValueProviders .class ))
4345 .filter (Objects ::nonNull )
@@ -46,32 +48,32 @@ public static MetricPayload with(Object base) {
4648 .filter (Objects ::nonNull )
4749 .toList ();
4850 if (prov .isEmpty ()) { continue ; }
49- var value = invoke (base , m );
50- if (value .isEmpty ()) { continue ; }
51- prov .forEach (i -> {
52- payload .addSeriesItem (
53- new MetricSeries ()
54- .metric (i .type ().toString ())
55- .type (i .type ().getIntakeType ())
56- .tags (mapOf .get (i .type ()).tagList ())
57- .points (List .of (
58- new MetricPoint ()
59- .timestamp (now ())
60- .value (value .get ()))));
61- });
51+
52+ Object value ;
53+ try {
54+ value = MethodUtils .invokeMethod (base , m .getName ());
55+ } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
56+ log .warn ("Error accessing metric value on {}" , base , e );
57+ continue ;
58+ }
59+
60+ if (value instanceof Number n ) {
61+ prov .forEach (i -> {
62+ payload .addSeriesItem (
63+ new MetricSeries ()
64+ .metric (i .type ().toString ())
65+ .type (i .type ().getIntakeType ())
66+ .tags (mapOf .get (i .type ()).tagList ())
67+ .points (List .of (
68+ new MetricPoint ()
69+ .timestamp (now ())
70+ .value (n .doubleValue ()))));
71+ });
72+ }
6273 }
6374 return payload ;
6475 }
6576
66- private static Optional <Double > invoke (Object ret , Method m ) {
67- try {
68- return Utilities .cast (Optional .ofNullable (m .invoke (ret )));
69- } catch (IllegalAccessException | InvocationTargetException e ) {
70- log .error (e );
71- return Optional .empty ();
72- }
73- }
74-
7577 private static Optional <? extends DataDogTags > invokeClass (Object ret , Method m ) {
7678 try {
7779 var r = Optional .ofNullable (m .invoke (ret ));
0 commit comments