io.tiler.collectors.loggly.LogglyCollectorVerticle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tiler-collector-loggly Show documentation
Show all versions of tiler-collector-loggly Show documentation
Loggly collector for the Tiler dashboard framework
The newest version!
package io.tiler.collectors.loggly;
import com.google.code.regexp.Matcher;
import io.tiler.collectors.loggly.config.*;
import io.tiler.core.BaseCollectorVerticle;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import org.simondean.vertx.async.Async;
import org.simondean.vertx.async.DefaultAsyncResult;
import org.vertx.java.core.AsyncResultHandler;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpClientRequest;
import org.vertx.java.core.json.JsonArray;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.logging.Logger;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
public class LogglyCollectorVerticle extends BaseCollectorVerticle {
private static final long TWO_MINUTES_IN_MILLISECONDS = 2 * 60 * 1000;
private Logger logger;
private Config config;
private Base64.Encoder base64Encoder = Base64.getEncoder();
private DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime().withZoneUTC();
public void start() {
logger = container.logger();
config = new ConfigFactory().load(container.config());
final RunState runState = new RunState();
runState.start();
collect(result -> runState.stop());
vertx.setPeriodic(config.collectionIntervalInMilliseconds(), timerID -> {
if (runState.isRunning() && !runState.hasTimedOut()) {
logger.warn("Collection aborted as previous run still executing");
return;
}
runState.start();
collect(aVoid -> runState.stop());
});
logger.info("LogglyCollectorVerticle started");
}
private void collect(Handler handler) {
logger.info("Collection started");
getMetrics(result -> {
if (result.failed()) {
logger.error("Failed to collect metrics", result.cause());
handler.handle(null);
return;
}
logger.info("Collection succeeded");
handler.handle(null);
});
}
private void getMetrics(AsyncResultHandler handler) {
MetricCollectionState state = new MetricCollectionState(vertx, logger, config, currentTimeInMicroseconds());
getMetrics(
state,
result -> {
state.dispose();
handler.handle(result);
});
}
private void getMetrics(MetricCollectionState state, AsyncResultHandler handler) {
if (!state.nextPoint()) {
logger.info("Processed " + state.totalFieldCount() + " fields");
handler.handle(DefaultAsyncResult.succeed(state.servers()));
return;
}
logger.info("Server " + (state.serverIndex() + 1) + " of " + state.serverCount() + ", " +
"metric " + (state.metricIndex() + 1) + " of " + state.metricCount() + ", " +
"field " + (state.fieldIndex() + 1) + " of " + state.fieldCount() + ", " +
"point " + (state.pointIndex() + 1) + " of " + state.pointCount());
String from = formatTimeInMicrosecondsAsISODateTime(state.fromTimeInMicroseconds());
Server serverConfig = state.serverConfig();
StringBuilder requestUriBuilder = new StringBuilder()
.append(serverConfig.path())
.append("/apiv2/fields/")
.append(urlEncode(state.fieldConfig().name()))
.append("/?from=")
.append(urlEncode(from))
.append("&facet_size=2000");
JsonObject point = state.point();
String query = getLogglyQuery(state.metricConfig(), point);
if (query.length() > 0) {
requestUriBuilder.append("&q=")
.append(query);
}
String requestUri = requestUriBuilder.toString();
logger.info("Request URI: '" + requestUri + "'");
Async.retry()
.task(taskHandler -> {
HttpClientRequest request = state.httpClient().get(requestUri, response -> {
if (response.statusCode() != 200) {
taskHandler.handle(DefaultAsyncResult.fail(new Exception("Unexpected " + response.statusCode() + "response code")));
return;
}
response.bodyHandler(body -> {
String bodyString = body.toString();
JsonObject bodyJson;
try {
bodyJson = new JsonObject(bodyString);
} catch (Exception e) {
taskHandler.handle(DefaultAsyncResult.fail(e));
return;
}
taskHandler.handle(DefaultAsyncResult.succeed(bodyJson));
});
});
setBasicAuthOnRequest(serverConfig.username(), serverConfig.password(), request);
request.setTimeout(TWO_MINUTES_IN_MILLISECONDS);
request.exceptionHandler(cause -> taskHandler.handle(DefaultAsyncResult.fail(cause)));
request.end();
})
.times(state.metricConfig().retryTimes())
.run(result -> {
if (result.failed()) {
handler.handle(DefaultAsyncResult.fail(result));
return;
}
JsonObject body = result.result();
Field fieldConfig = state.fieldConfig();
String fieldName = fieldConfig.name();
JsonArray items = body.getArray(fieldName);
logger.info("Received " + items.size() + " terms for field '" + fieldName + "'");
HashMap