Skip to content

Commit

Permalink
Java metrics 学习
Browse files Browse the repository at this point in the history
  • Loading branch information
qinfuxiang committed Dec 3, 2021
1 parent 2ccf5f7 commit afd5af4
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 0 deletions.
11 changes: 11 additions & 0 deletions fast-source-code-analysis/code-metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@
<version>3.2.6</version>
</dependency>

<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-jvm</artifactId>
<version>3.2.6</version>
</dependency>

<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-healthchecks</artifactId>
<version>3.2.6</version>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fast.cloud.nacos.metrics.jmx;

public class Hello implements HelloMBean {
private String name;

private String age;

@Override
public void getTelephone() {
System.out.println("get Telephone");
}

@Override
public void helloWorld() {
System.out.println("hello world");
}

@Override
public void helloWorld(String str) {
System.out.println("helloWorld:" + str);
}

@Override
public String getName() {
System.out.println("get name 123");
return name;
}

@Override
public void setName(String name) {
System.out.println("set name 123");
this.name = name;
}

@Override
public String getAge() {
System.out.println("get age 123");
return age;
}

@Override
public void setAge(String age) {
System.out.println("set age 123");
this.age = age;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fast.cloud.nacos.metrics.jmx;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

/**
* @author qinfuxiang
*/
public class HelloAgent {

public static void main(String[] args) throws Exception {
MBeanServer server = ManagementFactory.getPlatformMBeanServer();
ObjectName helloName = new ObjectName("jmxBean:name=hello");
//create mbean and register mbean
Hello hello = new Hello();
hello.setName("123");
server.registerMBean(hello, helloName);
Thread.currentThread().join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package fast.cloud.nacos.metrics.jmx;

public interface HelloMBean
{
public String getName();

public void setName(String name);

public String getAge();

public void setAge(String age);

public void helloWorld();

public void helloWorld(String str);

public void getTelephone();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fast.cloud.nacos.metrics.metric;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.health.jvm.ThreadDeadlockHealthCheck;

import java.util.concurrent.TimeUnit;

/**
* @author qinfuxiang
*/
public class DeadLockHealthCheckExample {

public static void main(String[] args) throws InterruptedException {
final HealthCheckRegistry healthService = new HealthCheckRegistry();
healthService.register("thread-dead-lock-hc", new ThreadDeadlockHealthCheck());

final MetricRegistry metricRegistry = new MetricRegistry();
ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry).build();

metricRegistry.gauge("thread-dead-lock-hc",() -> healthService::runHealthChecks);

consoleReporter.start(10, TimeUnit.SECONDS);

Thread.currentThread().join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fast.cloud.nacos.metrics.metric;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.jvm.ClassLoadingGaugeSet;
import com.codahale.metrics.jvm.GarbageCollectorMetricSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;

import java.util.concurrent.TimeUnit;

/**
* @author qinfuxiang
*/
public class JvmInstrumentationExample {
public final static MetricRegistry metricRegistry = new MetricRegistry();
public final static ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry).build();
public static void main(String[] args) throws InterruptedException {
consoleReporter.start(10, TimeUnit.SECONDS);
metricRegistry.registerAll(new GarbageCollectorMetricSet());
metricRegistry.registerAll(new ThreadStatesGaugeSet());
metricRegistry.registerAll(new ClassLoadingGaugeSet());
Thread.currentThread().join();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package fast.cloud.nacos.metrics.metric;

import com.codahale.metrics.health.HealthCheck;

/**
* @author qinfuxiang
*/
public class RestfulServiceHealthCheck extends HealthCheck {

@Override
protected Result check() throws Exception {
return Result.healthy("The Restful service well.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fast.cloud.nacos.metrics.metric;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.codahale.metrics.health.jvm.ThreadDeadlockHealthCheck;

import java.util.concurrent.TimeUnit;

/**
* @author qinfuxiang
*/
public class RestfulServiceHealthCheckExample {
public static void main(String[] args) throws InterruptedException {
final HealthCheckRegistry healthService = new HealthCheckRegistry();
healthService.register("thread-dead-lock-hc", new ThreadDeadlockHealthCheck());
healthService.register("restful-check-hc", new RestfulServiceHealthCheck());

final MetricRegistry metricRegistry = new MetricRegistry();
ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry).build();

metricRegistry.gauge("app-health-check",() -> healthService::runHealthChecks);

consoleReporter.start(10, TimeUnit.SECONDS);

Thread.currentThread().join();
}
}

0 comments on commit afd5af4

Please sign in to comment.