@DataProvider(value = { "42 | DAYS", "123 | SECONDS", "999 | MILLISECONDS", "3 | HOURS" }, splitBy = "\\|") @Test public void RollingWindowTimerBuilder_newMetric_creates_new_timer_with_SlidingTimeWindowReservoir_with_expected_values( long amount, TimeUnit timeUnit ) { // given RollingWindowTimerBuilder rwtb = new RollingWindowTimerBuilder(amount, timeUnit); // when Timer timer = rwtb.newMetric(); // then Histogram histogram = (Histogram) getInternalState(timer, "histogram"); Reservoir reservoir = (Reservoir) getInternalState(histogram, "reservoir"); assertThat(reservoir).isInstanceOf(SlidingTimeWindowReservoir.class); // The expected value here comes from logic in the SlidingTimeWindowReservoir constructor. assertThat(getInternalState(reservoir, "window")).isEqualTo(timeUnit.toNanos(amount) * 256); }
@DataProvider(value = { "42 | DAYS", "123 | SECONDS", "999 | MILLISECONDS", "3 | HOURS" }, splitBy = "\\|") @Test public void RollingWindowHistogramBuilder_newMetric_creates_new_histogram_with_SlidingTimeWindowReservoir_with_expected_values( long amount, TimeUnit timeUnit ) { // given RollingWindowHistogramBuilder rwhb = new RollingWindowHistogramBuilder(amount, timeUnit); // when Histogram histogram = rwhb.newMetric(); // then Reservoir reservoir = (Reservoir) getInternalState(histogram, "reservoir"); assertThat(reservoir).isInstanceOf(SlidingTimeWindowReservoir.class); // The expected value here comes from logic in the SlidingTimeWindowReservoir constructor. assertThat(getInternalState(reservoir, "window")).isEqualTo(timeUnit.toNanos(amount) * 256); }
@Test public void testCreateTimer() throws ReflectiveOperationException { final SlidingTimeWindowTimingStrategy strategy = new SlidingTimeWindowTimingStrategy(); final Timer timer = strategy.createTimer(new MapConfig()); SlidingTimeWindowReservoir reservoir = reservoir(timer); assertNotNull(reservoir); assertEquals("timeWindow", strategy.name()); }
/** add a metric (time) to a specific resource - for example * type = PostgresClient.get * time = single operation time in nanoseconds */ public static void addStatElement(String type, long time){ if(!registeredStatRequesters.contains(type)){ METRICS.register(type, new Histogram(new SlidingTimeWindowReservoir(60, TimeUnit.SECONDS))); registeredStatRequesters.add(type); } METRICS.histogram(type).update(time/1000000); }
/** * Get reservoir implementation based on the reservoir type * * @return The {@link Reservoir} implementation */ private Reservoir getReservoir() { // The Reservoir implementation is selected using a switch statement. // The ReservoirType enum is a part of YAML configuration // and foreign imports are not supported by Carbon Configuration Maven Plugin. // Therefore, the Reservoir class cannot be imported and the Reservoir // creation logic cannot be written inside ReservoirType enum. switch (reservoirType) { case EXPONENTIALLY_DECAYING: return new ExponentiallyDecayingReservoir(); case UNIFORM: return new UniformReservoir(reservoirParametersConfig.getSize()); case SLIDING_WINDOW: return new SlidingWindowReservoir(reservoirParametersConfig.getSize()); case SLIDING_TIME_WINDOW: return new SlidingTimeWindowReservoir(reservoirParametersConfig.getWindow(), reservoirParametersConfig.getWindowUnit()); case HDR_HISTOGRAM: Recorder recorder = new Recorder(reservoirParametersConfig.getNumberOfSignificantValueDigits()); if (reservoirParametersConfig.isResetOnSnapshot()) { return new HdrHistogramResetOnSnapshotReservoir(recorder); } else { return new HdrHistogramReservoir(recorder); } default: throw new RuntimeException("Invalid Reservoir Type"); } }
public static Timer createTimer(MetricRegistry metrics, String name, final String pipelineName, final String pipelineRev) { return create( metrics, new Timer(new SlidingTimeWindowReservoir(60, TimeUnit.SECONDS)), metricName(name, TIMER_SUFFIX), pipelineName, pipelineRev ); }
/** * Constructor. * @param metricRegistry Metrics registry used to log various metrics. * @param flakeId Flake's id to which this component belongs. * @param componentName Unique name of the component. * @param ctx Shared zmq context. * @param fieldName The fieldName used by the reducer for grouping. */ public ReducerStateBackupComponent(final MetricRegistry metricRegistry, final String flakeId, final String componentName, final ZMQ.Context ctx, final String fieldName) { super(metricRegistry, flakeId, componentName, ctx); this.messageBackup = new HashMap<>(); this.tupleSerializer = SerializerFactory.getSerializer(); this.keyFieldName = fieldName; this.stateBackup = new HashMap<>(); this.recoveringFlakes = new HashMap<>(); Gauge<Integer> lenMonitor = metricRegistry.register("BACkUPLEN", new Gauge<Integer>() { @Override public Integer getValue() { Integer sum = 0; for (String key : messageBackup.keySet()) { sum += messageBackup.get(key).size(); } return sum; } }); Histogram qhist = getMetricRegistry() .register( MetricRegistry.name(QueueLenMonitor.class, "bkp.len.histo"), new Histogram(new SlidingTimeWindowReservoir(2, TimeUnit.SECONDS))); BackupLenMonitor mon = new BackupLenMonitor(lenMonitor, qhist); mon.start(); //metricRegistry.meter(MetricRegistry.name // (ReducerStateBackupComponent.class, // "msg-q-len")); }
@Test public void testJsonMetricsTypes() { assertMetricType("counter", new Counter()); assertMetricType("histogram", new Histogram(new SlidingTimeWindowReservoir(10, TimeUnit.SECONDS))); assertMetricType("gauge", (Gauge<String>) () -> "whatever"); assertMetricType("meter", new Meter()); assertMetricType("timer", new Timer()); }
InnerHistogram(MetricContext context, String name, ContextAwareHistogram contextAwareHistogram, long windowSize, TimeUnit unit) { super(new SlidingTimeWindowReservoir(windowSize, unit)); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentHistogram = Optional.fromNullable(parentContext.get().contextAwareHistogram(name, windowSize, unit)); } else { this.parentHistogram = Optional.absent(); } this.contextAwareHistogram = new WeakReference<>(contextAwareHistogram); }
InnerTimer(MetricContext context, String name, ContextAwareTimer contextAwareTimer, long windowSize, TimeUnit unit) { super(new SlidingTimeWindowReservoir(windowSize, unit)); this.name = name; Optional<MetricContext> parentContext = context.getParent(); if (parentContext.isPresent()) { this.parentTimer = Optional.fromNullable(parentContext.get().contextAwareTimer(name, windowSize, unit)); } else { this.parentTimer = Optional.absent(); } this.timer = new WeakReference<>(contextAwareTimer); }
@Override public void markUsage(Table table) { Histogram window = usageMap.get(table); if (window == null) { final SlidingTimeWindowReservoir reservoir = new SlidingTimeWindowReservoir( usageTrackTime.getQuantity(), usageTrackTime.getUnit()); window = new Histogram(reservoir); usageMap.put(table, window); } window.update(1); }
public CodahaleMetricsListener generateCodahaleMetricsListenerWithSignalFxSupport( SignalFxReporterFactory signalFxReporterFactory, CodahaleMetricsCollector metricsCollector, MetricDimensionConfigurator<Timer> customRequestTimerDimensionConfigurator, ExtraRequestLogicHandler extraRequestLogicHandler ) { MetricRegistry metricRegistry = metricsCollector.getMetricRegistry(); // Use the identity function if customRequestTimerDimensionConfigurator is null. if (customRequestTimerDimensionConfigurator == null) customRequestTimerDimensionConfigurator = METRIC_DIMENSION_CONFIGURATOR_IDENTITY; // Create the SignalFxEndpointMetricsHandler with the customRequestTimerDimensionConfigurator and // extraRequestLogicHandler specifics. EndpointMetricsHandler endpointMetricsHandler = new SignalFxEndpointMetricsHandler( signalFxReporterFactory.getReporter(metricRegistry).getMetricMetadata(), metricRegistry, // Use a rolling window reservoir with the same window as the reporting frequency, // to prevent the dashboards from producing false or misleading data. new SignalFxEndpointMetricsHandler.RollingWindowTimerBuilder(signalFxReporterFactory.getInterval(), signalFxReporterFactory.getTimeUnit()), // Do the default request latency timer dimensions, chained with customRequestTimerDimensionConfigurator // for any custom logic desired. DEFAULT_REQUEST_LATENCY_TIMER_DIMENSION_CONFIGURATOR .chainedWith(customRequestTimerDimensionConfigurator) ) { @Override public void handleRequest(RequestInfo<?> requestInfo, ResponseInfo<?> responseInfo, HttpProcessingState httpState, int responseHttpStatusCode, int responseHttpStatusCodeXXValue, long requestElapsedTimeMillis) { // Do the normal endpoint stuff. super.handleRequest(requestInfo, responseInfo, httpState, responseHttpStatusCode, responseHttpStatusCodeXXValue, requestElapsedTimeMillis); // Do any extra logic (if desired). if (extraRequestLogicHandler != null) { extraRequestLogicHandler.handleExtraRequestLogic( requestInfo, responseInfo, httpState, responseHttpStatusCode, responseHttpStatusCodeXXValue, requestElapsedTimeMillis ); } } }; return CodahaleMetricsListener .newBuilder(metricsCollector) .withEndpointMetricsHandler(endpointMetricsHandler) // The metric names should be basic with no prefix - SignalFx dimensions do the job normally covered // by metric name prefixes. .withServerStatsMetricNamingStrategy(CodahaleMetricsListener.MetricNamingStrategy.defaultNoPrefixImpl()) .withServerConfigMetricNamingStrategy(CodahaleMetricsListener.MetricNamingStrategy.defaultNoPrefixImpl()) // Histograms should use a rolling window reservoir with the same window as the reporting frequency, // otherwise the dashboards will produce false or misleading data. .withRequestAndResponseSizeHistogramSupplier( () -> new Histogram( new SlidingTimeWindowReservoir(signalFxReporterFactory.getInterval(), signalFxReporterFactory.getTimeUnit()) ) ) .build(); }
@Override public Timer newMetric() { return new Timer(new SlidingTimeWindowReservoir(amount, timeUnit)); }
@Override public Histogram newMetric() { return new Histogram(new SlidingTimeWindowReservoir(amount, timeUnit)); }
@Override public Timer createTimer(Config config) { return new Timer(new SlidingTimeWindowReservoir( config.longValue(WINDOW_PROP).orElse(DEFAULT_WINDOW), config.enumValue(WINDOW_UNIT_PROP, TimeUnit.class).orElse(DEFAULT_WINDOW_UNIT))); }
public TryThisOut() { registry = new MetricRegistry(); gauge = new Gauge<Integer>() { @Override public Integer getValue() { return gaugeValue; } }; registry.register("gauge", gauge); counter = new Counter(); registry.register("counter", counter); histogram = new Histogram(new SlidingTimeWindowReservoir(10, TimeUnit.SECONDS)); registry.register("histogram", histogram); meter = new Meter(); registry.register("meter", meter); timer = new Timer(); registry.register("timer", timer); String instanceId = EC2MetadataUtils.getInstanceId(); // try out the default constructor. AmazonCloudWatchClient client = new AmazonCloudWatchClient(); reporter = new CloudWatchReporter.Builder(registry, client) .dimension("instanceId", instanceId) .namespace("some-namespace") .build(); reporter.start(5, TimeUnit.SECONDS); executorService = Executors.newSingleThreadScheduledExecutor(); executorService.scheduleAtFixedRate(new Runnable() { private int counterVal; private Random random = new Random(); @Override public void run() { gaugeValue = counterVal++; counter.inc(); Timer.Context context = timer.time(); meter.mark(); histogram.update((int)(random.nextGaussian() * 10)); context.stop(); } }, 0, 1, TimeUnit.SECONDS); }
protected Reservoir createReservoir() { return new SlidingTimeWindowReservoir(hysterixSettings.getRollingTimeWindowIntervalInMs(), TimeUnit.MILLISECONDS); }
ContextAwareHistogram(MetricContext context, String name, long windowSize, TimeUnit unit) { super(new SlidingTimeWindowReservoir(windowSize, unit)); this.innerHistogram = new InnerHistogram(context, name, this, windowSize, unit); this.context = context; }
ContextAwareTimer(MetricContext context, String name, long windowSize, TimeUnit unit) { super(new SlidingTimeWindowReservoir(windowSize, unit)); this.innerTimer = new InnerTimer(context, name, this, windowSize, unit); this.context = context; }
private SlidingTimeWindowReservoir createReservoir() { return new SlidingTimeWindowReservoir(samplingPeriodMinutes, TimeUnit.MINUTES); }
/** * Create this summary with a sliding time window reservoir. This reservoir keeps the measurements made in the * last {@code window} seconds (or other time unit). * * @param window the window to save * @param windowUnit the window's time units */ public SummaryBuilder withSlidingTimeWindowReservoir(final int window, final TimeUnit windowUnit) { reservoirSupplier = () -> new SlidingTimeWindowReservoir(window, windowUnit, codahaleClock); return SummaryBuilder.this; }