com.codahale.metrics.Timer Java Examples

The following examples show how to use com.codahale.metrics.Timer. 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: VideoService.java    From arcusplatform with Apache License 2.0 7 votes vote down vote up
private Consumer<PlatformMessage> consumer(MethodListener listener) {
   return (message) -> {
      long startTime = System.nanoTime();
      String type = message.getMessageType();
      try {
         listener.onEvent(message, message.getValue());

         Timer success = MESSAGE_SUCCESS.get(type);
         if (success != null) {
            success.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
         }
      } catch(Exception e) {
         log.warn("Error processing event", e);
         Timer fail = MESSAGE_FAIL.get(type);
         if (fail != null) {
            fail.update(System.nanoTime() - startTime, TimeUnit.NANOSECONDS);
         }
      }
   };
}
 
Example #2
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 #3
Source File: DynamoDbDelegate.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 6 votes vote down vote up
UpdateItemResult updateItem(final UpdateItemRequest request) throws BackendException {
    setUserAgent(request);
    UpdateItemResult result;
    final int bytes;
    if (request.getUpdateExpression() != null) {
        bytes = calculateExpressionBasedUpdateSize(request);
    } else {
        bytes = calculateItemUpdateSizeInBytes(request.getAttributeUpdates());
    }
    getBytesHistogram(UPDATE_ITEM, request.getTableName()).update(bytes);
    final int wcu = computeWcu(bytes);
    timedWriteThrottle(UPDATE_ITEM, request.getTableName(), wcu);

    final Timer.Context apiTimerContext = getTimerContext(UPDATE_ITEM, request.getTableName());
    try {
        result = client.updateItem(request);
    } catch (Exception e) {
        throw processDynamoDbApiException(e, UPDATE_ITEM, request.getTableName());
    } finally {
        apiTimerContext.stop();
    }
    meterConsumedCapacity(UPDATE_ITEM, result.getConsumedCapacity());

    return result;
}
 
Example #4
Source File: HttpDispatcher.java    From haystack-agent with Apache License 2.0 6 votes vote down vote up
@Override
public void dispatch(final byte[] ignored, final byte[] data) throws Exception {
    RequestBody body = RequestBody.create(PROTOBUF, data);
    Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();

    try (Timer.Context timer = dispatchTimer.time(); Response response = client.newCall(request).execute()) {
        if (!response.isSuccessful()) {
            dispatchFailure.mark();
            LOGGER.error("Fail to post the record to the http collector with status code {}", response.code());
        }
    } catch (Exception e) {
        dispatchFailure.mark();
        LOGGER.error("Fail to post the record to the http collector", e);
    }
}
 
Example #5
Source File: HiveMetaStoreBasedRegister.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
private void onPartitionExist(IMetaStoreClient client, Table table, HivePartition partition, Partition nativePartition, Partition existedPartition) throws TException {
  HivePartition existingPartition;
  if(existedPartition == null) {
    try (Timer.Context context = this.metricContext.timer(GET_HIVE_PARTITION).time()) {
      existingPartition = HiveMetaStoreUtils.getHivePartition(
          client.getPartition(table.getDbName(), table.getTableName(), nativePartition.getValues()));
    }
  } else {
    existingPartition = HiveMetaStoreUtils.getHivePartition(existedPartition);
  }

  if (needToUpdatePartition(existingPartition, partition)) {
    log.info(String.format("Partition update required. ExistingPartition %s, newPartition %s",
        stringifyPartition(existingPartition), stringifyPartition(partition)));
    Partition newPartition = getPartitionWithCreateTime(nativePartition, existingPartition);
    log.info(String.format("Altering partition %s", newPartition));
    try (Timer.Context context = this.metricContext.timer(ALTER_PARTITION).time()) {
      client.alter_partition(table.getDbName(), table.getTableName(), newPartition);
    }
    log.info(String.format("Updated partition %s in table %s with location %s", stringifyPartition(newPartition),
        table.getTableName(), nativePartition.getSd().getLocation()));
  } else {
    log.debug(String.format("Partition %s in table %s with location %s already exists and no need to update",
        stringifyPartition(nativePartition), table.getTableName(), nativePartition.getSd().getLocation()));
  }
}
 
