Add promethues endpoint, we need support the Header with application/openmetrics-text and text/plain,As the Promethuse server sends requests along with the header application/openmetrics-text, But it needs the data with the header text/plain when it receives the responses

pull/808/head
Your Name 2023-08-19 17:06:56 -05:00
parent 19f71cfa52
commit cda9e6866c
5 changed files with 165 additions and 4 deletions

View File

@ -19,6 +19,7 @@ import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import me.zhengjie.modules.security.security.PromethuseResponseFilter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
@ -33,6 +34,7 @@ import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@ -96,6 +98,11 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
List<MediaType> supportMediaTypeList = new ArrayList<>();
supportMediaTypeList.add(MediaType.APPLICATION_JSON);
supportMediaTypeList.add(MediaType.TEXT_PLAIN);
// Promethuse sends request with header 'Accept: application/openmetrics-text; version=1.0.0; charset=utf-8'
MediaType openMetrics = MediaType.parseMediaType("application/openmetrics-text;version=1.0.0;charset=utf-8");
supportMediaTypeList.add(openMetrics);
FastJsonConfig config = new FastJsonConfig();
config.setDateFormat("yyyy-MM-dd HH:mm:ss");
config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
@ -120,9 +127,7 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
*/
@Bean
public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier,
ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes,
CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties,
Environment environment) {
ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
allEndpoints.addAll(webEndpoints);
@ -149,4 +154,22 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath)
|| ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
}
/**
*
*
* @return
*/
@Bean
public FilterRegistrationBean someFilterRegistration()
{
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new PromethuseResponseFilter());// 配置一个返回值过滤器
registration.addUrlPatterns("/actuator/prometheus");
registration.addInitParameter("paramName", "paramValue");
registration.setName("responseFilter");
return registration;
}
}

View File

@ -0,0 +1,93 @@
package me.zhengjie.config;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
*
*
* @Title: ResponseWrapper
* @Description:
* @author fei
* @date 2023-07-26
*/
public class ResponseWrapper extends HttpServletResponseWrapper
{
private ByteArrayOutputStream buffer;
private ServletOutputStream out;
public ResponseWrapper(HttpServletResponse httpServletResponse)
{
super(httpServletResponse);
buffer = new ByteArrayOutputStream();
out = new WrapperOutputStream(buffer);
}
@Override
public ServletOutputStream getOutputStream()
throws IOException
{
return out;
}
@Override
public void flushBuffer()
throws IOException
{
if (out != null)
{
out.flush();
}
}
public byte[] getContent()
throws IOException
{
flushBuffer();
return buffer.toByteArray();
}
class WrapperOutputStream extends ServletOutputStream
{
private ByteArrayOutputStream bos;
public WrapperOutputStream(ByteArrayOutputStream bos)
{
this.bos = bos;
}
@Override
public void write(int b)
throws IOException
{
bos.write(b);
}
@Override
public boolean isReady()
{
// TODO Auto-generated method stub
return false;
}
@Override
public void setWriteListener(WriteListener arg0)
{
// TODO Auto-generated method stub
}
}
}

View File

@ -0,0 +1,40 @@
package me.zhengjie.modules.security.security;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.filter.OncePerRequestFilter;
import com.alibaba.fastjson.JSON;
import me.zhengjie.config.ResponseWrapper;
public class PromethuseResponseFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse)response);//转换成代理类
filterChain.doFilter(request, wrapperResponse);
byte[] content = wrapperResponse.getContent();//获取返回值
if (content.length > 0) {
String jsonString = new String(content);
Object plaObject = JSON.parse(jsonString);
//把返回值输出到客户端
ServletOutputStream out = response.getOutputStream();
// Prometheus actuator endpoint should produce a text/plain response
// https://github.com/spring-projects/spring-boot/issues/28446
response.setContentType("text/plain");
out.write(plaObject.toString().getBytes());
out.flush();
out.close();
}
}
}

View File

@ -120,7 +120,7 @@ management:
endpoints:
web:
exposure:
include: health,info, metrics, shutdown
include: health,info, metrics, shutdown, prometheus
# include: '*' # all
# default: http://localhost:8000/actuator/*
# base-path: http://localhost:8000/${base-path}/*

View File

@ -95,6 +95,11 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<!--监控sql日志-->
<dependency>
<groupId>org.bgee.log4jdbc-log4j2</groupId>