Skip to content

Commit

Permalink
Merge pull request #27 from beverly-hills-money-gangster/micro-meter-…
Browse files Browse the repository at this point in the history
…metrics

Add micrometer + up version 2.4.0
  • Loading branch information
beverly-hills-money-gangster authored Feb 17, 2024
2 parents c3820f8 + 6efda77 commit 3ba6f47
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 20 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ A sample [docker-compose file](/docker-compose.yaml) can be found in the root fo

### Server monitoring

#### JVM metrics

Server JVM metrics can be monitored through JMX remotely. See [docker-compose file](/docker-compose.yaml).
In the sample, JMX agent is running on port 9999 listening to incoming connections initiated by 127.0.0.1.
If server is running in the cloud, then it's possible to port-forward JMX using the following command:
Expand All @@ -62,8 +64,15 @@ If server is running in the cloud, then it's possible to port-forward JMX using
ssh -L 9999:127.0.0.1:9999 <user>@<remote_host>
```

#### Heap dumps

Apart from JMX metrics, the server is configured to drop a heap dump file on out-of-memory.


#### Micrometer

All command handlers publish Micrometer timer metrics through JMX. See "metrics" in Jconsole "MBeans" tab.

### Client monitoring

`GameConnection.java` has `networkStats` field that gathers basic network stats including:
Expand Down
8 changes: 4 additions & 4 deletions net-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>daikombat-server</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
</parent>

<artifactId>net-client</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>

<properties>
<maven.compiler.source>14</maven.compiler.source>
Expand Down Expand Up @@ -65,12 +65,12 @@
<dependency>
<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>schema</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>security</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.maven/maven-model -->
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>daikombat-server</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
<packaging>pom</packaging>

<modules>
Expand Down
4 changes: 2 additions & 2 deletions schema/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>daikombat-server</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
</parent>

<artifactId>schema</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>

<properties>
<maven.compiler.source>14</maven.compiler.source>
Expand Down
4 changes: 2 additions & 2 deletions security/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>daikombat-server</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
</parent>

<artifactId>security</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>

<properties>
<maven.compiler.source>14</maven.compiler.source>
Expand Down
17 changes: 12 additions & 5 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<parent>
<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>daikombat-server</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
</parent>

<artifactId>server</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>

<properties>
<maven.compiler.source>14</maven.compiler.source>
Expand Down Expand Up @@ -71,12 +71,12 @@
<dependency>
<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>schema</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>security</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>eu.rekawek.toxiproxy</groupId>
Expand All @@ -88,6 +88,13 @@
<artifactId>spring-context</artifactId>
<version>5.3.31</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.micrometer/micrometer-registry-jmx -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-jmx</artifactId>
<version>1.10.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
Expand All @@ -98,7 +105,7 @@
<dependency>
<groupId>com.beverly.hills.money.gang</groupId>
<artifactId>net-client</artifactId>
<version>2.3.0</version>
<version>2.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.beverly.hills.money.gang.handler.command;

import com.beverly.hills.money.gang.exception.GameLogicError;
import com.beverly.hills.money.gang.meter.RequestsMeter;
import com.beverly.hills.money.gang.proto.ServerCommand;
import io.netty.channel.Channel;
import org.slf4j.Logger;
Expand All @@ -11,18 +12,21 @@

public abstract class ServerCommandHandler {

private final RequestsMeter requestsMeter = new RequestsMeter(this.getClass().getSimpleName());
private static final Logger LOG = LoggerFactory.getLogger(ServerCommandHandler.class);

protected abstract boolean isValidCommand(ServerCommand msg, Channel currentChannel);

protected abstract void handleInternal(ServerCommand msg, Channel currentChannel) throws GameLogicError;

public final void handle(ServerCommand msg, Channel currentChannel) throws GameLogicError {
if (isValidCommand(msg, currentChannel)) {
handleInternal(msg, currentChannel);
} else {
LOG.error("Invalid command {}", msg);
throw new GameLogicError("Invalid command", COMMAND_NOT_RECOGNIZED);
}
requestsMeter.runAndMeasure(() -> {
if (isValidCommand(msg, currentChannel)) {
handleInternal(msg, currentChannel);
} else {
LOG.error("Invalid command {}", msg);
throw new GameLogicError("Invalid command", COMMAND_NOT_RECOGNIZED);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.beverly.hills.money.gang.meter;

import com.beverly.hills.money.gang.exception.GameLogicError;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import io.micrometer.jmx.JmxConfig;
import io.micrometer.jmx.JmxMeterRegistry;

import java.util.Locale;

public class RequestsMeter {

private final Timer requestTimer;

private final MeterRegistry meterRegistry = new JmxMeterRegistry(JmxConfig.DEFAULT, Clock.SYSTEM);

public RequestsMeter(final String name) {
this.requestTimer = Timer.builder("app.timer." + name.toLowerCase(Locale.ENGLISH))
.description("Request processing time")
.register(meterRegistry);
}

public void runAndMeasure(MeasureRunnable r) throws GameLogicError {
Timer.Sample sample = Timer.start(meterRegistry);
try {
r.run();
} finally {
sample.stop(requestTimer);
}
}

@FunctionalInterface
public interface MeasureRunnable {
void run() throws GameLogicError;
}
}

0 comments on commit 3ba6f47

Please sign in to comment.