• datadog数据json格式转换prometheus文本格式


    代码如下:

    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Enumeration;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.zip.InflaterInputStream;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import io.prometheus.client.Collector;
    import io.prometheus.client.Collector.MetricFamilySamples;
    import io.prometheus.client.Collector.MetricFamilySamples.Sample;
    import io.prometheus.client.exporter.common.TextFormat;
    
    @RestController
    public class ReceiveController {
    
    	@RequestMapping("/metrics")
    	public String foo() {
    		return "test";
    	}
    
    	@RequestMapping(value = { "/api/v1/series" }, method = RequestMethod.POST)
    	public void dataDogRev(HttpServletRequest request, HttpServletResponse response) {
    		try {
    			InflaterInputStream iis = new InflaterInputStream(request.getInputStream());
    			ByteArrayOutputStream baos = new ByteArrayOutputStream();
    			byte[] data = new byte[2048];
    			int len = 0;
    			while ((len = iis.read(data)) > -1) {
    				baos.write(data, 0, len);
    			}
    			String dataStr = new String(baos.toByteArray(), "UTF-8");
    			System.out.println(dataStr);
    			convert2PromText(dataStr);
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    
    	private void convert2PromText(String dataStr) {
    		try {
    			ObjectMapper mapper = new ObjectMapper();
    			Map<String, List<Map<String, Object>>> map = mapper.readValue(dataStr,
    					new TypeReference<Map<String, List<Map<String, Object>>>>() {
    					});
    			List<Map<String, Object>> list = map.get("series");
    			List<MetricFamilySamples> datas = new ArrayList<MetricFamilySamples>();
    			for (Map<String, Object> map2 : list) {
    				String sourceTypeName = (String) map2.get("source_type_name");
    				if (sourceTypeName == null) {
    					continue;
    				}
    				String metricName = (String) map2.get("metric");
    				if (metricName.startsWith("system") || metricName.indexOf("datadog") > -1) {
    					continue;
    				}
    				String typeStr = (String) map2.get("type");
    				Collector.Type type = Collector.Type.valueOf(typeStr.toUpperCase());
    				String host = (String) map2.get("host");
    				List<String> tags = (List<String>) map2.get("tags");
    				List<String> labelsK = new ArrayList<String>();
    				labelsK.add("host");
    				List<String> labelsV = new ArrayList<String>();
    				labelsV.add(host);
    				for (String tagStr : tags) {
    					String[] tagArray = tagStr.split(":");
    					labelsK.add(tagArray[0]);
    					labelsV.add(tagArray[1]);
    				}
    				List<List<Object>> points = (List<List<Object>>) map2.get("points");
    				String time = points.get(0).get(0).toString();
    				String value = points.get(0).get(1).toString();
    
    				MetricFamilySamples metricFamilySamples = new MetricFamilySamples(metricName, type, metricName,
    						Arrays.asList(new Sample(metricName, labelsK, labelsV, Double.parseDouble(value),
    								Long.parseLong(time))));
    				datas.add(metricFamilySamples);
    
    			}
    			ByteArrayOutputStream baos = new ByteArrayOutputStream();
    			OutputStreamWriter osw = new OutputStreamWriter(baos);
    			final Iterator<MetricFamilySamples> iterator = datas.iterator();
    			Enumeration<MetricFamilySamples> mfs = new Enumeration<MetricFamilySamples>() {
    				@Override
    				public boolean hasMoreElements() {
    					return iterator.hasNext();
    				}
    
    				@Override
    				public MetricFamilySamples nextElement() {
    					return iterator.next();
    				}
    			};
    			TextFormat.write004(osw, mfs);
    			osw.flush();
    			osw.close();
    			System.out.println(new String(baos.toByteArray()));
    		} catch (JsonParseException e) {
    			e.printStackTrace();
    		} catch (JsonMappingException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    	}
    
    	public static void main(String[] args) throws IOException {
    		String fileName = "E:\yaoyu\datadog.txt";
    		BufferedReader br = new BufferedReader(new FileReader(new File(fileName)));
    		String dataStr = br.readLine();
    		new ReceiveController().convert2PromText(dataStr);
    	}
    
    }
    

      

  • 相关阅读:
    [JavaScript]JS由来
    [HTML5]HTML表单(Forms)
    Linux 配置SSH 无密钥登陆
    Spring Boot 的 application.properties
    Spring Boot 全局异常捕获
    Linux hostname设置,静态ip设置,hostname与静态ip相互映射
    Hadoop 集群的三种方式
    网站列表
    Hadoop -- 概念
    shell 编程
  • 原文地址:https://www.cnblogs.com/yaoyu1983/p/12321688.html
Copyright © 2020-2023  润新知