use of org.pentaho.di.core.metrics.MetricsSnapshotInterface in project pentaho-kettle by pentaho.
the class LogChannel method snap.
@Override
public void snap(MetricsInterface metric, String subject, long... value) {
if (!isGatheringMetrics()) {
return;
}
String key = MetricsSnapshot.getKey(metric, subject);
Map<String, MetricsSnapshotInterface> metricsMap = null;
MetricsSnapshotInterface snapshot = null;
Queue<MetricsSnapshotInterface> metricsList = null;
switch(metric.getType()) {
case MAX:
//
if (value.length != 1) {
// ignore
break;
}
metricsMap = metricsRegistry.getSnapshotMap(logChannelId);
snapshot = metricsMap.get(key);
if (snapshot != null) {
if (value[0] > snapshot.getValue()) {
snapshot.setValue(value[0]);
snapshot.setDate(new Date());
}
} else {
snapshot = new MetricsSnapshot(MetricsSnapshotType.MAX, metric, subject, value[0], logChannelId);
metricsMap.put(key, snapshot);
}
break;
case MIN:
//
if (value.length != 1) {
// ignore
break;
}
metricsMap = metricsRegistry.getSnapshotMap(logChannelId);
snapshot = metricsMap.get(key);
if (snapshot != null) {
if (value[0] < snapshot.getValue()) {
snapshot.setValue(value[0]);
snapshot.setDate(new Date());
}
} else {
snapshot = new MetricsSnapshot(MetricsSnapshotType.MIN, metric, subject, value[0], logChannelId);
metricsMap.put(key, snapshot);
}
break;
case SUM:
metricsMap = metricsRegistry.getSnapshotMap(logChannelId);
snapshot = metricsMap.get(key);
if (snapshot != null) {
snapshot.setValue(snapshot.getValue() + value[0]);
} else {
snapshot = new MetricsSnapshot(MetricsSnapshotType.SUM, metric, subject, value[0], logChannelId);
metricsMap.put(key, snapshot);
}
break;
case COUNT:
metricsMap = metricsRegistry.getSnapshotMap(logChannelId);
snapshot = metricsMap.get(key);
if (snapshot != null) {
snapshot.setValue(snapshot.getValue() + 1L);
} else {
snapshot = new MetricsSnapshot(MetricsSnapshotType.COUNT, metric, subject, 1L, logChannelId);
metricsMap.put(key, snapshot);
}
break;
case START:
metricsList = metricsRegistry.getSnapshotList(logChannelId);
snapshot = new MetricsSnapshot(MetricsSnapshotType.START, metric, subject, 1L, logChannelId);
metricsList.add(snapshot);
break;
case STOP:
metricsList = metricsRegistry.getSnapshotList(logChannelId);
snapshot = new MetricsSnapshot(MetricsSnapshotType.STOP, metric, subject, 1L, logChannelId);
metricsList.add(snapshot);
break;
default:
break;
}
}
use of org.pentaho.di.core.metrics.MetricsSnapshotInterface in project pentaho-kettle by pentaho.
the class MetricsIT method testDatabaseGetRow.
@Test
public void testDatabaseGetRow() throws Exception {
MetricsRegistry metricsRegistry = MetricsRegistry.getInstance();
TransMeta insertTransMeta = new TransMeta("src/it/resources/metrics/insert-data.ktr");
Trans insertTrans = new Trans(insertTransMeta);
insertTrans.setGatheringMetrics(true);
insertTrans.execute(null);
insertTrans.waitUntilFinished();
LogChannelInterface log = insertTrans.getLogChannel();
Queue<MetricsSnapshotInterface> snapshotList = metricsRegistry.getSnapshotList(log.getLogChannelId());
assertTrue(snapshotList.size() >= 4);
TransMeta readTransMeta = new TransMeta("src/it/resources/metrics/read-data.ktr");
Trans readTrans = new Trans(readTransMeta);
readTrans.setGatheringMetrics(true);
readTrans.execute(null);
readTrans.waitUntilFinished();
log = readTrans.getLogChannel();
snapshotList = metricsRegistry.getSnapshotList(log.getLogChannelId());
assertTrue(snapshotList.size() >= 4);
Long rowCount = MetricsUtil.getResult(Metrics.METRIC_DATABASE_GET_ROW_COUNT);
assertNotNull(rowCount);
assertEquals(Long.valueOf(1001), rowCount);
Long sumTime = MetricsUtil.getResult(Metrics.METRIC_DATABASE_GET_ROW_SUM_TIME);
assertNotNull(sumTime);
assertTrue(sumTime > 0);
Long minTime = MetricsUtil.getResult(Metrics.METRIC_DATABASE_GET_ROW_MIN_TIME);
assertNotNull(minTime);
assertTrue(minTime < sumTime);
Long maxTime = MetricsUtil.getResult(Metrics.METRIC_DATABASE_GET_ROW_MAX_TIME);
assertNotNull(maxTime);
assertTrue(maxTime >= minTime);
}
use of org.pentaho.di.core.metrics.MetricsSnapshotInterface in project pentaho-kettle by pentaho.
the class Trans method writeMetricsInformation.
protected synchronized void writeMetricsInformation() throws KettleException {
//
List<MetricsDuration> metricsList = MetricsUtil.getDuration(log.getLogChannelId(), Metrics.METRIC_PLUGIN_REGISTRY_REGISTER_EXTENSIONS_START);
if ((log != null) && (log.isDebug()) && !metricsList.isEmpty()) {
log.logDebug(metricsList.get(0).toString());
}
metricsList = MetricsUtil.getDuration(log.getLogChannelId(), Metrics.METRIC_PLUGIN_REGISTRY_PLUGIN_REGISTRATION_START);
if ((log != null) && (log.isDebug()) && !metricsList.isEmpty()) {
log.logDebug(metricsList.get(0).toString());
}
long total = 0;
metricsList = MetricsUtil.getDuration(log.getLogChannelId(), Metrics.METRIC_PLUGIN_REGISTRY_PLUGIN_TYPE_REGISTRATION_START);
if ((log != null) && (log.isDebug()) && metricsList != null && !metricsList.isEmpty()) {
for (MetricsDuration duration : metricsList) {
total += duration.getDuration();
log.logDebug(" - " + duration.toString() + " Total=" + total);
}
}
Database db = null;
MetricsLogTable metricsLogTable = transMeta.getMetricsLogTable();
try {
db = new Database(this, metricsLogTable.getDatabaseMeta());
db.shareVariablesWith(this);
db.connect();
db.setCommit(logCommitSize);
List<String> logChannelIds = LoggingRegistry.getInstance().getLogChannelChildren(getLogChannelId());
for (String logChannelId : logChannelIds) {
Queue<MetricsSnapshotInterface> snapshotList = MetricsRegistry.getInstance().getSnapshotLists().get(logChannelId);
if (snapshotList != null) {
Iterator<MetricsSnapshotInterface> iterator = snapshotList.iterator();
while (iterator.hasNext()) {
MetricsSnapshotInterface snapshot = iterator.next();
db.writeLogRecord(metricsLogTable, LogStatus.START, new LoggingMetric(batchId, snapshot), null);
}
}
Map<String, MetricsSnapshotInterface> snapshotMap = MetricsRegistry.getInstance().getSnapshotMaps().get(logChannelId);
if (snapshotMap != null) {
synchronized (snapshotMap) {
Iterator<MetricsSnapshotInterface> iterator = snapshotMap.values().iterator();
while (iterator.hasNext()) {
MetricsSnapshotInterface snapshot = iterator.next();
db.writeLogRecord(metricsLogTable, LogStatus.START, new LoggingMetric(batchId, snapshot), null);
}
}
}
}
// Also time-out the log records in here...
//
db.cleanupLogRecords(metricsLogTable);
} catch (Exception e) {
throw new KettleException(BaseMessages.getString(PKG, "Trans.Exception.UnableToWriteMetricsInformationToLogTable"), e);
} finally {
if (!db.isAutoCommit()) {
db.commit(true);
}
db.disconnect();
}
}
use of org.pentaho.di.core.metrics.MetricsSnapshotInterface in project pentaho-kettle by pentaho.
the class MetricsLogTable method getLogRecord.
/**
* This method calculates all the values that are required
*
* @param id
* the id to use or -1 if no id is needed
* @param status
* the log status to use
*/
public RowMetaAndData getLogRecord(LogStatus status, Object subject, Object parent) {
if (subject == null || subject instanceof LoggingMetric) {
LoggingMetric loggingMetric = (LoggingMetric) subject;
MetricsSnapshotInterface snapshot = null;
if (subject != null) {
snapshot = loggingMetric.getSnapshot();
}
RowMetaAndData row = new RowMetaAndData();
for (LogTableField field : fields) {
if (field.isEnabled()) {
Object value = null;
if (subject != null) {
switch(ID.valueOf(field.getId())) {
case ID_BATCH:
value = new Long(loggingMetric.getBatchId());
break;
case CHANNEL_ID:
value = snapshot.getLogChannelId();
break;
case LOG_DATE:
value = new Date();
break;
case METRICS_DATE:
value = snapshot.getDate();
break;
case METRICS_CODE:
value = snapshot.getMetric().getCode();
break;
case METRICS_DESCRIPTION:
value = snapshot.getMetric().getDescription();
break;
case METRICS_SUBJECT:
value = snapshot.getSubject();
break;
case METRICS_TYPE:
value = snapshot.getMetric().getType().name();
break;
case METRICS_VALUE:
value = snapshot.getValue();
break;
default:
break;
}
}
row.addValue(field.getFieldName(), field.getDataType(), value);
row.getRowMeta().getValueMeta(row.size() - 1).setLength(field.getLength());
}
}
return row;
} else {
return null;
}
}
use of org.pentaho.di.core.metrics.MetricsSnapshotInterface in project pentaho-kettle by pentaho.
the class MetricsIT method testBasics.
@Test
public void testBasics() throws Exception {
LogChannel log = new LogChannel("BASICS");
log.setGatheringMetrics(true);
log.snap(METRIC_START);
Thread.sleep(50);
log.snap(METRIC_STOP);
Queue<MetricsSnapshotInterface> snapshotList = MetricsRegistry.getInstance().getSnapshotList(log.getLogChannelId());
assertEquals(2, snapshotList.size());
List<MetricsDuration> durationList = MetricsUtil.getDuration(log.getLogChannelId(), METRIC_START);
assertEquals(1, durationList.size());
MetricsDuration duration = durationList.get(0);
assertTrue(duration.getDuration() >= 50 && duration.getDuration() <= 100);
}
Aggregations