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);
}
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);
}
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));
}
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);
}
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);
}
Aggregations