Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Micrometer TomcatMetrics without spring (how to get tomcat manager)

I want to plug in the Micrometer TomcatMetrics metrics class into an existing tomcat application that doesn't have Spring integration

From the looks of the source that seems quite possible by just calling

 public static void monitor(MeterRegistry registry, @Nullable Manager manager, String... tags) 

However, I can't seem to figure out how to get a hold of the org.apache.catalina.Manager instance.

Without the manager (null) it works but lacks session info which i would like to have.

So how do a get a hold of it in a proper way (servletContextListener or something)

like image 483
pvgoddijn Avatar asked Oct 28 '25 08:10

pvgoddijn


1 Answers

private static Manager manager;

private static synchronized Manager getManager(ServletContext servletContext) {
    if (manager == null) {
        try {
            Field applicationContextField = servletContext.getClass().getDeclaredField("context");
            applicationContextField.setAccessible(true);
            ApplicationContext appContextObj = (ApplicationContext) applicationContextField.get(servletContext);
            Field standardContextField = appContextObj.getClass().getDeclaredField("context");
            standardContextField.setAccessible(true);
            StandardContext standardContextObj = (StandardContext) standardContextField.get(appContextObj);
            manager = standardContextObj.getManager();
        } catch (ReflectiveOperationException e) {
            throw new RuntimeException(e);
        }
    }
    return manager;
}
like image 70
cocorossello Avatar answered Oct 31 '25 12:10

cocorossello



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!