Skip to content

Commit

Permalink
Add promethuse Endpiont support
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jul 27, 2023
1 parent 87afa65 commit 9a51abe
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -33,6 +34,7 @@
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;
Expand Down Expand Up @@ -98,12 +100,7 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
supportMediaTypeList.add(MediaType.APPLICATION_JSON);
supportMediaTypeList.add(MediaType.TEXT_PLAIN);

/**
* curl -H 'Accept: application/openmetrics-text; version=1.0.0; charset=utf-8' http://localhost:8000/actuator/prometheus
* Todo: Check HttpMessageConverter, or How to use filter to handle the response?// todo can not handle '\n'
* 0.0\nexecutor_active_threads{name=\"elAsync\",} 0.0\n# HELP executor_queue_remaining_tasks The number of additional elements that this queue can ideally accept without blocking\n# TYPE executor_queue_remaining_tasks gauge\nexecutor_queue_remaining_tasks{name=\"otherAsync\",} 50.0\nexecutor_queue_remaining_tasks{name=\"elAsync\",} 50.0\n"
*/

// 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();
Expand Down Expand Up @@ -157,4 +154,22 @@ private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProp
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;
}

}
Original file line number Diff line number Diff line change
@@ -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

}
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
}

0 comments on commit 9a51abe

Please sign in to comment.