Search in sources :

Example 1 with MetricsSnapshotInterface

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;
    }
}
Also used : MetricsSnapshotInterface(org.pentaho.di.core.metrics.MetricsSnapshotInterface) MetricsSnapshot(org.pentaho.di.core.metrics.MetricsSnapshot) Date(java.util.Date)

Example 2 with MetricsSnapshotInterface

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);
}
Also used : MetricsSnapshotInterface(org.pentaho.di.core.metrics.MetricsSnapshotInterface) TransMeta(org.pentaho.di.trans.TransMeta) Trans(org.pentaho.di.trans.Trans) Test(org.junit.Test)

Example 3 with MetricsSnapshotInterface

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();
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) MetricsLogTable(org.pentaho.di.core.logging.MetricsLogTable) MetricsSnapshotInterface(org.pentaho.di.core.metrics.MetricsSnapshotInterface) ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleTransException(org.pentaho.di.core.exception.KettleTransException) DuplicateParamException(org.pentaho.di.core.parameters.DuplicateParamException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) LoggingMetric(org.pentaho.di.core.logging.LoggingMetric) Database(org.pentaho.di.core.database.Database) MetricsDuration(org.pentaho.di.core.metrics.MetricsDuration)

Example 4 with MetricsSnapshotInterface

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;
    }
}
Also used : RowMetaAndData(org.pentaho.di.core.RowMetaAndData) MetricsSnapshotInterface(org.pentaho.di.core.metrics.MetricsSnapshotInterface) Date(java.util.Date)

Example 5 with MetricsSnapshotInterface

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);
}
Also used : MetricsSnapshotInterface(org.pentaho.di.core.metrics.MetricsSnapshotInterface) MetricsDuration(org.pentaho.di.core.metrics.MetricsDuration) Test(org.junit.Test)

Aggregations

MetricsSnapshotInterface (org.pentaho.di.core.metrics.MetricsSnapshotInterface)6 Test (org.junit.Test)3 MetricsDuration (org.pentaho.di.core.metrics.MetricsDuration)3 Date (java.util.Date)2 Trans (org.pentaho.di.trans.Trans)2 TransMeta (org.pentaho.di.trans.TransMeta)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)1 Database (org.pentaho.di.core.database.Database)1 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)1 KettleException (org.pentaho.di.core.exception.KettleException)1 KettleFileException (org.pentaho.di.core.exception.KettleFileException)1 KettleTransException (org.pentaho.di.core.exception.KettleTransException)1 KettleValueException (org.pentaho.di.core.exception.KettleValueException)1 LoggingMetric (org.pentaho.di.core.logging.LoggingMetric)1 MetricsLogTable (org.pentaho.di.core.logging.MetricsLogTable)1 MetricsSnapshot (org.pentaho.di.core.metrics.MetricsSnapshot)1 DuplicateParamException (org.pentaho.di.core.parameters.DuplicateParamException)1 UnknownParamException (org.pentaho.di.core.parameters.UnknownParamException)1 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)1