|
1 | 1 | package org.cryptomator.hub.metrics; |
2 | 2 |
|
3 | | -import io.micrometer.core.instrument.Gauge; |
4 | | -import io.micrometer.core.instrument.MeterRegistry; |
5 | | -import io.quarkus.scheduler.Scheduled; |
6 | | -import jakarta.annotation.PostConstruct; |
| 3 | +import io.opentelemetry.api.common.AttributeKey; |
| 4 | +import io.opentelemetry.api.common.Attributes; |
| 5 | +import io.opentelemetry.api.metrics.Meter; |
| 6 | +import io.opentelemetry.api.metrics.ObservableLongGauge; |
| 7 | +import io.opentelemetry.api.metrics.ObservableLongMeasurement; |
| 8 | +import io.quarkus.narayana.jta.QuarkusTransaction; |
| 9 | +import io.quarkus.runtime.ShutdownEvent; |
7 | 10 | import jakarta.enterprise.context.ApplicationScoped; |
| 11 | +import jakarta.enterprise.event.Observes; |
8 | 12 | import jakarta.inject.Inject; |
9 | | -import jakarta.transaction.Transactional; |
10 | 13 | import org.cryptomator.hub.entities.Device; |
11 | 14 | import org.cryptomator.hub.entities.EffectiveVaultAccess; |
12 | 15 | import org.cryptomator.hub.entities.Vault; |
13 | 16 |
|
14 | | -import java.util.EnumMap; |
15 | | -import java.util.Map; |
16 | | -import java.util.concurrent.atomic.AtomicLong; |
17 | | - |
18 | 17 | @ApplicationScoped |
19 | 18 | public class SystemUsageMetrics { |
20 | 19 |
|
21 | | - private static final String VAULTS_TOTAL_METRIC = "hub_vaults_total"; |
22 | | - private static final String ACTIVE_USERS_TOTAL_METRIC = "hub_active_users_total"; |
23 | | - private static final String DEVICES_TOTAL_METRIC = "hub_devices_total"; |
24 | | - |
25 | | - @Inject |
26 | | - MeterRegistry meterRegistry; |
27 | | - |
28 | | - @Inject |
29 | | - Vault.Repository vaultRepo; |
| 20 | + private static final String VAULTS_METRIC = "hub_vaults"; |
| 21 | + private static final String ACTIVE_USERS_METRIC = "hub_active_users"; |
| 22 | + private static final String DEVICES_METRIC = "hub_devices"; |
| 23 | + private static final AttributeKey<String> DEVICE_TYPE_KEY = AttributeKey.stringKey("type"); |
30 | 24 |
|
31 | | - @Inject |
32 | | - EffectiveVaultAccess.Repository effectiveVaultAccessRepo; |
| 25 | + private final Vault.Repository vaultRepo; |
| 26 | + private final EffectiveVaultAccess.Repository effectiveVaultAccessRepo; |
| 27 | + private final Device.Repository deviceRepo; |
| 28 | + private final ObservableLongGauge vaultsGauge; |
| 29 | + private final ObservableLongGauge activeUsersGauge; |
| 30 | + private final ObservableLongGauge devicesGauge; |
33 | 31 |
|
34 | 32 | @Inject |
35 | | - Device.Repository deviceRepo; |
36 | | - |
37 | | - private final AtomicLong vaultsTotal = new AtomicLong(0); |
38 | | - private final AtomicLong activeUsersTotal = new AtomicLong(0); |
39 | | - private final Map<Device.Type, AtomicLong> devicesPerType = new EnumMap<>(Device.Type.class); |
| 33 | + SystemUsageMetrics(Meter meter, Vault.Repository vaultRepo, EffectiveVaultAccess.Repository effectiveVaultAccessRepo, Device.Repository deviceRepo) { |
| 34 | + this.vaultRepo = vaultRepo; |
| 35 | + this.effectiveVaultAccessRepo = effectiveVaultAccessRepo; |
| 36 | + this.deviceRepo = deviceRepo; |
| 37 | + this.vaultsGauge = meter.gaugeBuilder(VAULTS_METRIC) |
| 38 | + .ofLongs() |
| 39 | + .setDescription("Number of vaults") |
| 40 | + .buildWithCallback(this::recordVaultCount); |
| 41 | + this.activeUsersGauge = meter.gaugeBuilder(ACTIVE_USERS_METRIC) |
| 42 | + .ofLongs() |
| 43 | + .setDescription("Number of unique users with access to any non-archived vault") |
| 44 | + .buildWithCallback(this::recordSeatCount); |
| 45 | + this.devicesGauge = meter.gaugeBuilder(DEVICES_METRIC) |
| 46 | + .ofLongs() |
| 47 | + .setDescription("Number of devices grouped by type") |
| 48 | + .buildWithCallback(this::recordDeviceCount); |
| 49 | + } |
40 | 50 |
|
41 | | - @PostConstruct |
42 | | - void registerMetrics() { |
43 | | - Gauge.builder(VAULTS_TOTAL_METRIC, vaultsTotal, AtomicLong::get) |
44 | | - .description("Number of vaults") |
45 | | - .register(meterRegistry); |
| 51 | + private void recordVaultCount(ObservableLongMeasurement measurement) { |
| 52 | + measurement.record(QuarkusTransaction.requiringNew().call(vaultRepo::count)); |
| 53 | + } |
46 | 54 |
|
47 | | - Gauge.builder(ACTIVE_USERS_TOTAL_METRIC, activeUsersTotal, AtomicLong::get) |
48 | | - .description("Number of unique users with access to any non-archived vault") |
49 | | - .register(meterRegistry); |
| 55 | + private void recordSeatCount(ObservableLongMeasurement measurement) { |
| 56 | + measurement.record(QuarkusTransaction.requiringNew().call(effectiveVaultAccessRepo::countSeatOccupyingUsers)); |
| 57 | + } |
50 | 58 |
|
51 | | - for (var deviceType : Device.Type.values()) { |
52 | | - var value = new AtomicLong(0); |
53 | | - devicesPerType.put(deviceType, value); |
54 | | - Gauge.builder(DEVICES_TOTAL_METRIC, value, AtomicLong::get) |
55 | | - .description("Number of devices grouped by type") |
56 | | - .tag("type", deviceType.name()) |
57 | | - .register(meterRegistry); |
58 | | - } |
| 59 | + private void recordDeviceCount(ObservableLongMeasurement measurement) { |
| 60 | + QuarkusTransaction.requiringNew().run(() -> { |
| 61 | + for (var type : Device.Type.values()) { |
| 62 | + measurement.record(deviceRepo.count("type", type), Attributes.of(DEVICE_TYPE_KEY, type.name())); |
| 63 | + } |
| 64 | + }); |
59 | 65 | } |
60 | 66 |
|
61 | | - @Scheduled(every = "24h", delayed = "10s") |
62 | | - @Transactional |
63 | | - void collect() { |
64 | | - vaultsTotal.set(vaultRepo.count()); |
65 | | - activeUsersTotal.set(effectiveVaultAccessRepo.countSeatOccupyingUsers()); |
66 | | - for (var deviceType : Device.Type.values()) { |
67 | | - var count = deviceRepo.count("type", deviceType); |
68 | | - devicesPerType.get(deviceType).set(count); |
69 | | - } |
| 67 | + void onStop(@Observes ShutdownEvent event) { |
| 68 | + vaultsGauge.close(); |
| 69 | + activeUsersGauge.close(); |
| 70 | + devicesGauge.close(); |
70 | 71 | } |
| 72 | + |
71 | 73 | } |
0 commit comments