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)
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With