3

我想将 Micrometer TomcatMetrics 指标类插入到没有 Spring 集成的现有 tomcat 应用程序中

从源代码的外观看起来很可能只需调用

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

但是,我似乎无法弄清楚如何获得该org.apache.catalina.Manager实例。

没有经理(null),它可以工作,但缺少我想要的会话信息。

那么如何以适当的方式掌握它(servletContextListener 或其他东西)

4

2 回答 2

1
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;
}
于 2021-02-02T10:21:15.000 回答
0

带有 JVM 和 Tomcat 指标的 Prometheus 注册表的示例micrometer.jsp页面。

在您的应用程序类加载器中包含千分尺罐(例如 micrometer-core-1.8.2.jar、micrometer-registry-prometheus-1.8.2.jar)

<%@ page contentType="text/plain" trimDirectiveWhitespaces="true" %>
<%@ page import="io.micrometer.prometheus.PrometheusMeterRegistry" %>
<%@ page import="io.micrometer.prometheus.PrometheusConfig" %>
<%@ page import="io.prometheus.client.exporter.common.TextFormat" %>
<%@ page import="io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics"%>
<%@ page import="io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics"%>
<%@ page import="io.micrometer.core.instrument.binder.jvm.JvmGcMetrics"%>
<%@ page import="io.micrometer.core.instrument.binder.system.ProcessorMetrics"%>
<%@ page import="io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics"%>
<%@ page import="io.micrometer.core.instrument.binder.tomcat.TomcatMetrics"%>
<%@ page import="org.apache.catalina.core.ApplicationContextFacade"%>
<%@ page import="java.lang.reflect.Field"%>
<%@ page import="org.apache.catalina.core.ApplicationContext"%>
<%@ page import="org.apache.catalina.core.StandardContext"%>
<%
    PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
    prometheusRegistry.config().commonTags("application", "myset-worker");
    //prometheusRegistry.config().meterFilter(new PrometheusRenameFilter());

    // JVM metrics
    new ClassLoaderMetrics().bindTo(prometheusRegistry);
    new JvmMemoryMetrics().bindTo(prometheusRegistry);
    new JvmGcMetrics().bindTo(prometheusRegistry);
    new ProcessorMetrics().bindTo(prometheusRegistry);
    new JvmThreadMetrics().bindTo(prometheusRegistry);

    // Tomcat metrics
    ApplicationContextFacade acf = (ApplicationContextFacade)request.getServletContext();
    Field applicationContextFacadeField = ApplicationContextFacade.class.getDeclaredField("context");
    applicationContextFacadeField.setAccessible(true);
    ApplicationContext appContext = (ApplicationContext) applicationContextFacadeField.get(acf);
    Field applicationContextField = ApplicationContext.class.getDeclaredField("context");
    applicationContextField.setAccessible(true);
    StandardContext stdContext = (StandardContext) applicationContextField.get(appContext);
    new TomcatMetrics(stdContext.getManager(),null).bindTo(prometheusRegistry);

    // Print metrics
    //out.print(prometheusRegistry.scrape());
    out.print(prometheusRegistry.scrape(TextFormat.CONTENT_TYPE_OPENMETRICS_100));
%>
于 2022-02-08T21:29:10.697 回答