diff --git a/base/rico-core/src/main/java/dev/rico/internal/core/context/ContextConstants.java b/base/rico-core/src/main/java/dev/rico/internal/core/context/ContextConstants.java index dc2d3ea2..8f3d82f8 100644 --- a/base/rico-core/src/main/java/dev/rico/internal/core/context/ContextConstants.java +++ b/base/rico-core/src/main/java/dev/rico/internal/core/context/ContextConstants.java @@ -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"; diff --git a/base/rico-core/src/main/java/dev/rico/internal/core/context/RicoApplicationContextImpl.java b/base/rico-core/src/main/java/dev/rico/internal/core/context/RicoApplicationContextImpl.java index ff7572b9..138c56a2 100644 --- a/base/rico-core/src/main/java/dev/rico/internal/core/context/RicoApplicationContextImpl.java +++ b/base/rico-core/src/main/java/dev/rico/internal/core/context/RicoApplicationContextImpl.java @@ -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; @@ -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)); diff --git a/base/rico-core/src/test/java/dev/rico/internal/core/context/RicoApplicationContextImplTests.java b/base/rico-core/src/test/java/dev/rico/internal/core/context/RicoApplicationContextImplTests.java index d28bf6dc..0bee25a2 100644 --- a/base/rico-core/src/test/java/dev/rico/internal/core/context/RicoApplicationContextImplTests.java +++ b/base/rico-core/src/test/java/dev/rico/internal/core/context/RicoApplicationContextImplTests.java @@ -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; @@ -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"; @@ -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 diff --git a/metrics/rico-metrics-server/src/main/java/dev/rico/internal/metrics/server/servlet/RequestMetricsFilter.java b/metrics/rico-metrics-server/src/main/java/dev/rico/internal/metrics/server/servlet/RequestMetricsFilter.java index 99faf912..4a1a47c2 100644 --- a/metrics/rico-metrics-server/src/main/java/dev/rico/internal/metrics/server/servlet/RequestMetricsFilter.java +++ b/metrics/rico-metrics-server/src/main/java/dev/rico/internal/metrics/server/servlet/RequestMetricsFilter.java @@ -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(); } }