Skip to content

Commit

Permalink
feat: added subscriber metric
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Aug 8, 2024
1 parent 45b2045 commit 8bae1da
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.util.DaemonThreadFactory;
import com.swirlds.metrics.api.LongGauge;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.io.IOException;
import java.util.Map;
Expand Down Expand Up @@ -164,6 +165,8 @@ public void subscribe(

// Keep track of the subscriber
subscribers.put(handler, batchEventProcessor);

updateSubscriberMetrics();
}

@Override
Expand All @@ -183,6 +186,8 @@ public void unsubscribe(
// Remove the gating sequence from the ring buffer
ringBuffer.removeGatingSequence(batchEventProcessor.getSequence());
}

updateSubscriberMetrics();
}

@Override
Expand All @@ -200,4 +205,10 @@ private static SubscribeStreamResponse buildEndStreamResponse() {
.setStatus(SubscribeStreamResponse.SubscribeStreamResponseCode.READ_STREAM_SUCCESS)
.build();
}

private void updateSubscriberMetrics() {
@NonNull final MetricsService metricsService = blockNodeContext.metricsService();
@NonNull final LongGauge longGauge = metricsService.subscriberGauge;
longGauge.set(subscribers.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ public class MetricsService {
new Counter.Config(LIVE_CATEGORY, "liveBlockItemCounter")
.withDescription("Live BlockItem Counter");

// Subscriber Gauge
private static final String SUBSCRIBER_CATEGORY = "subscriber";
private static final LongGauge.Config SUBSCRIBER_GAUGE =
new LongGauge.Config(SUBSCRIBER_CATEGORY, "subscriberGauge")
.withDescription("Subscriber Gauge");

/** An example gauge. */
public final LongGauge exampleGauge;

/** An example counter. */
public final Counter exampleCounter;

public final Counter liveBlockItemCounter;
public final LongGauge subscriberGauge;

/**
* Creates a new instance of {@link MetricsService}.
Expand All @@ -55,5 +62,6 @@ public MetricsService(@NonNull final Metrics metrics) {
this.exampleCounter = metrics.getOrCreate(EXAMPLE_COUNTER);

this.liveBlockItemCounter = metrics.getOrCreate(LIVE_BLOCK_ITEM_COUNTER);
this.subscriberGauge = metrics.getOrCreate(SUBSCRIBER_GAUGE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ public void testSubscribeBlockStream() throws IOException {
// Calling onNext() with a BlockItem
streamObserver.onNext(publishStreamRequest);


// Verify the counter was incremented
assertEquals(1, blockNodeContext.metricsService().liveBlockItemCounter.get());

Expand Down

0 comments on commit 8bae1da

Please sign in to comment.