49. Monitoring

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-1.2.8.RC1.jar
sm monitoring 1

Execute some transitions.

sm monitoring 2

Metrics can be viewed from Boot.

# curl http://localhost:8080/metrics

{
"gauge.ssm.transition.INITIAL_S1.duration":0.0,
"gauge.ssm.transition.EXTERNAL_S2_S3.duration":0.0,
"gauge.ssm.action.demo.monitoring.StateMachineConfig$Config$$Lambda$8/[email protected]":0.0,
"gauge.ssm.transition.EXTERNAL_S1_S2.duration":1.0,
"gauge.ssm.action.demo.monitoring.StateMachineConfig$Config$$Lambda$7/[email protected]":0.0,
"gauge.ssm.action.demo.monitoring.StateMachineConfig$Config$$Lambda$9/[email protected]":0.0,
"counter.ssm.action.demo.monitoring.StateMachineConfig$Config$$Lambda$8/[email protected]":1,
"counter.ssm.action.demo.monitoring.StateMachineConfig$Config$$Lambda$9/[email protected]":1,
"counter.ssm.transition.EXTERNAL_S2_S3.transit":1,
"counter.ssm.action.demo.monitoring.StateMachineConfig$Config$$Lambda$7/[email protected]":1,
"counter.ssm.transition.EXTERNAL_S1_S2.transit":1,
"counter.ssm.transition.INITIAL_S1.transit":2,
}

Tracing can be viewed from Boot.

# curl http://localhost:8080/trace

[{
  "timestamp":1478419121956,
  "info":{
    "duration":0,
    "machine":null,
    "transition":
    "EXTERNAL_S2_S3"
  }
 },
 {
  "timestamp":1478419121956,
  "info":{
    "duration":0,
    "machine":null,
    "action":"demo.monitoring.StateMachineConfig$Config$$Lambda$9/28306193@10069f9"
  }
 },
 {
  "timestamp":1478419121956,
  "info":{
    "duration":0,
    "machine":null,
    "action":"demo.monitoring.StateMachineConfig$Config$$Lambda$8/12546741@a522d0"
  }
 },
 {
  "timestamp":1478419121956,
  "info":{
    "duration":1,
    "machine":null,
    "transition":"EXTERNAL_S1_S2"
  }
 },
 {
  "timestamp":1478419121955,
  "info":{
    "duration":0,
    "machine":null,
    "action":"demo.monitoring.StateMachineConfig$Config$$Lambda$7/25284245@1c21333"
  }
 }
]