Example #6
Source File: TimedMethodBeanLookupTest.java    From metrics-cdi with Apache License 2.0 6 votes vote down vote up
@Test
@InSequence(3)
public void removeTimerFromRegistry() {
    // Get a contextual instance of the bean
    TimedMethodBean bean = instance.get();

    assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
    Timer timer = registry.getTimers().get(TIMER_NAME);

    // Remove the timer from metrics registry
    registry.remove(TIMER_NAME);

    try {
        // Call the timed method and assert an exception is thrown
        bean.timedMethod();
    } catch (RuntimeException cause) {
        assertThat(cause, is(instanceOf(IllegalStateException.class)));
        assertThat(cause.getMessage(), is(equalTo("No timer with name [" + TIMER_NAME + "] found in registry [" + registry + "]")));
        // Make sure that the timer hasn't been called
        assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(TIMER_COUNT.get())));
        return;
    }

    fail("No exception has been re-thrown!");
}
 
Example #7
Source File: DistributedQueueServiceImpl.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public Status ackMessage(String queueName, UUID queueMessageId ) {

    if( logger.isTraceEnabled() ){
        logger.trace("Acking message for queue {} with id: {}", queueName, queueMessageId);
    }

    Timer.Context timer = metricsService.getMetricRegistry().timer( MetricsService.ACK_TIME_TOTAL ).time();
    try {

        QueueAckRequest message = new QueueAckRequest( queueName, queueMessageId );
        return sendMessageToLocalRouters( message );


    } finally {
        timer.close();
    }
}
 
Example #8
Source File: RocksRawKVStore.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public void putIfAbsent(final byte[] key, final byte[] value, final KVStoreClosure closure) {
    final Timer.Context timeCtx = getTimeContext("PUT_IF_ABSENT");
    final Lock readLock = this.readWriteLock.readLock();
    readLock.lock();
    try {
        final byte[] prevVal = this.db.get(key);
        if (prevVal == null) {
            this.db.put(this.writeOptions, key, value);
        }
        setSuccess(closure, prevVal);
    } catch (final Exception e) {
        LOG.error("Fail to [PUT_IF_ABSENT], [{}, {}], {}.", BytesUtil.toHex(key), BytesUtil.toHex(value),
            StackTraceUtil.stackTrace(e));
        setCriticalError(closure, "Fail to [PUT_IF_ABSENT]", e);
    } finally {
        readLock.unlock();
        timeCtx.stop();
    }
}
 
Example #9
Source File: DMetadataIO.java    From blueflood with Apache License 2.0 6 votes vote down vote up
@Override
public void put( Locator locator, String key, String value ) throws IOException {

    Timer.Context ctx = Instrumentation.getWriteTimerContext( CassandraModel.CF_METRICS_METADATA_NAME );

    Session session = DatastaxIO.getSession();

    try {

        BoundStatement bound = putValue.bind( locator.toString(), key, serDes.serialize( value ) );

        ResultSet result = session.execute( bound );
        LOG.trace( "result.size=" + result.all().size() );
    }
    finally {
        ctx.stop();
    }
}
 
Example #10
Source File: TestMetricsHelper.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateAndInitTimer() {
  MetricRegistryJson metricRegistryJson = new MetricRegistryJson();
  Map<String, TimerJson> timer = new HashMap<>();
  TimerJson timerJson = new TimerJson();
  timerJson.setCount(100);
  timerJson.setMean(10);
  timer.put("t" + MetricsConfigurator.TIMER_SUFFIX, timerJson);
  metricRegistryJson.setTimers(timer);

  Timer t = MetricsHelper.createAndInitTimer(metricRegistryJson, metricRegistry, "y", "x", "1");
  Assert.assertEquals(0, t.getCount());

  t = MetricsHelper.createAndInitTimer(metricRegistryJson, metricRegistry, "t", "x", "1");
  Assert.assertEquals(1, t.getCount());
  // convert milli seconds to nano seconds
  Assert.assertEquals(100 * 1000 *1000, t.getSnapshot().getValues()[0]);
}
 
Example #11
Source File: Main.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 6 votes vote down vote up
@Override
public Void call() throws Exception {

    int runs = 1;
    while (runs <= 100) {
        metricRegistry.counter("TheCounter").inc();
        metricRegistry.meter("TheMeter").mark(runs);
        metricRegistry.histogram("TheHistogram").update(runs);
        Timer.Context context = theTimer.time();
        Thread.sleep(250);
        context.stop();
        ++runs;

        Thread.sleep(1000);
    }

    return null;
}
 
Example #12
Source File: HubSessionRegistry.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
public void heartbeat() {
   try(Timer.Context timer = heartbeatTimer.time()) {
      Map<PlatformPartition, Set<String>> connectedHubs = new LinkedHashMap<>(2 * partitionsPerHeartbeat);
      int offset = nextHeartbeatPartition.getAndAdd(partitionsPerHeartbeat);
      for(int i=0; i<partitionsPerHeartbeat; i++) {
         connectedHubs.put(
               partitioner.getPartitionById(Math.floorMod(offset + i, partitioner.getPartitionCount())),
               new HashSet<>()
         );
      }
      for(Session session: getSessions()) {
         HubSession hs = (HubSession) session;
         accumulate(hs.getPartition(), hs.getHubId(), connectedHubs);
      }
      flush(connectedHubs);
   }
}
 
Example #13
Source File: MemoryRawKVStore.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public void compareAndPut(final byte[] key, final byte[] expect, final byte[] update, final KVStoreClosure closure) {
    final Timer.Context timeCtx = getTimeContext("COMPARE_PUT");
    try {
        final byte[] actual = this.defaultDB.get(key);
        if (Arrays.equals(expect, actual)) {
            this.defaultDB.put(key, update);
            setSuccess(closure, Boolean.TRUE);
        } else {
            setSuccess(closure, Boolean.FALSE);
        }
    } catch (final Exception e) {
        LOG.error("Fail to [COMPARE_PUT], [{}, {}, {}], {}.", BytesUtil.toHex(key), BytesUtil.toHex(expect),
            BytesUtil.toHex(update), StackTraceUtil.stackTrace(e));
        setCriticalError(closure, "Fail to [COMPARE_PUT]", e);
    } finally {
        timeCtx.stop();
    }
}
 
Example #14
Source File: CpEntityManager.java    From usergrid with Apache License 2.0 6 votes vote down vote up
@Override
public void addSetToDictionary( EntityRef entityRef, String dictionaryName, Set<?> elementValues )
        throws Exception {

    if ( ( elementValues == null ) || elementValues.isEmpty() ) {
        return;
    }

    EntityRef entity = get( entityRef );

    UUID timestampUuid = UUIDUtils.newTimeUUID();
    Mutator<ByteBuffer> batch = createMutator( cass.getApplicationKeyspace( applicationId ), be );

    for ( Object elementValue : elementValues ) {
        batch = batchUpdateDictionary( batch, entity, dictionaryName, elementValue, null, false, timestampUuid );
    }

    //Adding graphite metrics
    Timer.Context timeAddingSetDictionary = entAddDictionarySetTimer.time();
    CassandraPersistenceUtils.batchExecute( batch, CassandraService.RETRY_COUNT );
    timeAddingSetDictionary.stop();
}
 
Example #15
Source File: RollupBatchWriteRunnable.java    From blueflood with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    Timer.Context ctx = batchWriteTimer.time();
    try {
        metricsRW.insertRollups(writeContexts);
    } catch (Exception e) {
        LOG.warn("not able to insert rollups", e);
        executionContext.markUnsuccessful(e);
    } finally {
        executionContext.decrementWriteCounter(writeContexts.size());
        rollupsPerBatch.update(writeContexts.size());
        rollupsWriteRate.mark(writeContexts.size());
        RollupService.lastRollupTime.set(System.currentTimeMillis());
        ctx.stop();
    }
}
 
