Skip to content

Commit

Permalink
User friendly map toString in StatisticsUtils (#2203)
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm authored Jan 20, 2025
1 parent 1f71d6d commit 25e45d5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* <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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* <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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

/**
* (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() {
Expand Down Expand Up @@ -60,7 +61,7 @@ public static Map<String, Double> counters() {

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)) {
Expand All @@ -78,7 +79,7 @@ public static Map<String, Double> counters() {
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);
Expand All @@ -88,4 +89,24 @@ public static Map<String, Double> counters() {
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 25e45d5

Please sign in to comment.