Java Code Examples for com.codahale.metrics.Timer#getSnapshot()

The following examples show how to use com.codahale.metrics.Timer#getSnapshot() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Helper.java    From vertx-dropwizard-metrics with Apache License 2.0 6 votes vote down vote up
private static JsonObject toJson(Timer timer, TimeUnit rateUnit, TimeUnit durationUnit) {
  Snapshot snapshot = timer.getSnapshot();
  JsonObject json = new JsonObject();

  json.put("type", "timer");

  if (timer instanceof ThroughputTimer) {
    ThroughputTimer throughput = (ThroughputTimer) timer;
    json.put("oneSecondRate", throughput.getValue());
  }

  // Meter
  populateMetered(json, timer, rateUnit);

  // Snapshot
  double factor = 1.0 / durationUnit.toNanos(1);
  populateSnapshot(json, snapshot, factor);

  // Duration rate
  String duration = durationUnit.toString().toLowerCase();
  json.put("durationRate", duration);

  return json;
}
 
Example 2
Source File: NakadiMetricsModule.java    From nakadi with MIT License 6 votes vote down vote up
@Override
public void serialize(final Timer timer,
                      final JsonGenerator json,
                      final SerializerProvider provider) throws IOException {
    json.writeStartObject();
    final Snapshot snapshot = timer.getSnapshot();
    json.writeNumberField("count", timer.getCount());
    json.writeNumberField("mean", snapshot.getMean() * durationFactor);

    if (showSamples) {
        final long[] values = snapshot.getValues();
        final double[] scaledValues = new double[values.length];
        for (int i = 0; i < values.length; i++) {
            scaledValues[i] = values[i] * durationFactor;
        }
        json.writeObjectField("values", scaledValues);
    }

    json.writeNumberField("m1_rate", timer.getOneMinuteRate() * rateFactor);
    json.writeStringField("duration_units", durationUnit);
    json.writeStringField("rate_units", rateUnit);
    json.writeEndObject();
}
 
Example 3
Source File: CloudWatchReporter.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 6 votes vote down vote up
/**
 * The {@link Snapshot} values of {@link Timer} are reported as {@link StatisticSet} after conversion. The
 * conversion is done using the duration factor, which is deduced from the set duration unit.
 * <p>
 * Please note, the reported values submitted only if they show some data (greater than zero) in order to:
 * <p>
 * 1. save some money
 * 2. prevent com.amazonaws.services.cloudwatch.model.InvalidParameterValueException if empty {@link Snapshot}
 * is submitted
 * <p>
 * If {@link Builder#withZeroValuesSubmission()} is {@code true}, then all values will be submitted
 *
 * @see Timer#getSnapshot
 * @see #getDurationUnit
 * @see #convertDuration(double)
 */
private void processTimer(final String metricName, final Timer timer, final List<MetricDatum> metricData) {
    final Snapshot snapshot = timer.getSnapshot();

    if (builder.withZeroValuesSubmission || snapshot.size() > 0) {
        for (final Percentile percentile : builder.percentiles) {
            final double convertedDuration = convertDuration(snapshot.getValue(percentile.getQuantile()));
            stageMetricDatum(true, metricName, convertedDuration, durationUnit, percentile.getDesc(), metricData);
        }
    }

    // prevent empty snapshot from causing InvalidParameterValueException
    if (snapshot.size() > 0) {
        final String formattedDuration = String.format(" [in-%s]", getDurationUnit());
        stageMetricDatum(builder.withArithmeticMean, metricName, convertDuration(snapshot.getMean()), durationUnit, DIMENSION_SNAPSHOT_MEAN + formattedDuration, metricData);
        stageMetricDatum(builder.withStdDev, metricName, convertDuration(snapshot.getStdDev()), durationUnit, DIMENSION_SNAPSHOT_STD_DEV + formattedDuration, metricData);
        stageMetricDatumWithConvertedSnapshot(builder.withStatisticSet, metricName, snapshot, durationUnit, metricData);
    }
}
 
