“This is the 21st day of my participation in the Gwen Challenge in November. See details: The Last Gwen Challenge in 2021”


One, foreword

A few days ago, I had to play around with Java Metrics. I didn’t find this in the previous article.

In order to cover all of the performance-related topics you’ve seen, I’ll do it here as well.

Second, the introduction of

The front page of the Metrics website is simply this:

Metrics is a Java library which gives you unparalleled insight into what your code does in production.

Metrics provides a powerful toolkit of ways to measure the behavior of critical components in your production environment.With modules for common libraries like Jetty, Logback, Log4j, Apache HttpClient, Ehcache, JDBI, Jersey and reporting backends like Graphite, Metrics provides you with full-stack visibility.

That said, the toolkit allows you to generate measured data in a production environment and supports different output methods.

It measures key components in code, response times, counters, and so on, as well as operating system information.

Its basic types are as follows:

Look at the document structure of the website, I knew it can do many things, good in English, you can see the metrics. Dropwizard. IO / 4.0.0 /.

3. Give examples

Let’s look at an example Meter:

/** * Meter * Meters measure the rate (for example, TPS) * Meters count the rate of the last 1 minute (M1), 5 minutes (M5), 15 minutes (M15), and over time (the rate is the average). * /

public class TestMeter {
    public static void main(String[] args) throws InterruptedException {
        
        /** * Instantiates MetricRegistry, which is a container for metrics, and maintains a MAP */

        final MetricRegistry registry = new MetricRegistry();  // Final ConcurrentMap
      
        metrics is an attribute of this class
      ,>
        
        /** * instantiate ConsoleReporter, output */

        ConsoleReporter reporter = ConsoleReporter.forRegistry(registry)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
        reporter.start(3, TimeUnit.SECONDS);   // Poll data from the MetricRegistry clock every 3 seconds, starting from 3s after startup (so the first count is usually inaccurate, and getting more accurate from the second count)

        // Instantiate a Meter and register it with the container
        Meter meterTps = registry.meter(MetricRegistry.name(TestMeter.class, "request"."tps"));  // Add the metric of the Meter type specified by name to MetricsRegistry
        System.out.println("Execution and Business Logic");

        // Simulate data
        while (true) {
            meterTps.mark();   // Total and m1,m5,m15 are all +1
            Thread.sleep(500); }}}Copy the code

The entire sample code above is commented and won’t be explained in detail. The output is as follows:

-- Meters ---------------------------------------------------------------------- Com. Zee. Metrics. Zeemetrics. TestMeter. Request. The TPS count = 12 mean rate 1 - minute = 2.00 events per second rate = 2.00 Events /second 5-minute rate = 2.00 Events /second 15-minute rate = 2.00 events/secondCopy the code

This will allow you to count how many times the request was accessed during that time, which is TPS. But you can’t always watch it on console.

It is better to view it in a graphical interface, using influxDB+Grafana, and create a database within the influxDB.

[gaolou@7dgroup2 ~]$Influx Connected to http://localhost:8086 version 1.6.0 InfluxDB shell version: 1.6.0 > create database mydb; > use mydb; Using database mydb > show MEASUREMENTS >Copy the code

The MEASUREMENTS are empty.

Put the data in.

@Bean
public Meter requestMeter(MetricRegistry metrics) {
    return metrics.meter("request");
}

@Bean(name = "influxdbReporter")
public ScheduledReporter influxdbReporter(MetricRegistry metrics) throws Exception {
    return InfluxdbReporter.forRegistry(metrics)
            .protocol(InfluxdbProtocols.http("1.1.1.1".8086."admin"."admin"."mydb"))
            .convertRatesTo(TimeUnit.SECONDS)
            .convertDurationsTo(TimeUnit.MILLISECONDS)
            .filter(MetricFilter.ALL)
            .skipIdleMetrics(false)
            .build();
}
Copy the code

Select * from request.

Then configure the data source in grafana

Add panel to Dashboard, select the InfluxDB data source, view the list, select the data table you want to view, then select the column you want to view in the field, and save.

Then you can see the data in the dashboard.

Four,

This logic is not difficult in operation. However, from my own experience in the industry. In the research and development, let them make a good monitoring plan for the data they want to see, and finally in the operation and maintenance can be so clearly displayed, and ultimately useful for judging production problems, the communication success of this process will be very high.

Mainly depends on the organizational structure of the company, if it is a full stack structure will be easier

If r & D operation and maintenance are separate teams, and they do not cooperate well with each other, it will be quite painful.

In the previous project, I was asked how I could monitor the business performance. After thinking about the background of the company, I pondered for a long time and said: Difficult.

Performance can do a lot of things, the premise is how big the pattern.