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

com.signalfx.metrics.connection.HttpDataPointProtobufReceiverConnection Maven / Gradle / Ivy

package com.signalfx.metrics.connection;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.signalfx.shaded.apache.commons.io.IOUtils;
import com.signalfx.shaded.apache.http.HttpEntity;
import com.signalfx.shaded.apache.http.HttpStatus;
import com.signalfx.shaded.apache.http.client.methods.CloseableHttpResponse;
import com.signalfx.shaded.apache.http.conn.HttpClientConnectionManager;
import com.signalfx.shaded.apache.http.entity.ByteArrayEntity;
import com.signalfx.shaded.apache.http.entity.InputStreamEntity;

import com.signalfx.shaded.fasterxml.jackson.core.JsonProcessingException;
import com.signalfx.shaded.fasterxml.jackson.core.type.TypeReference;
import com.signalfx.shaded.fasterxml.jackson.databind.ObjectMapper;
import com.signalfx.shaded.google.common.collect.ImmutableMap;
import com.signalfx.common.proto.ProtocolBufferStreamingInputStream;
import com.signalfx.endpoint.SignalFxReceiverEndpoint;
import com.signalfx.metrics.SignalFxMetricsException;
import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers;

public class HttpDataPointProtobufReceiverConnection
        extends AbstractHttpDataPointProtobufReceiverConnection {
    public HttpDataPointProtobufReceiverConnection(
            SignalFxReceiverEndpoint endpoint, int timeoutMs,
            HttpClientConnectionManager httpClientConnectionManager) {
        super(endpoint, timeoutMs, httpClientConnectionManager);
    }

    @Override
    protected HttpEntity getEntityForVersion(List dataPoints) {
        return new InputStreamEntity(
                new ProtocolBufferStreamingInputStream(
                        dataPoints.iterator()), PROTO_TYPE);
    }

    @Override
    protected String getEndpointForAddDatapoints() {
        return "/v1/datapoint";
    }

    @Override
    public Map registerMetrics(String auth,
                                                Map metricTypes)
            throws SignalFxMetricsException {
        Map res = new HashMap();
        for (Map.Entry i : metricTypes.entrySet()) {
            res.put(i.getKey(), false);
        }
        if (metricTypes.isEmpty()) {
            return res;
        }
        List> postBodyList = new ArrayList>(
                metricTypes.size());
        for (Map.Entry entity : metricTypes
                .entrySet()) {
            postBodyList.add(ImmutableMap
                    .of("sf_metric", entity.getKey(), "sf_metricType",
                            entity.getValue().toString()));
        }

        final byte[] map_as_json;
        try {
            map_as_json = MAPPER.writeValueAsBytes(postBodyList);
        } catch (JsonProcessingException e) {
            throw new SignalFxMetricsException("Unable to write protocol buffer", e);
        }
        String body = "";
        try {
            CloseableHttpResponse resp = null;
            try {
                resp = postToEndpoint(auth,
                        new ByteArrayEntity(map_as_json, JSON_TYPE),
                        "/v1/metric?bulkupdate=true",
                        false);
                try {
                    body = IOUtils.toString(resp.getEntity().getContent());
                } catch (IOException e) {
                    throw new SignalFxMetricsException("Unable to get reponse content",
                            e);
                }
                if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                    throw new SignalFxMetricsException("Invalid status code "
                            + resp.getStatusLine().getStatusCode() + ": " + body);
                }
                List> respObject =
                        new ObjectMapper().readValue(body.getBytes(),
                                new TypeReference>>() {
                                });
                if (respObject.size() != metricTypes.size()) {
                    throw new SignalFxMetricsException(
                            String.format("json map mismatch: post_body=%s, resp=%s",
                                    new String(map_as_json), body));
                }
                for (int i = 0; i < respObject.size(); i++) {
                    Map m = respObject.get(i);
                    if (!m.containsKey("code") || "409".equals(m.get("code").toString())) {
                        res.put(postBodyList.get(i).get("sf_metric"), true);
                    }
                }
            } finally {
                if (resp != null) {
                    resp.close();
                }
            }
        } catch (IOException e) {
            throw new SignalFxMetricsException(
                    String.format("post_body=%s, resp=%s", new String(map_as_json), body), e);
        }
        return res;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy