metric driven (4) – metric code technology

Metirc记录中用到的技术点

1. AOP
例如想记录所有数据操作的耗时,传统的方式是每个调用之前和之后记录下时间然后相减,示例:

方法调用1:

Instant timeStart = Instant.now();
DataService.listAllUserStories();
long timeDurationInMs = Instant.now().toEpochMilli() - timeStart.toEpochMilli();

方法调用2:

Instant timeStart = Instant.now();
DataService.deleteUserStory(userStoryId);
long timeDurationInMs = Instant.now().toEpochMilli() - timeStart.toEpochMilli();

使用AOP后,直接拦截DataService层的所有调用即可:

@Around("(within(com.dashboard.DataService))")
public Object aroundDataService(ProceedingJoinPoint pjp) throws Throwable {
         Instant timeStart = Instant.now();
         try {
              return pjp.proceed();
         } finally {
             long timeDurationInMs = Instant.now().toEpochMilli() - timeStart.toEpochMilli();
             ……
         }
  }

然后假设对于一些调用并不想记录(例如health check),可以自定义一个注解,然后在拦截时,指定注解即可。

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface StepAnnotation {
}

@Around("(within(com.dashboard.DataService)) && annotation(stepAnnotation)")
public Object aroundDataService(ProceedingJoinPoint pjp , StepAnnotation stepAnnotation) throws Throwable
……

2 Thread Local
在记录metric时,除了最基本的一些信息(环境信息、消耗时间、发生时间、操作名等)外,很多调用都需要额外添加一些信息到metric里面,例如创建一个站点的api可能需要记录siteid,删除某个dashboard时,记录dashboard的id,诸如此类,需要绑定的信息可能不尽相同,这个时候可是使用thread local来绑定信息。

public class MetricThreadLocal {

private static final ThreadLocal FEAUTRE_METRIC_THREAD_LOCAL = new ThreadLocal();

public static FeatureMetric getFeatureMetric(){
     return FEAUTRE_METRIC_THREAD_LOCAL.get();
 }

public static void setFeatureMetric(FeatureMetric metricsRecord){
     FEAUTRE_METRIC_THREAD_LOCAL.set(metricsRecord);
 }

public static void setCurrentFeatureMetric(String attributeName, Object attributeValue){
    FeatureMetric featureMetric = getFeatureMetric();
    if(featureMetric != null) {
        featureMetric.setValue(attributeName, attributeValue);
    }
 }

public static void cleanFeatureMetirc(){
       FEAUTRE_METRIC_THREAD_LOCAL.remove();
  }

}

使用thread local时,要注意线程切换的问题:例如,假设想要在metric信息中,绑定trackingid.
(1)Thread切换
使用Thread local时,希望每个请求的日志都能绑定到对应的trackingid上,但是往往事与愿违,存在以下两种不可控情况:

(1)现象: 一些日志始终绑定某个trackingid

使用第三方或者其他人提供的包时,其他人采用的是异步线程去实现的,这个时候,第一个请求会触发第一个线程建立起来,而第一个线程的trackingid会和第一个请求一样(创建的线程的threadlocal会继承创建者):

Thread的构造器实现

        if (parent.inheritableThreadLocals != null)
            this.inheritableThreadLocals =
                ThreadLocal.createInheritedMap(parent.inheritableThreadLocals);

这样导致,只要这个线程一直存在,就一直是和第一个请求一致。

因为callable或runnable的task内容不是自己可以控制的范畴,导致再无机会去修改。

	private static final ExecutorService pool= Executors.newFixedThreadPool(3);

	public static final String checkAsync(String checkItem) {
  
			checkFuture= pool.submit(new Callable(checkItem) {
				public String call() throws Exception {
					......  //第三方库,无法修改,如果是自己库,直接MDC.put("TrackingID", trackingID)既可修改,或者更标准的搞法(slf4j支持):

“In such cases, it is recommended that MDC.getCopyOfContextMap() is invoked on the original (master) thread before submitting a task to the executor. When the task runs, as its first action, it should invoke MDC.setContextMapValues() to associate the stored copy of the original MDC values with the new Executor managed thread.”

				}
			});

代码示例的情况没有太大危险,因为线程一旦创建,就不会消亡,所以最多某个首次请求,查询到的日志特别多,后面的请求对不上号。但是如果某个线程池是有timeout回收的,则有可能导致很多次请求查询到的trackingid日志都特别多。

解决方案,不想固定死某个trackingid,则调用那个api前clean掉mdc里的trackingid,这样创建的线程就不会带有,即既然不属于我一个人,干脆放弃。调用完再找回。但是这样修改后,调用过程的log就都没有trackingid了。所以很难完美解决,要么有很多且对不上号的,要么一个都没有。

(2)现象:某个请求中,tracking中途丢失了或者变成别的了。

这是因为调用了第三方库,而第三库做了一些特殊处理,比如


	public String call(String checkItem) { 
              call(checkItem, null)
        }
	public String call(String checkItem, Map config) { 
                        String trackingID = config.get("TrackingID");
                        if(trackingID == null)
                              trackingID = "";
                        MDC.put("TrackingID", trackingID);  //因为没有显示trackingid来调用,导致后面的这段逻辑把之前设置的trackingid给清空了(="")。
                        ......
        }
        

解决方案: 方案(1)显式传入trackingid。而不是直接调用call(String checkItem); 方案(2)既然使用mdc,为什么不去check下mdc里面实现是不是有值,如果有,也算传入了,而不是直接覆盖掉。

以上问题很容易出现在第三方库的调用上,且如果不看代码,很难预知会出现什么清空或一直绑定某个。不管哪种情况,都要意识到所以使用mdc不是完美的,因为很多第三库的调用对于你而言都是不透明且不可修改的。

3 Filter/Task

记录metric第一件需要做的事情是选择好记录的位置,一般常见的就2种,对于web service常见就是各种filter,而对于内部实现,大多是一个thread的task创建的位置好。

   services.add(new MetricsRequestFilter());
   services.add(new MetricsResponseFilter());
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
try {
        ResourceMethodInvoker resourceMethodInvoker = (ResourceMethodInvoker) 
        requestContext.getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
        if(null != resourceMethodInvoker) {
             Method method = resourceMethodInvoker.getMethod();
             writeFeatureMetricsStart(method, requestContext);
}
   }catch (Exception e){
       logger.error("filter metrics request failed.", e);
}
}
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
try {
          ResourceMethodInvoker resourceMethodInvoker = (ResourceMethodInvoker) requestContext
            .getProperty("org.jboss.resteasy.core.ResourceMethodInvoker");
          if (null != resourceMethodInvoker) {
              Method method = resourceMethodInvoker.getMethod();
              writeFeatureMetricsEnd(method, requestContext, responseContext);
   }
} catch (Exception e) {
          logger.error("filter metrics response failed.", e);
}
}
public abstract class TaskWithMetric implements Runnable {

public void run() {

  TaskExecuteResult taskExecuteResult = null;
  try {
           taskExecuteResult = execute();
   } catch(Exception ex){
           taskExecuteResult = TaskExecuteResult.fromException(ex);
           throw ex;
   } finally {
           MetricThreadLocal.cleanFeatureMetirc();
     if (featureMetric != null) {
           writeMetrics(waitingDurationForThread, featureMetric, taskExecuteResult);
        }
    }
}

(1)Filter优先级

@Priority(value = 10)
public class MetricsRequestFilter implements javax.ws.rs.container.ContainerRequestFilter


4 codahales

com.datastax.driver.core.Metrics



private final Gauge knownHosts = registry.register("known-hosts", new Gauge() {
@Override
public Integer getValue() {
    return manager.metadata.allHosts().size();
}
});

5 JMX

基本现在主流的Java服务都提供jmx监控的方式, 如果只是想做展示,不想自定义更多的,这直接开启即可:

5.1 开启jmx:

-Dcom.sun.management.jmxremote=true 
-Dcom.sun.management.jmxremote.port=8091 //定义port
-Dcom.sun.management.jmxremote.ssl=false 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.password.file=/conf/jmxremote.password  //定义了用户名和密码
-Dcom.sun.management.jmxremote.access.file=/conf/jmxremote.access  //定义了权限

例如:

jmxremote.password

admin P@ssword123

jmxremote.access

admin readwrite

然后可以通过第三方组件来读取信息以供展示,例如collectd的GenericJMX plugin来获取信息:

<Plugin "java">
  JVMARG "-Djava.class.path=/opt/collectd/share/collectd/java/collectd-api.jar:/opt/collectd/share/collectd/java/generic-jmx.jar"
  LoadPlugin "org.collectd.java.GenericJMX"
  
