Skip to content

Commit

Permalink
Revert changes to audit metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
fhanik committed Sep 21, 2017
1 parent 35ff63b commit 3b0b313
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 90 deletions.
Expand Up @@ -10,7 +10,6 @@
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*******************************************************************************/

package org.cloudfoundry.identity.statsd;

import com.timgroup.statsd.NonBlockingStatsDClient;
Expand Down
Expand Up @@ -39,32 +39,32 @@
*/
public class Log4jContextInitializer implements ServletContextListener, Filter {

@Override
public void contextInitialized(ServletContextEvent sce) {
MDC.put("context", sce.getServletContext().getContextPath());
}
@Override
public void contextInitialized(ServletContextEvent sce) {
MDC.put("context", sce.getServletContext().getContextPath());
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
MDC.put("context", ((HttpServletRequest)request).getContextPath());
try {
chain.doFilter(request, response);
} finally {
MDC.remove("context");
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
MDC.put("context", ((HttpServletRequest)request).getContextPath());
try {
chain.doFilter(request, response);
} finally {
MDC.remove("context");
}
}

@Override
public void destroy() {
}
@Override
public void destroy() {
}

}
Expand Up @@ -86,7 +86,7 @@ public Set<java.util.Map.Entry<String, Object>> entrySet() {
for (MBeanOperationInfo operation : operations) {
String key = operation.getName();
if (key.startsWith("get") && operation.getSignature().length == 0) {
String attribute = StringUtils.camelToPeriod(key.substring(3));
String attribute = StringUtils.camelToUnderscore(key.substring(3));
if (map.containsKey(attribute)) {
continue;
}
Expand Down Expand Up @@ -168,7 +168,7 @@ private void verySafePut(Map<? extends Object, Object> map, Object key, Object v
private void safePut(Map<Object, Object> map, Object key, Object value, boolean prettifyKeys) {
Object property = key;
if (key instanceof String && prettifyKeys) {
property = StringUtils.camelToPeriod((String) key);
property = StringUtils.camelToUnderscore((String) key);
}
// Don't prettify system property keys in case user has added upper case properties
map.put(property, getCompositeWrapper(value, prettifyKeys && !key.equals("SystemProperties")));
Expand Down
Expand Up @@ -13,32 +13,29 @@

package org.cloudfoundry.identity.statsd;

/**
* @author Dave Syer
*
*/

public class StringUtils {

/**
* Convert a string from camel case to underscores, also replacing periods with underscores (so for example a fully
* qualified Java class name gets underscores everywhere).
*
* @param value a camel case String
* @return the same value with camels converted to underscores
*/
public static String camelToUnderscore(String value) {
String result = value.replace(" ", "_");
result = result.replaceAll("([a-z])([A-Z])", "$1_$2");
result = result.replace(".", "_");
result = result.toLowerCase();
return result;
}
/**
* Convert a string from camel case to underscores, also replacing periods with underscores (so for example a fully
* qualified Java class name gets underscores everywhere).
*
* @param value a camel case String
* @return the same value with camels converted to underscores
*/
public static String camelToUnderscore(String value) {
return camelToDelimiter(value, "_");
}

public static String camelToDelimiter(String value, String delimiter) {
String result = value.replace(" ", delimiter);
result = result.replaceAll("([a-z])([A-Z])", "$1" + delimiter + "$2");
result = result.replace(".", delimiter);
result = result.toLowerCase();
return result;
}

public static String camelToPeriod(String key) {
String result = key.replace(" ", ".");
result = result.replaceAll("([a-z])([A-Z])", "$1.$2");
result = result.replace("_", ".");
result = result.toLowerCase();
return result;
}
public static String camelToPeriod(String value) {
return camelToDelimiter(value, "_");
}
}
Expand Up @@ -25,25 +25,25 @@

public class MBeanMapTests {

private MBeanServerConnection server;

@Before
public void start() throws Exception {
MBeanServerFactoryBean factory = new MBeanServerFactoryBean();
factory.setLocateExistingServerIfPossible(true);
factory.afterPropertiesSet();
server = factory.getObject();
}

@Test
public void testListDomain() throws Exception {
Set<ObjectName> names = server.queryNames(ObjectName.getInstance("java.lang:type=Runtime,*"), null);
System.err.println(names);
assertTrue(names.size() == 1);
MBeanMap result = new MBeanMap(server, names.iterator().next());
@SuppressWarnings("unchecked")
Map<String,String> properties = (Map<String, String>) result.get("system.properties");
assertTrue(properties.containsKey("java.vm.version"));
}

}
private MBeanServerConnection server;

@Before
public void start() throws Exception {
MBeanServerFactoryBean factory = new MBeanServerFactoryBean();
factory.setLocateExistingServerIfPossible(true);
factory.afterPropertiesSet();
server = factory.getObject();
}

@Test
public void testListDomain() throws Exception {
Set<ObjectName> names = server.queryNames(ObjectName.getInstance("java.lang:type=Runtime,*"), null);
System.err.println(names);
assertTrue(names.size() == 1);
MBeanMap result = new MBeanMap(server, names.iterator().next());
@SuppressWarnings("unchecked")
Map<String,String> properties = (Map<String, String>) result.get("system_properties");
assertTrue(properties.containsKey("java.vm.version"));
}

}
Expand Up @@ -18,18 +18,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/*******************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2015] Pivotal Software, Inc. All Rights Reserved.
* <p/>
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
* <p/>
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*******************************************************************************/
public class IntegrationTestUtils {

public static final String UAA_BASE_URL = "http://localhost:8080/uaa";
Expand Down
Expand Up @@ -50,11 +50,20 @@ public class UaaMetricsEmitterIT {
private static DatagramPacket receivePacket;
private static Map<String, String> firstBatch;
private static List<String> gaugeFragments = Arrays.asList(
"uaa.audit_service.user_authentication_count",
"uaa.audit_service.principal_not_found_count",
"uaa.audit_service.client_authentication_failure_count",
"uaa.audit_service.user_authentication_count",
"uaa.audit_service.user_authentication_failure_count",
"uaa.audit_service.user_not_found_count",
"uaa.audit_service.principal_authentication_failure_count",
"uaa.audit_service.user_password_failures",
"uaa.audit_service.client_authentication_count",
"uaa.audit_service.user_password_changes",
"uaa.requests.global.completed.count",
"uaa.requests.global.completed.count",
"uaa.requests.global.unhealthy.time",
"uaa.requests.global.unhealthy.count",
"uaa.audit_service.user.authentication.count:",
"uaa.server.inflight.count",
"uaa.requests.global.status_1xx.count",
"uaa.requests.global.status_2xx.count",
Expand Down Expand Up @@ -96,13 +105,13 @@ public static void setUpOnce() throws IOException {
@Test
public void assert_gauge_metric() throws IOException {
String data1 = firstBatch.get(statsDKey);
assertNotNull("Expected to find message for:"+statsDKey+" in the first batch.", data1);
assertNotNull("Expected to find message for:'"+statsDKey+"' in the first batch.", data1);
String data2 = secondBatch.get(statsDKey);
assertNotNull("Expected to find message for:"+statsDKey+" in the second batch.", data2);
assertNotNull("Expected to find message for:'"+statsDKey+"' in the second batch.", data2);
long first = IntegrationTestUtils.getGaugeValueFromMessage(data1);
long second = IntegrationTestUtils.getGaugeValueFromMessage(data2);
assertThat(statsDKey+" has a positive value.", first, greaterThanOrEqualTo(0l));
assertThat(statsDKey+" has a positive value larger than or equal to the first.", second, greaterThanOrEqualTo(first));
assertThat(statsDKey+" must have a positive value.", first, greaterThanOrEqualTo(0l));
assertThat(statsDKey+" must have a positive value larger than or equal to the first.", second, greaterThanOrEqualTo(first));
}


Expand Down

0 comments on commit 3b0b313

Please sign in to comment.