Example 4
Source File: SiddhiStreamReporter.java    From Decision with Apache License 2.0 6 votes vote down vote up
private void reportTimer(long timestamp, String name, Timer timer) {
    final Snapshot snapshot = timer.getSnapshot();
    report(TIMER_STREAM_NAME,
            TIMER_PROPERTIES,
            name,
            timestamp,
            timer.getCount(),
            convertDuration(snapshot.getMax()),
            convertDuration(snapshot.getMean()),
            convertDuration(snapshot.getMin()),
            convertDuration(snapshot.getStdDev()),
            convertDuration(snapshot.getMedian()),
            convertDuration(snapshot.get75thPercentile()),
            convertDuration(snapshot.get95thPercentile()),
            convertDuration(snapshot.get98thPercentile()),
            convertDuration(snapshot.get99thPercentile()),
            convertDuration(snapshot.get999thPercentile()),
            convertRate(timer.getMeanRate()),
            convertRate(timer.getOneMinuteRate()),
            convertRate(timer.getFiveMinuteRate()),
            convertRate(timer.getFifteenMinuteRate()),
            getRateUnit(),
            getDurationUnit());
}
 
Example 5
Source File: MetricsUtils.java    From hermes with Apache License 2.0 6 votes vote down vote up
public static String printTimer(String name, Timer timer) {
	StringBuilder sb = new StringBuilder();
	final Snapshot snapshot = timer.getSnapshot();
	sb.append(String.format("              name = %s%s", name));
	sb.append(String.format("             count = %d%n", timer.getCount()));
	sb.append(String.format("         mean rate = %2.2f calls/%s%n", timer.getMeanRate(), "s"));
	sb.append(String.format("     1-minute rate = %2.2f calls/%s%n", timer.getOneMinuteRate(), "s"));
	sb.append(String.format("     5-minute rate = %2.2f calls/%s%n", timer.getFiveMinuteRate(), "s"));
	sb.append(String.format("    15-minute rate = %2.2f calls/%s%n", timer.getFifteenMinuteRate(), "s"));

	sb.append(String.format("               min = %2.2f %s%n", snapshot.getMin(), "s"));
	sb.append(String.format("               max = %2.2f %s%n", snapshot.getMax(), "s"));
	sb.append(String.format("              mean = %2.2f %s%n", snapshot.getMean(), "s"));
	sb.append(String.format("            stddev = %2.2f %s%n", snapshot.getStdDev(), "s"));
	sb.append(String.format("            median = %2.2f %s%n", snapshot.getMedian(), "s"));
	sb.append(String.format("              75%% <= %2.2f %s%n", snapshot.get75thPercentile(), "s"));
	sb.append(String.format("              95%% <= %2.2f %s%n", snapshot.get95thPercentile(), "s"));
	sb.append(String.format("              98%% <= %2.2f %s%n", snapshot.get98thPercentile(), "s"));
	sb.append(String.format("              99%% <= %2.2f %s%n", snapshot.get99thPercentile(), "s"));
	sb.append(String.format("            99.9%% <= %2.2f %s%n", snapshot.get999thPercentile(), "s"));
	return sb.toString();
}
 
Example 6
Source File: MqMetricReporter.java    From pmq with Apache License 2.0 6 votes vote down vote up
private Set<Metric> buildTimers(String name, Timer timer, long timestamp, Map<String, String> tags) {
    final MetricsCollector collector = MetricsCollector.createNew(name, tags, timestamp);
    final Snapshot snapshot = timer.getSnapshot();
    if (getChangeCount(name, timer.getCount()) == 0) {
        return Collections.emptySet();
    }
    return collector.addMetric("count", timer.getCount())
            //convert rate
            .addMetric("m15", convertRate(timer.getFifteenMinuteRate()))
            .addMetric("m5", convertRate(timer.getFiveMinuteRate()))
            .addMetric("m1", convertRate(timer.getOneMinuteRate()))
            .addMetric("mean_rate", convertRate(timer.getMeanRate()))
            // convert duration
            .addMetric("max", convertDuration(snapshot.getMax()))
            .addMetric("min", convertDuration(snapshot.getMin()))
            .addMetric("mean", convertDuration(snapshot.getMean()))
            .addMetric("stddev", convertDuration(snapshot.getStdDev()))
            .addMetric("median", convertDuration(snapshot.getMedian()))
            .addMetric("p75", convertDuration(snapshot.get75thPercentile()))
            .addMetric("p95", convertDuration(snapshot.get95thPercentile()))
            .addMetric("p98", convertDuration(snapshot.get98thPercentile()))
            .addMetric("p99", convertDuration(snapshot.get99thPercentile()))
            .addMetric("p999", convertDuration(snapshot.get999thPercentile()))
            .build();
}
 
Example 7
Source File: MqMetricReporter.java    From pmq with Apache License 2.0 6 votes vote down vote up
private Set<Metric> buildTimers(String name, Timer timer, long timestamp, Map<String, String> tags) {
	final MetricsCollector collector = MetricsCollector.createNew(name, tags, timestamp);
	final Snapshot snapshot = timer.getSnapshot();
	if (getChangeCount(name, timer.getCount()) == 0) {
		return Collections.emptySet();
	}
	return collector.addMetric("count", timer.getCount())
			// convert rate
			.addMetric("m15", convertRate(timer.getFifteenMinuteRate()))
			.addMetric("m5", convertRate(timer.getFiveMinuteRate()))
			.addMetric("m1", convertRate(timer.getOneMinuteRate()))
			.addMetric("mean_rate", convertRate(timer.getMeanRate()))
			// convert duration
			.addMetric("max", convertDuration(snapshot.getMax()))
			.addMetric("min", convertDuration(snapshot.getMin()))
			.addMetric("mean", convertDuration(snapshot.getMean()))
			.addMetric("stddev", convertDuration(snapshot.getStdDev()))
			.addMetric("median", convertDuration(snapshot.getMedian()))
			.addMetric("p75", convertDuration(snapshot.get75thPercentile()))
			.addMetric("p95", convertDuration(snapshot.get95thPercentile()))
			.addMetric("p98", convertDuration(snapshot.get98thPercentile()))
			.addMetric("p99", convertDuration(snapshot.get99thPercentile()))
			.addMetric("p999", convertDuration(snapshot.get999thPercentile())).build();
}
 
Example 8
Source File: OpenTsdbReporter.java    From metrics-opentsdb with Apache License 2.0 6 votes vote down vote up
private Set<OpenTsdbMetric> buildTimers(String name, Timer timer, long timestamp, Map<String, String> tags) {
    final MetricsCollector collector = MetricsCollector.createNew(prefix(name), tags, timestamp);
    final Snapshot snapshot = timer.getSnapshot();

    return collector.addMetric("count", timer.getCount())
            //convert rate
            .addMetric("m15", convertRate(timer.getFifteenMinuteRate()))
            .addMetric("m5", convertRate(timer.getFiveMinuteRate()))
            .addMetric("m1", convertRate(timer.getOneMinuteRate()))
            .addMetric("mean_rate", convertRate(timer.getMeanRate()))
            // convert duration
            .addMetric("max", convertDuration(snapshot.getMax()))
            .addMetric("min", convertDuration(snapshot.getMin()))
            .addMetric("mean", convertDuration(snapshot.getMean()))
            .addMetric("stddev", convertDuration(snapshot.getStdDev()))
            .addMetric("median", convertDuration(snapshot.getMedian()))
            .addMetric("p75", convertDuration(snapshot.get75thPercentile()))
            .addMetric("p95", convertDuration(snapshot.get95thPercentile()))
            .addMetric("p98", convertDuration(snapshot.get98thPercentile()))
            .addMetric("p99", convertDuration(snapshot.get99thPercentile()))
            .addMetric("p999", convertDuration(snapshot.get999thPercentile()))
            .build();
}
 
Example 9
Source File: MqMetricReporter.java    From pmq with Apache License 2.0 6 votes vote down vote up
private Set<Metric> buildTimers(String name, Timer timer, long timestamp, Map<String, String> tags) {
    final MetricsCollector collector = MetricsCollector.createNew(name, tags, timestamp);
    final Snapshot snapshot = timer.getSnapshot();
    if (getChangeCount(name, timer.getCount()) == 0) {
        return Collections.emptySet();
    }
    return collector.addMetric("count", timer.getCount())
            //convert rate
            .addMetric("m15", convertRate(timer.getFifteenMinuteRate()))
            .addMetric("m5", convertRate(timer.getFiveMinuteRate()))
            .addMetric("m1", convertRate(timer.getOneMinuteRate()))
            .addMetric("mean_rate", convertRate(timer.getMeanRate()))
            // convert duration
            .addMetric("max", convertDuration(snapshot.getMax()))
            .addMetric("min", convertDuration(snapshot.getMin()))
            .addMetric("mean", convertDuration(snapshot.getMean()))
            .addMetric("stddev", convertDuration(snapshot.getStdDev()))
            .addMetric("median", convertDuration(snapshot.getMedian()))
            .addMetric("p75", convertDuration(snapshot.get75thPercentile()))
            .addMetric("p95", convertDuration(snapshot.get95thPercentile()))
            .addMetric("p98", convertDuration(snapshot.get98thPercentile()))
            .addMetric("p99", convertDuration(snapshot.get99thPercentile()))
            .addMetric("p999", convertDuration(snapshot.get999thPercentile()))
            .build();
}
 
Example 10
Source File: MqMetricReporter.java    From pmq with Apache License 2.0 6 votes vote down vote up
private Set<Metric> buildTimers(String name, Timer timer, long timestamp, Map<String, String> tags) {
    final MetricsCollector collector = MetricsCollector.createNew(name, tags, timestamp);
    final Snapshot snapshot = timer.getSnapshot();
    if (getChangeCount(name, timer.getCount()) == 0) {
        return Collections.emptySet();
    }
    return collector.addMetric("count", timer.getCount())
            //convert rate
            .addMetric("m15", convertRate(timer.getFifteenMinuteRate()))
            .addMetric("m5", convertRate(timer.getFiveMinuteRate()))
            .addMetric("m1", convertRate(timer.getOneMinuteRate()))
            .addMetric("mean_rate", convertRate(timer.getMeanRate()))
            // convert duration
            .addMetric("max", convertDuration(snapshot.getMax()))
            .addMetric("min", convertDuration(snapshot.getMin()))
            .addMetric("mean", convertDuration(snapshot.getMean()))
            .addMetric("stddev", convertDuration(snapshot.getStdDev()))
            .addMetric("median", convertDuration(snapshot.getMedian()))
            .addMetric("p75", convertDuration(snapshot.get75thPercentile()))
            .addMetric("p95", convertDuration(snapshot.get95thPercentile()))
            .addMetric("p98", convertDuration(snapshot.get98thPercentile()))
            .addMetric("p99", convertDuration(snapshot.get99thPercentile()))
            .addMetric("p999", convertDuration(snapshot.get999thPercentile()))
            .build();
}
 
Example 11
Source File: MetricsCommand.java    From enkan with Eclipse Public License 1.0 5 votes vote down vote up
private void printTimer(Transport t, Timer timer) {
    final Snapshot snapshot = timer.getSnapshot();
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "             count = %d", timer.getCount())));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "         mean rate = %2.2f calls/sec", timer.getMeanRate())));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "     1-minute rate = %2.2f calls/sec", timer.getMeanRate())));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "     5-minute rate = %2.2f calls/sec", timer.getFiveMinuteRate())));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "    15-minute rate = %2.2f calls/sec", timer.getFifteenMinuteRate())));

    t.send(ReplResponse.withOut(
            String.format(Locale.US, "               min = %2.2f sec", convertDuration(snapshot.getMin()))));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "               max = %2.2f sec", convertDuration(snapshot.getMax()))));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "              mean = %2.2f sec", convertDuration(snapshot.getMean()))));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "            stddev = %2.2f sec", convertDuration(snapshot.getStdDev()))));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "            median = %2.2f sec", convertDuration(snapshot.getMedian()))));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "              75%% <= %2.2f sec", convertDuration(snapshot.get75thPercentile()))));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "              95%% <= %2.2f sec", convertDuration(snapshot.get95thPercentile()))));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "              98%% <= %2.2f sec", convertDuration(snapshot.get98thPercentile()))));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "              99%% <= %2.2f sec", convertDuration(snapshot.get99thPercentile()))));
    t.send(ReplResponse.withOut(
            String.format(Locale.US, "            99.9%% <= %2.2f sec", convertDuration(snapshot.get999thPercentile()))));

}
 
Example 12
Source File: MetricUtils.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Adds metrics from a Timer to a NamedList, using well-known back-compat names.
 * @param lst The NamedList to add the metrics data to
 * @param timer The Timer to extract the metrics from
 */
public static void addMetrics(NamedList<Object> lst, Timer timer) {
  Snapshot snapshot = timer.getSnapshot();
  lst.add("avgRequestsPerSecond", timer.getMeanRate());
  lst.add("5minRateRequestsPerSecond", timer.getFiveMinuteRate());
  lst.add("15minRateRequestsPerSecond", timer.getFifteenMinuteRate());
  lst.add("avgTimePerRequest", nsToMs(snapshot.getMean()));
  lst.add("medianRequestTime", nsToMs(snapshot.getMedian()));
  lst.add("75thPcRequestTime", nsToMs(snapshot.get75thPercentile()));
  lst.add("95thPcRequestTime", nsToMs(snapshot.get95thPercentile()));
  lst.add("99thPcRequestTime", nsToMs(snapshot.get99thPercentile()));
  lst.add("999thPcRequestTime", nsToMs(snapshot.get999thPercentile()));
}
 
Example 13
Source File: OverseerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void printTimingStats(Timer timer) {
  Snapshot snapshot = timer.getSnapshot();
  if (log.isInfoEnabled()) {
    log.info("\t avgRequestsPerSecond: {}", timer.getMeanRate());
    log.info("\t 5minRateRequestsPerSecond: {}", timer.getFiveMinuteRate()); // logOk
    log.info("\t 15minRateRequestsPerSecond: {}", timer.getFifteenMinuteRate()); // logOk
    log.info("\t avgTimePerRequest: {}", nsToMs(snapshot.getMean())); // logOk
    log.info("\t medianRequestTime: {}", nsToMs(snapshot.getMedian())); // logOk
    log.info("\t 75thPcRequestTime: {}", nsToMs(snapshot.get75thPercentile())); // logOk
    log.info("\t 95thPcRequestTime: {}", nsToMs(snapshot.get95thPercentile())); // logOk
    log.info("\t 99thPcRequestTime: {}", nsToMs(snapshot.get99thPercentile())); // logOk
    log.info("\t 999thPcRequestTime: {}", nsToMs(snapshot.get999thPercentile())); // logOk
  }
}
 
Example 14
Source File: MetricUtilsTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings({"unchecked"})
public void testSolrTimerGetSnapshot() {
  // create a timer with up to 100 data points
  final Timer timer = new Timer();
  final int iterations = random().nextInt(100);
  for (int i = 0; i < iterations; ++i) {
    timer.update(Math.abs(random().nextInt()) + 1, TimeUnit.NANOSECONDS);
  }
  // obtain timer metrics
  Map<String,Object> map = new HashMap<>();
  MetricUtils.convertTimer("", timer, MetricUtils.PropertyFilter.ALL, false, false, ".", (k, v) -> {
    map.putAll((Map<String,Object>)v);
  });
  @SuppressWarnings({"rawtypes"})
  NamedList lst = new NamedList(map);
  // check that expected metrics were obtained
  assertEquals(14, lst.size());
  final Snapshot snapshot = timer.getSnapshot();
  // cannot test avgRequestsPerMinute directly because mean rate changes as time increases!
  // assertEquals(lst.get("avgRequestsPerSecond"), timer.getMeanRate());
  assertEquals(timer.getFiveMinuteRate(), lst.get("5minRate"));
  assertEquals(timer.getFifteenMinuteRate(), lst.get("15minRate"));
  assertEquals(MetricUtils.nsToMs(snapshot.getMean()), lst.get("mean_ms"));
  assertEquals(MetricUtils.nsToMs(snapshot.getMedian()), lst.get("median_ms"));
  assertEquals(MetricUtils.nsToMs(snapshot.get75thPercentile()), lst.get("p75_ms"));
  assertEquals(MetricUtils.nsToMs(snapshot.get95thPercentile()), lst.get("p95_ms"));
  assertEquals(MetricUtils.nsToMs(snapshot.get99thPercentile()), lst.get("p99_ms"));
  assertEquals(MetricUtils.nsToMs(snapshot.get999thPercentile()), lst.get("p999_ms"));
}
 
Example 15
Source File: TimerConverter.java    From cf-java-logging-support with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Metric> convertMetricEntry(Entry<String, Timer> metricEntry, long timestamp) {
    List<Metric> result = new ArrayList<>();
    Timer timer = metricEntry.getValue();
    Snapshot snapshot = timer.getSnapshot();
    String key = metricEntry.getKey();
    MetricType type = MetricType.TIMER;

    result.add(buildCustomMetric(key + ".count", timer.getCount(), type, timestamp));
    result.add(buildCustomMetric(key + ".max", snapshot.getMax(), type, timestamp));
    result.add(buildCustomMetric(key + ".min", snapshot.getMin(), type, timestamp));
    result.add(buildCustomMetric(key + ".p50", snapshot.getMedian(), type, timestamp));
    result.add(buildCustomMetric(key + ".p95", snapshot.get95thPercentile(), type, timestamp));
    result.add(buildCustomMetric(key + ".p99", snapshot.get99thPercentile(), type, timestamp));
    result.add(buildCustomMetric(key + ".m1_rate", timer.getOneMinuteRate(), type, timestamp));

    if (metricQuantiles) {
        result.add(buildCustomMetric(key + ".mean", snapshot.getMean(), type, timestamp));
        result.add(buildCustomMetric(key + ".p75", snapshot.get75thPercentile(), type, timestamp));
        result.add(buildCustomMetric(key + ".p98", snapshot.get98thPercentile(), type, timestamp));
        result.add(buildCustomMetric(key + ".p999", snapshot.get999thPercentile(), type, timestamp));
        result.add(buildCustomMetric(key + ".stddev", snapshot.getStdDev(), type, timestamp));
        result.add(buildCustomMetric(key + ".m15_rate", timer.getFifteenMinuteRate(), type, timestamp));
        result.add(buildCustomMetric(key + ".m5_rate", timer.getFiveMinuteRate(), type, timestamp));
        result.add(buildCustomMetric(key + ".mean_rate", timer.getMeanRate(), type, timestamp));
    }
    return result;
}
 
Example 16
Source File: InfluxDbReporter.java    From dropwizard-metrics-influxdb with Apache License 2.0 5 votes vote down vote up
private void reportTimer(String name, Timer timer, long now) {
    if (canSkipMetric(name, timer)) {
        return;
    }
    final Snapshot snapshot = timer.getSnapshot();
    Map<String, Object> fields = new HashMap<String, Object>();
    fields.put("count", timer.getCount());
    fields.put("min", convertDuration(snapshot.getMin()));
    fields.put("max", convertDuration(snapshot.getMax()));
    fields.put("mean", convertDuration(snapshot.getMean()));
    fields.put("stddev", convertDuration(snapshot.getStdDev()));
    fields.put("p50", convertDuration(snapshot.getMedian()));
    fields.put("p75", convertDuration(snapshot.get75thPercentile()));
    fields.put("p95", convertDuration(snapshot.get95thPercentile()));
    fields.put("p98", convertDuration(snapshot.get98thPercentile()));
    fields.put("p99", convertDuration(snapshot.get99thPercentile()));
    fields.put("p999", convertDuration(snapshot.get999thPercentile()));
    fields.put("m1_rate", convertRate(timer.getOneMinuteRate()));
    fields.put("m5_rate", convertRate(timer.getFiveMinuteRate()));
    fields.put("m15_rate", convertRate(timer.getFifteenMinuteRate()));
    fields.put("mean_rate", convertRate(timer.getMeanRate()));

    if (includeTimerFields != null) {
        fields.keySet().retainAll(includeTimerFields);
    }

    influxDb.appendPoints(
        new InfluxDbPoint(
            getMeasurementName(name),
            getTags(name),
            now,
            fields));
}
 
Example 17
Source File: MetricsFormatter.java    From emissary with Apache License 2.0 4 votes vote down vote up
public String formatTimer(final String name, final Timer timer) {
    final Snapshot snapshot = timer.getSnapshot();
    return String.format("STAT: %s => min=%2.2f,  max=%2.2f, avg=%2.2f, events=%d", name, convertDuration(snapshot.getMin()),
            convertDuration(snapshot.getMax()), convertDuration(snapshot.getMedian()), timer.getCount());
}
 
