fastjson序列化导致prometheus返回监控数据格式错乱
在springboot 中集成prometheus的监控时遇见问题。
因为项目里在StaticResourceConfig配置了fastjson 序列化,导致prometheus接口返回数据被转化为json格式,无法正常展示
正常情况格式应为这种

实际返回了这种

StaticResourceConfig 配置如下:
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        for (HttpMessageConverter<?> converter : converters) {
            if (converter instanceof MappingJackson2HttpMessageConverter) {
                converters.remove(converter);
            }
        }
        converters.add(new ByteArrayHttpMessageConverter());
        converters.add(getFastJsonConverter());
    }
    private FastJsonHttpMessageConverter getFastJsonConverter() {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        //提供对admin的类型支持mediaType
        MediaType mediaType = MediaType.valueOf("application/vnd.spring-boot.actuator.v2+json");
        supportedMediaTypes.add(mediaType);
        supportedMediaTypes.add(MediaType.ALL);
        converter.setSupportedMediaTypes(supportedMediaTypes);
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.DisableCircularReferenceDetect);
        //日期格式化
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        ParserConfig parserConfig = ParserConfig.getGlobalInstance();
        parserConfig.setSafeMode(true);
        fastJsonConfig.setParserConfig(parserConfig);
        converter.setFastJsonConfig(fastJsonConfig);
        return converter;
    }
解决办法
通过增加一层转发,调用监控接口获取到数据,然后反序列化为原来的格式,然后通过response.write方式返回监控结果。
因为采用response.write的方式,不会被spring mvc的HttpMessageConverter所拦截,所以可以直接返回plain/text格式的数据
package com.yuanian.monitor;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
/**
 * @author liujy
 * @since Wed Feb 24 17:55:16 CST 2021
 */
