Skip to content

Commit

Permalink
Merge pull request #154 from rico-projects/additional-context-attributes
Browse files Browse the repository at this point in the history
Additional context attributes
  • Loading branch information
sclassen authored Jul 10, 2020
2 parents ccc9e13 + 685d823 commit 76fe775
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ public interface ContextConstants {

String APPLICATION_NAME_CONTEXT = "application.name";

String APPLICATION_START_TIME_CONTEXT = "application.startTime";

String APPLICATION_START_LOCALE_CONTEXT = "application.startLocale";

String APPLICATION_SYSTEM_TIMEZONE_CONTEXT = "application.systemTimezone";

String PLATFORM_VERSION_CONTEXT = "rico.version";

String JAVA_VERSION_CONTEXT = "java.version";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@
import org.slf4j.LoggerFactory;

import java.net.InetAddress;
import java.time.ZoneId;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import static dev.rico.internal.core.context.ContextConstants.APPLICATION_NAME_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.APPLICATION_START_LOCALE_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.APPLICATION_START_TIME_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.APPLICATION_SYSTEM_TIMEZONE_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.CANONICAL_HOST_NAME_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.HOST_ADDRESS_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.HOST_NAME_CONTEXT;
Expand All @@ -55,9 +60,10 @@ public class RicoApplicationContextImpl implements RicoApplicationContext {
RicoApplicationContextImpl() {
setGlobalAttribute(APPLICATION_NAME_CONTEXT, UNNAMED_APPLICATION);
setGlobalAttribute(PLATFORM_VERSION_CONTEXT, PlatformVersion.getVersion());

setGlobalAttribute(APPLICATION_START_TIME_CONTEXT, System.currentTimeMillis() + "");
setGlobalAttribute(APPLICATION_START_LOCALE_CONTEXT, Locale.getDefault() + "");
setGlobalAttribute(APPLICATION_SYSTEM_TIMEZONE_CONTEXT, ZoneId.systemDefault().getId());
setGlobalAttribute(OS_CONTEXT, OperationSystem.getLocalSystem().getShortName());

setGlobalAttribute(JAVA_VERSION_CONTEXT, System.getProperty(JAVA_VERSION_SYSTEM_PROPERTY));
setGlobalAttribute(JAVA_VENDOR_CONTEXT, System.getProperty(JAVA_VENDOR_SYSTEM_PROPERTY));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@

import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;

import static dev.rico.internal.core.context.ContextConstants.APPLICATION_NAME_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.APPLICATION_START_LOCALE_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.APPLICATION_START_TIME_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.APPLICATION_SYSTEM_TIMEZONE_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.CANONICAL_HOST_NAME_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.HOST_ADDRESS_CONTEXT;
import static dev.rico.internal.core.context.ContextConstants.HOST_NAME_CONTEXT;
Expand All @@ -38,7 +42,9 @@

public class RicoApplicationContextImplTests {

private static final int INITIAL_SIZE = 8;
private static final String[] INITIAL_GLOBAL_ATTRIBUTES = {HOST_NAME_CONTEXT, PLATFORM_VERSION_CONTEXT, CANONICAL_HOST_NAME_CONTEXT,
HOST_ADDRESS_CONTEXT, APPLICATION_NAME_CONTEXT, JAVA_VERSION_CONTEXT, JAVA_VENDOR_CONTEXT,
OS_CONTEXT, APPLICATION_START_TIME_CONTEXT, APPLICATION_START_LOCALE_CONTEXT, APPLICATION_SYSTEM_TIMEZONE_CONTEXT};

private static final String key_1 = "KEY-1";
private static final String key_2 = "KEY-2";
Expand All @@ -56,34 +62,12 @@ public void setup() {
public void testInitialState() {
assertEquals(manager.getThreadLocalAttributes().size(), 0);

assertEquals(manager.getGlobalAttributes().size(), INITIAL_SIZE);
assertNameExists(manager.getGlobalAttributes(), HOST_NAME_CONTEXT);
assertNameExists(manager.getGlobalAttributes(), PLATFORM_VERSION_CONTEXT);
assertNameExists(manager.getGlobalAttributes(), CANONICAL_HOST_NAME_CONTEXT);
assertNameExists(manager.getGlobalAttributes(), HOST_ADDRESS_CONTEXT);
assertNameExists(manager.getGlobalAttributes(), APPLICATION_NAME_CONTEXT);
assertNameExists(manager.getGlobalAttributes(), JAVA_VERSION_CONTEXT);
assertNameExists(manager.getGlobalAttributes(), JAVA_VENDOR_CONTEXT);
assertNameExists(manager.getGlobalAttributes(), OS_CONTEXT);

assertEquals(manager.getAttributes().size(), INITIAL_SIZE);
assertNameExists(manager.getAttributes(), HOST_NAME_CONTEXT);
assertNameExists(manager.getAttributes(), PLATFORM_VERSION_CONTEXT);
assertNameExists(manager.getAttributes(), CANONICAL_HOST_NAME_CONTEXT);
assertNameExists(manager.getAttributes(), HOST_ADDRESS_CONTEXT);
assertNameExists(manager.getAttributes(), APPLICATION_NAME_CONTEXT);
assertNameExists(manager.getAttributes(), JAVA_VERSION_CONTEXT);
assertNameExists(manager.getAttributes(), JAVA_VENDOR_CONTEXT);
assertNameExists(manager.getAttributes(), OS_CONTEXT);

assertTrue(manager.getAttribute(HOST_NAME_CONTEXT).isPresent());
assertTrue(manager.getAttribute(PLATFORM_VERSION_CONTEXT).isPresent());
assertTrue(manager.getAttribute(CANONICAL_HOST_NAME_CONTEXT).isPresent());
assertTrue(manager.getAttribute(HOST_ADDRESS_CONTEXT).isPresent());
assertTrue(manager.getAttribute(APPLICATION_NAME_CONTEXT).isPresent());
assertTrue(manager.getAttribute(JAVA_VERSION_CONTEXT).isPresent());
assertTrue(manager.getAttribute(JAVA_VENDOR_CONTEXT).isPresent());
assertTrue(manager.getAttribute(OS_CONTEXT).isPresent());
assertEquals(manager.getGlobalAttributes().size(), INITIAL_GLOBAL_ATTRIBUTES.length);
assertEquals(manager.getAttributes().size(), INITIAL_GLOBAL_ATTRIBUTES.length);

Stream.of(INITIAL_GLOBAL_ATTRIBUTES).forEach(n -> assertNameExists(manager.getGlobalAttributes(), n));
Stream.of(INITIAL_GLOBAL_ATTRIBUTES).forEach(n -> assertNameExists(manager.getAttributes(), n));
Stream.of(INITIAL_GLOBAL_ATTRIBUTES).forEach(n -> assertTrue(manager.getAttribute(n).isPresent()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public void doFilter(final ServletRequest request, final ServletResponse respons
final StringPair contextPathTag = ContextServerUtil.createContextPathTag((HttpServletRequest) request);
final StringPair portTag = ContextServerUtil.createPortTag((HttpServletRequest) request);
MetricsImpl.getInstance()
.getOrCreateTimer(HTTP_REQUESTS_METRIC_NAME, contextPathTag, uriTag, methodTag, portTag)
.getOrCreateTimer(HTTP_REQUEST_TIME_METRIC_NAME, contextPathTag, uriTag, methodTag, portTag)
.record(timeInMs, TimeUnit.MILLISECONDS);
MetricsImpl.getInstance()
.getOrCreateCounter(HTTP_REQUEST_TIME_METRIC_NAME, contextPathTag, uriTag, methodTag, portTag)
.getOrCreateCounter(HTTP_REQUESTS_METRIC_NAME, contextPathTag, uriTag, methodTag, portTag)
.increment();
}
}
Expand Down

0 comments on commit 76fe775

Please sign in to comment.