All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.metric.pipeline.FalconPipeline Maven / Gradle / Ivy

/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
 */
package com.github.dennisit.vplus.data.metric.pipeline;

import com.alibaba.fastjson.JSON;
import com.github.dennisit.vplus.data.criteria.RetryCriteria;
import com.github.dennisit.vplus.data.metric.JMetricCollector;
import com.github.dennisit.vplus.data.metric.JMetricPipeline;
import com.github.dennisit.vplus.data.criteria.action.RetryAction;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Created by Elon.su on 17/11/2.
 */
public class FalconPipeline implements JMetricPipeline {

    private static final Logger LOG = LoggerFactory.getLogger(FalconPipeline.class);

    public static final String DEFAULT_AGENT_URL = "http://127.0.0.1:1988/v1/push";

    private String httpUrl;
    private String endpoint;
    private String tags;
    private int step;
    private static RestTemplate template;

    static {
        template = new RestTemplate();
    }


    public FalconPipeline() {

    }


    public FalconPipeline(String endpoint, String tags, int step) {
        this(DEFAULT_AGENT_URL, endpoint, tags, step);
    }


    public FalconPipeline(String httpUrl, String endpoint, String tags, int step) {
        this.endpoint = endpoint;
        this.tags = tags;
        this.step = step;
        this.httpUrl = httpUrl;
    }

    @Override
    public void initialized() {

    }


    @Override
    public void pipeline(JMetricCollector collector, String key, Object value, long milliseconds) {
        FalconMeta meta = builder(key, value, milliseconds);
        RetryAction.doWithTry(new RetryCriteria() {
            @Override
            public Object handle() {
                ResponseEntity responseEntity = template.postForEntity(httpUrl, JSON.toJSONString(meta), String.class);
                if(HttpStatus.OK != responseEntity.getStatusCode()){
                    LOG.error("falcon agent post error !");
                }
                return null;
            }
        });
    }


    @Override
    public void pipeline(JMetricCollector collector, Map metrics, long milliseconds) {
        List metas = builder(metrics, milliseconds);
        RetryAction.doWithTry(new RetryCriteria() {

            @Override
            public Object handle() {
                ResponseEntity responseEntity = template.postForEntity(httpUrl, JSON.toJSON(metas), String.class);
                if(HttpStatus.OK != responseEntity.getStatusCode()){
                    LOG.error("falcon agent post error !");
                };
                return null;
            }
        });
    }

    @Override
    public void destroyed() {

    }

    public FalconMeta builder(String key, Object value, long milliseconds){
        FalconMeta meta = new FalconMeta();
        meta.setEndpoint(this.endpoint);
        meta.setStep(this.step);
        meta.setTags(this.tags);
        meta.setCounterType("GAUGE");
        meta.setMetric(StringUtils.trim(key));
        meta.setValue(String.valueOf(value));
        meta.setTimestamp(milliseconds/1000);
        return meta;
    }


    public List builder(Map metrics, long milliseconds){
        List list = Lists.newArrayList();
        Optional.ofNullable(metrics).orElse(Maps.newHashMap()).forEach((k, v)->{
            FalconMeta meta = builder(k, v, milliseconds);
            if(null != meta){
                list.add(meta);
            }
        });
        return list;
    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class FalconMeta {

        private String endpoint;

        private String metric;

        private String value;

        private long timestamp;

        private int step;

        private String counterType;

        private String tags;
    }

}