下面列出了怎么用com.codahale.metrics.Sampling的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @param rescale the submitted sum by this multiplier. 1.0 is the identity (no rescale).
*/
void reportSampling(Map.Entry<String, ? extends Sampling> entry, String typeDimValue, double rescale, List<MetricDatum> data) {
Sampling metric = entry.getValue();
Snapshot snapshot = metric.getSnapshot();
double scaledSum = sum(snapshot.getValues()) * rescale;
final StatisticSet statisticSet = new StatisticSet()
.withSum(scaledSum)
.withSampleCount((double) snapshot.size())
.withMinimum((double) snapshot.getMin() * rescale)
.withMaximum((double) snapshot.getMax() * rescale);
DemuxedKey key = new DemuxedKey(appendGlobalDimensions(entry.getKey()));
Iterables.addAll(data, key.newDatums(typeDimName, typeDimValue, new Function<MetricDatum, MetricDatum>() {
@Override
public MetricDatum apply(MetricDatum datum) {
return datum.withStatisticValues(statisticSet);
}
}));
}
static <X extends Sampling & Counting> SamplingCounting adaptSamplingCounting(final X metric) {
return new SamplingCounting() {
@Override
public long getCount() {
return metric.getCount();
}
@Override
public Iterable<Interval> getIntervals() {
final Snapshot snapshot = metric.getSnapshot();
return Interval.asIntervals(Interval.Quantile.STANDARD_PERCENTILES, q -> (float) snapshot.getValue(q.value));
}
};
}
/**
* Timers and Histograms can be optimised if access to the "raw" {@link com.codahale.metrics.Metric} is available (in-process only).
* This function tries to access the given {@link NamedObject}'s raw Metric, and adapt it to a {@link SamplingCounting}, failing back to adapting
* the JMX proxy object to a {@link SamplingCounting}.
*/
private static <RawT extends Sampling & Counting, MBeanT> NamedObject<SamplingCounting> mBeanAsSamplingCounting(final NamedObject<?> mBean, final Function<MBeanT, SamplingCounting> mBeanAdapterFunction) {
try {
return CassandraMetricsUtilities.<RawT>metricForMBean(mBean).map((n, o) -> adaptSamplingCounting(o));
} catch (final Exception e) {
return mBean.<MBeanT>cast().map((n, o) -> mBeanAdapterFunction.apply(o));
}
}
private static void assertSummary(Map<String, ?> map, String property, int expectedCount) {
assertThat(((Counting) map.get(clientMetricNameWithStatus(property, 200))).getCount())
.isEqualTo(expectedCount);
assertThat(((Sampling) map.get(clientMetricNameWithStatus(property, 200))).getSnapshot().getMean())
.isPositive();
assertThat(((Counting) map.get(serverMetricNameWithStatus(property, 200))).getCount())
.isEqualTo(expectedCount);
assertThat(((Sampling) map.get(serverMetricNameWithStatus(property, 200))).getSnapshot().getMean())
.isPositive();
}
public static Builder<Sampling> whenMaxValueGreaterThan(final long value) {
return LimitCheckers.<Sampling>builder().limit(new MaxSnapshotValue(new GreaterThan<>(value)));
}
@Override
Long getMetric(Sampling metric) {
return metric.getSnapshot().getMax();
}
private void addSampling(String baseName, Sampling sampling) {
Metric metric = (Metric)sampling;
final Snapshot snapshot = sampling.getSnapshot();
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.MEDIAN,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getMedian());
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.PERCENT_75,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get75thPercentile());
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.PERCENT_95,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get95thPercentile());
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.PERCENT_98,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get98thPercentile());
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.PERCENT_99,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get99thPercentile());
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.PERCENT_999,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.get999thPercentile());
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.MAX,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getMax());
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.MIN,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getMin());
// These are slower to calculate. Only calculate if we need.
if (detailsToAdd.contains(SignalFxReporter.MetricDetails.STD_DEV)) {
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.STD_DEV,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getStdDev());
}
if (detailsToAdd.contains(SignalFxReporter.MetricDetails.MEAN)) {
addMetric(metric, baseName,
SignalFxReporter.MetricDetails.MEAN,
SignalFxProtocolBuffers.MetricType.GAUGE, snapshot.getMean());
}
}