在 spring-boot 2.1.9.release 中寻找匹配的 CachePublicMetrics [英] Looking for Matched CachePublicMetrics in spring-boot 2.1.9.release

查看:36
本文介绍了在 spring-boot 2.1.9.release 中寻找匹配的 CachePublicMetrics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在(spring-boot.1.5.12 版本)的控制器之一中使用以下类

I am using following classes in one of controllers of (spring-boot.1.5.12 release)

我在 spring 2.1.9 版本中找不到匹配的类.

I am unable to find matching classes in spring 2.1.9 release.

以下是代码片段

        import org.springframework.boot.actuate.endpoint.CachePublicMetrics;
        import org.springframework.boot.actuate.metrics.Metric;  

        public class CachingController extends CloudRestTemplate {

        @Autowired
        private CachePublicMetrics metrics;

        public @ResponseBody Map<String, Object> getData(@Pattern(regexp=Constants.STRING_VALID_PATTERN, message=Constants.STRING_INVALID_MSG) @PathVariable(required = true) final String name) throws Exception {
        boolean success = false;
        Map<String, Object> m = Maps.newHashMap();
        Collection<Metric<?>> resp = new ArrayList<>();
        Collection<Metric<?>> mets = metrics.metrics();

        for (Iterator<Metric<?>> iterator = mets.iterator(); iterator.hasNext();) {
        Metric<?> met = iterator.next();
        String metName = met.getName();
        logger.debug(metName+":"+met.getValue());

        if(StringUtils.isNotEmpty(metName)
        && metName.indexOf(name) != -1 ){
        resp.add(met);
        }
        }
        }

推荐答案

我认为您应该更深入地了解 Spring Boot 执行器,一旦您公开所有端点,您可能会找到所需的内容.Spring Boot 提供了一堆预定义的端点,下面是 Spring Boot 执行器端点列表 (来源)

I think you should take a deeper look into Spring boot actuator, once you expose your all endpoints you might find what you are looking for. Spring Boot provides bunch of pre-defined endpoints, below is a list of Spring boot actuator endpoints (source)

  • /auditevents:公开当前应用程序的审计事件信息.
  • /beans:返回应用程序中所有 spring bean 的列表.
  • /caches:提供有关可用缓存的信息.
  • /health:提供应用程序运行状况信息.
  • /conditions:提供在自动配置期间评估的条件列表.
  • /configprops:返回应用程序级属性列表.
  • /info:提供有关当前应用程序的信息.可以在属性文件中配置此信息.
  • /loggers:显示日志配置.此外,此端点可用于修改配置.
  • /headdump:生成头部转储文件并返回.
  • /metrics:返回有关应用程序的各种指标.包括内存、堆和线程信息.但是,此端点不返回任何指标.虽然它只返回可用指标的列表,
    指标名称可以在单独的请求中使用以获取相应的详细信息.例如,像这样的/actuator/metrics/jvm.memory.max.
  • /scheduledtasks:返回应用程序中的计划任务列表.
  • /httptrace:以请求和响应的形式返回最近的 100 个 http 交互.包括执行器端点.
  • /mappings:所有 Http 请求映射的列表.还包括执行器端点.

如评论中所述,您需要访问 /actuator/metrics/jvm.memory.max ,如果您想使用 Java 访问,您可以使用 RestTemplate 调用它,您需要探索 Actuator APIs,我写了一个快速程序,大家可以参考一下

As discussed in comments, you need to access /actuator/metrics/jvm.memory.max , you can invoke same using RestTemplate if you want to access using Java, you need to explore Actuator APIs, I wrote a quick program, you can refer the same

@Autowired
private MetricsEndpoint metricsEndpoint;

public MetricResponse printJavaMaxMemMetrics() {

        ListNamesResponse listNames = metricsEndpoint.listNames();

        listNames.getNames().stream().forEach(name -> System.out.println(name));

        MetricResponse metric = metricsEndpoint.metric("jvm.memory.max", new ArrayList<>());
        System.out.println("metric (jvm.memory.max) ->" + metric);

        List<AvailableTag> availableTags = metric.getAvailableTags();

        availableTags.forEach(tag -> System.out.println(tag.getTag() + " : " + tag.getValues()));

        List<Sample> measurements = metric.getMeasurements();
        measurements.forEach(sample -> System.out.println(sample.getStatistic() + " : " + sample.getValue()));

        return metric;

    }

这篇关于在 spring-boot 2.1.9.release 中寻找匹配的 CachePublicMetrics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