Monitoring: Clean log messages in exporters

This commit adds the node name and the exporter name as log message prefixes.

Original commit: elastic/x-pack-elasticsearch@085b2ecf3c
This commit is contained in:
Tanguy Leroux 2016-03-23 11:13:50 +01:00
parent 926ff76971
commit 2c1dbf3eb6
4 changed files with 46 additions and 44 deletions

View File

@ -95,7 +95,7 @@ public abstract class Exporter implements AutoCloseable {
}
public ESLogger logger(Class clazz) {
return Loggers.getLogger(clazz, globalSettings);
return Loggers.getLogger(clazz, globalSettings, name);
}
}

View File

@ -15,6 +15,7 @@ import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.settings.SettingsException;
import org.elasticsearch.marvel.MarvelSettings;
import org.elasticsearch.marvel.agent.exporter.local.LocalExporter;
import org.elasticsearch.node.Node;
import java.util.ArrayList;
import java.util.Collection;
@ -116,6 +117,11 @@ public class Exporters extends AbstractLifecycleComponent<Exporters> implements
}
Map<String, Exporter> initExporters(Settings settings) {
Settings globalSettings = Settings.builder()
.put(settings)
.put(Node.NODE_NAME_SETTING.getKey(), nodeName())
.build();
Set<String> singletons = new HashSet<>();
Map<String, Exporter> exporters = new HashMap<>();
boolean hasDisabled = false;
@ -129,7 +135,7 @@ public class Exporters extends AbstractLifecycleComponent<Exporters> implements
if (factory == null) {
throw new SettingsException("unknown exporter type [" + type + "] set for exporter [" + name + "]");
}
Exporter.Config config = new Exporter.Config(name, settings, exporterSettings);
Exporter.Config config = new Exporter.Config(name, globalSettings, exporterSettings);
if (!config.enabled()) {
hasDisabled = true;
if (logger.isDebugEnabled()) {
@ -155,7 +161,7 @@ public class Exporters extends AbstractLifecycleComponent<Exporters> implements
// fallback on the default
//
if (exporters.isEmpty() && !hasDisabled) {
Exporter.Config config = new Exporter.Config("default_" + LocalExporter.TYPE, settings, Settings.EMPTY);
Exporter.Config config = new Exporter.Config("default_" + LocalExporter.TYPE, globalSettings, Settings.EMPTY);
exporters.put(config.name(), factories.get(LocalExporter.TYPE).create(config));
}

View File

@ -223,15 +223,14 @@ public class HttpExporter extends Exporter {
out.write(CONTENT_TYPE.xContent().streamSeparator());
if (logger.isTraceEnabled()) {
logger.trace("http exporter [{}] - added index request [index={}, type={}, id={}]",
name(), index, type, id);
logger.trace("added index request [index={}, type={}, id={}]", index, type, id);
}
} else if (logger.isTraceEnabled()) {
logger.trace("http exporter [{}] - no resolver found for monitoring document [class={}, id={}, version={}]",
name(), doc.getClass().getName(), doc.getMonitoringId(), doc.getMonitoringVersion());
logger.trace("no resolver found for monitoring document [class={}, id={}, version={}]",
doc.getClass().getName(), doc.getMonitoringId(), doc.getMonitoringVersion());
}
} catch (Exception e) {
logger.warn("http exporter [{}] - failed to render document [{}], skipping it", e, name(), doc);
logger.warn("failed to render document [{}], skipping it", e, doc);
}
}
@ -426,7 +425,7 @@ public class HttpExporter extends Exporter {
// Works like LocalExporter on master:
// Install the index template for timestamped indices first, so that other nodes can ship data
if (!templateInstalled) {
logger.debug("http exporter [{}] - could not find existing monitoring template, installing a new one", name());
logger.debug("could not find existing monitoring template, installing a new one");
if (!putTemplate(host, templateName, MarvelTemplateUtils.loadTimestampedIndexTemplate())) {
return false;
}
@ -435,7 +434,7 @@ public class HttpExporter extends Exporter {
// Install the index template for data index
templateName = MarvelTemplateUtils.dataTemplateName(templateVersion);
if (!hasTemplate(templateName, host)) {
logger.debug("http exporter [{}] - could not find existing monitoring template for data index, installing a new one", name());
logger.debug("could not find existing monitoring template for data index, installing a new one");
if (!putTemplate(host, templateName, MarvelTemplateUtils.loadDataIndexTemplate())) {
return false;
}
@ -451,8 +450,7 @@ public class HttpExporter extends Exporter {
HttpURLConnection connection = null;
try {
logger.debug("http exporter [{}] - checking if monitoring template [{}] exists on the monitoring cluster",
name(), templateName);
logger.debug("checking if monitoring template [{}] exists on the monitoring cluster", templateName);
connection = openConnection(host, "GET", url, null);
if (connection == null) {
throw new IOException("no available connection to check for monitoring template [" + templateName + "] existence");
@ -464,8 +462,7 @@ public class HttpExporter extends Exporter {
return true;
}
} catch (Exception e) {
logger.error("http exporter [{}] - failed to verify the monitoring template [{}] on [{}]:\n{}", name(), templateName, host,
e.getMessage());
logger.error("failed to verify the monitoring template [{}] on [{}]:\n{}", templateName, host, e.getMessage());
return false;
} finally {
if (connection != null) {
@ -480,12 +477,12 @@ public class HttpExporter extends Exporter {
}
boolean putTemplate(String host, String template, byte[] source) {
logger.debug("http exporter [{}] - installing template [{}]", name(), template);
logger.debug("installing template [{}]", template);
HttpURLConnection connection = null;
try {
connection = openConnection(host, "PUT", "_template/" + template, XContentType.JSON.mediaType());
if (connection == null) {
logger.debug("http exporter [{}] - no available connection to update monitoring template [{}]", name(), template);
logger.debug("no available connection to update monitoring template [{}]", template);
return false;
}
@ -496,11 +493,10 @@ public class HttpExporter extends Exporter {
return false;
}
logger.info("http exporter [{}] - monitoring template [{}] updated to version [{}]", name(), template, templateVersion);
logger.info("monitoring template [{}] updated to version [{}]", template, templateVersion);
return true;
} catch (IOException e) {
logger.error("http exporter [{}] - failed to update monitoring template [{}] on host [{}]:\n{}", name(), template, host,
e.getMessage());
logger.error("failed to update monitoring template [{}] on host [{}]:\n{}", template, host, e.getMessage());
return false;
} finally {
if (connection != null) {

View File

@ -99,7 +99,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
@Override
public void doClose() {
if (state.getAndSet(State.TERMINATED) != State.TERMINATED) {
logger.debug("local exporter [{}] - stopped", name());
logger.debug("stopped");
clusterService.remove(this);
cleanerService.remove(this);
}
@ -113,7 +113,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
if (clusterState.blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)) {
// wait until the gateway has recovered from disk, otherwise we think may not have .monitoring-es-
// indices but they may not have been restored from the cluster state on disk
logger.debug("local exporter [{}] - waiting until gateway has recovered from disk", name());
logger.debug("waiting until gateway has recovered from disk");
return null;
}
@ -127,11 +127,11 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
// We only need to check the index template for timestamped indices
if (templateInstalled == false) {
// the template for timestamped indices is not yet installed in the given cluster state, we'll wait.
logger.debug("local exporter [{}] - monitoring index template does not exist, so service cannot start", name());
logger.debug("monitoring index template does not exist, so service cannot start");
return null;
}
logger.debug("local exporter [{}] - monitoring index template found, service can start", name());
logger.debug("monitoring index template found, service can start");
} else {
@ -139,14 +139,13 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
//
// Check that there is nothing that could block metadata updates
if (clusterState.blocks().hasGlobalBlock(ClusterBlockLevel.METADATA_WRITE)) {
logger.debug("local exporter [{}] - waiting until metadata writes are unblocked", name());
logger.debug("waiting until metadata writes are unblocked");
return null;
}
// Install the index template for timestamped indices first, so that other nodes can ship data
if (templateInstalled == false) {
logger.debug("local exporter [{}] - could not find existing monitoring template for timestamped indices, " +
"installing a new one", name());
logger.debug("could not find existing monitoring template for timestamped indices, installing a new one");
putTemplate(templateName, MarvelTemplateUtils.loadTimestampedIndexTemplate());
// we'll get that template on the next cluster state update
return null;
@ -155,18 +154,17 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
// Install the index template for data index
templateName = MarvelTemplateUtils.dataTemplateName(templateVersion);
if (hasTemplate(templateName, clusterState) == false) {
logger.debug("local exporter [{}] - could not find existing monitoring template for data index, " +
"installing a new one", name());
logger.debug("could not find existing monitoring template for data index, installing a new one");
putTemplate(templateName, MarvelTemplateUtils.loadDataIndexTemplate());
// we'll get that template on the next cluster state update
return null;
}
logger.debug("local exporter [{}] - monitoring index template found on master node, service can start", name());
logger.debug("monitoring index template found on master node, service can start");
}
if (state.compareAndSet(State.INITIALIZED, State.RUNNING)) {
logger.debug("local exporter [{}] - started!", name());
logger.debug("started");
}
return new LocalBulk(name(), logger, client, resolvers);
}
@ -185,7 +183,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
try {
Integer version = Integer.parseInt(template.value.substring(templatePattern.length() - 1));
templates.put(template.value, version);
logger.debug("found index template [{}] in version [{}]", template, version);
logger.debug("found index template [{}] in version [{}]", template.value, version);
} catch (NumberFormatException e) {
logger.warn("cannot extract version number for template [{}]", template.value);
}
@ -200,7 +198,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
}
void putTemplate(String template, byte[] source) {
logger.debug("local exporter [{}] - installing template [{}]", name(), template);
logger.debug("installing template [{}]",template);
PutIndexTemplateRequest request = new PutIndexTemplateRequest(template).source(source);
assert !Thread.currentThread().isInterrupted() : "current thread has been interrupted before putting index template!!!";
@ -210,15 +208,15 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
@Override
public void onResponse(PutIndexTemplateResponse response) {
if (response.isAcknowledged()) {
logger.trace("local exporter [{}] - successfully installed monitoring template [{}]", name(), template);
logger.trace("successfully installed monitoring template [{}]", template);
} else {
logger.error("local exporter [{}] - failed to update monitoring index template [{}]", name(), template);
logger.error("failed to update monitoring index template [{}]", template);
}
}
@Override
public void onFailure(Throwable throwable) {
logger.error("local exporter [{}] - failed to update monitoring index template [{}]", throwable, name(), template);
logger.error("failed to update monitoring index template [{}]", throwable, template);
}
});
}
@ -226,7 +224,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
@Override
public void onCleanUpIndices(TimeValue retention) {
if (state.get() != State.RUNNING) {
logger.debug("local exporter [{}] - not ready yet", name());
logger.debug("exporter not ready");
return;
}
@ -234,7 +232,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
// Reference date time will be compared to index.creation_date settings,
// that's why it must be in UTC
DateTime expiration = new DateTime(DateTimeZone.UTC).minus(retention.millis());
logger.debug("local exporter [{}] - cleaning indices [expiration={}, retention={}]", name(), expiration, retention);
logger.debug("cleaning indices [expiration={}, retention={}]", expiration, retention);
ClusterState clusterState = clusterService.state();
if (clusterState != null) {
@ -268,7 +266,7 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
long creationDate = index.value.getCreationDate();
if (creationDate <= expirationTime) {
if (logger.isDebugEnabled()) {
logger.debug("local exporter [{}] - detected expired index [name={}, created={}, expired={}]", name(),
logger.debug("detected expired index [name={}, created={}, expired={}]",
indexName, new DateTime(creationDate, DateTimeZone.UTC), expiration);
}
indices.add(indexName);
@ -277,31 +275,33 @@ public class LocalExporter extends Exporter implements ClusterStateListener, Cle
}
if (!indices.isEmpty()) {
logger.info("local exporter [{}] - cleaning up [{}] old indices", name(), indices.size());
logger.info("cleaning up [{}] old indices", indices.size());
deleteIndices(indices);
} else {
logger.debug("local exporter [{}] - no old indices found for clean up", name());
logger.debug("no old indices found for clean up");
}
}
}
}
private void deleteIndices(Set<String> indices) {
logger.trace("local exporter [{}] - deleting {} indices: {}", name(), indices.size(), collectionToCommaDelimitedString(indices));
logger.trace("deleting {} indices: [{}]", indices.size(), collectionToCommaDelimitedString(indices));
client.admin().indices().delete(new DeleteIndexRequest(indices.toArray(new String[indices.size()])),
new ActionListener<DeleteIndexResponse>() {
@Override
public void onResponse(DeleteIndexResponse response) {
if (response.isAcknowledged()) {
logger.debug("local exporter [{}] - indices deleted", name());
logger.debug("{} indices deleted", indices.size());
} else {
logger.warn("local exporter [{}] - unable to delete {} indices", name(), indices.size());
// Probably means that the delete request has timed out,
// the indices will survive until the next clean up.
logger.warn("deletion of {} indices wasn't acknowledged", indices.size());
}
}
@Override
public void onFailure(Throwable e) {
logger.error("local exporter [{}] - failed to delete indices", e, name());
logger.error("failed to delete indices", e);
}
});
}