All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.signalfx.metrics.connection.AbstractHttpDataPointProtobufReceiverConnection Maven / Gradle / Ivy
package com.signalfx.metrics.connection;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import com.signalfx.shaded.google.common.collect.Lists;
import com.signalfx.shaded.apache.http.HttpEntity;
import com.signalfx.shaded.apache.http.HttpStatus;
import com.signalfx.shaded.apache.http.NameValuePair;
import com.signalfx.shaded.apache.http.client.methods.CloseableHttpResponse;
import com.signalfx.shaded.apache.http.client.utils.URLEncodedUtils;
import com.signalfx.shaded.apache.http.conn.HttpClientConnectionManager;
import com.signalfx.shaded.apache.http.entity.ContentType;
import com.signalfx.shaded.apache.http.entity.InputStreamEntity;
import com.signalfx.shaded.apache.http.message.BasicNameValuePair;
import com.signalfx.common.proto.ProtocolBufferStreamingInputStream;
import com.signalfx.connection.AbstractHttpReceiverConnection;
import com.signalfx.endpoint.SignalFxReceiverEndpoint;
import com.signalfx.metrics.SignalFxMetricsException;
import com.signalfx.metrics.protobuf.SignalFxProtocolBuffers;
import com.signalfx.shaded.apache.http.util.EntityUtils;
public abstract class AbstractHttpDataPointProtobufReceiverConnection extends AbstractHttpReceiverConnection implements DataPointReceiver {
protected static final ContentType PROTO_TYPE = ContentType.create("application/x-protobuf");
private final boolean compress;
public AbstractHttpDataPointProtobufReceiverConnection(SignalFxReceiverEndpoint endpoint,
int timeoutMs,
HttpClientConnectionManager httpClientConnectionManager) {
super(endpoint, timeoutMs, httpClientConnectionManager);
this.compress = !Boolean.getBoolean(DISABLE_COMPRESSION_PROPERTY);
}
public AbstractHttpDataPointProtobufReceiverConnection(SignalFxReceiverEndpoint endpoint,
int timeoutMs,
int maxRetries,
HttpClientConnectionManager httpClientConnectionManager) {
super(endpoint, timeoutMs, maxRetries, httpClientConnectionManager);
this.compress = !Boolean.getBoolean(DISABLE_COMPRESSION_PROPERTY);
}
@Override
public void addDataPoints(String auth, List dataPoints)
throws SignalFxMetricsException {
if (dataPoints.isEmpty()) {
return;
}
try {
CloseableHttpResponse resp = null;
try {
resp = postToEndpoint(auth,
getEntityForVersion(dataPoints),
getEndpointForAddDatapoints(),
compress);
int code = resp.getStatusLine().getStatusCode();
// SignalFx may respond with various 2xx return codes for success.
if (code < 200 || code > 299) {
throw new SignalFxMetricsException("Invalid status code " + code);
}
} finally {
if (resp != null) {
try {
HttpEntity entity = resp.getEntity();
EntityUtils.consume(entity);
} finally {
resp.close();
}
}
}
} catch (IOException e) {
throw new SignalFxMetricsException("Exception posting to addDataPoints", e);
}
}
protected abstract String getEndpointForAddDatapoints();
protected abstract HttpEntity getEntityForVersion(
List dataPoints);
@Override
public void backfillDataPoints(String auth, String metric, String metricType, String orgId, Map dimensions,
List datumPoints)
throws SignalFxMetricsException {
if (datumPoints.isEmpty()) {
return;
}
List params = Lists.newArrayList();
params.add(new BasicNameValuePair("orgid", orgId));
params.add(new BasicNameValuePair("metric_type", metricType));
params.add(new BasicNameValuePair("metric", metric));
// Each dimension is added as a param in the form of "sfxdim_DIMNAME"
for (Map.Entry entry : dimensions.entrySet()) {
params.add(new BasicNameValuePair("sfxdim_" + entry.getKey(), entry.getValue()));
}
try {
CloseableHttpResponse resp = null;
try {
resp = postToEndpoint(auth,
new InputStreamEntity(
new ProtocolBufferStreamingInputStream(
datumPoints.iterator()), PROTO_TYPE),
"/v1/backfill?" + URLEncodedUtils.format(params, StandardCharsets.UTF_8),
false);
int code = resp.getStatusLine().getStatusCode();
// SignalFx may respond with various 2xx return codes for success.
if (code < 200 || code > 299) {
throw new SignalFxMetricsException("Invalid status code " + code);
}
} finally {
if (resp != null) {
HttpEntity entity = resp.getEntity();
if (entity != null) {
entity.getContent().close();
}
resp.close();
}
}
} catch (IOException e) {
throw new SignalFxMetricsException("Exception posting to backfillDataPoints", e);
}
}
}