【问题标题】:How to show Dropwizard Metrics Servlet with spring-boot?如何使用 spring-boot 显示 Dropwizard Metrics Servlet?
【发布时间】:2015-07-28 13:01:35
【问题描述】:

我使用spring-boot-starter-actuator 来获取localhost/metrics 端点。

现在我还想使用 dropwizard.metricsmetrics-servlets 依赖项。在他们的网页 (https://dropwizard.github.io/metrics/3.1.0/getting-started/) 上声明,通过此 AdminServet,将创建一些用于度量、健康、线程转储和 ping 的管理菜单。

但我没有看到那个 servlet。我可能需要在 spring-boot 中显式注册它吗?

【问题讨论】:

    标签: java spring spring-boot metrics dropwizard


    【解决方案1】:

    我必须显式实例化 servlet 并提供一个 servlet 映射路径,如下所示:

    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
        return new ServletRegistrationBean(new AdminServlet(),"/metrics/admin/*");
    }
    

    【讨论】:

    • 我看到了菜单,但从那个链接我看不到矩阵是空的。但是,直接访问 /metrics 会给出带有数据的结果。你是如何将这两者联系起来的?
    【解决方案2】:

    如果有人使用 5.0.0 版,则需要执行以下步骤才能使其正常工作:

    @Inject
    private ServletContext servletContext;
    
    @Inject
    private MetricRegistry metricRegistry;
    
    @Inject
    private HealthCheckRegistry healthCheckRegistry;
    
    @Bean
    public ServletRegistrationBean<Servlet> servletRegistrationBean(){
        servletContext.setAttribute(MetricsServlet.METRICS_REGISTRY,
            metricRegistry);
        servletContext.setAttribute(HealthCheckServlet.HEALTH_CHECK_REGISTRY,
            healthCheckRegistry);
    
        return new ServletRegistrationBean<>(new AdminServlet(), "/metrics/*");
    }
    

    来源:https://stackoverflow.com/a/41382649/1047418

    【讨论】:

      猜你喜欢
      • 2017-07-23
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 2018-07-05
      相关资源
      最近更新 更多