Skip to content

Commit

Permalink
优化会话统计
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed May 28, 2021
1 parent 76652c7 commit ca8735e
Showing 1 changed file with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
import org.jetlinks.core.server.monitor.GatewayServerMetrics;
import org.jetlinks.core.server.session.DeviceSession;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public class MicrometerGatewayServerMetrics implements GatewayServerMetrics {

private String serverId;
private final String serverId;

private final MeterRegistry registry;

private MeterRegistry registry;
private final Map<String, AtomicLong> sessionRecord = new ConcurrentHashMap<>();

public MicrometerGatewayServerMetrics(String serverId) {
this(serverId, null);
Expand All @@ -23,6 +29,14 @@ public MicrometerGatewayServerMetrics(String serverId, MeterRegistry registry) {
registry = Metrics.globalRegistry;
}
this.registry = registry;

Gauge.builder("gateway-server-session", sessionRecord, sessionRecord -> sessionRecord
.values()
.stream()
.mapToLong(Number::longValue)
.sum())
.tag("server", getServerId())
.register(registry);
}

public String getServerId() {
Expand All @@ -31,11 +45,9 @@ public String getServerId() {

@Override
public void reportSession(String transport, int sessionTotal) {
Gauge.builder("gateway-server-session", sessionTotal, Number::doubleValue)
.tag("transport", transport)
.tag("server", getServerId())
.description("当前" + transport + "会话数量")
.register(registry);
sessionRecord
.computeIfAbsent(transport, key -> new AtomicLong())
.set(sessionTotal);
}

@Override
Expand Down Expand Up @@ -65,9 +77,9 @@ public void rejectedConnection(String transport) {
@Override
public void receiveFromDeviceMessage(DeviceSession session) {
registry.counter("received_messages",
"transport", session.getTransport().getId(),
"deviceId", session.getDeviceId(),
"server", getServerId())
.increment(-1);
"transport", session.getTransport().getId(),
"deviceId", session.getDeviceId(),
"server", getServerId())
.increment(1);
}
}

0 comments on commit ca8735e

Please sign in to comment.