Monitoring is an example how state machine concepts can be used to monitor machine transitions and actions.
@Configuration @EnableStateMachine public static class Config extends StateMachineConfigurerAdapter<String, String> { @Override public void configure(StateMachineStateConfigurer<String, String> states) throws Exception { states .withStates() .initial("S1") .state("S2", null, (c) -> {System.out.println("hello");}) .state("S3", (c) -> {System.out.println("hello");}, null); } @Override public void configure(StateMachineTransitionConfigurer<String, String> transitions) throws Exception { transitions .withExternal() .source("S1").target("S2").event("E1") .action((c) -> {System.out.println("hello");}) .and() .withExternal() .source("S2").target("S3").event("E2"); } }
Let’s get into actual demo. Run the boot based sample application:
# java -jar spring-statemachine-samples-monitoring-2.0.1.RELEASE.jar
Execute some transitions.
Metrics can be viewed from Boot.
# curl http://localhost:8080/actuator/metrics/ssm.transition.duration { "name":"ssm.transition.duration", "measurements":[ { "statistic":"COUNT", "value":3.0 }, { "statistic":"TOTAL_TIME", "value":0.007 }, { "statistic":"MAX", "value":0.004 } ], "availableTags":[ { "tag":"transitionName", "values":[ "INITIAL_S1", "EXTERNAL_S1_S2" ] } ] }
# curl http://localhost:8080/actuator/metrics/ssm.transition.transit { "name":"ssm.transition.transit", "measurements":[ { "statistic":"COUNT", "value":3.0 } ], "availableTags":[ { "tag":"transitionName", "values":[ "EXTERNAL_S1_S2", "INITIAL_S1" ] } ] }
Tracing can be viewed from Boot.
# curl http://localhost:8080/actuator/statemachinetrace [ { "timestamp":"2018-02-11T06:44:12.723+0000", "info":{ "duration":2, "machine":null, "transition":"EXTERNAL_S1_S2" } }, { "timestamp":"2018-02-11T06:44:12.720+0000", "info":{ "duration":0, "machine":null, "action":"demo.monitoring.StateMachineConfig$Config$$Lambda$576/1499688007@22b47b2f" } }, { "timestamp":"2018-02-11T06:44:12.714+0000", "info":{ "duration":1, "machine":null, "transition":"INITIAL_S1" } }, { "timestamp":"2018-02-11T06:44:09.689+0000", "info":{ "duration":4, "machine":null, "transition":"INITIAL_S1" } } ]