User friendly map toString in StatisticsUtils (#2203)
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -40,7 +40,7 @@ import org.springframework.scheduling.annotation.Scheduled;
|
||||
* <ol>
|
||||
* <li>The Spring property spring.jpa.properties.eclipselink.profiler=PerformanceMonitor shall be set - enables Eclipselink statistics
|
||||
* collecting</li>
|
||||
* <li>By default the stdout log is disabled by setting hawkbit.jpa.statistics.dumpPeriodMS=9223372036854775807 (Long.MAX_VALUE) -
|
||||
* <li>By default the periodic stdout log is disabled by setting hawkbit.jpa.statistics.dumpPeriodMS=9223372036854775807 (Long.MAX_VALUE) -
|
||||
* i.e. effectively <b>never</b>. If log is required it should be set to the required period</li>
|
||||
* <li>The MeterRegistry shall be registered available - e.g. include org.springframework.boot:spring-boot-actuator-autoconfigure</li>
|
||||
* <li>(?) When using in test the metrics MAYBE shall be enabled with @AutoConfigureObservability(tracing = false)</li>
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
* <ol>
|
||||
* <li>The Spring property spring.jpa.properties.hibernate.generate_statistics=true shall be set - enables Hibernate statistics
|
||||
* collecting</li>
|
||||
* <li>If don't need log in the stdout set logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=WARN</li>
|
||||
* <li>If don't need periodic log (Slf4J) set logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=WARN</li>
|
||||
* <li>The MeterRegistry shall be registered available - e.g. include org.springframework.boot:spring-boot-actuator-autoconfigure</li>
|
||||
* <li>Hibernate reporting to micrometer shall be enabled - include org.hibernate.orm:hibernate-micrometer</li>
|
||||
* <li>(?) When using in test the metrics MAYBE shall be enabled with @AutoConfigureObservability(tracing = false)</li>
|
||||
|
||||
@@ -26,11 +26,12 @@ import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* (Experimental) Utility class to get some statistics.
|
||||
* It's main purpose is to be used for debugging / testing (performance) purposes.
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class StatisticsUtils {
|
||||
|
||||
private static final ThreadLocal<Map<String, Double>> LAST_COUNTERS = ThreadLocal.withInitial(HashMap::new);
|
||||
private static final ThreadLocal<Map<String, Double>> LAST_COUNTERS = ThreadLocal.withInitial(MapUFToString::new);
|
||||
|
||||
// for test purposes we may want to flush the statistics and to get diff from the last get int THIS thread
|
||||
public static Map<String, Double> diff() {
|
||||
@@ -60,7 +61,7 @@ public class StatisticsUtils {
|
||||
|
||||
Statistics.flush();
|
||||
|
||||
final Map<String, Double> counters = new HashMap<>();
|
||||
final Map<String, Double> counters = new MapUFToString();
|
||||
meterRegistry.forEachMeter(m -> {
|
||||
final Meter.Id id = m.getId();
|
||||
if (id.getName().startsWith(Statistics.METER_PREFIX)) {
|
||||
@@ -78,7 +79,7 @@ public class StatisticsUtils {
|
||||
if (!ObjectUtils.isEmpty(tags)) {
|
||||
key.append(" [");
|
||||
tags.forEach(tag -> key.append(tag.getKey()).append('=').append(tag.getValue()).append(", "));
|
||||
key.setLength(key.length() - 2);
|
||||
key.setLength(key.length() - 2); // remove the last ", "
|
||||
key.append(']');
|
||||
}
|
||||
counters.put(key.toString(), value);
|
||||
@@ -88,4 +89,24 @@ public class StatisticsUtils {
|
||||
LAST_COUNTERS.set(counters);
|
||||
return counters;
|
||||
}
|
||||
|
||||
// Map with user-friendly toString, sorted and without the last ", "
|
||||
private static class MapUFToString extends HashMap<String, Double> {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (isEmpty()) {
|
||||
return "{}";
|
||||
}
|
||||
|
||||
final StringBuilder sb = new StringBuilder("{");
|
||||
entrySet().stream()
|
||||
.sorted()
|
||||
.forEach(e -> sb.append(e.getKey()).append(": ").append(e.getValue()).append(", "));
|
||||
sb.setLength(sb.length() - 2); // remove the last ", "
|
||||
sb.append("}");
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,7 @@ logging.level.org.eclipse.persistence=ERROR
|
||||
## Enable Hibernate statistics
|
||||
#spring.jpa.properties.hibernate.generate_statistics=true
|
||||
## Disables info log messages from Hibernate statistics
|
||||
logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=WARN
|
||||
#logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=WARN
|
||||
# Debug & Monitor Hibernate - END
|
||||
|
||||
#logging.level.org.springframework.security=TRACE
|
||||
|
||||
Reference in New Issue
Block a user