Thursday, March 30, 2017

Spring framework Instrumentation

Instrumentation is the ability to monitor the level of product's performance, to diagnose errors and to write trace information.

Instrumentation is one of the key features from Spring framework to review application performance.

Spring supports instrumentation through AOP and Logging.

Types of Instrumentation

Code tracing - Tracing the information messages about the execution of an application at run time including:

  • Business state changes
  • Beginning and end of major business transactions
  • Resource creation and deletion
  • Events related to performance and reliability
  • Logging events
  • Monitoring database activities
Instrumenting applications by placing trace statement at relevant locations in code is particularly useful for distributed applications, as diagnosing is an issue that crosses process and machine boundaries is easier when all of the logging information can be collected into a centralized location and stored into a cohesive transcript of single business process.

Spring uses AOP framework to provide tracing and debugging logic to the application.

Performance monitoring - Three forms of performance monitors used in spring framework

  • Out of box statistical monitors (CPU, Memory, Network, Storage usage etc.,)
  • Custom specific statistical monitors (average time for specific method)
  • Custom counters for events of interest (exceptions, cache etc.,)
The out of box statistical monitors are part of open source infrastructure.  These are the primary methods used to monitor system performance of any environment.  We can develop custom performance counters that will provide similar aggregate counters for our application code.  The primary use of these counters is for counting and timing key methods within the application.
Java instrumentation does the following process to count the timings of key methods:

  • Takes the current time of the invocation request on the inbound
  • Retakes the current time on outbound response
  • Calculate the elapsed time as a delta of the two measurements
  • Submits the elapsed time of the invocation to the Application Performance Management (APM) system

INSTRUMENTATION APIs

Logging API

Logging API abstracts logging functions from the implementation
Logging API will work as common logging utility for the the application,if we want we can  change the logging implementation at the utility framework to meet with application requirements.

Logging levels 


Where logging is implemented through IoC interceptors such as a Trace logging, an administrator API will be provided to change the interceptor configuration at run time.  This will allow an administrator to enable to disable trace logging at a granular level in the application without restarting the server.

Logging in Clustered environment

In clustered environment log information will be collected and stored in a centralized log server.  With this approach all log information from many servers is brought together into a single location.  Decoupling logging in this manner reduces contention for the single data store and improves logging performance.
Logging in a multi-server distributed architecture can generate a huge amount of logging content from many severs and layers where many disparate items of trace and logging information are created.  The logging solution will collect and store all of this information in one common repository.

Logging in cloud environment

Logging in cloud environment can be exactly the same as local environment except for the publishing mechanism.  The services running in the cloud will use a message queue appender into a local message queue.  The custom publising solution will collect and write diagnostics from the cloud services to the on premises store.


Logging interceptors

AOP enables developers to write code for a cross cutting functionality and then apply it decoratively to existing code.  The IoC framework will resolve services which are wrapped in an interceptor which has the same interface as the service.  The consuming component thinks it's calling the service, but is actually calling the logging interceptor, which then calls the service.
Configuring a logging interceptor


 Logging interceptor class implementation
Logging interceptor class implements three interfaces of Spring AOP framework
MethodBeforeAdvice
AfterRunningAdvice
ThrowsAdvice


Performance API  
Spring AOP provides custom counters for a specific statistical monitoring such as calculating average time taken by a method to complete the transaction.  These statistical monitors will be used for real-time monitoring, alerting, fault diagnosis and informing scaling planning.
Out of box statistical monitors

Java's JConsole can be used for out-of-box statistical monitoring in development and test environment.  It supports CPU, memory, network, and storage usage.

Custom specific statistical monitors ( average time for a specific operation)

Spring AOP provides configuration for performance monitoring, which does not require any changes to existing code.

Spring framework provides a class called "JamonPerformanceMonitorInterceptor" for performance monitoring.

JamonPerformanceMonitorInterceptor is an AOP interceptor that hooks into the framework.

JamonPerformanceMonitorInterceptor configuration
  
autoProxy creator will be created and hooked in the interceptor, which includes the beans for which performance monitoring is required.



No comments: