1. Overview
1.概述
Spectator is a library for instrumenting code and collecting data for dimensional time series backend systems. Spectator originated at Netflix for various metrics collection, and the corresponding backend system used with it is mainly Atlas.
Spectator是一个用于仪表代码和收集维度时间序列后端系统数据的库。Spectator起源于Netflix的各种指标收集,而与之对应的后端系统主要是Atlas。
In this tutorial, we’ll learn what Spectator provides and how we can use it to collect metrics.
在本教程中,我们将学习Spectator提供的内容以及如何使用它来收集指标。
2. Maven Dependencies
2.Maven的依赖性
Before we dive into the actual implementation, let’s add the Spectator dependency to the pom.xml file:
在我们深入研究实际实现之前,让我们将Spectator依赖性添加到pom.xml文件中。
<dependency>
<groupId>com.netflix.spectator</groupId>
<artifactId>spectator-api</artifactId>
<version>1.0.11</version>
</dependency>
The spectator-api is the core spectator library.
spectator-api是spectator的核心库。
3. Basics of Registry, Meter, and Metrics
3.注册表、表和指标的基础知识
Before we start digging more into this library, we should first understand the basics of Registry, Meter, and Metrics.
在我们开始深入研究这个库之前,我们应该首先了解注册表、计量表和度量表的基本知识。
- Registry is where we maintain a set of meters
- Meter is used for collecting a set of measurements about our application, e.g., Counter, Timer, Gauge, etc.
- Metrics are individual measurements that we display on a Meter, e.g., count, duration, max, average, etc.
Let’s explore these further and understand how they are used in the Spectator library.
让我们进一步探讨这些问题,了解它们在Spectator库中的使用情况。
4. Registry
4.登记处
The Spectator library comes with Registry as an interface, with some built-in implementations, e.g., DefaultRegistry and NoopRegistry. We can also create custom Registry implementations as per our requirements.
Spectator库自带的Registry是一个接口,有一些内置的实现,例如DefaultRegistry和NoopRegistry。我们还可以根据我们的要求创建自定义的Registry实现。
A Registry implementation can be used as below:
一个注册表的实现可以像下面这样使用。
Registry registry = new DefaultRegistry();
5. Meter
5.米特
Meter is of mainly two types, i.e., Active and Passive.
仪表主要有两种类型,即主动和被动。
5.1. Active Meters
5.1.有源计量器
These meters are used to measure some event’s occurrence. We have three types of such meters:
这些仪表是用来测量一些事件的发生的。我们有三种类型的此类仪表。
- Counter
- Timers
- DistributionSummary
5.2. Passive Meters
5.2 被动式仪表
These meters are used to get the value of the metric when needed. For example, the number of running threads could be a metric we want to measure. We have a single type of such meter, Gauge.
在需要的时候,这些仪表被用来获取指标的值。例如,运行线程的数量可能是我们想要测量的指标。我们有一个单一类型的此类仪表,Gauge.。
Next, let’s explore these different types of meters in detail.
接下来,让我们详细探讨一下这些不同类型的仪表。
6. Counter
6.计数器
These meters measure the rate at which an event occurs. For example, suppose we want to measure the rate at which elements are inserted or removed from a list.
这些仪表测量的是事件发生的速度。例如,假设我们想测量元素从一个列表中插入或移除的速度。
Let’s first register a counter to the Registry object on initialization:
让我们首先在初始化时向Registry对象注册一个计数器。
insertCounter = registry.counter("list.insert.count");
removeCounter = registry.counter("list.remove.count");
Here, we can allow users to use any Registry implementation using dependency injection.
在这里,我们可以允许用户使用依赖注入的任何Registry实现。
Now, we can increment or decrement the Counter meter for addition to list or deletion from a list, respectively:
现在,我们可以增加或减少Counter表,分别用于添加到列表或从列表中删除。
requestList.add(element);
insertCounter.increment();
requestList.remove(0);
removeCounter.increment();
In this way, we can generate two meters, and later, we can push the metrics to Atlas for visualization.
通过这种方式,我们可以生成两个仪表,之后,我们可以将指标推送到Atlas进行可视化。
7. Timers
7.计时器
These meters measure the time spent on some event. Spectatorsupports two types of timers:
这些仪表测量的是在某些事件上花费的时间。Spectator支持两种类型的计时器。
7.1. Timer
7.1. Timer
These timers are mainly used to measure short-duration events. Therefore, they usually measure the time spent after the completion of the event.
这些计时器主要用于测量短时间的事件。因此,它们通常测量的是事件完成后的时间。
First, we need to register this meter in the Registry:
首先,我们需要在注册表中注册这个表。
requestLatency = registry.timer("app.request.latency");
Next, we can invoke the record() method of Timer to measure the time spent in handling the request:
接下来,我们可以调用Timer的record()方法来测量处理请求的时间。
requestLatency.record(() -> handleRequest(input));
7.2. LongTaskTimer
7.2. LongTaskTimer
These timers are mainly used to measure the duration of long-running tasks. Therefore, we can query these timers even when the event is in process. This is also a type of Gauge. We can see metrics like duration and activeTasks when the event is in progress.
这些定时器主要用于测量长期运行的任务的持续时间。因此,即使事件正在进行中,我们也可以查询这些定时器。这也是Gauge的一种类型。我们可以在事件进行中看到duration和activeTasks等指标。
Again, as a first step, we need to register this meter:
同样,作为第一步,我们需要注册这个仪表。
refreshDuration = LongTaskTimer.get(registry, registry.createId("metadata.refreshDuration"));
Next, we can use LongTaskTimer to start and stop measurements around the long-running task:
接下来,我们可以使用LongTaskTimer来启动和停止围绕长期运行的任务的测量。
long taskId = refreshDuration.start();
try {
Thread.sleep(input);
return "Done";
} catch (InterruptedException e) {
e.printStackTrace();
throw e;
} finally {
refreshDuration.stop(taskId);
}
8. Gauges
8. 仪表
As we discussed earlier, gauges are passive meters. Therefore, these give a value that is sampled at any point in time for a running task. So, for example, we would use it if we wanted to know the number of running threads in a JVM or the heap memory usage at any point in time.
正如我们前面所讨论的,仪表是被动的仪表。因此,这些仪表给出了一个在运行任务的任何时间点上采样的值。因此,举例来说,如果我们想知道JVM中运行的线程数或任何时间点的堆内存使用情况,我们就会使用它。
We have two types of gauges:
我们有两种类型的测量仪。
- Polled Gauges
- Active Gauges
8.1. Polled Gauges
8.1.有投票权的测量仪
This type of gauges polls a running task’s value in the background. It creates a hook on the task it monitors. Hence, there is no need to update the value in this gauge.
这种类型的测量仪在后台对一个正在运行的任务的值进行调查。它在它所监控的任务上创建一个钩子。因此,不需要更新这个仪表的值。
Now, let’s see how to use this gauge to monitor a List‘s size:
现在,让我们看看如何使用这个仪表来监测List的大小。
PolledMeter.using(registry)
.withName("list.size")
.monitorValue(listSize);
Here, PolledMeter is the class that allows background polling on listSize using the monitorValue() method. Further, listSize is the variable that tracks the size of our sample list.
这里,PolledMeter是一个允许使用monitorValue()方法对listSize进行后台轮询的类。此外,listSize是跟踪我们样本列表大小的变量。
8.2. Active Gauges
8.2.主动测量仪
This type of gauge requires regular manual updates on the value with respect to updates in monitoring tasks. Here is an example of using active gauges:
这种类型的仪表需要在监测任务的更新方面定期手动更新数值。下面是一个使用活动仪表的例子:。
gauge = registry.gauge("list.size");
We first register this gauge in the Registry. Then, we manually update it on the addition or removal of elements from the list:
我们首先在注册表中注册这个仪表。然后,我们在添加或删除列表中的元素时手动更新它。
list.add(element);
gauge.set(listSize);
list.remove(0);
gauge.set(listSize);
9. DistributionSummary
9.DistributionSummary
Now, we will look into another meter known as DistributionSummary. It tracks the distribution of events. This meter can measure the size of the request payload. For example, we’ll use DistributionSummary to measure a request’s size.
现在,我们将研究另一个被称为DistributionSummary.的仪表,它跟踪事件的分布。这个仪表可以测量请求有效载荷的大小。例如,我们将使用DistributionSummary来测量一个请求的大小。
First, as always, we register this meter in Registry:
首先,像往常一样,我们在Registry中注册这个仪表。
distributionSummary = registry.distributionSummary("app.request.size");
Now, we can use this meter similar to a Timer to record the request’s size:
现在,我们可以使用这个类似于Timer的仪表来记录请求的大小。
distributionSummary.record((long) input.length());
handleRequest();
10. Spectator vs. Servo vs. Micrometer
10.观察者 VS 伺服机 VS 测微仪
Servo is also a library to measure different code metrics. Spectator is a successor to Servo, built by Netflix. Spectator was initially launched for Java 8, and from a future support perspective, it is a better option.
Servo也是一个用于测量不同代码指标的库。Spectator是Servo的后继者,由Netflix构建。Spectator最初是为Java 8推出的,从未来支持的角度来看,它是一个更好的选择。
These Netflix libraries are one of the various options available in the market to measure different metrics. We can always use them individually, or we can go for a facade like Micrometer. Micrometer lets users switch between different metrics measuring libraries with ease. Hence, it also allows a choice of different backend monitoring systems.
这些Netflix库是市场上可用来测量不同指标的各种选择之一。我们总是可以单独使用它们,或者我们可以选择像Micrometer这样的门面。Micrometer可以让用户轻松地在不同的度量衡测量库之间切换。因此,它也允许选择不同的后端监控系统。
11. Conclusion
11.结语
In this article, we have introduced Spectator, a library from Netflix for metrics measurement. Also, we looked into the usage of its various active and passive meters. We can push and publish the instrumented data to the time-series database Atlas.
在这篇文章中,我们介绍了Spectator,一个来自Netflix的指标测量库。同时,我们还研究了它的各种主动和被动仪表的用法。我们可以推送和发布仪表数据到时间序列数据库Atlas。
As always, the full implementation code of this article can be found over on GitHub.
一如既往,本文的完整实施代码可以在GitHub上找到over。