66import com .swisscom .daisy .cosmos .candyfloss .transformations .TransformedMessage ;
77import io .micrometer .core .instrument .Counter ;
88import io .micrometer .core .instrument .Metrics ;
9+ import io .micrometer .core .instrument .Timer ;
10+ import io .micrometer .core .instrument .Timer .Sample ;
911import java .util .List ;
1012import java .util .stream .Collectors ;
1113import org .apache .kafka .streams .KeyValue ;
1517
1618public class FlattenProcessor
1719 implements Processor <String , TransformedMessage , String , ValueErrorMessage <FlattenedMessage >> {
20+ private static final String metricTag = "pipeline" ;
21+ private static final String timerMetric = "latency_flatten" ;
22+ private static final String CounterOutMetric = "json_streams_flatten_out" ;
23+
1824 private final Counter counterIn =
1925 Counter .builder ("json_streams_flatten_in" )
2026 .description ("Number of message incoming to the Json Flatten step" )
2127 .register (Metrics .globalRegistry );
22- private final Counter counterOut =
23- Counter .builder ("json_streams_flatten_out" )
24- .description (
25- "Number of output messages after the flatten step. "
26- + "Note: json_streams_flatten_out/(json_streams_flatten_in-json_streams_flatten_error)"
27- + " is flatten message ration" )
28- .register (Metrics .globalRegistry );
2928 private final Counter counterError =
3029 Counter .builder ("json_streams_flatten_error" )
3130 .description ("Number of error messages that are discarded to dlq topic" )
@@ -40,17 +39,23 @@ public void init(ProcessorContext<String, ValueErrorMessage<FlattenedMessage>> c
4039
4140 @ Override
4241 public void process (Record <String , TransformedMessage > record ) {
42+ Sample timer = Timer .start (Metrics .globalRegistry );
43+ counterIn .increment ();
44+
4345 var key = record .key ();
4446 var value = record .value ();
4547 var ts = record .timestamp ();
4648
4749 handleRecord (key , value , ts ).forEach (kv -> context .forward (new Record <>(kv .key , kv .value , ts )));
50+
51+ timer .stop (
52+ Metrics .globalRegistry .timer (
53+ timerMetric , metricTag , value .getTag () == null ? "" : value .getTag ()));
4854 }
4955
5056 public Iterable <KeyValue <String , ValueErrorMessage <FlattenedMessage >>> handleRecord (
5157 String key , TransformedMessage value , long ts ) {
5258 try {
53- counterIn .increment ();
5459 return flattenMessage (key , value );
5560 } catch (Exception e ) {
5661 counterError .increment ();
@@ -69,7 +74,14 @@ private List<KeyValue<String, ValueErrorMessage<FlattenedMessage>>> flattenMessa
6974 x ->
7075 KeyValue .pair (
7176 key , new ValueErrorMessage <>(new FlattenedMessage (x , value .getTag ()))))
72- .peek (x -> counterOut .increment ())
77+ .peek (
78+ x -> {
79+ FlattenedMessage flatMsg = x .value .getValue ();
80+ Counter .builder (CounterOutMetric )
81+ .tag (metricTag , flatMsg .getTag () == null ? "" : flatMsg .getTag ())
82+ .register (Metrics .globalRegistry )
83+ .increment ();
84+ })
7385 .collect (Collectors .toList ());
7486 }
7587}
0 commit comments