  <Plugin "GenericJMX">
     <MBean "Memory">
      ObjectName "java.lang:type=Memory"
      InstancePrefix "Memory"
      <Value>
        Type "memory"
        Table true
        InstancePrefix "HeapMemoryUsage`"
        Attribute "HeapMemoryUsage"
      </Value>
      <Value>
        Type "memory"
        Table true
        InstancePrefix "NonHeapMemoryUsage`"
        Attribute "NonHeapMemoryUsage"
      </Value>
    </MBean>
    <MBean "GarbageCollector">
      ObjectName "java.lang:type=GarbageCollector,*"
      InstancePrefix "GarbageCollector`"
      InstanceFrom "name"
      <Value>
        Type "invocations"
        Table false
        Attribute "CollectionCount"
      </Value>
      <Value>
        Type "total_time_in_ms"
        Table false
        Attribute "CollectionTime"
      </Value>
    </MBean>
    <Connection>
      Host "localhost"
      ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:8091/jmxrmi"  //8091为上文中定义的端口
      User "admin" //admin为上文中定义的用户名
      Password "P@ssword123"  //P@ssword123为上文中定义的密码
      Collect "MemoryPool"
      Collect "Memory"
      Collect "GarbageCollector"
      Collect "OperatingSystem"
      Collect "Threading"
      Collect "Runtime"
      Collect "BufferPool"
      Collect "Compilation"
      Collect "GlobalRequestProcessor"
      Collect "ThreadPool"
      Collect "DataSource"
    </Connection>
  </Plugin>
</Plugin>

5.2 自定义jmx:

定义:

public interface MetricsMBean {
	
	public int getTotalCount();

}


public class Metrics implements MetricsMBean {
  	
	private int totalCountConnections;
 	
	public Metrics(int totalCountConnections) {
 		this.totalCountConnections = totalCountConnections;
	}
	
	@Override
	public int getTotalCount() {
 		return totalCountConnections;
	}
  
}

启动:

	MBeanServer server = ManagementFactory.getPlatformMBeanServer();
	ObjectName metricsName = new ObjectName("Metrics:name=MetricsNameOne");
	server.registerMBean(new Metrics(100), metricsName);  

另外我们可以使用jmx的通知来实现一些有趣的功能。例如当gc时,输出一条metric:

定义通知行为:

private final static class NotificationListenerImplementation implements NotificationListener {
		
		private long jvmStartTime;
		private MetricsHandler metricHandler;
		
 		public NotificationListenerImplementation(long jvmStartTime, MetricsHandler metricHandler) {
			super();
			this.jvmStartTime = jvmStartTime;
			this.metricHandler = metricHandler; 
		}

		@Override
		public void handleNotification(Notification notification, Object handback) {
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug("received notifcation: " + notification.getType());
			}
 
			//write one GC metrics
 		}

		
	}

注册通知:

long jvmStartTime = ManagementFactory.getRuntimeMXBean().getStartTime();
			List<GarbageCollectorMXBean> gcbeans = ManagementFactory.getGarbageCollectorMXBeans();
			for (GarbageCollectorMXBean gcbean : gcbeans) {
 				LOGGER.info("GC bean: " + gcbean);
				if (!(gcbean instanceof NotificationEmitter))
					continue;

				NotificationEmitter emitter = (NotificationEmitter) gcbean;
				emitter.addNotificationListener(new NotificationListenerImplementation(jvmStartTime, metricHandler), notification -> {
 						return GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION
						.equals(notification.getType());
 				}, null);

			}

这样就可以得到类似如下的metrics:

{
  "featureName": "java_gc",
  "componentType": "DSA",
  "componentAddress": "10.224.56.146",
  "componentVer": "1.5.0 ",
  "poolName": "production",
  "metricType": "innerApi",
  "timestamp": "2017-05-25T04:45:33.235Z",
  "values": {
    "steps": [
        
    ],
    "totalDurationInMS": 254
  },
  "trackingID": "3"
}

6 threshold
在输出metric时,有时需要平衡数据量和实际用途。对于一些数据量会很大,但是大多实际并无有用,但是需要时,又可能抓狂没有到数据,大多我们可以使用threadhold来均衡数据和实际用途,例如对于dns解析,大多时候,dns解析都很快,我们不需要metric来记录这些信息,但是有的时候,排除排查延时较大时,需要知道dns是否是个影响因素,这个时候,可以采用 threadhold来控制,例如超过500ms的dns解析才需要记录:

 
public class MetricsDnsResolver implements DnsResolver {

