Skip to content
This repository has been archived by the owner on Sep 26, 2019. It is now read-only.

Add subscribe and unsubscribe count metrics #1541

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.SubscriptionType;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.UnsubscribeRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.response.SubscriptionResponse;
import tech.pegasys.pantheon.metrics.Counter;
import tech.pegasys.pantheon.metrics.LabelledMetric;
import tech.pegasys.pantheon.metrics.MetricCategory;
import tech.pegasys.pantheon.metrics.MetricsSystem;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -45,6 +49,23 @@ public class SubscriptionManager extends AbstractVerticle {
private final AtomicLong subscriptionCounter = new AtomicLong(0);
private final Map<Long, Subscription> subscriptions = new ConcurrentHashMap<>();
private final SubscriptionBuilder subscriptionBuilder = new SubscriptionBuilder();
private final LabelledMetric<Counter> subscribeCounter;
private final LabelledMetric<Counter> unsubscribeCounter;

public SubscriptionManager(final MetricsSystem metricsSystem) {
subscribeCounter =
metricsSystem.createLabelledCounter(
MetricCategory.RPC,
"subscription_subscribe_total",
"Total number of subscriptions",
"type");
unsubscribeCounter =
metricsSystem.createLabelledCounter(
MetricCategory.RPC,
"subscription_unsubscribe_total",
"Total number of unsubscriptions",
"type");
}

@Override
public void start() {
Expand All @@ -53,6 +74,7 @@ public void start() {

public Long subscribe(final SubscribeRequest request) {
LOG.debug("Subscribe request {}", request);
subscribeCounter.labels(request.getSubscriptionType().getCode()).inc();

final long subscriptionId = subscriptionCounter.incrementAndGet();
final Subscription subscription =
Expand All @@ -79,7 +101,10 @@ public boolean unsubscribe(final UnsubscribeRequest request) {
}

private void destroySubscription(final long subscriptionId) {
subscriptions.remove(subscriptionId);
final Subscription removed = subscriptions.remove(subscriptionId);
if (removed != null) {
unsubscribeCounter.labels(removed.getSubscriptionType().getCode()).inc();
}
}

private void removeSubscriptions(final Message<String> message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.methods.WebSocketMethodsFactory;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.SubscriptionManager;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;

import java.net.InetSocketAddress;
import java.util.Arrays;
Expand Down Expand Up @@ -65,7 +66,9 @@ public void initServerAndClient() {
vertx = Vertx.vertx();

final Map<String, JsonRpcMethod> websocketMethods =
new WebSocketMethodsFactory(new SubscriptionManager(), new HashMap<>()).methods();
new WebSocketMethodsFactory(
new SubscriptionManager(new NoOpMetricsSystem()), new HashMap<>())
.methods();
webSocketRequestHandlerSpy = spy(new WebSocketRequestHandler(vertx, websocketMethods));

websocketService =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.methods.WebSocketMethodsFactory;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.SubscriptionManager;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;

import java.net.URISyntaxException;
import java.nio.file.Paths;
Expand Down Expand Up @@ -74,7 +75,9 @@ public void before() throws URISyntaxException {
websocketConfiguration.setHostsWhitelist(Collections.singleton("*"));

final Map<String, JsonRpcMethod> websocketMethods =
new WebSocketMethodsFactory(new SubscriptionManager(), new HashMap<>()).methods();
new WebSocketMethodsFactory(
new SubscriptionManager(new NoOpMetricsSystem()), new HashMap<>())
.methods();
webSocketRequestHandlerSpy = spy(new WebSocketRequestHandler(vertx, websocketMethods));

websocketService =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.internal.methods.JsonRpcMethod;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.methods.WebSocketMethodsFactory;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.SubscriptionManager;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;

import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -60,7 +61,9 @@ public void before() {
websocketConfiguration.setHostsWhitelist(Collections.singleton("*"));

final Map<String, JsonRpcMethod> websocketMethods =
new WebSocketMethodsFactory(new SubscriptionManager(), new HashMap<>()).methods();
new WebSocketMethodsFactory(
new SubscriptionManager(new NoOpMetricsSystem()), new HashMap<>())
.methods();
webSocketRequestHandlerSpy = spy(new WebSocketRequestHandler(vertx, websocketMethods));

websocketService =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.SubscriptionManager;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.SubscriptionType;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.syncing.SyncingSubscription;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;

import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -49,7 +50,7 @@ public class EthSubscribeIntegrationTest {
@Before
public void before() {
vertx = Vertx.vertx();
subscriptionManager = new SubscriptionManager();
subscriptionManager = new SubscriptionManager(new NoOpMetricsSystem());
webSocketMethodsFactory = new WebSocketMethodsFactory(subscriptionManager, new HashMap<>());
webSocketRequestHandler = new WebSocketRequestHandler(vertx, webSocketMethodsFactory.methods());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.SubscriptionManager;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.SubscribeRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.SubscriptionType;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;

import java.util.HashMap;

Expand All @@ -45,7 +46,7 @@ public class EthUnsubscribeIntegrationTest {
@Before
public void before() {
vertx = Vertx.vertx();
subscriptionManager = new SubscriptionManager();
subscriptionManager = new SubscriptionManager(new NoOpMetricsSystem());
webSocketMethodsFactory = new WebSocketMethodsFactory(subscriptionManager, new HashMap<>());
webSocketRequestHandler = new WebSocketRequestHandler(vertx, webSocketMethodsFactory.methods());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.SubscribeRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.SubscriptionType;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.response.SubscriptionResponse;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;

import java.util.UUID;

Expand All @@ -42,7 +43,7 @@ public class SubscriptionManagerSendMessageTest {
@Before
public void before(final TestContext context) {
vertx = Vertx.vertx();
subscriptionManager = new SubscriptionManager();
subscriptionManager = new SubscriptionManager(new NoOpMetricsSystem());
vertx.deployVerticle(subscriptionManager, context.asyncAssertSuccess());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.SubscriptionType;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.request.UnsubscribeRequest;
import tech.pegasys.pantheon.ethereum.jsonrpc.websocket.subscription.syncing.SyncingSubscription;
import tech.pegasys.pantheon.metrics.noop.NoOpMetricsSystem;

import java.util.List;
import java.util.UUID;
Expand All @@ -42,7 +43,7 @@ public class SubscriptionManagerTest {

@Before
public void before() {
subscriptionManager = new SubscriptionManager();
subscriptionManager = new SubscriptionManager(new NoOpMetricsSystem());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ private Map<String, JsonRpcMethod> jsonRpcMethods(

private SubscriptionManager createSubscriptionManager(
final Vertx vertx, final TransactionPool transactionPool) {
final SubscriptionManager subscriptionManager = new SubscriptionManager();
final SubscriptionManager subscriptionManager = new SubscriptionManager(metricsSystem);
final PendingTransactionSubscriptionService pendingTransactions =
new PendingTransactionSubscriptionService(subscriptionManager);
final PendingTransactionDroppedSubscriptionService pendingTransactionsRemoved =
Expand Down