Yammer Metrics实现服务指标收集与监控

1.用途

可以对应用程序的运行状态以及各种指标进行收集。

2.官网地址

https://metrics.dropwizard.io/2.2.0/manual/core/

3.yammer metrics中基本的总结:

  • Gauges: an instantaneous measurement of a discrete value.
  • Counters: a value that can be incremented and decremented. Can be used in queues to monitorize the remaining number of pending jobs.
  • Meters: measure the rate of events over time. You can specify the rate unit, the scope of events or event type.
  • Histograms: measure the statistical distribution of values in a stream of data.
  • Timers: measure the amount of time it takes to execute a piece of code and the distribution of its duration.
  • Healthy checks: as his name suggests, it centralize our service’s healthy checks of external systems.

3.1.Gauges

Gauge是一个瞬时的测量值,例如,我们想要测量在队列中准备运行的Job数量

Metrics.newGauge(QueueManager.class, "pending-jobs", new Gauge<Integer>() {
    
    
	@Override
	public Integer value() {
    
    
		return queue.size();
	}
});

Gauges每次被测量时候,都将返回队列中job的数量

For most queue and queue-like structures, you won’t want to simply return queue.size(). Most of java.util and java.util.concurrent have implementations of #size() which are O(n), which means your gauge will be slow (potentially while holding a lock).

3.2.Counters

counter仅是 AtomicLong 实例的一个计量器。我们可以增加或降低这个值,例如,我们可能想要有更有效的方式测量队列中的Job

private final Counter pendingJobs = Metrics.newCounter(QueueManager.class, "pending-jobs");
public void addJob(Job job) {
    
    
	pendingJobs.inc();
	queue.offer(job);
}
public Job takeJob() {
    
    
	pendingJobs.dec();
	return queue.take();
}

counters每次被测量时候,都将返回队列中job的数量

3.3.Meters

Meter测量随事件时间推移的速率

A meter measures the rate of events over time (e.g., “requests per second”). In addition to the mean rate, meters also track 1-, 5-, and 15-minute moving averages.

private final Meter requests = Metrics.newMeter(RequestHandler.class, "requests", "requests", TimeUnit.SECONDS);
public void handleRequest(Request request, Response response) {
    
    
	requests.mark();
	// etc
}

meter将度量每秒请求中请求的速率

3.4.Histograms

A histogram measures the statistical distribution of values in a stream of data. In addition to minimum, maximum, mean, etc., it also measures median, 75th, 90th, 95th, 98th, 99th, and 99.9th percentiles.

private final Histogram responseSizes = Metrics.newHistogram(RequestHandler.class, "response-sizes");
public void handleRequest(Request request, Response response) {
    
    
	// etc
	responseSizes.update(response.getContent().length);
}

This histogram will measure the size of responses in bytes.

3.5.Timers

A timer measures both the rate that a particular piece of code is called and the distribution of its duration.

private final Timer responses = Metrics.newTimer(RequestHandler.class, "responses", TimeUnit.MILLISECONDS, TimeUnit.SECONDS);
public String handleRequest(Request request, Response response) {
    
    
	final TimerContext context = responses.time();
	try {
    
    
		// etc;
		return "OK";
	} finally {
    
    
		context.stop();
	}
}

This timer will measure the amount of time it takes to process each request in milliseconds and provide a rate of requests in requests per second.

3.6.Health Checks

Metrics also has the ability to centralize your service’s health checks. First, implement a HealthCheck instance:

import com.yammer.metrics.core.HealthCheck.Result;
public class DatabaseHealthCheck extends HealthCheck {
    
    
	private final Database database;
	public DatabaseHealthCheck(Database database) {
    
    
		super("database");
		this.database = database;
	}
	@Override
	public Result check() throws Exception {
    
    
		if (database.isConnected()) {
    
    
			return Result.healthy();
		} else {
    
    
			return Result.unhealthy("Cannot connect to " + database.getUrl());
		}
	}
}

Then register an instance of it with Metrics:

HealthChecks.register(new DatabaseHealthCheck(database));
To run all of the registered health checks:

f

inal Map<String, Result> results = HealthChecks.runHealthChecks();
for (Entry<String, Result> entry : results.entrySet()) {
    
    
	if (entry.getValue().isHealthy()) {
    
    
		System.out.println(entry.getKey() + " is healthy");
	} else {
    
    
		System.err.println(entry.getKey() + " is UNHEALTHY: " + entry.getValue().getMessage());
		final Throwable e = entry.getValue().getError();
		if (e != null) {
    
    
			e.printStackTrace();
		}
	}
}

Metrics comes with a pre-built health check: DeadlockHealthCheck, which uses Java 1.6’s built-in thread deadlock detection to determine if any threads are deadlocked.

4.Yammer metrics用法

<dependency>
 	<groupId>com.yammer.metrics</groupId>
	<artifactId>metrics-core</artifactId>
 	<version>2.2.0</version>
</dependency>
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Gauge;
import com.yammer.metrics.reporting.ConsoleReporter;
 
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
public class LearnGauge {
    
    
    private List<String> stringList = new LinkedList<>();
 
    Gauge<Integer> gauge = Metrics.newGauge(LearnGauge.class, "list-size-gauge", new Gauge<Integer>() {
    
    
        @Override
        public Integer value() {
    
    
            return stringList.size();
        }
    });
 
    public void inputElement(String input){
    
    
        stringList.add(input);
    }
 
    public static void main(String[] args) throws Exception {
    
    
        //周期性报告注册的所有指标到控制台上 这里设定为1秒钟
        ConsoleReporter.enable(1, TimeUnit.SECONDS);
        LearnGauge learnGauge=new LearnGauge();
 
        for(int i=0;i<100000;i++){
    
    
            learnGauge.inputElement(String.valueOf(i));
            Thread.sleep(500);
        }
    }
}
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Counter;
import com.yammer.metrics.reporting.ConsoleReporter;
 
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
public class LearnCounter {
    
    
    private List<String> stringList = new LinkedList<String>();
    private Counter listSizeCounter = Metrics.newCounter(LearnCounter.class, "string-list-counter");
 
    private void push(String input){
    
    
        listSizeCounter.inc();
        stringList.add(input);
    }
 
    private void pop(String input){
    
    
        listSizeCounter.dec();
        stringList.remove(input);
    }
 
    public static void main(String[] args) throws InterruptedException{
    
    
        ConsoleReporter.enable(1, TimeUnit.SECONDS);
        LearnCounter learnCounter = new LearnCounter();
 
        for(int times = 0; times < 5; times++){
    
    
            learnCounter.push(String.valueOf(times));
            Thread.sleep(1000);
        }
        for(int times = 0; times < 5; times++){
    
    
            learnCounter.pop(String.valueOf(times));
            Thread.sleep(1000);
        }
    }
}
 
package com.bigdata.kafka.sourceparse.metrix.yammer.meter;
 
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Meter;
import com.yammer.metrics.reporting.ConsoleReporter;
 
import java.util.concurrent.TimeUnit;
 
public class LearnMeter {
    
    
 
    private Meter meter= Metrics.newMeter(LearnMeter.class,"meter-event","request", TimeUnit.SECONDS);
 
    public void handleRequest(){
    
    
        meter.mark();
    }
 
 
    public static void main(String[] args) throws Exception{
    
    
        ConsoleReporter.enable(1,TimeUnit.SECONDS);
        LearnMeter learnMeter=new LearnMeter();
 
        for(int times = 0; times < 200; times++){
    
    
            learnMeter.handleRequest();
            Thread.sleep(100);
        }
    }
}
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Histogram;
import com.yammer.metrics.reporting.ConsoleReporter;
 
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class LearnHistogram {
    
    
    private List<String> stringList = new LinkedList<String>();
    private Histogram histogram = Metrics.newHistogram(LearnHistogram.class, "size-histogram");
    public void push(String input){
    
    
        stringList.add(input);
    }
    public void pop(String output){
    
    
        stringList.remove(output);
    }
    public void updateHisto(){
    
    
        histogram.update(stringList.size());
    }
    public static void main(String[] args) throws InterruptedException{
    
    
        ConsoleReporter.enable(1, TimeUnit.SECONDS);
        LearnHistogram learnHistogram = new LearnHistogram();
        for(int time = 0 ; time < 100000 ; time++){
    
    
            learnHistogram.push(String.valueOf(time));
            if(time%10 == 0){
    
    
                learnHistogram.updateHisto();
            }
            if(time%2 == 2){
    
    
                learnHistogram.pop(String.valueOf(time));
            }
            Thread.sleep(1);
        }
    }
}
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Timer;
import com.yammer.metrics.core.TimerContext;
import com.yammer.metrics.reporting.ConsoleReporter;
import java.util.concurrent.TimeUnit;
 
public class LearnTimer {
    
    
    private Timer timer = Metrics.newTimer(LearnTimer.class, "response-timer", TimeUnit.MILLISECONDS, TimeUnit.SECONDS);
    public void handleRequest() throws InterruptedException{
    
    
        TimerContext context = timer.time();
        for(int i = 0 ; i < 2 ; i++){
    
    
            Thread.sleep(1);
        }
        context.stop();
    }
    public static void main(String[] args) throws InterruptedException{
    
    
        ConsoleReporter.enable(1, TimeUnit.SECONDS);
        LearnTimer learnTimer = new LearnTimer();
        for(int time = 0 ; time < 10000 ; time++){
    
    
            learnTimer.handleRequest();
        }
        Thread.sleep(10000);
    }
}

猜你喜欢

转载自blog.csdn.net/yangyijun1990/article/details/109136903