Example #16
Source File: HiveMetaStoreBasedRegister.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Override
protected void registerPath(HiveSpec spec) throws IOException {
  try (Timer.Context context = this.metricContext.timer(PATH_REGISTER_TIMER).time();
      AutoReturnableObject<IMetaStoreClient> client = this.clientPool.getClient()) {
    Table table = HiveMetaStoreUtils.getTable(spec.getTable());

    createDbIfNotExists(client.get(), table.getDbName());
    createOrAlterTable(client.get(), table, spec);

    Optional<HivePartition> partition = spec.getPartition();
    if (partition.isPresent()) {
      addOrAlterPartition(client.get(), table, partition.get());
    }
    HiveMetaStoreEventHelper.submitSuccessfulPathRegistration(eventSubmitter, spec);
  } catch (TException e) {
    HiveMetaStoreEventHelper.submitFailedPathRegistration(eventSubmitter, spec, e);
    throw new IOException(e);
  }
}
 
Example #17
Source File: PistachiosClient.java    From Pistachio with Apache License 2.0 6 votes vote down vote up
/**
 * To lookup a list of ids asynchronously. Given the id list return the futures to get the values. One can also register callback
 * on the server side upon lookup, see {@link com.yahoo.ads.pb.customization.LookupCallback}
 *
 * @param ids       id to look up as list of byte[].
 * @param callback  whether need callback on the same side or not
 * @return          <code>Map<byte[], Future<byte[]>></code> return in a map of futre of value for each corresponding ids
 * @exception       MasterNotFoundException when fail because no master found
 * @exception       ConnectionBrokenException when fail because connection is broken in the middle
 * @exception       Exception other errors indicating failure
 */
public Future<Map<byte[], byte[]>> multiLookUpAsync(List<byte[]> ids, boolean callback) throws MasterNotFoundException, ConnectionBrokenException, Exception {
    final Timer.Context context = multiLookupAsyncTimer.time();
    RetryWaiter retryWaiter = new RetryWaiter(multiLookupAsyncFailureRequests);

    try {
        while (true) {
            try {
                return clientImpl.multiLookupAsync(ids, callback);
            }catch (Exception e) {
                retryWaiter.waitBeforeRetry(e);
            }
        }
    } finally {
        context.stop();
    }
}
 
Example #18
Source File: OpenTsdbReporterTest.java    From metrics-opentsdb with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecorateDisabledGauge() {
    reporter = OpenTsdbReporter.forRegistry(registry)
            .withClock(clock)
            .prefixedWith("prefix")
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .filter(MetricFilter.ALL)
            .withTags(Collections.singletonMap("foo", "bar"))
            .withBatchSize(100)
            .withCounterGaugeDecorations(false)
            .build(opentsdb);

    when(gauge.getValue()).thenReturn(1L);
    reporter.report(this.map("gauge", gauge), this.<Counter>map(), this.<Histogram>map(), this.<Meter>map(), this.<Timer>map());
    verify(opentsdb).send(captor.capture());
    final Set<OpenTsdbMetric> metrics = captor.getValue();
    assertEquals(1, metrics.size());
    OpenTsdbMetric metric = metrics.iterator().next();
    assertEquals("prefix.gauge", metric.getMetric());
    assertEquals(1L, metric.getValue());
    assertEquals((Long) timestamp, metric.getTimestamp());
}
 
Example #19
Source File: MetricInstrumentedStore.java    From titan1withtp3.1 with Apache License 2.0 6 votes vote down vote up
static <T> T runWithMetrics(String prefix, String storeName, String name, UncheckedCallable<T> impl) {

        if (null == prefix) {
            return impl.call();
        }

        Preconditions.checkNotNull(name);
        Preconditions.checkNotNull(impl);

        final MetricManager mgr = MetricManager.INSTANCE;

        mgr.getCounter(prefix, storeName, name, M_CALLS).inc();

        final Timer.Context tc = mgr.getTimer(prefix, storeName, name, M_TIME).time();

        try {
            return impl.call();
        } catch (RuntimeException e) {
            mgr.getCounter(prefix, storeName, name, M_EXCEPTIONS).inc();
            throw e;
        } finally {
            tc.stop();
        }
    }
 
Example #20
Source File: StreamingFPGrowth.java    From macrobase with Apache License 2.0 6 votes vote down vote up
private void updateFrequentItemOrder() {
    Timer.Context context = updateFrequentItemOrder.time();

    sortedNodes.clear();

    frequentItemOrder.clear();

    // we have to materialize a canonical order so that items with equal counts
    // are consistently ordered when they are sorted during transaction insertion
    List<Map.Entry<Integer, Double>> sortedItemCounts = Lists.newArrayList(frequentItemCounts.entrySet());
    sortedItemCounts.sort((i1, i2) -> frequentItemCounts.get(i1.getKey())
            .compareTo(frequentItemCounts.get(i2.getKey())));
    for (int i = 0; i < sortedItemCounts.size(); ++i) {
        frequentItemOrder.put(sortedItemCounts.get(i).getKey(), i);
    }

    context.stop();
}
 
Example #21
Source File: EndpointMetricsHandlerDefaultImpl.java    From riposte with Apache License 2.0 6 votes vote down vote up
private Timer requestTimer(HttpMethod m) {
    if (m == null) {
        return otherRequests;
    }
    else {
        if (m.equals(HttpMethod.GET))
            return getRequests;
        else if (m.equals(HttpMethod.POST))
            return postRequests;
        else if (m.equals(HttpMethod.PUT))
            return putRequests;
        else if (m.equals(HttpMethod.DELETE))
            return deleteRequests;
        else
            return otherRequests;
    }
}
 
Example #22
Source File: SignalFxAwareCodahaleMetricsCollectorTest.java    From riposte with Apache License 2.0 6 votes vote down vote up
@DataProvider(value = {
    "null",
    "0",
    "1",
    "2"
}, splitBy = "\\|")
@Test
public void getNamedTimer_with_varargs_dimensions_creates_dimensioned_timer_using_sfx_mechanisms(
    Integer numDimensions
) {
    // given
    String timerName = UUID.randomUUID().toString();
    Pair<String, String>[] varargDims = generateVarargDimensions(numDimensions);
    List<Pair<String, String>> dimsAsList = (varargDims == null) ? null : Arrays.asList(varargDims);

    // when
    Timer result = sfxImpl.getNamedTimer(timerName, varargDims);

    // then
    verifyMetricCreation(timerBuilderMock, timerTaggerMock, timerName, dimsAsList, timerMock, result);
}
 
Example #23
Source File: InstrumentedInvokerFactoryTest.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@Test
public void noAnnotation() {

    Timer timer = testMetricRegistry.timer("timed");
    Meter meter = testMetricRegistry.meter("metered");
    when(mockMetricRegistry.timer(anyString())).thenReturn(timer);
    when(mockMetricRegistry.meter(anyString())).thenReturn(meter);

    long oldtimervalue = timer.getCount();
    long oldmetervalue = meter.getCount();

    Invoker invoker = invokerBuilder.create(instrumentedService, new FooInvoker());
    this.setTargetMethod(exchange, "foo"); // simulate CXF behavior

    Object result = invoker.invoke(exchange, null);
    assertEquals("fooReturn", result);

    assertThat(timer.getCount(), is(oldtimervalue));
    assertThat(meter.getCount(), is(oldmetervalue));
}
 
Example #24
Source File: ConcreteExtendedTimedBeanTest.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
@Test
@InSequence(1)
public void timedMethodNotCalledYet(MetricRegistry registry) {
    assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMED_NAME));
    Timer timer = registry.getTimers().get(TIMED_NAME);

    // Make sure that the timer hasn't been called yet
    assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(0L)));
}
 
Example #25
Source File: Metrics.java    From heftydb with Apache License 2.0 5 votes vote down vote up
private void initMetrics() {
    //Main DB Metrics
    metrics.register(metricName("write"), new Timer(new UniformReservoir()));
    metrics.register(metricName("write.rate"), new Meter());
    metrics.register(metricName("read"), new Timer(new UniformReservoir()));
    metrics.register(metricName("read.rate"), new Meter());
    metrics.register(metricName("scan"), new Timer(new UniformReservoir()));
    metrics.register(metricName("scan.rate"), new Meter());

    //Write
    metrics.register(metricName("write.concurrentMemoryTableSerializers"), new Histogram(new UniformReservoir()));
    metrics.register(metricName("write.memoryTableSerialize"), new Timer(new UniformReservoir()));

    //Read
    metrics.register(metricName("read.tablesConsulted"), new Histogram(new UniformReservoir()));
    metrics.register(metricName("read.bloomFilterFalsePositiveRate"), new CacheHitGauge());
    metrics.register(metricName("read.recordNotFoundRate"), new CacheHitGauge());

    //FileTable
    metrics.register(metricName("table.cacheHitRate"), new CacheHitGauge());

    //Index
    metrics.register(metricName("index.searchLevels"), new Histogram(new UniformReservoir()));
    metrics.register(metricName("index.cacheHitRate"), new CacheHitGauge());

    //Compactor
    metrics.register(metricName("compactor.concurrentTasks"), new Histogram(new UniformReservoir()));
    metrics.register(metricName("compactor.taskExecution"), new Timer(new UniformReservoir()));
}
 
Example #26
Source File: FilteringMetricNamingStrategyTest.java    From metrics-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void testPreparedStatement() throws SQLException {
    // Act
    Connection connection = dataSource.getConnection();
    PreparedStatement statement = connection.prepareStatement("select * from METRICS_TEST");
    ResultSet resultSet = statement.executeQuery();        
    H2DbUtil.close(resultSet, statement, connection);
    // Assert
    assertTrue(Proxy.isProxyClass(statement.getClass()));
    final SortedMap<String, Timer> timers = metricRegistry.getTimers();
    assertNull(timers.get("java.sql.Connection"));
    assertNull(timers.get("java.sql.PreparedStatement.[select * from metrics_test]"));
    assertNotNull(timers.get("java.sql.PreparedStatement.[select * from metrics_test].exec"));
    assertNotNull(timers.get("java.sql.ResultSet.[select * from metrics_test]"));
}
 
Example #27
Source File: TimedStaticMethodWithRegistryFromStaticPropertyTest.java    From metrics-aspectj with Apache License 2.0 5 votes vote down vote up
@Test
public void callTimedStaticMethodOnce() {
    // Call the timed static method and assert it's been timed once
    TimedStaticMethodWithRegistryFromStaticProperty.singleTimedStaticMethod();

    MetricRegistry registry = TimedStaticMethodWithRegistryFromStaticProperty.REGISTRY;

    assertThat("Timer is not registered correctly", registry.getTimers(), hasKey(TIMER_NAME));
    Timer timer = registry.getTimers().get(TIMER_NAME);

    assertThat("Timer count is incorrect", timer.getCount(), is(equalTo(1L)));
}
 
Example #28
Source File: DatabusResourcePoller.java    From emodb with Apache License 2.0 5 votes vote down vote up
public Response poll(Subject subject, SubjectDatabus databus, String subscription, Duration claimTtl, int limit, HttpServletRequest request,
                     boolean ignoreLongPoll, PeekOrPollResponseHelper helper) {
    Timer.Context timerContext = _pollTimer.time();
    boolean synchronousResponse = true;
    Response response;

    try {
        // Calculate when the request should internally time out (do this before our first poll() request because we
        // want that to count toward our total run-time)
        long longPollStopTime = System.currentTimeMillis() + MAX_LONG_POLL_TIME.toMillis();

        // Always issue a synchronous request at the start . . this will allow us to bypass our thread pool logic
        // altogether in cases where we might return the value immediately (see more below). There is a danger here
        // that the thread will stall if "databus" is an instance of DatabusClient and we are stuck waiting for a
        // response - however, since we use the server-side client we know that it will always execute synchronously
        // itself (no long-polling) and return in a reasonable period of time.
        PollResult result = databus.poll(subject, subscription, claimTtl, limit);
        if (ignoreLongPoll || result.getEventIterator().hasNext() || _keepAliveExecutorService == null || _pollingExecutorService == null) {
            // If ignoreLongPoll == true or we have no executor services to schedule long-polling on then always
            // return a response, even if it's empty. Alternatively, if we have data to return - return it!
            response = Response.ok()
                    .header(POLL_DATABUS_EMPTY_HEADER, String.valueOf(!result.hasMoreEvents()))
                    .entity(helper.asEntity(result.getEventIterator()))
                    .build();
        } else {
            // If the response is empty then go into async-mode and start up the runnables for our long-polling.
            response = scheduleLongPollingRunnables(request, longPollStopTime, subject, databus, claimTtl, limit, subscription,
                    result.hasMoreEvents(), helper, timerContext);
            synchronousResponse = false;
        }
    } finally {
        // Stop our timer if our request is finished here . . otherwise we are in async mode and it is the
        // responsibility of DatabusPollRunnable to close it out.
        if (synchronousResponse) {
            timerContext.stop();
        }
    }

    return response;
}
 
Example #29
Source File: InfluxDbReporterTest.java    From dropwizard-metrics-influxdb with Apache License 2.0 5 votes vote down vote up
@Test
public void reportsHistograms() throws Exception {
    final Histogram histogram = mock(Histogram.class);
    when(histogram.getCount()).thenReturn(1L);

    final Snapshot snapshot = mock(Snapshot.class);
    when(snapshot.getMax()).thenReturn(2L);
    when(snapshot.getMean()).thenReturn(3.0);
    when(snapshot.getMin()).thenReturn(4L);
    when(snapshot.getStdDev()).thenReturn(5.0);
    when(snapshot.getMedian()).thenReturn(6.0);
    when(snapshot.get75thPercentile()).thenReturn(7.0);
    when(snapshot.get95thPercentile()).thenReturn(8.0);
    when(snapshot.get98thPercentile()).thenReturn(9.0);
    when(snapshot.get99thPercentile()).thenReturn(10.0);
    when(snapshot.get999thPercentile()).thenReturn(11.0);

    when(histogram.getSnapshot()).thenReturn(snapshot);

    reporter.report(this.<Gauge>map(), this.<Counter>map(), this.map("histogram", histogram), this.<Meter>map(), this.<Timer>map());

    final ArgumentCaptor<InfluxDbPoint> influxDbPointCaptor = ArgumentCaptor.forClass(InfluxDbPoint.class);
    verify(influxDb, atLeastOnce()).appendPoints(influxDbPointCaptor.capture());
    InfluxDbPoint point = influxDbPointCaptor.getValue();

    assertThat(point.getMeasurement()).isEqualTo("histogram");
    assertThat(point.getFields()).isNotEmpty();
    assertThat(point.getFields()).hasSize(11);
    assertThat(point.getFields()).contains(entry("max", 2L));
    assertThat(point.getFields()).contains(entry("mean", 3.0));
    assertThat(point.getFields()).contains(entry("min", 4L));
    assertThat(point.getFields()).contains(entry("stddev", 5.0));
    assertThat(point.getFields()).contains(entry("p50", 6.0));
    assertThat(point.getFields()).contains(entry("p75", 7.0));
    assertThat(point.getFields()).contains(entry("p95", 8.0));
    assertThat(point.getFields()).contains(entry("p98", 9.0));
    assertThat(point.getFields()).contains(entry("p99", 10.0));
    assertThat(point.getFields()).contains(entry("p999", 11.0));
    assertThat(point.getTags()).containsEntry("metricName", "histogram");
}
 
Example #30
Source File: CassandraAppHandoffDao.java    From arcusplatform with Apache License 2.0 5 votes vote down vote up
@Override
public String newToken(SessionHandoff handoff) {
	String token = Utils.randomTokenString(tokenLength);
	try(Timer.Context ctx = newToken.time()) {
		BatchStatement batch = new BatchStatement(Type.LOGGED);
		batch.add( insertToken.bind(token, handoff.getPersonId(), handoff.getIp(), handoff.getUrl(), handoff.getUsername()) );
		batch.add( updateToken.bind(token, handoff.getPersonId()) );
		session.execute( batch );
		return token;
	}
}