TrainingFetcher(MetricSampler metricSampler, Cluster cluster, SampleStore sampleStore, Set<TopicPartition> assignedPartitions, long startTimeMs, long endTimeMs, Timer fetcherTimer, Meter fetcherFailureRate) { _cluster = cluster; _sampleStore = sampleStore; _metricSampler = metricSampler; _startTimeMs = startTimeMs; _endTimeMs = endTimeMs; _assignedPartitions = assignedPartitions; _fetcherTimer = fetcherTimer; _fetcherFailureRate = fetcherFailureRate; }
@Override protected void fetchMetricsForAssignedPartitions() throws MetricSamplingException { final Timer.Context ctx = _fetcherTimer.time(); try { MetricSampler.Samples samples = _metricSampler.getSamples(_cluster, _assignedPartitions, _startTimeMs, _endTimeMs, MetricSampler.SamplingMode.BROKER_METRICS_ONLY); ModelParameters.addMetricObservation(samples.brokerMetricSamples()); _sampleStore.storeSamples(samples); } catch (Exception e) { _fetcherFailureRate.mark(); throw e; } finally { ctx.stop(); } }
/** * Create a new {@code ChangeLoader}. * * @param cxt the Bireme Context * @param pipeLine the {@code PipeLine} belongs to * @param mappedTable the target table * @param taskIn a queue to get {@code LoadTask} */ public ChangeLoader(Context cxt, PipeLine pipeLine, String mappedTable, LinkedBlockingQueue<Future<LoadTask>> taskIn) { this.cxt = cxt; this.conf = cxt.conf; this.conn = null; this.mappedTable = mappedTable; this.table = cxt.tablesInfo.get(mappedTable); this.taskIn = taskIn; this.copyThread = Executors.newFixedThreadPool(1); // add statistics Timer[] timers = pipeLine.stat.addTimerForLoader(mappedTable); copyForDeleteTimer = timers[0]; deleteTimer = timers[1]; copyForInsertTimer = timers[2]; logger = pipeLine.logger; }
public MatchingServiceHealthCheckResponseDto sendHealthCheckRequest( final Element matchingServiceHealthCheckRequest, final URI matchingServiceUri) { // Use a custom timer so that we get separate metrics for each matching service final String scope = matchingServiceUri.toString().replace(':','_').replace('/', '_'); final Timer timer = metricsRegistry.timer(MetricRegistry.name(MatchingServiceHealthCheckClient.class, "sendHealthCheckRequest", scope)); final Timer.Context context = timer.time(); HealthCheckResponse healthCheckResponse; try { healthCheckResponse = client.makeSoapRequestForHealthCheck(matchingServiceHealthCheckRequest, matchingServiceUri); } catch(ApplicationException ex) { final String errorMessage = MessageFormat.format("Failed to complete matching service health check to {0}.", matchingServiceUri); LOG.warn(errorMessage, ex); return new MatchingServiceHealthCheckResponseDto(Optional.<String>absent(), Optional.<String>absent()); } finally { context.stop(); } return new MatchingServiceHealthCheckResponseDto( Optional.of(XmlUtils.writeToString(healthCheckResponse.getResponseElement())), healthCheckResponse.getVersionNumber()); }
public static void main(String[] args) throws InterruptedException { Bench<JedisPool> bench = new JedisBench() { @Override public void executeOperation(String data, JedisPool benchInstance, int threadNumber, int iteration, MetricRegistry metrics) { Jedis jedis = benchInstance.getResource(); Timer.Context time = metrics.timer("incr").time(); jedis.incr("incr_" + threadNumber + "_" + iteration); time.stop(); jedis.close(); } }; Benchmark benchmark = new Benchmark(bench); benchmark.run(args); }
private void removeGraphRelation( Group group, String relation, String subjectType, String subjectKey, String objectType, String objectKey, Timer timer ) { final String rel = REL_MARK; final String inv = INV_MARK; final String relationHashKey = subjectType + "." + subjectKey; final String relationRangeKey = rel + relation + "." + objectType + "." + objectKey; timed(timer, () -> groupStorage.removeRelation(group, relationHashKey, relationRangeKey)); final String inverseRelationHashKey = objectType + "." + objectKey; final String inverseRelationRangeKey = inv + relation + "." + subjectType + "." + subjectKey; timed(timer, () -> groupStorage.removeRelation(group, inverseRelationHashKey, inverseRelationRangeKey)); }
@Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { EnableMetricTimer annotation = methodInvocation.getThis().getClass().getAnnotation(EnableMetricTimer.class); String name = StringUtils.isBlank(annotation.value()) ? methodInvocation.getThis().getClass().getName() + "." + methodInvocation.getMethod().getName() : annotation.value(); Timer meter = Jboot.me().getMetric().timer(name); Timer.Context timerContext = meter.time(); try { return methodInvocation.proceed(); } finally { timerContext.stop(); } }
public static void main(String[] args) throws InterruptedException { Bench<JedisPool> bench = new JedisBench() { @Override public void executeOperation(String data, JedisPool benchInstance, int threadNumber, int iteration, MetricRegistry metrics) { Jedis jedis = benchInstance.getResource(); Timer.Context time = metrics.timer("set").time(); String key = "set_" + threadNumber; jedis.sadd(key, data); time.stop(); jedis.close(); } }; Benchmark benchmark = new Benchmark(bench); benchmark.run(args); }
public static void main(String[] args) throws InterruptedException { Bench<JedisPool> bench = new JedisBench() { @Override public void executeOperation(String data, JedisPool benchInstance, int threadNumber, int iteration, MetricRegistry metrics) { Jedis jedis = benchInstance.getResource(); Timer.Context time = metrics.timer("list").time(); String key = "list_" + threadNumber; jedis.rpush(key, data); time.stop(); jedis.close(); } }; Benchmark benchmark = new Benchmark(bench); benchmark.run(args); }
@Test public void rxJava() throws Exception { CountDownLatch latch = new CountDownLatch(1); RxJavaClient client = retrofit.create(RxJavaClient.class); client.timed() .subscribe(response -> { try { assertThat(response.code()).isEqualTo(HttpURLConnection.HTTP_OK); assertThat(response.body()).isEqualTo(RESPONSE_OBJECT); } finally { latch.countDown(); } }, error -> { throw new RuntimeException("Test failed"); }); latch.await(1L, TimeUnit.SECONDS); Timer timer = metrics.timer(TIMER_NAME); assertThat(timer).isNotNull(); assertThat(timer.getCount()).isEqualTo(1); assertThat(timer.getMeanRate()).isGreaterThan(0); }
private void adjustRateLimit() { final long count = acquireCount.incrementAndGet(); if (count >= pollOnCount) { final Timer commitTimer = actorContext.getOperationTimer(ActorContext.COMMIT); double newRateLimit = calculateNewRateLimit(commitTimer, commitTimeoutInSeconds); if (newRateLimit < 1.0) { newRateLimit = getRateLimitFromOtherDataStores(); } if (newRateLimit >= 1.0) { txRateLimiter.setRate(newRateLimit); pollOnCount = count + (long) newRateLimit / 2; } } }
@Before public void setup() throws RpcCallException { handlerDictionary = new MethodHandlerDictionary(); handlerDictionary.put("a", null); ServiceMethodHandlerUnderTest mockHandlerThrowsRpcCallEx = new ServiceMethodHandlerUnderTest(); handlerDictionary.put("jsonRpcWithException", mockHandlerThrowsRpcCallEx); metricRegistry = mock(MetricRegistry.class); when(metricRegistry.counter(anyString())).thenReturn(mock(Counter.class)); when(metricRegistry.timer(anyString())).thenReturn(mock(Timer.class)); handlerMetrics = mock(RpcHandlerMetrics.class); when(handlerMetrics.getMethodTimer(any(), any(), any())).thenReturn(mock(GoTimer.class)); servlet = new JsonHandler(handlerDictionary, metricRegistry, handlerMetrics, new ServiceProperties(), null); }
private void assertMetrics() { Timer timer = metricRegistry.getTimers().values().iterator().next(); assertEquals("wrong number of invocations in metric.", 1, timer.getCount()); assertTrue("wrong value of mean in metric.", timer.getMeanRate() > 0); assertEquals("wrong number of meter metrics.", 2, metricRegistry.getMeters().values().size()); Set<Map.Entry<String, Meter>> entries = metricRegistry.getMeters().entrySet(); entries.forEach(entry -> { if (entry.getKey().endsWith("Metered")) { assertEquals(String.format("wrong number of invocations in metric %s", entry.getKey()), 1, entry.getValue().getCount()); } }); }
public void onCallbackComplete(Operation operation) { Long startTime = removeObjectProperty(operation, OPERATION_PROPERTY_NAME); if (startTime == null) { return;//re-entrant } String op = Operations.getOperationName(operation); long t = clock.getTick() - startTime; Timer timer = opVsTimer.computeIfAbsent(op, s -> { String metricName = ROOT_NAME.withTags("command", op).toString(); return RegistryService.getMetricRegistry().timer(metricName); }); timer.update(t, TimeUnit.NANOSECONDS); }
public void onResponseReceived(HttpRequest request, HttpResponse response) { Long startTime = removeObjectProperty(request, START_TIME_PROPERTY_NAME); if (startTime == null) { return; } long t = Clock.defaultClock().getTick() - startTime; String method = request.getRequestLine().getMethod(); int statusCode = response.getStatusLine().getStatusCode(); String metricName = ROOT_NAME.withTags( "method", method, "status", "" + statusCode).toString(); Timer timer = RegistryService.getMetricRegistry().timer(metricName); timer.update(t, TimeUnit.NANOSECONDS); }
public void onGetInputStream(HttpURLConnection urlConnection, int statusCode) { Long startTime = removeObjectProperty(urlConnection, START_TIME_PROPERTY_NAME); if (startTime == null) { return; } long t = Clock.defaultClock().getTick() - startTime; String method = urlConnection.getRequestMethod(); String status = "" + statusCode; Timer timer = timers.computeIfAbsent(status + method, s -> { TagEncodedMetricName metricName = ROOT_NAME.withTags( "method", method, "status", status); return getTimer(metricName); }); timer.update(t, TimeUnit.NANOSECONDS); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); actorContext = new ActorContext(getSystem(), actorFactory.createActor(Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration(), DatastoreContext.newBuilder().build(), new PrimaryShardInfoFutureCache()) { @Override public Timer getOperationTimer(final String operationName) { return commitTimer; } @Override public double getTxCreationLimit() { return 10.0; } }; doReturn(commitTimerContext).when(commitTimer).time(); doReturn(commitSnapshot).when(commitTimer).getSnapshot(); for (int i = 1; i < 11; i++) { // Keep on increasing the amount of time it takes to complete transaction for each tenth of a // percentile. Essentially this would be 1ms for the 10th percentile, 2ms for 20th percentile and so on. doReturn(TimeUnit.MILLISECONDS.toNanos(i) * 1D).when(commitSnapshot).getValue(i * 0.1); } }
public static void stop(OperationId id, Supplier<Timer> timerSupplier) { OperationId lastId = lastId(); if (lastId != id) { //TODO better error handling LOGGER.error("Operation Context Mismatch. Expected: " + id + " got " + lastId); return; } OperationContext context = CONTEXT_STACK.get().pop(); if (context == RENTRANT) { return; } Timer timer = timerSupplier.get(); if (timer != null) { long t = clock.getTick() - context.getStartTime(); timer.update(t, TimeUnit.NANOSECONDS); } }
@Override public void report(final SortedMap<String, Gauge> gauges, final SortedMap<String, Counter> counters, final SortedMap<String, Histogram> histograms, final SortedMap<String, Meter> meters, final SortedMap<String, Timer> timers) { final long timestamp = clock.instant().toEpochMilli(); final ImmutableList<InfluxDbMeasurement> influxDbMeasurements = ImmutableList.<InfluxDbMeasurement>builder() .addAll(transformer.fromGauges(gauges, timestamp)) .addAll(transformer.fromCounters(counters, timestamp)) .addAll(transformer.fromHistograms(histograms, timestamp)) .addAll(transformer.fromMeters(meters, timestamp)) .addAll(transformer.fromTimers(timers, timestamp)) .build(); sender.send(influxDbMeasurements); }
public ServiceResponse send(List<IUserAccount> users, String message, int messageType, String subject, String port, String logMessage, Integer notif, String eventType) throws MessageHandlingException, IOException { xLogger.fine("Entered send"); // Get the address list List<String> addresses = getAddresses(users); final Timer.Context context = SMS.equals(type) ? smsTimer.time() : emailTimer.time(); ServiceResponse resp = null; try { // Send the message and get response resp = doSend(addresses, message, messageType, subject, port, domainId); } finally { context.stop(); } // Store the response if (logging) { logMessage(domainId, sendingUserId, logMessage != null ? logMessage : message, users, resp, notif, eventType); } xLogger.fine("Exiting send"); return resp; }
/** * Builds a mapping of block locations to file byte range */ private ImmutableRangeMap<Long,BlockLocation> buildBlockMap(FileStatus status) throws IOException { final Timer.Context context = metrics.timer(BLOCK_MAP_BUILDER_TIMER).time(); BlockLocation[] blocks; ImmutableRangeMap<Long,BlockLocation> blockMap; blocks = fs.getFileBlockLocations(status, 0 , status.getLen()); ImmutableRangeMap.Builder<Long, BlockLocation> blockMapBuilder = new ImmutableRangeMap.Builder<Long,BlockLocation>(); for (BlockLocation block : blocks) { long start = block.getOffset(); long end = start + block.getLength(); Range<Long> range = Range.closedOpen(start, end); blockMapBuilder = blockMapBuilder.put(range, block); } blockMap = blockMapBuilder.build(); blockMapMap.put(status.getPath(), blockMap); context.stop(); return blockMap; }
public static void main(String[] args) throws InterruptedException { Bench<RedissonClient> bench = new RedissonBench() { @Override public void executeOperation(String data, RedissonClient benchInstance, int threadNumber, int iteration, MetricRegistry metrics) { RList<String> list = benchInstance.getList("list_" + threadNumber); Timer.Context time = metrics.timer("list").time(); list.add(data); time.stop(); } }; Benchmark benchmark = new Benchmark(bench); benchmark.run(args); }
@RequestMapping("/all-links") public String allLinks(Model model) { Timer timer = metricRegistry.timer("all-links"); Context context = timer.time(); try { List<Link> asdex = linkRepo.findByNameContainingOrderByNameAsc("ASDE-X"); model.addAttribute("asdex", asdex); List<Link> assc = linkRepo.findByNameContainingOrderByNameAsc("ASSC"); model.addAttribute("assc", assc); List<Link> tdls = linkRepo.findByNameContainingOrderByNameAsc("TDLS"); model.addAttribute("tdls", tdls); List<Link> efsts = linkRepo.findByNameContainingOrderByNameAsc("EFSTS"); model.addAttribute("efsts", efsts); List<Link> stars = linkRepo.findByNameContainingOrderByNameAsc("STARS"); model.addAttribute("stars", stars); List<Link> rvr = linkRepo.findByNameContainingOrderByNameAsc("RVR"); model.addAttribute("rvr", rvr); return "all-links"; } finally { context.stop(); } }
/** * Execute one iteration of metric sampling for all the assigned partitions. */ @Override protected void fetchMetricsForAssignedPartitions() throws MetricSamplingException { final Timer.Context ctx = _fetchTimer.time(); try { MetricSampler.Samples samples = fetchSamples(); _sampleStore.storeSamples(samples); } catch (Exception e) { _fetchFailureRate.mark(); throw e; } finally { ctx.stop(); } }
@Override public Integer generateId() { final Timer.Context context = timer.time(); try { return (int) rangeSequence.incrementAndGet(); } catch (Exception e) { throw e; } finally { context.stop(); } }
public void stop(Object exchange) { if (exchange != null && exchange instanceof Exchange) { Exchange exchange1 = (Exchange) exchange; Object timerContext = exchange1.getIn().getHeader(MetricsOperationTimer); if (timerContext != null) { ((Timer.Context) timerContext).stop(); } } }
/** * Add {@code Timer}s for a {@link ChangeLoader}. Each {@code ChangeLoader} has three Timer. * <ul> * <li>Record the time of copy followed by delete</li> * <li>Record the time of delete</li> * <li>Record the time of insert in copy way</li> * </ul> * * @param table the {@code ChangeLoader}'s table * @return the registered Timer */ public Timer[] addTimerForLoader(String table) { Timer[] timers = new Timer[3]; timers[0] = register.timer(MetricRegistry.name(pipeLineName, table, "Loader", "CopyForDelete")); timers[1] = register.timer(MetricRegistry.name(pipeLineName, table, "Loader", "Delete")); timers[2] = register.timer(MetricRegistry.name(pipeLineName, table, "Loader", "Insert")); copyForDelete.put(table, timers[0]); delete.put(table, timers[1]); insert.put(table, timers[2]); return timers; }
public String handleRequest() { final Timer.Context context = responses.time(); try { // etc; waitForMillis(new Random().nextInt(500)); return "OK"; } finally { context.stop(); } }
default <T> T timed(Timer timer, Supplier<T> supplier) { Timer.Context time = timer.time(); try { return supplier.get(); } finally { time.stop(); } }
@Test public void synchronous() throws IOException { TimedClient client = retrofit.create(TimedClient.class); Response<NamedObject> response = client.timed().execute(); assertThat(response.code()).isEqualTo(HttpURLConnection.HTTP_OK); assertThat(response.body()).isEqualTo(RESPONSE_OBJECT); Timer timer = metrics.timer(TIMER_NAME); assertThat(timer).isNotNull(); assertThat(timer.getCount()).isEqualTo(1); assertThat(timer.getMeanRate()).isGreaterThan(0); }
@Override public Object invoke(Object[] argv) throws Throwable { try { final Meter meter = this.meters.get(method); if (meter != null) { meter.mark(); } final Timer timer = this.timers.get(method); if (timer != null) { this.context = timer.time(); } return methodHandler.invoke(argv); } catch (Exception e) { final FeignOutboundMetricsDecorator.ExceptionMeterMetric metric = (method != null) ? this.exceptionMeters.get(method) : null; if (metric != null && (metric.cause.isAssignableFrom(e.getClass()) || (e.getCause() != null && metric.cause.isAssignableFrom(e.getCause().getClass())))) { metric.meter.mark(); } throw e; } finally { if (this.context != null) { this.context.close(); } } }
@Override public int readArea(AreaType area, int db, int start, int amount, DataType type, byte[] buffer) throws S7Exception { final Timer.Context context = reads.time(); try { return super.readArea(area, db, start, amount, type, buffer); } finally { context.stop(); } }
@Override public boolean writeArea(AreaType area, int db, int start, int amount, DataType type, byte[] buffer) throws S7Exception { final Timer.Context context = writes.time(); try { return super.writeArea(area, db, start, amount, type, buffer); } finally { context.stop(); } }
public static void main(String[] args) throws InterruptedException { Bench<RedissonClient> bench = new RedissonBench() { @Override public void executeOperation(String data, RedissonClient benchInstance, int threadNumber, int iteration, MetricRegistry metrics) { RBucket<Object> bucket = benchInstance.getBucket("bucket_" + threadNumber + "_" + iteration); Timer.Context time = metrics.timer("bucket").time(); bucket.set(value); time.stop(); } }; Benchmark benchmark = new Benchmark(bench); benchmark.run(args); }
@Override public boolean readMultiVars(S7DataItem[] items, int itemsCount) throws S7Exception { final Timer.Context context = multiReads.time(); try { return super.readMultiVars(items, itemsCount); } finally { context.stop(); } }
@Override public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, Timer> timers) { DataPointCollector collector = new DataPointCollector(System.currentTimeMillis() / 1000); buildReportTimer.time(() -> { debug("################"); debug(">>>>>>>> Guages <<<<<<<<<"); gauges.forEach(collector::collectGauge); debug(">>>>>>>> Counters <<<<<<<<<"); counters.forEach(collector::collectCounter); debug(">>>>>>>> Histograms <<<<<<<<<"); histograms.forEach(collector::collectHistogram); debug(">>>>>>>> Meters <<<<<<<<<"); meters.forEach(collector::collectMeter); debug(">>>>>>>> Timers <<<<<<<<<"); timers.forEach(collector::collectTimer); debug("################"); }); sendReportTimer.time(() -> { Collection<DataPoint> dataPoints = collector.dataPoints; dataPointsReporter.put(dataPoints); //dataPoints.forEach(System.out::println); }); }
private Timer getTimer(String method, int status) { String key = String.valueOf(status) + method; return timers.computeIfAbsent(key, s -> { TagEncodedMetricName metric = requestCountRootMetric; if (method != null) { metric = metric.withTags("method", method, "status", String.valueOf(status)); } return registry.timer(metric.toString()); }); }
private Timer getTimerForOp(String op) { switch (op) { case "set": case "cas": return SET_OPERATION_TIMER; case "get": case "gets": return GET_OPERATION_TIMER; case "delete": return DELETE_OPERATION_TIMER; case "add": return ADD_OPERATION_TIMER; case "replace": return REPLACE_OPERATION_TIMER; case "append": return APPEND_OPERATION_TIMER; case "prepend": return PREPEND_OPERATION_TIMER; case "addOrIncr": case "incr": return INCR_OPERATION_TIMER; case "addOrDecr": case "decr": return DECR_OPERATION_TIMER; default: throw new IllegalArgumentException("Unsupported op: " + op); } }
private Timer getTimer(String methName) { String op = commands.get(methName); return timers.computeIfAbsent(op, s -> { String metric = COMMANDS_BASE_NAME.withTags("command", op).toString(); return RegistryService.getMetricRegistry().timer(metric); }); }
public static void main(String[] args) throws InterruptedException { Bench<RedissonClient> bench = new RedissonBench() { @Override public void executeOperation(String data, RedissonClient benchInstance, int threadNumber, int iteration, MetricRegistry metrics) { RBucket<String> bucket = benchInstance.getBucket("bucket_" + threadNumber + "_" + iteration); Timer.Context time = metrics.timer("bucket").time(); bucket.set(data); time.stop(); } }; Benchmark benchmark = new Benchmark(bench); benchmark.run(args); }