Example 18
Source File: MetricsModule.java    From hugegraph with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(Timer timer, JsonGenerator json,
                      SerializerProvider provider) throws IOException {
    json.writeStartObject();
    final Snapshot snapshot = timer.getSnapshot();
    json.writeNumberField("count", timer.getCount());
    json.writeNumberField("max", snapshot.getMax() *
                                 this.durationFactor);
    json.writeNumberField("mean", snapshot.getMean() *
                                  this.durationFactor);
    json.writeNumberField("min", snapshot.getMin() *
                                 this.durationFactor);

    json.writeNumberField("p50", snapshot.getMedian() *
                                 this.durationFactor);
    json.writeNumberField("p75", snapshot.get75thPercentile() *
                                 this.durationFactor);
    json.writeNumberField("p95", snapshot.get95thPercentile() *
                                 this.durationFactor);
    json.writeNumberField("p98", snapshot.get98thPercentile() *
                                 this.durationFactor);
    json.writeNumberField("p99", snapshot.get99thPercentile() *
                                 this.durationFactor);
    json.writeNumberField("p999", snapshot.get999thPercentile() *
                                  this.durationFactor);

    if (this.showSamples) {
        final long[] values = snapshot.getValues();
        final double[] scaledValues = new double[values.length];
        for (int i = 0; i < values.length; i++) {
            scaledValues[i] = values[i] * this.durationFactor;
        }
        json.writeObjectField("values", scaledValues);
    }

    json.writeNumberField("stddev", snapshot.getStdDev() *
                                    this.durationFactor);
    json.writeNumberField("m15_rate", timer.getFifteenMinuteRate() *
                                      this.rateFactor);
    json.writeNumberField("m1_rate", timer.getOneMinuteRate() *
                                     this.rateFactor);
    json.writeNumberField("m5_rate", timer.getFiveMinuteRate() *
                                     this.rateFactor);
    json.writeNumberField("mean_rate", timer.getMeanRate() *
                                       this.rateFactor);
    json.writeStringField("duration_units", this.durationUnit);
    json.writeStringField("rate_units", this.rateUnit);
    json.writeEndObject();
}
 
Example 19
Source File: MetricsElasticsearchModule.java    From oneops with Apache License 2.0 4 votes vote down vote up
@Override
public void serialize(JsonTimer jsonTimer,
                      JsonGenerator json,
                      SerializerProvider provider) throws IOException {
    json.writeStartObject();
    json.writeStringField("name", jsonTimer.name());
    json.writeObjectField(timestampFieldname, jsonTimer.timestampAsDate());
    Timer timer = jsonTimer.value();
    final Snapshot snapshot = timer.getSnapshot();
    json.writeNumberField("count", timer.getCount());
    json.writeNumberField("max", snapshot.getMax() * durationFactor);
    json.writeNumberField("mean", snapshot.getMean() * durationFactor);
    json.writeNumberField("min", snapshot.getMin() * durationFactor);

    json.writeNumberField("p50", snapshot.getMedian() * durationFactor);
    json.writeNumberField("p75", snapshot.get75thPercentile() * durationFactor);
    json.writeNumberField("p95", snapshot.get95thPercentile() * durationFactor);
    json.writeNumberField("p98", snapshot.get98thPercentile() * durationFactor);
    json.writeNumberField("p99", snapshot.get99thPercentile() * durationFactor);
    json.writeNumberField("p999", snapshot.get999thPercentile() * durationFactor);

    /*
    if (showSamples) {
        final long[] values = snapshot.getValues();
        final double[] scaledValues = new double[values.length];
        for (int i = 0; i < values.length; i++) {
            scaledValues[i] = values[i] * durationFactor;
        }
        json.writeObjectField("values", scaledValues);
    }
    */

    json.writeNumberField("stddev", snapshot.getStdDev() * durationFactor);
    json.writeNumberField("m1_rate", timer.getOneMinuteRate() * rateFactor);
    json.writeNumberField("m5_rate", timer.getFiveMinuteRate() * rateFactor);
    json.writeNumberField("m15_rate", timer.getFifteenMinuteRate() * rateFactor);
    json.writeNumberField("mean_rate", timer.getMeanRate() * rateFactor);
    json.writeStringField("duration_units", durationUnit);
    json.writeStringField("rate_units", rateUnit);
    addOneOpsMetadata(json);
    json.writeEndObject();
}
 
