mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-25 13:58:04 +01:00
Move JSON reporter to Dropwizard ReporterFactory structure.
This commit is contained in:
@@ -27,25 +27,25 @@ import java.util.SortedMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Adapted from MetricsServlet.
|
||||
*/
|
||||
public class JsonMetricsReporter extends ScheduledReporter {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(JsonMetricsReporter.class);
|
||||
private static final Pattern SIMPLE_NAMES = Pattern.compile("[^a-zA-Z0-9_.\\-~]");
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(JsonMetricsReporter.class);
|
||||
private final JsonFactory factory = new JsonFactory();
|
||||
|
||||
private final String table;
|
||||
private final String sunnylabsHost;
|
||||
private final String token;
|
||||
private final String hostname;
|
||||
private final String host;
|
||||
|
||||
public JsonMetricsReporter(MetricRegistry registry, String token, String sunnylabsHost)
|
||||
public JsonMetricsReporter(MetricRegistry registry, String token, String hostname,
|
||||
MetricFilter filter, TimeUnit rateUnit, TimeUnit durationUnit)
|
||||
throws UnknownHostException
|
||||
{
|
||||
super(registry, "jsonmetrics-reporter", MetricFilter.ALL, TimeUnit.SECONDS, TimeUnit.MILLISECONDS);
|
||||
this.table = token;
|
||||
this.sunnylabsHost = sunnylabsHost;
|
||||
this.host = InetAddress.getLocalHost().getHostName();
|
||||
super(registry, "json-reporter", filter, rateUnit, durationUnit);
|
||||
this.token = token;
|
||||
this.hostname = hostname;
|
||||
this.host = InetAddress.getLocalHost().getHostName();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -57,7 +57,7 @@ public class JsonMetricsReporter extends ScheduledReporter {
|
||||
{
|
||||
try {
|
||||
logger.debug("Reporting metrics...");
|
||||
URL url = new URL("https", sunnylabsHost, 443, "/report/metrics?t=" + table + "&h=" + host);
|
||||
URL url = new URL("https", hostname, 443, String.format("/report/metrics?t=%s&h=%s", token, host));
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
|
||||
connection.setDoOutput(true);
|
||||
@@ -175,10 +175,66 @@ public class JsonMetricsReporter extends ScheduledReporter {
|
||||
json.writeNumberField("m15", convertRate(meter.getFifteenMinuteRate()));
|
||||
}
|
||||
|
||||
private static final Pattern SIMPLE_NAMES = Pattern.compile("[^a-zA-Z0-9_.\\-~]");
|
||||
|
||||
private String sanitize(String metricName) {
|
||||
return SIMPLE_NAMES.matcher(metricName).replaceAll("_");
|
||||
}
|
||||
|
||||
public static Builder forRegistry(MetricRegistry registry) {
|
||||
return new Builder(registry);
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
|
||||
private final MetricRegistry registry;
|
||||
private MetricFilter filter = MetricFilter.ALL;
|
||||
private TimeUnit rateUnit = TimeUnit.SECONDS;
|
||||
private TimeUnit durationUnit = TimeUnit.MILLISECONDS;
|
||||
private String token;
|
||||
private String hostname;
|
||||
|
||||
private Builder(MetricRegistry registry) {
|
||||
this.registry = registry;
|
||||
this.rateUnit = TimeUnit.SECONDS;
|
||||
this.durationUnit = TimeUnit.MILLISECONDS;
|
||||
this.filter = MetricFilter.ALL;
|
||||
}
|
||||
|
||||
public Builder convertRatesTo(TimeUnit rateUnit) {
|
||||
this.rateUnit = rateUnit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder convertDurationsTo(TimeUnit durationUnit) {
|
||||
this.durationUnit = durationUnit;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder filter(MetricFilter filter) {
|
||||
this.filter = filter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withToken(String token) {
|
||||
this.token = token;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder withHostname(String hostname) {
|
||||
this.hostname = hostname;
|
||||
return this;
|
||||
}
|
||||
|
||||
public JsonMetricsReporter build() throws UnknownHostException {
|
||||
if (hostname == null) {
|
||||
throw new IllegalArgumentException("No hostname specified!");
|
||||
}
|
||||
|
||||
if (token == null) {
|
||||
throw new IllegalArgumentException("No token specified!");
|
||||
}
|
||||
|
||||
return new JsonMetricsReporter(registry, token, hostname, filter, rateUnit, durationUnit);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package org.whispersystems.textsecuregcm.metrics;
|
||||
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import com.codahale.metrics.ScheduledReporter;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeName;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import io.dropwizard.metrics.BaseReporterFactory;
|
||||
|
||||
@JsonTypeName("json")
|
||||
public class JsonMetricsReporterFactory extends BaseReporterFactory {
|
||||
|
||||
@JsonProperty
|
||||
@NotNull
|
||||
private String hostname;
|
||||
|
||||
@JsonProperty
|
||||
@NotNull
|
||||
private String token;
|
||||
|
||||
@Override
|
||||
public ScheduledReporter build(MetricRegistry metricRegistry) {
|
||||
try {
|
||||
return JsonMetricsReporter.forRegistry(metricRegistry)
|
||||
.withHostname(hostname)
|
||||
.withToken(token)
|
||||
.convertRatesTo(getRateUnit())
|
||||
.convertDurationsTo(getDurationUnit())
|
||||
.filter(getFilter())
|
||||
.build();
|
||||
} catch (UnknownHostException e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user