CsapMicroMeter - csap-platform/csap-core GitHub Wiki

source: CsapMicroMeter.java

Adding CsapMicroMeter.java to a java project adds:

  • a Micrometer repository, as outlined in the Spring Boot reference guide.
  • helper methods to quickly implement health and performance capabilities
  • a rich UI for adhoc collection and display of performance data

CsapMicroMeter.java provides a CompositeRegistry to co-exist with prometheous registry (and any others). The embedded @RestController exposes:

  • /csap/metrics/micrometers: includes health report, and meters filtered by names and tags
  • /health/report - just a condensed report
  • /health/test/... - test methods

Integration

  • copy CsapMicrometer.java from the csapstarter git.
  • Place it in a location where it will be autoconfigured by SpringBoot (eg. Same package as "@SpringBootApplication"
  • Enable it to be collected with out permissions: httpSecurity.antMatchers( "/csap/**" ).permitAll()
  • Add timers and counters to your code(See below)

Best Practices and Recommendations

  • Use the spring boot default servlet provider (tomcat) due to greater metric coverage (see diagram on right)
  • Use of "default" meters and meter names enables large numbers of meters to be browsed with CSAP Service Live.
  • For infra monitor meters(CSAP Service Health), use of defined names is strongly encouraged.
    • @Timed ("csap.functionname.attribute") will ensure meters continue to be collected following code refactorings, etc.

Adding Health Report Item(s)


// Option 1: invoke anywhere in your code
@Autowired CsapMicroMeter.HealthReport healthReport ;
 
public void someMethod() {
    ...
    healthReport.getOffLineErrors().add( healthReport.buildError( "test-message-id", "dev-only", errMessage ) ) ;
    ...
}
 
// Option 2: Add code to: CsapMicroMeter.HealthReport.update_health_status()

Adding Counters and Timers


  • CsapMicroMeter.CSAP_COLLECTION_TAG is used to limit the number of meters exported when http://<>/csap/metrics/micrometers&aggegate=true
  • recommended: ONLY 5-15 meters should be tagged.
    • Note: health reports, and browsing will expose ALL meters, not just those tagged
  • Alternate to explicity tagging of timers: any name starting with "csap." will be autotagged with CSAP_COLLECTION_TAG

@Timed

// unless "full" aop is enabled, @Timer will only work on methods crossing component boundaries
@Timed ( value = "csap.ui.page.landing" ,  description = "Root landing page" )
@GetMapping ( value = "/" )
public String demoGet( Model springViewModel ) {
 
   ...
}
 
@Timed(value="csap.jms.test-scenario",  description="Jms Processing" )
@JmsListener ( id = TEST_JMS_LISTENER_ID , destination = SCENARIO_Q , containerFactory = FACTORY )
public void processRawMessageForTestScenarios ( Message message, Session session ) {
...
}
@Autowired
SimpleMeterRegistry simpleMeterRegistry ;
 
// Option 1: use lamda
Timer testTimer = springMeterRegistry.timer("your.simple.name.a") ;
testTimer.record(() -> {
    try {
        TimeUnit.MILLISECONDS.sleep(55);
    } catch (InterruptedException ignored) { }
});
 
// Option 2
Timer.Sample    sample        = Timer.start( simpleMeterRegistry ) ;
// do some stuff
sample.stop( Timer.builder( "your.simple.name.b" ).tag( CsapMicroMeter.CSAP_COLLECTION_TAG, "true")
            .register( simpleMeterRegistry ) ) ;

AOP

@Autowired
CsapMicroMeter.Helpers microMeterHelper ;
 
@Bean // only 1 of the 10 methods record will be exported in aggregation mode
public MeterFilter add_csap_collection_tags( CsapMicroMeter.Helpers microMeterHelper ) {
    return microMeterHelper.addCsapCollectionTag( "dbcp.BasicDataSource.getConnection" ) ;
}
 
@Pointcut("execution(* org.apache.commons.dbcp2.BasicDataSource.get*(..))")
private void dbcpPC() {
};
 
@Around("dbcpPC()")  // 10 methods in dbcp will be viewable
public Object dbcpMicroMeter(ProceedingJoinPoint pjp)
        throws Throwable {
 
    return  microMeterHelper.timedExecution( pjp, "dbcp." );
}
⚠️ **GitHub.com Fallback** ⚠️