org.graylog2.telemetry.TelemetryNodePeriodical Maven / Gradle / Ivy
package org.graylog2.telemetry;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.EvictingQueue;
import com.squareup.okhttp.OkHttpClient;
import org.graylog2.plugin.ServerStatus;
import org.graylog2.plugin.system.NodeId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
@Singleton
public class TelemetryNodePeriodical extends TelemetryPeriodical {
private static final Logger LOG = LoggerFactory.getLogger(TelemetryNodePeriodical.class);
private final ServerStatus serverStatus;
private final TelemetryNodeService telemetryNodeService;
@Inject
public TelemetryNodePeriodical(TelemetryNodeService telemetryNodeService,
NodeId nodeId,
ServerStatus serverStatus,
TelemetryConfiguration telemetryConfiguration,
@Named("telemetryHttpClient") OkHttpClient httpClient,
@Named("telemetryObjectMapper") ObjectMapper objectMapper) {
this(
telemetryNodeService,
nodeId,
serverStatus,
telemetryConfiguration,
EvictingQueue.create(telemetryConfiguration.getMaxQueueSize()),
httpClient,
objectMapper);
}
private TelemetryNodePeriodical(TelemetryNodeService telemetryNodeService,
NodeId nodeId,
ServerStatus serverStatus,
TelemetryConfiguration config,
EvictingQueue evictingQueue,
OkHttpClient httpClient,
ObjectMapper objectMapper) {
super(config, evictingQueue, httpClient, objectMapper,
buildTelemetryUrl(config.getUrl(), nodeId),
"node-" + nodeId + "-%s.smile");
this.serverStatus = serverStatus;
this.telemetryNodeService = telemetryNodeService;
}
private static URL buildTelemetryUrl(URI uri, final NodeId nodeId) {
try {
return uri.resolve("node/" + nodeId).toURL();
} catch (MalformedURLException e) {
return null;
}
}
@Override
protected byte[] buildPayload() {
try {
return objectMapper.writeValueAsBytes(telemetryNodeService.buildNodeDataSet());
} catch (JsonProcessingException e) {
LOG.error("Error while serializing telemetry data", e);
return null;
}
}
@Override
public boolean startOnThisNode() {
return config.isEnabled()
&& telemetryUrl != null
&& !serverStatus.hasCapability(ServerStatus.Capability.LOCALMODE);
}
@Override
protected Logger getLogger() {
return LOG;
}
}