Search in sources :

Example 41 with Metric

use of com.codahale.metrics.Metric in project graylog2-server by Graylog2.

the class MetricsResource method byNamespace.

@GET
@Timed
@Path("/namespace/{namespace}")
@ApiOperation(value = "Get all metrics of a namespace")
@ApiResponses(value = { @ApiResponse(code = 404, message = "No such metric namespace") })
@Produces(MediaType.APPLICATION_JSON)
public MetricsSummaryResponse byNamespace(@ApiParam(name = "namespace", required = true) @PathParam("namespace") String namespace) {
    final List<Map<String, Object>> metrics = Lists.newArrayList();
    for (Map.Entry<String, Metric> e : metricRegistry.getMetrics().entrySet()) {
        final String metricName = e.getKey();
        if (metricName.startsWith(namespace) && isPermitted(RestPermissions.METRICS_READ, metricName)) {
            try {
                final Metric metric = e.getValue();
                metrics.add(MetricUtils.map(metricName, metric));
            } catch (Exception ex) {
                LOG.warn("Could not read metric in namespace list.", ex);
            }
        }
    }
    if (metrics.isEmpty()) {
        final String msg = "No metrics with namespace [" + namespace + "] found.";
        LOG.debug(msg);
        throw new NotFoundException(msg);
    }
    return MetricsSummaryResponse.create(metrics);
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) Metric(com.codahale.metrics.Metric) Map(java.util.Map) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 42 with Metric

use of com.codahale.metrics.Metric in project graylog2-server by Graylog2.

the class MetricsResource method multipleMetrics.

@POST
@Timed
@Path("/multiple")
@ApiOperation("Get the values of multiple metrics at once")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Malformed body") })
@NoAuditEvent("only used to retrieve multiple metrics")
public MetricsSummaryResponse multipleMetrics(@ApiParam(name = "Requested metrics", required = true) @Valid @NotNull MetricsReadRequest request) {
    final Map<String, Metric> metrics = metricRegistry.getMetrics();
    final List<Map<String, Object>> metricsList = Lists.newArrayList();
    for (String name : request.metrics()) {
        if (!isPermitted(RestPermissions.METRICS_READ, name)) {
            continue;
        }
        final Metric metric = metrics.get(name);
        if (metric != null) {
            metricsList.add(MetricUtils.map(name, metric));
        }
    }
    return MetricsSummaryResponse.create(metricsList);
}
Also used : Metric(com.codahale.metrics.Metric) Map(java.util.Map) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 43 with Metric

use of com.codahale.metrics.Metric in project riposte by Nike-Inc.

the class EndpointMetricsHandlerDefaultImplTest method setupMetricRegistryMock.

private void setupMetricRegistryMock() {
    metricRegistryMock = mock(MetricRegistry.class);
    registeredTimers = new HashMap<>();
    doAnswer(invocation -> {
        Timer originalTimer = (Timer) invocation.callRealMethod();
        return spy(originalTimer);
    }).when(instance).createAndRegisterRequestTimer(anyString(), any(MetricRegistry.class));
    doThrow(new RuntimeException("All timer creation should go through createAndRegisterRequestTimer(), *not* metricRegistry.timer(...).")).when(metricRegistryMock).timer(anyString());
    registeredMeterMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Meter meterMock = mock(Meter.class);
        registeredMeterMocks.put(name, meterMock);
        return meterMock;
    }).when(metricRegistryMock).meter(anyString());
    registeredCounterMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Counter counterMock = mock(Counter.class);
        registeredCounterMocks.put(name, counterMock);
        return counterMock;
    }).when(metricRegistryMock).counter(anyString());
    registeredHistogramMocks = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Histogram histogramMock = mock(Histogram.class);
        registeredHistogramMocks.put(name, histogramMock);
        return histogramMock;
    }).when(metricRegistryMock).histogram(anyString());
    registeredGauges = new HashMap<>();
    doAnswer(invocation -> {
        String name = invocation.getArgumentAt(0, String.class);
        Metric metric = invocation.getArgumentAt(1, Metric.class);
        if (metric instanceof Gauge)
            registeredGauges.put(name, (Gauge) metric);
        else if (metric instanceof Timer)
            registeredTimers.put(name, (Timer) metric);
        else
            throw new RuntimeException("Expected Gauge or Timer, but received: " + metric.getClass().getName());
        return metric;
    }).when(metricRegistryMock).register(anyString(), any(Metric.class));
}
Also used : Histogram(com.codahale.metrics.Histogram) Counter(com.codahale.metrics.Counter) Timer(com.codahale.metrics.Timer) Meter(com.codahale.metrics.Meter) MetricRegistry(com.codahale.metrics.MetricRegistry) Metric(com.codahale.metrics.Metric) Matchers.anyString(org.mockito.Matchers.anyString) Gauge(com.codahale.metrics.Gauge)

Example 44 with Metric

use of com.codahale.metrics.Metric in project riposte by Nike-Inc.

the class SignalFxEndpointMetricsHandlerTest method RollingWindowTimerBuilder_isInstance_works_as_expected.

