Mockito.when(metricsUtils.getUaaMetrics(any())).thenReturn(uaaMetrics1, uaaMetrics2); uaaMetricsEmitter.emitGlobalRequestMetrics(); Mockito.verify(statsDClient).count("requests.global.completed.count", 3087l); Mockito.verify(statsDClient).gauge("requests.global.completed.time", 29l); Mockito.verify(statsDClient).count("requests.global.unhealthy.count", 1l); Mockito.verify(statsDClient).gauge("requests.global.unhealthy.time", 4318l); Mockito.verify(statsDClient).count("requests.global.status_1xx.count", 0l); Mockito.verify(statsDClient).count("requests.global.status_2xx.count", 2148l); Mockito.verify(statsDClient).count("requests.global.status_3xx.count", 763l); Mockito.verify(statsDClient).count("requests.global.status_4xx.count", 175l); Mockito.verify(statsDClient).count("requests.global.status_5xx.count", 1l); Mockito.verify(statsDClient).gauge("server.inflight.count", 3l); Mockito.verify(statsDClient).gauge("server.up.time", 12349843l); Mockito.verify(statsDClient).gauge("server.idle.time", 12349l); Mockito.verify(statsDClient).count("database.global.completed.count", 83797l); Mockito.verify(statsDClient).gauge("database.global.completed.time", 0l); Mockito.verify(statsDClient).count("database.global.unhealthy.count", 17549l); Mockito.verify(statsDClient).gauge("database.global.unhealthy.time", 0l); reset(statsDClient); uaaMetricsEmitter.emitGlobalRequestMetrics(); Mockito.verify(statsDClient).count("requests.global.completed.count", 4l); Mockito.verify(statsDClient).count("requests.global.unhealthy.count", 1l); Mockito.verify(statsDClient).count("requests.global.status_1xx.count", 0l); Mockito.verify(statsDClient).count("requests.global.status_2xx.count", 1l); Mockito.verify(statsDClient).count("requests.global.status_3xx.count", 1l); Mockito.verify(statsDClient).count("requests.global.status_4xx.count", 1l); Mockito.verify(statsDClient).count("requests.global.status_5xx.count", 1l); Mockito.verify(statsDClient).count("database.global.completed.count", 2l); Mockito.verify(statsDClient).count("database.global.unhealthy.count", 5l);
@Override public void close() { statsd.stop(); }
private void reportMetrics() { for (String metricName: _metricNames) { String mbeanExpr = metricName.substring(0, metricName.lastIndexOf(":")); String attributeExpr = metricName.substring(metricName.lastIndexOf(":") + 1); List<MbeanAttributeValue> attributeValues = getMBeanAttributeValues(mbeanExpr, attributeExpr); for (MbeanAttributeValue attributeValue: attributeValues) { final String statsdMetricName = generateStatsdMetricName(attributeValue.mbean(), attributeValue.attribute()); _statsdClient.recordGaugeValue(statsdMetricName, new Double(attributeValue.value()).longValue()); } } } }
switch (statsDMetric.type) { case count: statsd.count(fullName, value.doubleValue(), tags); break; case timer: statsd.time(fullName, value.longValue(), tags); break; case gauge: statsd.gauge(fullName, value.doubleValue(), tags); break; statsd.count(fullName, val, tags); break; case timer: statsd.time(fullName, val, tags); break; case gauge: statsd.gauge(fullName, val, tags); break;
@Test public void auditService_metrics_emitted() throws Exception { Mockito.when(metricsUtils.pullUpMap("cloudfoundry.identity", "*", server)).thenReturn((Map)mBeanMap2); uaaMetricsEmitter.emitMetrics(); Mockito.verify(statsDClient).gauge("audit_service.user_authentication_count", 3); Mockito.verify(statsDClient).gauge("audit_service.user_not_found_count", 1); Mockito.verify(statsDClient).gauge("audit_service.principal_authentication_failure_count", 4); Mockito.verify(statsDClient).gauge("audit_service.principal_not_found_count", 5); Mockito.verify(statsDClient).gauge("audit_service.user_authentication_failure_count", 6); Mockito.verify(statsDClient).gauge("audit_service.client_authentication_count", 7); Mockito.verify(statsDClient).gauge("audit_service.client_authentication_failure_count", 42); }
@Override public void updateImpl(List<Metric> metrics) { // The statsd client doesn't do any checks on the underlying socket's state // and the socket connects only once, so we cannot trust the socket to stay // open over a period of time. If this is changed/fixed we could reuse the // client but until then it cannot be safely reused. StatsDClient statsd = createClient(); LOGGER.debug("sending data"); try { for (Metric metric : metrics) { String aspect = namingConvention.getName(metric); if (metric.getConfig().getTags().getTag(DataSourceType.COUNTER.getValue()) != null) { statsd.count(aspect, metric.getNumberValue().longValue()); } else if (metric.hasNumberValue()) { statsd.gauge(aspect, metric.getNumberValue().longValue()); } else { statsd.set(aspect, metric.getValue().toString()); } statsd.time(aspect, metric.getTimestamp() / 1000); } } finally { statsd.stop(); } }
/** Gets specs for all entities registered in the registry. */ @Override public void listEntities(Empty request, StreamObserver<ListEntitiesResponse> responseObserver) { long now = System.currentTimeMillis(); statsDClient.increment("list_entities_request_count"); try { List<EntitySpec> entitySpecs = specService .listEntities() .stream() .map(EntityInfo::getEntitySpec) .collect(Collectors.toList()); ListEntitiesResponse response = ListEntitiesResponse.newBuilder().addAllEntities(entitySpecs).build(); responseObserver.onNext(response); responseObserver.onCompleted(); statsDClient.increment("list_entities_request_success"); } catch (RetrievalException e) { statsDClient.increment("list_entities_request_failed"); log.error("Error in listEntities: {}", e); responseObserver.onError(getRuntimeException(e)); } finally { long duration = System.currentTimeMillis() - now; statsDClient.gauge("list_entities_latency_ms", duration); } }
@Override public void recordCount(String parameterName, Long value){ statsd.count(parameterName, value); }
protected void sendMetricPoint( String metricType, String metricName, double value, String[] tags) { if (System.currentTimeMillis() - this.initializationTime > 300 * 1000) { this.statsDClient.stop(); init(); } if (metricType.equals("histogram")) { statsDClient.histogram(metricName, value, tags); } else { statsDClient.gauge(metricName, value, tags); } }
statsd.incrementCounter("completed", tagsToCounter); logger.fine(String.format("Attempted to send 'completed' counter with tags: %s", Arrays.toString(tagsToCounter))); } catch (StatsDClientException e) { logger.severe(ex.getMessage()); statsd.stop();
/** * Submits service check. * */ public void doSendServiceCheck(String checkName, String status, String message, String[] tags) { if (System.currentTimeMillis() - this.initializationTime > 300 * 1000) { this.statsDClient.stop(); init(); } ServiceCheck sc = new ServiceCheck( String.format("%s.can_connect", checkName), this.statusToInt(status), message, tags); statsDClient.serviceCheck(sc); }
@Override public void time(String name, long duration, TimeUnit unit) { client.recordExecutionTime(name, unit.toMillis(duration)); } }
/** * Increment the count for the given counter type. * * @param type the type of counter */ public synchronized void incrementCounter(Type type) { long currentCount = 0; if (counters.get(type) != null) { currentCount = counters.get(type); } counters.put(type, currentCount + 1); statsDClient.incrementCounter( String.format(REPLICATION_STATUS_COUNT, type.toString().toLowerCase())); }
@Test public void testNotifications() { uaaMetricsEmitter.enableNotification(); emitter.sendNotification(new Notification("/api", 45L, 0)); Mockito.verify(statsDClient).time("requests.api.latency", 45L); }
@Test public void vm_vitals() { uaaMetricsEmitter.emitVmVitals(); Mockito.verify(statsDClient).gauge(eq("vitals.vm.cpu.count"), gt(0l)); Mockito.verify(statsDClient).gauge(eq("vitals.vm.cpu.load"), geq(0l)); Mockito.verify(statsDClient).gauge(eq("vitals.vm.memory.total"), geq(134217728l)); Mockito.verify(statsDClient).gauge(eq("vitals.vm.memory.committed"), geq(1l)); Mockito.verify(statsDClient).gauge(eq("vitals.vm.memory.free"), geq(1l)); }
public void sendLiveStat(String groupName, Counter counter, long value) { if (client != null) { if (log.isTraceEnabled()) { log.trace("Looking for aspect matching " + groupName + " / " + counter.getName()); } CounterToStatsDConfiguration.StatsDAspect aspect = config.getAspect(CounterToStatsDConfiguration.StatsDOutputType.LIVE, groupName, counter); if (aspect != null) { log.trace("Found aspect " + aspect); String fullName = aspect.getFullName(counter.getName()); log.trace("Sending " + aspect.getType() + '(' + fullName + " -> " + counter.getValue() + ')'); switch (aspect.getType()) { case GAUGE: client.gauge(fullName, value); break; case COUNTER: client.count(fullName, value); break; default: client.time(fullName, value); } } } }
/** Gets specs for all features registered in the registry. TODO: some kind of pagination */ @Override public void listFeatures(Empty request, StreamObserver<ListFeaturesResponse> responseObserver) { long now = System.currentTimeMillis(); statsDClient.increment("list_features_request_count"); try { List<FeatureSpec> featureSpecs = specService .listFeatures() .stream() .map(FeatureInfo::getFeatureSpec) .collect(Collectors.toList()); ListFeaturesResponse response = ListFeaturesResponse.newBuilder().addAllFeatures(featureSpecs).build(); responseObserver.onNext(response); responseObserver.onCompleted(); statsDClient.increment("list_features_request_success"); } catch (RetrievalException e) { statsDClient.increment("list_features_request_failed"); log.error("Error in listFeatures: {}", e); responseObserver.onError(getRuntimeException(e)); } finally { long duration = System.currentTimeMillis() - now; statsDClient.gauge("list_features_latency_ms", duration); } }
@Override protected void processDataPoint(IMetricsConsumer.DataPoint dataPoint) { statsDClient.count(dataPoint.name, (long) dataPoint.value); }
public void recordDelta(String parameterName, Long delta) { if (log.isTraceEnabled()) { log.trace(String.format("Recording time. parameterName: %s; delta: %s", parameterName, delta)); } statsd.recordExecutionTime(parameterName, delta); }
/** * Increment the count for the given counter type. * * @param type the type of counter */ public synchronized void incrementCounter(Type type) { long currentCount = 0; if (counters.get(type) != null) { currentCount = counters.get(type); } counters.put(type, currentCount + 1); statsDClient.incrementCounter( String.format(REPLICATION_STATUS_COUNT, type.toString().toLowerCase())); }