com.signalfx.metrics.connection.AbstractHttpDataPointProtobufReceiverConnection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of signalfx-java Show documentation
Show all versions of signalfx-java Show documentation
Bare minimum core library needed to sending metrics to SignalFx from Java clients
The newest version!
package com.signalfx.metrics.connection;
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 java.util.ArrayList;
import com.signalfx.shaded.apache.http.HttpEntity;
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.shaded.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import static com.signalfx.connection.RetryDefaults.DEFAULT_MAX_RETRIES;
import static com.signalfx.connection.RetryDefaults.DEFAULT_NON_RETRYABLE_EXCEPTIONS;
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) {
this(endpoint, timeoutMs, DEFAULT_MAX_RETRIES, httpClientConnectionManager, DEFAULT_NON_RETRYABLE_EXCEPTIONS);
}
public AbstractHttpDataPointProtobufReceiverConnection(SignalFxReceiverEndpoint endpoint,
int timeoutMs,
int maxRetries,
HttpClientConnectionManager httpClientConnectionManager) {
this(endpoint, timeoutMs, maxRetries, httpClientConnectionManager, DEFAULT_NON_RETRYABLE_EXCEPTIONS);
}
public AbstractHttpDataPointProtobufReceiverConnection(SignalFxReceiverEndpoint endpoint,
int timeoutMs,
int maxRetries,
HttpClientConnectionManager httpClientConnectionManager,
List> nonRetryableExceptions) {
super(endpoint, timeoutMs, maxRetries, httpClientConnectionManager, nonRetryableExceptions);
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 = new ArrayList<>();
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);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy