Search in sources :

Example 91 with MetricRegistry

use of com.codahale.metrics.MetricRegistry in project vertx-examples by vert-x3.

the class PrometheusMetricsVerticle method start.

@Override
public void start() throws Exception {
    MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate("exported");
    CollectorRegistry.defaultRegistry.register(new DropwizardExports(metricRegistry));
    // Bind metrics handler to /metrics
    Router router = Router.router(vertx);
    router.get("/metrics").handler(new MetricsHandler());
    // Start httpserver on localhost:8080
    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    // Increase counter every second
    vertx.setPeriodic(1_000L, e -> metricRegistry.counter("testCounter").inc());
}
Also used : DropwizardExports(io.prometheus.client.dropwizard.DropwizardExports) MetricRegistry(com.codahale.metrics.MetricRegistry) MetricsHandler(io.prometheus.client.vertx.MetricsHandler) Router(io.vertx.ext.web.Router)

Example 92 with MetricRegistry

use of com.codahale.metrics.MetricRegistry in project cruise-control by linkedin.

the class LoadMonitorTaskRunnerTest method testSimpleFetch.

@Test
public void testSimpleFetch() throws InterruptedException {
    KafkaCruiseControlConfig config = new KafkaCruiseControlConfig(getLoadMonitorProperties());
    Metadata metadata = new Metadata(10, 10, false);
    MetadataClient metadataClient = new MetadataClient(config, metadata, -1L, TIME);
    MockMetricSampleAggregator mockMetricSampleAggregator = new MockMetricSampleAggregator(config, metadata);
    List<MetricSampler> samplers = new ArrayList<>();
    MetricRegistry dropwizardMetricRegistry = new MetricRegistry();
    for (int i = 0; i < NUM_METRIC_FETCHERS; i++) {
        samplers.add(new MockSampler(0));
    }
    MetricFetcherManager fetcherManager = new MetricFetcherManager(config, mockMetricSampleAggregator, metadataClient, METRIC_DEF, TIME, dropwizardMetricRegistry, samplers);
    LoadMonitorTaskRunner loadMonitorTaskRunner = new LoadMonitorTaskRunner(config, fetcherManager, mockMetricSampleAggregator, metadataClient, TIME);
    while (metadata.fetch().topics().size() < NUM_TOPICS) {
        Thread.sleep(10);
        metadataClient.refreshMetadata();
    }
    loadMonitorTaskRunner.start(true);
    Set<TopicPartition> partitionsToSample = new HashSet<>();
    for (int i = 0; i < NUM_TOPICS; i++) {
        for (int j = 0; j < NUM_PARTITIONS; j++) {
            partitionsToSample.add(new TopicPartition("topic-" + i, j));
        }
    }
    long startMs = System.currentTimeMillis();
    BlockingQueue<PartitionMetricSample> sampleQueue = mockMetricSampleAggregator.metricSampleQueue();
    while (!partitionsToSample.isEmpty() && System.currentTimeMillis() < startMs + 10000) {
        PartitionMetricSample sample = sampleQueue.poll();
        if (sample != null) {
            assertTrue("The topic partition should have been sampled and sampled only once.", partitionsToSample.contains(sample.entity().tp()));
            partitionsToSample.remove(sample.entity().tp());
        }
    }
    assertTrue("Did not see sample for partitions " + Arrays.toString(partitionsToSample.toArray()), partitionsToSample.isEmpty());
    fetcherManager.shutdown();
    assertTrue(sampleQueue.isEmpty());
}
Also used : MetricFetcherManager(com.linkedin.kafka.cruisecontrol.monitor.sampling.MetricFetcherManager) MetricSampler(com.linkedin.kafka.cruisecontrol.monitor.sampling.MetricSampler) MetricRegistry(com.codahale.metrics.MetricRegistry) Metadata(org.apache.kafka.clients.Metadata) ArrayList(java.util.ArrayList) PartitionMetricSample(com.linkedin.kafka.cruisecontrol.monitor.sampling.PartitionMetricSample) MetadataClient(com.linkedin.kafka.cruisecontrol.common.MetadataClient) TopicPartition(org.apache.kafka.common.TopicPartition) KafkaCruiseControlConfig(com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 93 with MetricRegistry

use of com.codahale.metrics.MetricRegistry in project cruise-control by linkedin.

the class KafkaCruiseControlServletDataFromTest method test.

@Test
public void test() throws Exception {
    AsyncKafkaCruiseControl mockKCC = EasyMock.createMock(AsyncKafkaCruiseControl.class);
    HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
    HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
    HttpSession session = EasyMock.createMock(HttpSession.class);
    EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
    EasyMock.expect(request.getMethod()).andReturn("GET").anyTimes();
    EasyMock.expect(request.getRequestURI()).andReturn("/test").anyTimes();
    EasyMock.expect(request.getParameterMap()).andReturn(Collections.emptyMap()).anyTimes();
    EasyMock.expect(session.getLastAccessedTime()).andReturn(Long.MAX_VALUE);
    KafkaCruiseControlState kccState = getState(_numReadyGoals, _totalGoals, _numValidWindows);
    OperationFuture<KafkaCruiseControlState> kccStateFuture = new OperationFuture<>("test");
    kccStateFuture.complete(kccState);
    EasyMock.expect(mockKCC.state()).andReturn(kccStateFuture).anyTimes();
    EasyMock.replay(mockKCC, request, response, session);
    KafkaCruiseControlServlet servlet = new KafkaCruiseControlServlet(mockKCC, 10, 100, new MetricRegistry());
    KafkaCruiseControlServlet.GoalsAndRequirements goalsAndRequirements = servlet.getGoalsAndRequirements(request, response, Collections.emptyList(), _dataFrom, false);
    assertEquals(new HashSet<>(goalsAndRequirements.goals()), new HashSet<>(_expectedGoalsToUse));
    if (_expectedRequirements != null) {
        assertEquals(_expectedRequirements.minRequiredNumWindows(), goalsAndRequirements.requirements().minRequiredNumWindows());
        assertEquals(_expectedRequirements.minMonitoredPartitionsPercentage(), goalsAndRequirements.requirements().minMonitoredPartitionsPercentage(), 0.0);
        assertEquals(_expectedRequirements.includeAllTopics(), goalsAndRequirements.requirements().includeAllTopics());
    } else {
        assertNull("The requirement should be null", goalsAndRequirements.requirements());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncKafkaCruiseControl(com.linkedin.kafka.cruisecontrol.async.AsyncKafkaCruiseControl) KafkaCruiseControlState(com.linkedin.kafka.cruisecontrol.KafkaCruiseControlState) HttpSession(javax.servlet.http.HttpSession) MetricRegistry(com.codahale.metrics.MetricRegistry) HttpServletResponse(javax.servlet.http.HttpServletResponse) OperationFuture(com.linkedin.kafka.cruisecontrol.async.OperationFuture) Test(org.junit.Test)

Example 94 with MetricRegistry

use of com.codahale.metrics.MetricRegistry in project cruise-control by linkedin.

the class SessionManagerTest method testCreateAndCloseSession.

@Test
public void testCreateAndCloseSession() {
    TestContext context = prepareRequests(true, 1);
    SessionManager sessionManager = new SessionManager(1, 1000, context.time(), new MetricRegistry());
    sessionManager.getAndCreateSessionIfNotExist(context.request(0), () -> new OperationFuture<>("testCreateSession"), 0);
    assertEquals(1, sessionManager.numSessions());
    sessionManager.closeSession(context.request(0));
    assertEquals(0, sessionManager.numSessions());
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Test(org.junit.Test)

Example 95 with MetricRegistry

use of com.codahale.metrics.MetricRegistry in project cruise-control by linkedin.

the class SessionManagerTest method testSessionExpiration.

@Test
public void testSessionExpiration() {
    TestContext context = prepareRequests(true, 2);
    SessionManager sessionManager = new SessionManager(2, 1000, context.time(), new MetricRegistry());
    List<OperationFuture<Integer>> futures = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
        futures.add(sessionManager.getAndCreateSessionIfNotExist(context.request(i), () -> new OperationFuture<>("testSessionExpiration"), 0));
    }
    assertEquals(2, sessionManager.numSessions());
    // Sleep to 1 ms before expiration.
    context.time().sleep(999);
    sessionManager.expireOldSessions();
    assertEquals(2, sessionManager.numSessions());
    for (Future future : futures) {
        assertFalse(future.isDone());
        assertFalse(future.isCancelled());
    }
    // Sleep to the exact time to expire
    context.time().sleep(1);
    sessionManager.expireOldSessions();
    assertEquals("All the sessions should have been expired", 0, sessionManager.numSessions());
    for (Future future : futures) {
        assertTrue(future.isDone());
        assertTrue(future.isCancelled());
    }
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) ArrayList(java.util.ArrayList) OperationFuture(com.linkedin.kafka.cruisecontrol.async.OperationFuture) Future(java.util.concurrent.Future) OperationFuture(com.linkedin.kafka.cruisecontrol.async.OperationFuture) Test(org.junit.Test)

Aggregations

MetricRegistry (com.codahale.metrics.MetricRegistry)505 Test (org.junit.Test)177 Before (org.junit.Before)61 Test (org.junit.jupiter.api.Test)45 VerifiableProperties (com.github.ambry.config.VerifiableProperties)42 ArrayList (java.util.ArrayList)33 Counter (com.codahale.metrics.Counter)30 File (java.io.File)29 Properties (java.util.Properties)28 List (java.util.List)23 Metric (com.codahale.metrics.Metric)22 Map (java.util.Map)22 IOException (java.io.IOException)21 HashMap (java.util.HashMap)20 Size (com.github.joschi.jadconfig.util.Size)17 CountDownLatch (java.util.concurrent.CountDownLatch)17 TimeUnit (java.util.concurrent.TimeUnit)17 Timer (com.codahale.metrics.Timer)15 DefaultTaggedMetricRegistry (com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry)15 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)15