	private static final int THRESHOLD_IN_MS = 100;
	private static final Logger LOGGER = Logger.getLogger(MetricsDnsResolver .class);
	 
     public InetAddress[] resolve(String host) throws UnknownHostException {
    	long startTime = System.currentTimeMillis();
    	InetAddress[] allByName = null;
    	try {
            allByName = InetAddress.getAllByName(host);
    	}finally {
            long duration = System.currentTimeMillis() - startTime;
            if(duration > THRESHOLD_IN_MS) {
                LOGGER.warn("DNS: " + host + ", take too long time(ms): " + duration);
                //write metric here
            }
    	}

这样既避免了大多无用的数据量,同时,在真正需要时,又能有据可查。

Threshold的思想在metric设计时,非常重要,能保持数据的有效有力。

总结: 以上几种技术要点很多时候,都是混合在一起使用的,例如将jmx和codahales结合在一起,更方便的统计metric, 以Cassandra的jmx metric作为例子:

org.apache.cassandra.metrics.CassandraMetricsRegistry

    public interface JmxHistogramMBean extends MetricMBean
    {
        long getCount();

        long getMin();

        long getMax();

        double getMean();

        double getStdDev();

        double get50thPercentile();

        double get75thPercentile();

        double get95thPercentile();

        double get98thPercentile();

        double get99thPercentile();

        double get999thPercentile();

        long[] values();
    }

    private static class JmxHistogram extends AbstractBean implements JmxHistogramMBean
    {
        private final Histogram metric;

        private JmxHistogram(Histogram metric, ObjectName objectName)
        {
            super(objectName);
            this.metric = metric;
        }

        @Override
        public double get50thPercentile()
        {
            return metric.getSnapshot().getMedian();
        }

        @Override
        public long getCount()
        {
            return metric.getCount();
        }

        @Override
        public long getMin()
        {
            return metric.getSnapshot().getMin();
        }

        @Override
        public long getMax()
        {
            return metric.getSnapshot().getMax();
        }

        @Override
        public double getMean()
        {
            return metric.getSnapshot().getMean();
        }

        @Override
        public double getStdDev()
        {
            return metric.getSnapshot().getStdDev();
        }

        @Override
        public double get75thPercentile()
        {
            return metric.getSnapshot().get75thPercentile();
        }

        @Override
        public double get95thPercentile()
        {
            return metric.getSnapshot().get95thPercentile();
        }

        @Override
        public double get98thPercentile()
        {
            return metric.getSnapshot().get98thPercentile();
        }

        @Override
        public double get99thPercentile()
        {
            return metric.getSnapshot().get99thPercentile();
        }

        @Override
        public double get999thPercentile()
        {
            return metric.getSnapshot().get999thPercentile();
        }

        @Override
        public long[] values()
        {
            return metric.getSnapshot().getValues();
        }
    }

    private abstract static class AbstractBean implements MetricMBean
    {
        private final ObjectName objectName;

        AbstractBean(ObjectName objectName)
        {
            this.objectName = objectName;
        }

        @Override
        public ObjectName objectName()
        {
            return objectName;
        }
    }

再如,现在新兴的spring metric,也是将以上的一些技术进行了一些组合,提供了良好的封装,例如下面的使用方式非常简易。

@SpringBootApplication
@EnablePrometheusMetrics
public class MyApp {
}

@RestController
@Timed
class PersonController {
    Map<Integer, Person> people = new Map<Integer, Person>();

    public PersonController(MeterRegistry registry) {
        // constructs a gauge to monitor the size of the population
        registry.mapSize("population", people);
    }

    @GetMapping("/api/people")
    public List<Person> listPeople() {
        return people;
    }

    @GetMapping("/api/person/")
    public Person findPerson(@PathVariable Integer id) {
        return people.get(id);
    }
}

参考文献:
1. https://docs.spring.io/spring-metrics/docs/current/public/prometheus

metric driven (3) – basic terms and concepts

在学习metric之前,需要对metric领域涉及的基本概念有个初步的整体认识:

一 统计学指标:
以下面一段Cassandra的metric做示例:
Cassandra Metrics
Timer:
name=requests, count=171396, min=0.480973, max=4.228569, mean=1.0091534824902724, stddev=0.3463516148968051, median=0.975965, p75=1.1458145, p95=1.4784138999999996, p98=1.6538238999999988, p99=2.185363660000002, p999=4.22124273, mean_rate=0.0, m1=0.0, m5=0.0, m15=0.0, rate_unit=events/millisecond, duration_unit=milliseconds

1.1 集中量
(1)最大值、最小值、平均数:容易理解,不做赘述,即例子中的min=0.480973, max=4.228569,mean=1.0091534824902724,表明最小、最大和平均响应时间;
(2)中位数:一个样本、种群或概率分布中的一个数值,用它可以讲所有数据划分为相等的上下两部分。对于有限的数集,可以通过把所有观察值高低排序后找出正中间的一个作为中位数。如果数据总数是偶数个,通常取最中间的两个数值的平均数作为中位数。如示例中的median=0.975965,代表所有响应时间中最中间的那个数字,可见和平均数(mean=1.0091534824902724)并不相等。

1.2 差异量
(1)全距:将上述的最大值减去最小值即为全距,代表变化的范围,数值越大,说明数据越分散。例子中即为3.747596(max-min),数据跨度并不大;
(2)方差:每个样本值与全体样本值的平均数之差的平方值的平均数,用来度量随机变量和其数学期望(即均值)之间的偏离程度. 数值越小,表明数据分布越集中,例子中即为stddev=0.3463516148968051,表明数据相对很集中。

1.3 地位量
(1)分位数:分位数是将总体的全部数据按大小顺序排列后,处于各等分位置的变量值。例如上面的p95, p999都是这种概念。例如p999=4.22124273,代表99.9%的请求响应时间不大于4.22124273ms;
(2)四分位数:如果分成四等分,就是四分位数;八等分就是八分位数等。四分位数也称为四分位点,它是将全部数据分成相等的四部分,其中每部分包括25%的数据,处在各分位点的数值就是四分位数。四分位数有三个,第一个四分位数就是通常所说的四分位数,称为下四分位数,第二个四分位数就是中位数,第三个四分位数称为上四分位数,分别用Q1、Q2、Q3表示 [1] 。
第一四分位数 (Q1),又称”较小四分位数”,等于该样本中所有数值由小到大排列后第25%的数字。
第二四分位数 (Q2),又称“中位数”,等于该样本中所有数值由小到大排列后第50%的数字,即median=0.975965。
第三四分位数 (Q3),又称“较大四分位数”,等于该样本中所有数值由小到大排列后第75%的数字, 例如p75=1.1458145

二 相关的术语

2.1 QPS/TPS/PV/

QPS: Query Per Second每秒的查询次数;
TPS: Transaction per second 每秒的业务量;
PV: page view: 即文章的浏览量,常用于web服务器页面的统计。

2.2 度量类型

(1) Counter
代表一个递增的值,例如请求数,在server正常运行时,只会增加不会减少,在重启时会归0,例如:Cassandra的metric中的下列指标,都是只增不减,重启归0的值。
Counters:
name=client-timeouts, count=0
name=connection-errors, count=0
name=ignores, count=0
name=ignores-on-write-timeout, count=0
name=other-errors, count=0
name=read-timeouts, count=0
name=retries, count=0
name=speculative-executions, count=0
name=unavailables, count=0
name=write-timeouts, count=0

(2) Gauge
代表一个变化的值,表明是个瞬间的值。例如温度等可变化因素,下例中的Cassandra的metric值都是可以变化:

GAUGE:
name=blocking-executor-queue-depth, value=0
name=connected-to, value=7
name=executor-queue-depth, value=0
name=known-hosts, value=36
name=open-connections, value=8
name=reconnection-scheduler-task-count, value=0
name=task-scheduler-task-count, value=1
name=trashed-connections, value=0

(3) Histogram
即直方图: 主要使用来统计数据的分布情况,最大值、最小值、平均值、中位数,百分比(75%、90%、95%、98%、99%和99.9%)

count=171396, min=0.480973, max=4.228569, mean=1.0091534824902724, stddev=0.3463516148968051, median=0.975965, p75=1.1458145, p95=1.4784138999999996, p98=1.6538238999999988, p99=2.185363660000002, p999=4.22124273

注意关于百分比的相关参数并不是实际整个server运行期间的百分比,而是基于一定的统计方法来计算的值,试想,不可能存储所有的请求的数据,只能从时间和空间两个维度来推算。

例如:
com.codahale.metrics.SlidingWindowReservoir

package com.codahale.metrics;</code>

import static java.lang.Math.min;

/**
* A {@link Reservoir} implementation backed by a sliding window that stores the last {@code N}
* measurements.
*/
public class SlidingWindowReservoir implements Reservoir {
private final long[] measurements;
private long count;

/**
* Creates a new {@link SlidingWindowReservoir} which stores the last {@code size} measurements.
*
* @param size the number of measurements to store
*/
public SlidingWindowReservoir(int size) {
this.measurements = new long[size];
this.count = 0;
}

@Override
public synchronized int size() {
return (int) min(count, measurements.length);
}

@Override
public synchronized void update(long value) {
measurements[(int) (count++ % measurements.length)] = value;
}

@Override
public Snapshot getSnapshot() {
final long[] values = new long[size()];
for (int i = 0; i &lt; values.length; i++) {
synchronized (this) {
values[i] = measurements[i];
}
}
return new Snapshot(values);
}
}

(4) Meters
用来度量某个时间段的平均处理次数(request per second),每1、5、15分钟的TPS。比如一个service的请求数,统计结果有总的请求数,平均每秒的请求数,以及最近的1、5、15分钟的平均TPS:

mean_rate=0.0, m1=0.0, m5=0.0, m15=0.0, rate_unit=events/millisecond, duration_unit=milliseconds

(5) Timer
主要是用来统计某一块代码段的执行时间以及其分布情况,具体是基于Histograms和Meters来实现的,所以结果是上面两个指标的合并结果。

三 Metric处理的一些术语:

3.1 Event time / Ingestion time / Processing Time
在流程序中支持不同概念的时间。以读取一条存储在kafka中的数据为例,存在多个时间字段:

Tue Jul 03 11:02:20 CST 2018 offset = 1472200, key = null, value = {"path":"/opt/application/logs/metrics_07012018_0.24536.log","component":"app","@timestamp":"2018-07-01T03:14:53.982Z","@version":"1","host":"10.224.2.116","eventtype":"metrics","message":{"componentType":"app","metricType":"innerApi","metricName":"demo","componentAddress":"10.224.57.67","featureName":"PageCall","componentVer":"2.2.0","poolName":"test","trackingID":"0810823d-dc92-40e4-8e9a-18774d549e21","timestamp":"2018-07-01T03:14:53.728Z"}}

Event time: 是事件发生的时间,而不考虑事件记录通过什么系统、什么时候到达、以什么样的顺序到达等因素。上例中2018-07-01T03:14:53.728Z即为server日志产生的时间;相当于邮寄快递时的邮寄时间。

Ingestion time:即摄入时间,是事件进入Metric系统的时间,在源操作中每个记录都会获得源的当前时间作为时间戳,当一个metric经过n条系统时,相对每条进入的数据,摄入时间就会存在多个。上例中的”@timestamp”:”2018-07-01T03:14:53.982Z”即为logstash获取这个server log的时间。相当于邮寄快递时,快递员的揽收时间。

Processing Time: 是处理系统开始处理某个事件的时间,上例中“Tue Jul 03 11:02:20 CST”为处理时间。相当于邮寄快递中的签收时间。

明确各种时间概率后,可见事件时间是最重要的。

3.2 tulbmimg window / hop window / session window

(1) Tumbling windows
Tumbling即翻跟头的意思,窗口不可能重叠的;在流数据中进行滚动,这种窗口不存在重叠,也就是说一个metric只可能出现在一个窗口中。

(2)Sliding Windows
是可能存在重叠的,即滑动窗口。

(3)session window
以上两种都是基于时间的,固定的窗口,还存在一种window: session window
当我们需要分析用户的一段交互的行为事件时,通常的想法是将用户的事件流按照“session”来分组。session 是指一段持续活跃的期间,由活跃间隙分隔开。通俗一点说,消息之间的间隔小于超时阈值(sessionGap),如果两个元素的时间戳间隔小于 session gap,则会在同一个session中。如果两个元素之间的间隔大于session gap,且没有元素能够填补上这个gap,那么它们会被放到不同的session中。

通过以上不同类别的基本指标和术语的了解,可以大体了解metric领域的一些知识,为以后的metric工作的进展做铺垫。

参考文献:
https://baike.baidu.com/item/%E4%B8%AD%E4%BD%8D%E6%95%B0
https://www.jianshu.com/p/68ab40c7f347
https://yq.aliyun.com/articles/64818
https://kafka.apache.org/11/documentation/streams/developer-guide/dsl-api.html#windowing
https://prometheus.io/docs/concepts/metric_types/
https://www.app-metrics.io/getting-started/metric-types/
http://www.cnblogs.com/nexiyi/p/metrics_sample_1.html