com.orientechnologies.agent.profiler.source.CSVAggregateReporter Maven / Gradle / Ivy
package com.orientechnologies.agent.profiler.source;
import com.codahale.metrics.*;
import com.opencsv.CSVWriter;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.enterprise.server.OEnterpriseServer;
import com.orientechnologies.orient.server.OClientConnectionStats;
import com.orientechnologies.orient.server.network.protocol.ONetworkProtocolData;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** Created by Enrico Risa on 25/10/2018. */
public class CSVAggregateReporter {
private static final String DEFAULT_SEPARATOR = ",";
private final OEnterpriseServer server;
private final MetricRegistry registry;
private final File directory;
private final Locale locale;
private final String separator;
private final TimeUnit rateUnit;
private final TimeUnit durationUnit;
private final Clock clock;
private final MetricFilter filter;
private final ScheduledExecutorService executor;
private final boolean shutdownExecutorOnStop;
private final CsvFileProvider csvFileProvider;
private final Callable callback;
private ScheduledFuture> scheduledFuture;
public static CSVAggregateReporter.Builder forRegistry(
OEnterpriseServer server, MetricRegistry registry) {
return new CSVAggregateReporter.Builder(server, registry);
}
public static class Builder {
private final MetricRegistry registry;
private final OEnterpriseServer server;
private Locale locale;
private String separator;
private TimeUnit rateUnit;
private TimeUnit durationUnit;
private Clock clock;
private MetricFilter filter;
private ScheduledExecutorService executor;
private boolean shutdownExecutorOnStop;
private CsvFileProvider csvFileProvider;
private Callable callback;
public Builder(OEnterpriseServer server, MetricRegistry registry) {
this.registry = registry;
this.server = server;
this.locale = Locale.getDefault();
this.separator = DEFAULT_SEPARATOR;
this.rateUnit = TimeUnit.SECONDS;
this.durationUnit = TimeUnit.MILLISECONDS;
this.clock = Clock.defaultClock();
this.filter = MetricFilter.ALL;
this.executor = Executors.newSingleThreadScheduledExecutor();
this.shutdownExecutorOnStop = true;
this.csvFileProvider = new FixedNameCsvFileProvider();
}
public Builder withCallback(Callable callback) {
this.callback = callback;
return this;
}
/**
* Builds a {@link CsvReporter} with the given properties, writing {@code .csv} files to the
* given directory.
*
* @param directory the directory in which the {@code .csv} files will be created
* @return a {@link CsvReporter}
*/
public CSVAggregateReporter build(File directory) {
return new CSVAggregateReporter(
server,
registry,
directory,
locale,
separator,
rateUnit,
durationUnit,
clock,
filter,
executor,
shutdownExecutorOnStop,
csvFileProvider,
callback);
}
}
private CSVAggregateReporter(
OEnterpriseServer server,
MetricRegistry registry,
File directory,
Locale locale,
String separator,
TimeUnit rateUnit,
TimeUnit durationUnit,
Clock clock,
MetricFilter filter,
ScheduledExecutorService executor,
boolean shutdownExecutorOnStop,
CsvFileProvider csvFileProvider,
Callable callback) {
this.server = server;
this.registry = registry;
this.directory = directory;
this.locale = locale;
this.separator = separator;
this.rateUnit = rateUnit;
this.durationUnit = durationUnit;
this.clock = clock;
this.filter = filter;
this.executor = executor;
this.shutdownExecutorOnStop = shutdownExecutorOnStop;
this.csvFileProvider = csvFileProvider;
this.callback = callback;
}
public void start(long period, TimeUnit unit) {
if (this.scheduledFuture != null) {
if (this.scheduledFuture != null) {
throw new IllegalArgumentException("Reporter already started");
}
}
this.scheduledFuture =
executor.scheduleAtFixedRate(
() -> {
report();
},
period,
period,
unit);
}
public void report() {
Pattern p = Pattern.compile("(?s)db.*.query.*");
SortedMap histograms =
registry.getHistograms((name, metric) -> p.matcher(name).matches());
final long timestamp = TimeUnit.MILLISECONDS.toSeconds(clock.getTime());
List> collected = getQueryStats(histograms);
report(
timestamp,
"db.queries",
"database,language,query,count,min(millis),mean(millis),max(millis)",
collected);
List> runningQueries = getRunningQueries();
report(
timestamp,
"db.runningQueries",
"queryId,sessionId,database,user,language,query,startTime,elapsedTime(millis)",
runningQueries);
List> stats = getConnections();
report(
timestamp,
"server.network.activeSessions",
"connectionId,remoteAddress,database,user,totalRequests,commandInfo,commandDetail,lastCommandOn,lastCommandInfo,"
+ "lastCommandDetail,lastExecutionTime,totalWorkingTime,activeQueries,connectedOn,protocol,sessionId,clientId,driver",
stats);
if (callback != null) {
try {
callback.call();
} catch (Exception e) {
// Ignore
}
}
}
private List> getQueryStats(SortedMap histograms) {
return histograms.entrySet().stream()
.sorted(
(v1, v2) -> {
Snapshot snapshot1 = v1.getValue().getSnapshot();
Snapshot snapshot2 = v2.getValue().getSnapshot();
return Double.compare(snapshot2.getMean(), snapshot1.getMean());
})
.map(
(e) -> {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy