UaaMetricsEmitter { @Scheduled(fixedRate = 5000, initialDelay = 0) public void emitMetrics() throws Exception { Map<String, Object> result = new LinkedHashMap<>(); Map<String, ?> spring = metricsUtils.pullUpMap("cloudfoundry.identity", "*", server); if (spring != null) { result.put("audit_service", getValueFromMap(spring, "#this['UaaAudit']")); } for (Map.Entry entry : result.entrySet()) { String prefix = entry.getKey() + "."; MBeanMap properties = (MBeanMap) entry.getValue(); if (properties != null) { properties.entrySet() .stream() .filter(e -> e.getValue() != null && e.getValue() instanceof Integer) .forEach(e -> statsDClient.gauge(prefix+e.getKey(), ((Integer) e.getValue()).longValue())); } } } UaaMetricsEmitter(MetricsUtils metricsUtils, StatsDClient statsDClient, MBeanServerConnection server); @Scheduled(fixedRate = 5000, initialDelay = 0) void emitMetrics(); @Scheduled(fixedRate = 5000, initialDelay = 1000) void emitGlobalRequestMetrics(); @Scheduled(fixedRate = 5000, initialDelay = 1000) void emitUrlGroupRequestMetrics(); void emitGlobalServerStats(UaaMetrics metrics); void emitGlobalRequestMetrics(UaaMetrics metrics); @Scheduled(fixedRate = 5000, initialDelay = 2000) void emitVmVitals(); @Scheduled(fixedRate = 5000, initialDelay = 3000) void emitJvmVitals(); void throwIfOtherThanNotFound(Exception x); Number getValueFromBean(Object mbean, String getter); Object getValueFromMap(Map<String, ?> map, String path); void enableNotification(); long getMetricDelta(String name, long gaugeValue); boolean isNotificationEnabled();  }
@Test 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); }
@Test void auditService_metricValues_areNull() throws Exception { mBeanMap1.put("user_authentication_count", null); Mockito.when(metricsUtils.pullUpMap("cloudfoundry.identity", "*", server)).thenReturn((Map) mBeanMap2); uaaMetricsEmitter.emitMetrics(); Mockito.verify(statsDClient).gauge("audit_service.user_not_found_count", 1); Mockito.verify(statsDClient, times(6)).gauge(anyString(), anyLong()); }
@Test void auditService_Key_isNull() throws Exception { mBeanMap2.put("UaaAudit", null); Mockito.when(metricsUtils.pullUpMap("cloudfoundry.identity", "*", server)).thenReturn((Map) mBeanMap2); uaaMetricsEmitter.emitMetrics(); Mockito.verify(statsDClient, times(0)).gauge(anyString(), anyLong()); }