Example 20
Source File: MeteringTest.java    From metrics-sql with Apache License 2.0 4 votes vote down vote up
@Test
public void testVolume() throws SQLException {
    final int iterations = 100, inserts = 100, textSize=10; // Increase iterations
    for (int i = 0; i < iterations; i++) {
        Connection connection = dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("insert into METRICS_TEST(ID, TEXT, CREATED) values (?,?,?)");
        for (int j = 0; j < inserts; j++) {
            preparedStatement.setInt(1, i * inserts + j + 100);
            preparedStatement.setString(2, randomString(textSize));
            preparedStatement.setTimestamp(3, new Timestamp(System.currentTimeMillis()));
            preparedStatement.execute();
        }
        H2DbUtil.close(preparedStatement);

        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select count(*) from METRICS_TEST");
        if (resultSet.next()) {
            int count = resultSet.getInt(1);
            assertThat(count, greaterThanOrEqualTo(i*inserts));
        }
        H2DbUtil.close(resultSet);

        resultSet = statement.executeQuery("select * from METRICS_TEST order by ID asc");
        readWholeResultSet(resultSet);
        H2DbUtil.close(resultSet, statement);

        preparedStatement = connection.prepareStatement("select * from METRICS_TEST where TEXT=? order by ID asc");
        preparedStatement.setString(1, randomString(textSize));
        resultSet = preparedStatement.executeQuery();
        readWholeResultSet(resultSet);
        H2DbUtil.close(resultSet, preparedStatement, connection);
    }

    metricsReporter.report(metricRegistry.getGauges(), metricRegistry.getCounters(), metricRegistry.getHistograms(), metricRegistry.getMeters(), metricRegistry.getTimers());
    assertThat(metricRegistry.getTimers().size(), equalTo(
            2 // connection
            +2 // inserts
            +5 // statements
            +3 // prepared statement
            ));

    // connection
    Timer timer = metricRegistry.timer("java.sql.Connection");
    assertThat(timer.getCount(), equalTo((long) iterations));
    timer = metricRegistry.timer("java.sql.Connection.get");
    assertThat(timer.getCount(), equalTo((long) iterations));

    // statement
    timer = metricRegistry.timer("java.sql.Statement");
    assertThat(timer.getCount(), equalTo((long) iterations));
    timer = metricRegistry.timer("java.sql.Statement.[select count(*) from metrics_test].exec");
    assertThat(timer.getCount(), equalTo((long) iterations));
    timer = metricRegistry.timer("java.sql.Statement.[select * from metrics_test order by id asc].exec");
    assertThat(timer.getCount(), equalTo((long) iterations));

    // prepared statement
    timer = metricRegistry.timer("java.sql.PreparedStatement.[insert into metrics_test(id, text, created) values (?,?,?)].exec");
    assertThat(timer.getCount(), equalTo((long) iterations * inserts));

    timer = metricRegistry.timer("java.sql.PreparedStatement.[insert into metrics_test(id, text, created) values (?,?,?)]");
    assertThat(timer.getCount(), equalTo((long) iterations));

    timer = metricRegistry.timer("java.sql.PreparedStatement.[select * from metrics_test where text=? order by id asc].exec");
    assertThat(timer.getCount(), equalTo((long) iterations));
    Snapshot timerSnapshot = timer.getSnapshot();
    double preparedStatementExecMean = timerSnapshot.getMean();
    assertThat(preparedStatementExecMean, greaterThan(0.0));
    assertThat(timerSnapshot.getMax(), greaterThan(0L));
    assertThat(timerSnapshot.getMax(), greaterThan(timerSnapshot.getMin()));

    timer = metricRegistry.timer("java.sql.PreparedStatement.[select * from metrics_test where text=? order by id asc]");
    assertThat(timer.getCount(), equalTo((long) iterations));
    timerSnapshot = timer.getSnapshot();
    assertThat(timerSnapshot.getMean(), greaterThan(preparedStatementExecMean));

}