Wednesday, October 30, 2013

Spring Boot Actuator: How to change the endpoint base url


By default, actuator management endpoints are mapped to the top level url: "/info", "/metrics", "/beans", etc.
That may be good. But probably it would be better to have some base path. For example, "/admin/metrics" or "/manage/info".
Spring Boot provides quick and easy solution.

Configuration

In your configuration resource file (application.properties or application.yml): 
management.contextPath: /admin
Now, all the management endpoints are mapped under "/admin" base path.
"/admin/metrics", "/admin/info", "/admin/health"...

Implementation Classes

In spring boot actuator source code:
  • org.springframework.boot.actuate.properties.ManagementServerProperties
    • property values representation class
  • org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration
    • @Configuration which uses above ManagementServerProperties


Friday, October 18, 2013

How spring-boot enables actuator by adding jar dependency?


I was reading spring-boot source code, and wondered how it is auto-detecting actuator features just by adding jar dependency.


spring-boot-actuator


"spring-boot-actuator" adds management-endpoints(http) to the spring-boot application just by adding the jar file.

endpoints are metrics, health-check, spring-security auth audit, etc.

please see here for feature details.


To add actuator to your spring-boot application, add this dependencies:


    org.springframework.boot
    spring-boot-starter-actuator


    org.springframework.boot
    spring-boot-starter-web


Mechanism of auto-detecting spring-boot-actuator


So, how spring-boot application detects spring-boot-actuator?

Sample spring-boot application:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

"org.springframework.boot.SpringApplication" is the starting point for spring-boot application.

Auto-detection Steps

  1. During "SpringApplication#run" method, it creates an application context from the given source classes("@Configuration" annotated classes). In this case "MyApplication" class.
  2. When "MyApplication" class is being processed as a spring's Java-configuration, "@EnableAutoConfiguration" annotation gets interpreted. "@EnableAutoConfiguration" is a spring-boot(autoconfigure) annotation which is handled by "org.springframework.boot.autoconfigure.EnableAutoConfigurationImportSelector". (how spring handles @Enable* annotation is well documented in this blog post.)
  3. In "EnableAutoConfigurationImportSelector", it uses "org.springframework.core.io.support.SpringFactoriesLoader#loadFactoryNames" from spring-core to load configurations whose key is  "org.springframework.boot.autoconfigure.EnableAutoConfiguration".
    This method reads "META-INF/spring.factories" from jar files.(multiple jar files can have "spring.factories" and when they have same key, comma delimited values will be merged.)
    spring-boot-actuator contains "META-INF/spring.factories" file. The value of the file is a comma delimited list of "@Configuration" classes under "org.springframework.boot.actuate.autoconfigure" package, which are actuator bean definition classes.
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    org.springframework.boot.actuate.autoconfigure.AuditAutoConfiguration,\
    org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration,\
    org.springframework.boot.actuate.autoconfigure.EndpointWebMvcAutoConfiguration,\
    org.springframework.boot.actuate.autoconfigure.ErrorMvcAutoConfiguration,\
    org.springframework.boot.actuate.autoconfigure.ManagementServerPropertiesAutoConfiguration,\
    org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration,\
    org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration,\
    org.springframework.boot.actuate.autoconfigure.SecurityAutoConfiguration,\
    org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration,\
    org.springframework.boot.actuate.autoconfigure.TraceWebFilterAutoConfiguration
  4. These configuration classes will be imported into the main application-context, so that, spring-boot-actuator beans become available in the application.

Same mechanism is used for "spring-boot-autoconfigure". 
Its jar file contains "spring.factories" file.


Writing a Custom Spring-Boot Module


In sum, you can write a spring-boot module which will be auto-detected by spring-boot application based on the presence of the jar file:
  • include "META-INF/spring.factories" file
    • key: "org.springframework.boot.autoconfigure.EnableAutoConfiguration"
    • value: comma delimited path to @Configuration file(s) for your module








Thursday, October 10, 2013

released datasource-proxy v1.2


I released datasource-proxy version 1.2

main feature:
  • query and parameter replacement
    • new QueryTransformer and ParameterTransformer APIs to transform query and parameter before executing queries
  • many refactoring

They are available in maven central.

dependency:

 project homepage: https://github.com/ttddyy/datasource-proxy


Tuesday, December 11, 2012

display ec2-name and ip-address as ssh-config or hosts file


Inspired by this instagram engineering blog, I wrote a python script to printout ec2 hostname and ip-address as ssh-config or hosts file format.

In our environment, unfortunately we don't have dns for ec2 hosts, for now. 
I use /etc/hosts. Some people use ssh-config file.

This script may help others who are in similar environment where using ssh-config or hosts file to manage ec2-host mapping.

Usage



ec2-printconfig script


Thursday, June 14, 2012

util method that runs TestNG test classes and verify the result



The method does:
  • run TestNG tests programmatically
  • verify the failures happened in TestNG tests run
  • If there is a failure, combine all assertion messages including stack trace, then make a single AssertionError

(I'm also using google-guava)






Sunday, March 25, 2012

run jetty on dependency resolved war in gradle



Background:
  • test classes(such as selenium tests) are in an independent module(maven project, etc.).
  • have an external dependency(mave, ivy, etc) to war file(s)
  • want to run jetty on dependent war file(s)
Gradle can start up jetty on dependency resolved war file(s).


When you want to run your tests after jetty started, set "daemon=true" and make the test task depends on your new run-war task.


Monday, March 5, 2012

run multiple jetty in gradle


Background:
I have a selenium test suite for end-to-end web testing.
Currently, it is running on jenkins box but it is taking so long to finish all the tests.
So, next step is to parallelize the selenium tests.
As a first step, I need to start multiple jetty servers using different port.

With Gradle jetty plugin, I camp up how to run the multiple jetty servers before tests.