@RestController
@RequestMapping("prometheus")
public class PrometheusController {
    @Value("${server.servlet.context-path}")
    private String contextPath;
    @Value("${server.port}")
    private Integer port;
    @GetMapping(path = "/metrics", produces = MediaType.TEXT_PLAIN_VALUE)
    public void healthz(HttpServletResponse response) throws URISyntaxException {
        RestTemplate restTemplate = new RestTemplate();
        StringBuilder prometheusUrl = new StringBuilder("http://127.0.0.1:");
        prometheusUrl.append(port);
        prometheusUrl.append(contextPath);
        prometheusUrl.append("/actuator/prometheus");
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(prometheusUrl.toString(), String.class);
        String body = responseEntity.getBody();
        String s = JSON.parseObject(body, String.class);
        try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
            bos.write(s.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
fastjson序列化导致prometheus返回监控数据格式错乱的更多相关文章
- FastJson序列化Json自定义返回字段,普通类从spring容器中获取bean
		前言: 数据库的字段比如:price:1 ,返回需要price:1元. 这时两种途径修改: ① 比如sql中修改或者是在实体类转json前遍历修改. ②返回json,序列化时候修改.用到的是fastj ... 
- 实战 Prometheus 搭建监控系统
		实战 Prometheus 搭建监控系统 Prometheus 是一款基于时序数据库的开源监控告警系统,说起 Prometheus 则不得不提 SoundCloud,这是一个在线音乐分享的平台,类似于 ... 
- Grafana+Prometheus系统监控之webhook
		概述 Webhook是一个API概念,并且变得越来越流行.我们能用事件描述的事物越多,webhook的作用范围也就越大.Webhook作为一个轻量的事件处理应用,正变得越来越有用. 准确的说webho ... 
- 用自定义注解实现fastjson序列化的扩展
		这篇文章起源于项目中一个特殊的需求.由于目前的开发方式是前后端分离的,基本上是通过接口提供各个服务. 而前两天前端fe在开发中遇到了一些问题:他们在处理字符串类型的时间时会出现精度丢失的情况,所以希望 ... 
- cAdvisor+Prometheus+Grafana监控docker
		cAdvisor+Prometheus+Grafana监控docker 一.cAdvisor(需要监控的主机都要安装) 官方地址:https://github.com/google/cadvisor ... 
- fastjson序列化出现StackOverflowError
		今天在一个web项目里开发功能,记录日志用到了fastjson的序列化,把类型为RetreatRecord的数据对象序列化后打印出来.结果出现StackOverflowError.先贴出来异常堆栈: ... 
- prometheus自定义监控指标——入门
		grafana结合prometheus提供了大量的模板,虽然这些模板几乎监控到了常见的监控指标,但是有些特殊的指标还是没能提供(也可能是我没找到指标名称).受zabbix的影响,自然而然想到了自定义监 ... 
- kubernetes(k8s) Prometheus+grafana监控告警安装部署
		主机数据收集 主机数据的采集是集群监控的基础:外部模块收集各个主机采集到的数据分析就能对整个集群完成监控和告警等功能.一般主机数据采集和对外提供数据使用cAdvisor 和node-exporter等 ... 
- FastJson序列化时过滤字段(属性)的方法总结
		FastJson序列化时(即转成JSON字符串时),可以过滤掉部分字段,或者只保留部分字段,方法有很多,下面举一些常用的方法. 方法一.FastJson的注解 @JSONField(serialize ... 
- Prometheus+Grafana监控
		什么是Prometheus? Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB).Prometheus使用Go语言开发,是Google BorgMon监控系统 ... 
随机推荐
- 【Windows】解决微软商店打不开的问题
			参考贴吧的帖子: https://tieba.baidu.com/p/6028738660#123983609458l 1.打开"运行"输入 inetcpl.cpl (" ... 
- MyBatisPlus公共字段自动填充
			公共字段自动填充 公共字段 新增员工,修改.编辑员工信息时,都需要设置创建时间,修改时间等操作,这些操作过于重复.繁琐,为了有更快捷高效的,MyBatisPlus提供了公共字段自动填充功能,当我们执行 ... 
- 记一次 .NET某智慧出行系统 CPU爆高分析
			一:背景 1. 讲故事 前些天有位朋友找到我,说他们的系统出现了CPU 100%的情况,让你帮忙看一下怎么回事?dump也拿到了,本想着这种情况让他多抓几个,既然有了就拿现有的分析吧. 二:WinDb ... 
- 02-canvas注意点
			1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ... 
- dubbo超时异常
                                                                                                    
                                                                            荐
			dubbo超时异常 在调用dubbo服务时经常看到如下错误: Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting serve ... 
- Atcoder ABC364 D-F
			Atcoder ABC364 D-F D - K-th Nearest 链接: D - K-th Nearest (atcoder.jp) 简要题意: 问题陈述 在一条数线上有 \(N+Q\) 个点 ... 
- 基于surging的产品项目-木舟开源了!
			一 . 概述 因为前段时间电脑坏了,导致代码遗失,踌躇满志马上上线的平台产品付之东流,现在熬夜在写代码希望能尽快推出企业正常使用的平台产品,而这次把代码开源,一是让大家对surging 使用有个深入的 ... 
- WinUI 3学习笔记(1)—— First Desktop App
			随着Visual Studio 2019 16.10版本的正式发布,创建WinUI 3的APP对我们来说,已不存在任何的难度.本篇我们就试着来一探究竟,看看WinUI 3 APP到底是个啥玩意,能不能 ... 
- [postgres]使用pgbench进行基准测试
			前言 pgbench是一种在postgres上进行基准测试的简单程序,一般安装后就会自带.pgbench可以再并发的数据库绘画中一遍遍地进行相同序列的SQL语句,并且计算平均事务率. 测试准备 既然要 ... 
- 全网最适合入门的面向对象编程教程:42 Python常用复合数据类型-collections容器数据类型
			全网最适合入门的面向对象编程教程:42 Python 常用复合数据类型-collections 容器数据类型 摘要: 在 Python 中,collections 模块提供了一组高效.功能强大的容器数 ... 
