• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knowledgepixels / nanopub-registry / 21167271454

20 Jan 2026 10:01AM UTC coverage: 18.763% (-0.02%) from 18.779%
21167271454

push

github

ashleycaselli
refactor(logging): replace System.err with SLF4J logger for improved logging consistency

96 of 586 branches covered (16.38%)

Branch coverage included in aggregate %.

350 of 1791 relevant lines covered (19.54%)

3.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
src/main/java/com/knowledgepixels/registry/MetricsCollector.java
1
package com.knowledgepixels.registry;
2

3
import com.mongodb.client.ClientSession;
4
import io.micrometer.core.instrument.Gauge;
5
import io.micrometer.core.instrument.MeterRegistry;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8

9
import java.util.Map;
10
import java.util.Optional;
11
import java.util.concurrent.ConcurrentHashMap;
12
import java.util.concurrent.atomic.AtomicInteger;
13

14
import static com.knowledgepixels.registry.RegistryDB.*;
15

16
public final class MetricsCollector {
17

18
    private final Logger logger = LoggerFactory.getLogger(MetricsCollector.class);
×
19
    private final AtomicInteger loadCounter = new AtomicInteger(0);
×
20
    private final AtomicInteger trustStateCounter = new AtomicInteger(0);
×
21
    private final AtomicInteger agentCount = new AtomicInteger(0);
×
22
    private final AtomicInteger accountCount = new AtomicInteger(0);
×
23

24
    private final Map<ServerStatus, AtomicInteger> statusStates = new ConcurrentHashMap<>();
×
25

26
    public MetricsCollector(MeterRegistry meterRegistry) {
×
27
        // Numeric metrics
28
        Gauge.builder("registry.load.counter", loadCounter, AtomicInteger::get).register(meterRegistry);
×
29
        Gauge.builder("registry.trust.state.counter", trustStateCounter, AtomicInteger::get).register(meterRegistry);
×
30
        Gauge.builder("registry.agent.count", agentCount, AtomicInteger::get).register(meterRegistry);
×
31
        Gauge.builder("registry.account.count", accountCount, AtomicInteger::get).register(meterRegistry);
×
32

33
        // Status label metrics
34
        for (final var status : ServerStatus.values()) {
×
35
            AtomicInteger stateGauge = new AtomicInteger(0);
×
36
            statusStates.put(status, stateGauge);
×
37
            Gauge.builder("registry.server.status", stateGauge, AtomicInteger::get)
×
38
                    .description("Server status (1 if current)")
×
39
                    .tag("status", status.name())
×
40
                    .register(meterRegistry);
×
41
        }
42
    }
×
43

44
    public void updateMetrics() {
45
        try (final var session = RegistryDB.getClient().startSession()) {
×
46
            // Update numeric metrics
47
            extractMaximalIntegerValueFromField(session, Collection.NANOPUBS.toString(), "counter")
×
48
                    .ifPresent(loadCounter::set);
×
49

50
            extractIntegerValueFromField(session, Collection.SERVER_INFO.toString(), "trustStateCounter")
×
51
                    .ifPresent(trustStateCounter::set);
×
52

53
            agentCount.set(countDocumentsInCollection(session, Collection.AGENTS.toString()));
×
54
            accountCount.set(countDocumentsInCollection(session, Collection.ACCOUNTS.toString()));
×
55

56
            // Update status gauge
57
            final var currentStatus = extractStringValueFromField(session, Collection.SERVER_INFO.toString(), "status")
×
58
                    .map(ServerStatus::valueOf)
×
59
                    .orElse(null);
×
60
            for (final var status : ServerStatus.values()) {
×
61
                statusStates.get(status).set(status.equals(currentStatus) ? 1 : 0);
×
62
            }
63
        } catch (Exception e) {
×
64
            logger.error("Error updating metrics: {}", e.getMessage());
×
65
        }
×
66
    }
×
67

68
    private Optional<Integer> extractMaximalIntegerValueFromField(
69
            ClientSession session,
70
            String collectionName,
71
            String fieldName
72
    ) {
73
        return Optional
×
74
                .ofNullable(getMaxValue(session, collectionName, fieldName))
×
75
                .filter(Number.class::isInstance)
×
76
                .map(Number.class::cast)
×
77
                .map(Number::intValue);
×
78
    }
79

80
    private Optional<Integer> extractIntegerValueFromField(
81
            ClientSession session,
82
            String collectionName,
83
            String fieldName
84
    ) {
85
        return Optional
×
86
                .ofNullable(getValue(session, collectionName, fieldName))
×
87
                .filter(Number.class::isInstance)
×
88
                .map(Number.class::cast)
×
89
                .map(Number::intValue);
×
90
    }
91

92
    private Optional<String> extractStringValueFromField(
93
            ClientSession session,
94
            String collectionName,
95
            String fieldName
96
    ) {
97
        return Optional
×
98
                .ofNullable(getValue(session, collectionName, fieldName))
×
99
                .filter(String.class::isInstance)
×
100
                .map(String.class::cast);
×
101
    }
102

103
    private int countDocumentsInCollection(ClientSession session, String collectionName) {
104
        return (int) collection(collectionName).countDocuments(session);
×
105
    }
106
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc