-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
76 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/de/antragsgruen/live/metrics/ActiveWebsocketConnectionMetric.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package de.antragsgruen.live.metrics; | ||
|
||
import de.antragsgruen.live.multisite.ConsultationScope; | ||
import de.antragsgruen.live.websocket.TopicPermissionChecker; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import io.micrometer.core.instrument.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.messaging.simp.user.SimpUserRegistry; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
record SubscriptionDimensions(String installationId, String siteId, String consultationId) { } | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ActiveWebsocketConnectionMetric { | ||
private final SimpUserRegistry userRegistry; | ||
private final MeterRegistry registry; | ||
|
||
private final String metricName = "antragsgruen_live.ws.active_connections"; | ||
private final Integer metricIntervalMs = 5000; | ||
|
||
@Scheduled(fixedRate = metricIntervalMs) | ||
public void processGauge() { | ||
userRegistry.findSubscriptions(subscription -> true) | ||
.stream() | ||
.map(subscription -> TopicPermissionChecker.consultationScopeFromPathParts(subscription.getDestination().split("/"))) | ||
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) | ||
.forEach(this::trackMetric); | ||
} | ||
|
||
private void trackMetric(ConsultationScope scope, Long subscribers) { | ||
log.debug("Logging active websocket metrics: " + scope + " - " + subscribers); | ||
|
||
registry.gauge( | ||
metricName, | ||
List.of( | ||
Tag.of("installation", scope.installation()), | ||
Tag.of("site", scope.site()), | ||
Tag.of("consultation", scope.consultation()) | ||
), | ||
subscribers | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@NonNullApi | ||
@NonNullFields | ||
package de.antragsgruen.live.metrics; | ||
|
||
import org.springframework.lang.NonNullApi; | ||
import org.springframework.lang.NonNullFields; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters