在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返回监控数据格式错乱的更多相关文章

  1. FastJson序列化Json自定义返回字段,普通类从spring容器中获取bean

    前言: 数据库的字段比如:price:1 ,返回需要price:1元. 这时两种途径修改: ① 比如sql中修改或者是在实体类转json前遍历修改. ②返回json,序列化时候修改.用到的是fastj ...

  2. 实战 Prometheus 搭建监控系统

    实战 Prometheus 搭建监控系统 Prometheus 是一款基于时序数据库的开源监控告警系统,说起 Prometheus 则不得不提 SoundCloud,这是一个在线音乐分享的平台,类似于 ...

  3. Grafana+Prometheus系统监控之webhook

    概述 Webhook是一个API概念,并且变得越来越流行.我们能用事件描述的事物越多,webhook的作用范围也就越大.Webhook作为一个轻量的事件处理应用,正变得越来越有用. 准确的说webho ...

  4. 用自定义注解实现fastjson序列化的扩展

    这篇文章起源于项目中一个特殊的需求.由于目前的开发方式是前后端分离的,基本上是通过接口提供各个服务. 而前两天前端fe在开发中遇到了一些问题:他们在处理字符串类型的时间时会出现精度丢失的情况,所以希望 ...

  5. cAdvisor+Prometheus+Grafana监控docker

    cAdvisor+Prometheus+Grafana监控docker 一.cAdvisor(需要监控的主机都要安装) 官方地址:https://github.com/google/cadvisor ...

  6. fastjson序列化出现StackOverflowError

    今天在一个web项目里开发功能,记录日志用到了fastjson的序列化,把类型为RetreatRecord的数据对象序列化后打印出来.结果出现StackOverflowError.先贴出来异常堆栈: ...

  7. prometheus自定义监控指标——入门

    grafana结合prometheus提供了大量的模板,虽然这些模板几乎监控到了常见的监控指标,但是有些特殊的指标还是没能提供(也可能是我没找到指标名称).受zabbix的影响,自然而然想到了自定义监 ...

  8. kubernetes(k8s) Prometheus+grafana监控告警安装部署

    主机数据收集 主机数据的采集是集群监控的基础:外部模块收集各个主机采集到的数据分析就能对整个集群完成监控和告警等功能.一般主机数据采集和对外提供数据使用cAdvisor 和node-exporter等 ...

  9. FastJson序列化时过滤字段(属性)的方法总结

    FastJson序列化时(即转成JSON字符串时),可以过滤掉部分字段,或者只保留部分字段,方法有很多,下面举一些常用的方法. 方法一.FastJson的注解 @JSONField(serialize ...

  10. Prometheus+Grafana监控

    什么是Prometheus? Prometheus是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB).Prometheus使用Go语言开发,是Google BorgMon监控系统 ...

随机推荐

  1. 【Java】JDBC Part1 数据库连接的演变

    环境搭建 使用Maven工程的依赖项,如果普通工程就点注释的地址下载jar包即可 <dependencies> <!-- https://mvnrepository.com/arti ...

  2. 【JavaWeb】接口请求404的问题排查

    响应状态404:404 Page Not Found 根本原因: 服务器找不到这个地址描述的页面资源, 注意是页面资源 可能的出现的开发情况: 1.请求的资源可能真的不存在,是接口,也可以是页面 2. ...

  3. 《A Palestinian Woman Embraces the Body of Her Niece》—— 4月19日报道 2024年世界新闻摄影大赛结果在荷兰出炉,一张巴勒斯坦妇女在加沙地带抱着被杀害的五岁侄女尸体的照片被评为年度最佳作品

    The genocide is not just a matter between the parties involved; it's a concern for all humanity. Gen ...

  4. WarpDrive 教程 第一部分修改版

    本文参考: https://www.cnblogs.com/devilmaycry812839668/p/15327509.html warpDrive是一个python库,目的是使用GPU并行运行多 ...

  5. 再探 游戏 《 2048 》 —— AI方法—— 缘起、缘灭(2) —— 游戏环境设计篇

    注意: 本文为前文 再探 游戏 < 2048 > -- AI方法-- 缘起.缘灭(1) -- Firefox浏览器自动运行篇 接续篇. ========================== ...

  6. AI 大模型时代呼唤新一代基础设施,DataOps 2.0和调度编排愈发重要

    在 AI 时代,DataOps 2.0 代表了一种全新的数据管理和操作模式,通过自动化数据管道.实时数据处理和跨团队协作,DataOps 2.0 能够加速数据分析和决策过程.它融合了人工智能和机器学习 ...

  7. 掌握 Nuxt 3 的页面元数据:使用 definePageMeta 进行自定义配置

    title: 掌握 Nuxt 3 的页面元数据:使用 definePageMeta 进行自定义配置 date: 2024/8/11 updated: 2024/8/11 author: cmdrago ...

  8. RabbitMq消息可靠性之回退模式 通俗易懂 超详细 【内含案例】

    RabbitMq保证消息可靠性之回退模式 介绍 生产者生产的消息没有正确的到达队列就会触发回退模式,进行二次发送 前提 完成SpringBoot 整合 RabbitMq 中的Topic通配符模式 一. ...

  9. ucos cpu_a.asm 注释

    EXPORT CPU_SR_Save import:翻译为进口或引入,表明要调用的函数为外部文件定义 export:翻译为出口或输出,表明该符号可以被外部模块使用,类似于C中的extern功能. ;* ...

  10. bazel 简介(一)—— 基础概念与原理

    0x01 背景 bazel目前已广泛用于云计算领域的开源软件的构建如k8s.kubevirt等,本文以一个新手的角度分享下bazel的基础知识,其存在的价值.对比下,它与其他已经存在的构建系统的差别, ...