SpringBoot Actuator潜在的OOM问题的解决
作者:glmapper 发布时间:2021-08-26 06:36:43
此问题背景产生于近期需要上线的一个功能的埋点;主要表现就是在应用启动之后的一段时间内,内存使用一直呈现递增趋势。
下图为场景复线后,本地通过 jconsole 查看到的内部使用走势图。
实际环境受限于配置,内存不会膨胀
背景&问题
应用 a 使用 rest template 通过 http 方式调用 应用 b,应用项目中开启了 actuator,api 使用的是 micrometer;在 client 调用时,actuator 会产生一个 name 为 http.client.requests 的 metrics,此 metric 的 tag 中包含点目标的 uri。
应用 b 提供的接口大致如下:
@RequestMapping("test_query_params")
public String test_query_params(@RequestParam String value) {
return value;
}
@RequestMapping("test_path_params/{value}")
public String test_path_params(@PathVariable String value) {
return value;
}
http://localhost:8080/api/test/test_query_params?value=
http://localhost:8080/api/test/test_path_params/{value}_
期望在 metric 的收集结果中应该包括两个 metrics,主要区别是 tag 中的 uri 不同,一个是 api/test/test_query_params, 另一个是 api/test/test_path_params/{value};实际上从拿到的 metrics 数据来看,差异很大,这里以 pathvariable 的 metric 为例,数据如下:
tag: "uri",
values: [
"/api/test/test_path_params/glmapper58",
"/api/test/test_path_params/glmapper59",
"/api/test/test_path_params/glmapper54",
"/api/test/test_path_params/glmapper55",
"/api/test/test_path_params/glmapper56",
"/api/test/test_path_params/glmapper57",
"/api/test/test_path_params/glmapper50",
"/api/test/test_path_params/glmapper51",
"/api/test/test_path_params/glmapper52",
"/api/test/test_path_params/glmapper53",
"/api/test/test_path_params/glmapper47",
"/api/test/test_path_params/glmapper48",
"/api/test/test_path_params/glmapper49",
"/api/test/test_path_params/glmapper43",
"/api/test/test_path_params/glmapper44",
"/api/test/test_path_params/glmapper45",
"/api/test/test_path_params/glmapper46",
"/api/test/test_path_params/glmapper40",
"/api/test/test_path_params/glmapper41",
"/api/test/test_path_params/glmapper42",
"/api/test/test_path_params/glmapper36",
"/api/test/test_path_params/glmapper37",
"/api/test/test_path_params/glmapper38",
"/api/test/test_path_params/glmapper39",
"/api/test/test_path_params/glmapper32",
"/api/test/test_path_params/glmapper33",
"/api/test/test_path_params/glmapper34",
"/api/test/test_path_params/glmapper35",
"/api/test/test_path_params/glmapper30",
"/api/test/test_path_params/glmapper31",
"/api/test/test_path_params/glmapper25",
"/api/test/test_path_params/glmapper26",
....
]
可以非常明显的看到,这里将{value} 参数作为了 uri 组件部分,并且体现在 tag 中,并不是期望的 api/test/test_path_params/{value}。
问题原因及解决
两个问题,1、这个埋点是怎么生效的,先搞清楚这个问题,才能顺藤摸瓜。2、怎么解决。
默认埋点是如何生效的
因为是通过 resttemplate 进行调用访问,那么埋点肯定也是基于对 resttemplate 的代理;按照这个思路,笔者找到了 org.springframework.boot.actuate.metrics.web.client.MetricsRestTemplateCustomizer
这个类。RestTemplateCustomizer 就是对 resttemplate 进行定制的,MetricsRestTemplateCustomizer 通过名字也能得知期作用是为了给 resttemplate 增加 metric 能力。
再来讨论 RestTemplateCustomizer,当使用RestTemplateBuilder构建RestTemplate时,可以通过RestTemplateCustomizer进行更高级的定制,所有RestTemplateCustomizer beans 将自动添加到自动配置的RestTemplateBuilder。也就是说如果 想 MetricsRestTemplateCustomizer 生效,那么构建 resttemplate 必须通过 RestTemplateBuilder 方式构建,而不是直接 new。
http.client.requests 中的 uri
塞 tag 的代码在org.springframework.boot.actuate.metrics.web.client.RestTemplateExchangeTags
类中,作用时机是在 MetricsClientHttpRequestInterceptor * 中。当调用执行完成后,会将当次请求 metric 记录下来,在这里就会使用到 RestTemplateExchangeTags 来填充 tags。 下面仅给出 uri 的部分代码
/**
* Creates a {@code uri} {@code Tag} for the URI of the given {@code request}.
* @param request the request
* @return the uri tag
*/
public static Tag uri(HttpRequest request) {
return Tag.of("uri", ensureLeadingSlash(stripUri(request.getURI().toString())));
}
/**
* Creates a {@code uri} {@code Tag} from the given {@code uriTemplate}.
* @param uriTemplate the template
* @return the uri tag
*/
public static Tag uri(String uriTemplate) {
String uri = (StringUtils.hasText(uriTemplate) ? uriTemplate : "none");
return Tag.of("uri", ensureLeadingSlash(stripUri(uri)));
其余的还有 status 和 clientName 等 tag name。
通过断点,可以看到,这里 request.getURI() 拿到的是带有参数的完整请求链接。
这些 tag 的组装最终在 DefaultRestTemplateExchangeTagsProvider 中完成,并返回一个 列表。
private Timer.Builder getTimeBuilder(HttpRequest request, ClientHttpResponse response) {
return this.autoTimer.builder(this.metricName)
// tagProvider 为 DefaultRestTemplateExchangeTagsProvider
.tags(this.tagProvider.getTags(urlTemplate.get().poll(), request, response))
.description("Timer of RestTemplate operation");
}
解决
这里先来看下官方对于 request.getURI 的解释
/**
* Return the URI of the request (including a query string if any,
* but only if it is well-formed for a URI representation).
* @return the URI of the request (never {@code null})
*/
URI getURI();
返回请求的 URI,这里包括了任何的查询参数。那么是不是拿到不用参数的 path 就行呢?
这里尝试通过 request.getURI().getPath() 拿到了预期的 path(@pathvariable 拿到的是模板)。
再回到 DefaultRestTemplateExchangeTagsProvider,所有的 tag 都是在这里完成组装,这个类明显是一个默认的实现(Spring 体系下基本只要是Defaultxxx 的,一般都能扩展 ),查看它的接口类 RestTemplateExchangeTagsProvider 如下:
/**
* Provides {@link Tag Tags} for an exchange performed by a {@link RestTemplate}.
*
* @author Jon Schneider
* @author Andy Wilkinson
* @since 2.0.0
*/
@FunctionalInterface
public interface RestTemplateExchangeTagsProvider {
/**
* Provides the tags to be associated with metrics that are recorded for the given
* {@code request} and {@code response} exchange.
* @param urlTemplate the source URl template, if available
* @param request the request
* @param response the response (may be {@code null} if the exchange failed)
* @return the tags
*/
Iterable<Tag> getTags(String urlTemplate, HttpRequest request, ClientHttpResponse response);
}
RestTemplateExchangeTagsProvider 的作用就是为 resttemplate 提供 tag 的,所以这里通过自定义一个 RestTemplateExchangeTagsProvider,来替换DefaultRestTemplateExchangeTagsProvider,以达到我们的目标,大致代码如下:
@Override
public Iterable<Tag> getTags(String urlTemplate, HttpRequest request, ClientHttpResponse response) {
Tag uriTag;
// 取 request.getURI().getPath() 作为 uri 的 value
if (StringUtils.hasText(request.getURI().getPath())) {
uriTag = Tag.of("uri", ensureLeadingSlash(stripUri(request.getURI().getPath())));
} else {
uriTag = (StringUtils.hasText(urlTemplate) ? RestTemplateExchangeTags.uri(urlTemplate)
: RestTemplateExchangeTags.uri(request));
}
return Arrays.asList(RestTemplateExchangeTags.method(request), uriTag,
RestTemplateExchangeTags.status(response), RestTemplateExchangeTags.clientName(request));
}
会不会 OOM
理论上,应该参数不同,在使用默认 DefaultRestTemplateExchangeTagsProvider 的情况下,meter 会随着 tags 的不同迅速膨胀,在 micrometer 中,这些数据是存在 map 中的
// Even though writes are guarded by meterMapLock, iterators across value space are supported
// Hence, we use CHM to support that iteration without ConcurrentModificationException risk
private final Map<Id, Meter> meterMap = new ConcurrentHashMap<>();
一般情况下不会,这里是因为 spring boot actuator 自己提供了保护机制,对于默认情况,tags 在同一个 metric 下,最多只有 100 个
/**
* Maximum number of unique URI tag values allowed. After the max number of
* tag values is reached, metrics with additional tag values are denied by
* filter.
*/
private int maxUriTags = 100;
如果你想使得这个数更大一些,可以通过如下配置配置
management.metrics.web.client.max-uri-tags=10000
如果配置值过大,会存在潜在的 oom 风险。
来源:https://juejin.cn/post/7035967350226550815


猜你喜欢
- 目录@ConfigurationProperties使用@ConfigurationProperties特点宽松绑定支持复杂属性类型激活@C
- 1.kotlin的字符串操作和Java有些不同,有些新增。1)先看字符串比较java中==比较的是变量的引用是否指向同一个地址,Kotlin
- 本文实例讲述了Android开发实现判断通知栏是否打开及前往设置页面的方法。分享给大家供大家参考,具体如下:项目中用到日程提醒功能,如果应用
- 数组是一个存储相同类型元素的固定大小的顺序集合。数组是用来存储数据的集合,通常认为数组是一个同一类型变量的集合。声明数组变量并不是声明 nu
- 1,设置预处理,设置不需要拦截的请求@Componentpublic class MyWebConfig implements WebMvc
- 本文实例为大家分享了Unity3D实现旋钮控制灯光效果的具体代码,供大家参考,具体内容如下前言实际上使用的是非常简单的方式,通过开启以及关闭
- 引言: 在Spring Boot应用中,基于数据某个字段进行排序是一个非常常用的需求,这里将给出Sort的三种常用用法,基于分页的应用,大家
- 本文实例讲述了Spring实战之使用注解实现声明式事务操作。分享给大家供大家参考,具体如下:一 配置文件<?xml version=&
- 本文实例为大家分享了Java猜拳游戏的具体代码,供大家参考,具体内容如下先来看一下效果图: 首先我们创建一个Person类,这个类
- 近期由于负责项目的一个模块,该模块下有很多分类,每个分类都有一个编码code,这个值是作为一个参数携带过来的。但是每个code确实对应一个方
- idea去掉不想commit的文件我们项目在每次commit代码时,有时候会有一些不想提交又不能删除的代码,怎么做呢?此方法亲测最方便!!!
- 前言:在 Java 中,让线程休眠的方法有很多,这些方法大致可以分为两类,一类是设置时间,在一段时间后自动唤醒,而另一个类是提供了一对休眠和
- 下面的这些都算是比较高级的问题了,面试中一般也很少问到,因为它们可能会把面试者拒之门外。不过你可以自己找个时间来实践一下。 1.
- 最近比较无聊,随便翻着博客,无意中看到了有的人用VBS读文本内容,也就是读几句中文,emmm,挺有趣的,实现也很简单,都不需要安装什么环境,
- 本文为大家分享了Android studio安装与配置,具体内容如下1、首先下载Android studio安装包,可以从http://ww
- 本文实例为大家分享了java实现计算器功能具体代码,供大家参考,具体内容如下效果图组成结构从结构上来说,一个简单的图形界面,需要由界面组件、
- 随着移动互联网的快速发展,它已经和我们的生活息息相关了,在公交地铁里面都能看到很多人的人低头看着自己的手机屏幕,从此“低头族”一词就产生了,
- 做Android应用,不可避免的会与SQLite打交道。随着应用的不断升级,原有的数据库结构可能已经不再适应新的功能,这时候,就需要对SQL
- 我们今天来聊下如何做实时通讯(先给知识点,实现原理,最后给出实现实时通信的具体代码--使用工具 android studio)现在先说下用到
- java 数据结构中栈和队列的实例详解栈和队列是两种重要的线性数据结构,都是在一个特定的范围的存储单元中的存储数据。与线性表相比,它们的插入