@DataProvider(value = { "true   |   true", "false  |   false" }, splitBy = "\\|")
@Test
public void RollingWindowTimerBuilder_isInstance_works_as_expected(boolean useTimer, boolean expectedResult) {
    // given
    Metric metric = (useTimer) ? mock(Timer.class) : mock(Gauge.class);
    RollingWindowTimerBuilder rwtb = new RollingWindowTimerBuilder(42, TimeUnit.DAYS);
    // when
    boolean result = rwtb.isInstance(metric);
    // then
    assertThat(result).isEqualTo(expectedResult);
}
Also used : Timer(com.codahale.metrics.Timer) Metric(com.codahale.metrics.Metric) RollingWindowTimerBuilder(com.nike.riposte.metrics.codahale.impl.SignalFxEndpointMetricsHandler.RollingWindowTimerBuilder) Gauge(com.codahale.metrics.Gauge) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 45 with Metric

use of com.codahale.metrics.Metric in project alluxio by Alluxio.

the class WebInterfaceMasterMetricsServlet method populateValues.

/**
   * Populates key, value pairs for UI display.
   *
   * @param request The {@link HttpServletRequest} object
   * @throws IOException if an I/O error occurs
   */
private void populateValues(HttpServletRequest request) throws IOException {
    MetricRegistry mr = MetricsSystem.METRIC_REGISTRY;
    Long masterCapacityTotal = (Long) mr.getGauges().get(MetricsSystem.getMasterMetricName(BlockMaster.Metrics.CAPACITY_TOTAL)).getValue();
    Long masterCapacityUsed = (Long) mr.getGauges().get(MetricsSystem.getMasterMetricName(BlockMaster.Metrics.CAPACITY_USED)).getValue();
    int masterCapacityUsedPercentage = (masterCapacityTotal > 0) ? (int) (100L * masterCapacityUsed / masterCapacityTotal) : 0;
    request.setAttribute("masterCapacityUsedPercentage", masterCapacityUsedPercentage);
    request.setAttribute("masterCapacityFreePercentage", 100 - masterCapacityUsedPercentage);
    Long masterUnderfsCapacityTotal = (Long) mr.getGauges().get(MetricsSystem.getMasterMetricName(FileSystemMaster.Metrics.UFS_CAPACITY_TOTAL)).getValue();
    Long masterUnderfsCapacityUsed = (Long) mr.getGauges().get(MetricsSystem.getMasterMetricName(FileSystemMaster.Metrics.UFS_CAPACITY_USED)).getValue();
    int masterUnderfsCapacityUsedPercentage = (masterUnderfsCapacityTotal > 0) ? (int) (100L * masterUnderfsCapacityUsed / masterUnderfsCapacityTotal) : 0;
    request.setAttribute("masterUnderfsCapacityUsedPercentage", masterUnderfsCapacityUsedPercentage);
    request.setAttribute("masterUnderfsCapacityFreePercentage", 100 - masterUnderfsCapacityUsedPercentage);
    Map<String, Counter> counters = mr.getCounters(new MetricFilter() {

        @Override
        public boolean matches(String name, Metric metric) {
            return !(name.endsWith("Ops"));
        }
    });
    Map<String, Counter> rpcInvocations = mr.getCounters(new MetricFilter() {

        @Override
        public boolean matches(String name, Metric metric) {
            return name.endsWith("Ops");
        }
    });
    Map<String, Metric> operations = new TreeMap<>();
    // Remove the instance name from the metrics.
    for (Map.Entry<String, Counter> entry : counters.entrySet()) {
        operations.put(MetricsSystem.stripInstanceAndHost(entry.getKey()), entry.getValue());
    }
    String filesPinnedProperty = MetricsSystem.getMasterMetricName(FileSystemMaster.Metrics.FILES_PINNED);
    operations.put(MetricsSystem.stripInstanceAndHost(filesPinnedProperty), mr.getGauges().get(filesPinnedProperty));
    Map<String, Counter> rpcInvocationsUpdated = new TreeMap<>();
    for (Map.Entry<String, Counter> entry : rpcInvocations.entrySet()) {
        rpcInvocationsUpdated.put(MetricsSystem.stripInstanceAndHost(entry.getKey()), entry.getValue());
    }
    populateCounterValues(operations, rpcInvocationsUpdated, request);
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) TreeMap(java.util.TreeMap) MetricFilter(com.codahale.metrics.MetricFilter) Counter(com.codahale.metrics.Counter) Metric(com.codahale.metrics.Metric) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

Metric (com.codahale.metrics.Metric)58 Test (org.junit.Test)27 MetricRegistry (com.codahale.metrics.MetricRegistry)16 Map (java.util.Map)16 HashMap (java.util.HashMap)13 Counter (com.codahale.metrics.Counter)10 Gauge (com.codahale.metrics.Gauge)10 Timer (com.codahale.metrics.Timer)10 MetricFilter (com.codahale.metrics.MetricFilter)8 MetricFilter (com.kixeye.chassis.support.metrics.MetricFilter)8 Meter (com.codahale.metrics.Meter)7 Histogram (com.codahale.metrics.Histogram)4 SolrMetricManager (org.apache.solr.metrics.SolrMetricManager)4 Timed (com.codahale.metrics.annotation.Timed)3 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 IOException (java.io.IOException)3 List (java.util.List)3 Random (java.util.Random)3 Collectors (java.util.stream.Collectors)3