1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.clientmetrics;
import com.yahoo.documentapi.messagebus.protocol.DocumentIgnoredReply;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.messagebus.Reply;
import com.yahoo.concurrent.SystemTimer;
import com.yahoo.metrics.*;
import com.yahoo.messagebus.Error;
import java.util.List;
import java.util.stream.Stream;
/**
* @author thomasg
*/
public class MessageTypeMetricSet extends MetricSet {
ValueMetric<Long> latency;
CountMetric count;
CountMetric ignored;
SumMetric errorSum;
MetricSet errors;
String msgName;
class ErrorMetric extends CountMetric {
ErrorMetric(String name, MetricSet owner) {
super(name, "", "Number of errors of type " + name, owner);
}
ErrorMetric(ErrorMetric other, CopyType copyType, MetricSet owner) {
super(other, copyType, owner);
}
@Override
public String getXMLTag() {
return "error";
}
@Override
public Metric clone(CopyType type, MetricSet owner, boolean includeUnused) {
return new ErrorMetric(this, type, owner);
}
}
public MessageTypeMetricSet(String msgName, MetricSet owner) {
super(msgName.toLowerCase(), "", "", owner);
this.msgName = msgName;
latency = new ValueMetric<Long>("latency", "", "Latency (in ms)", this).averageMetric();
count = new CountMetric("count", "", "Number received", this);
ignored = new CountMetric("ignored", "", "Number ignored due to no matching document routing selectors", this);
errors = new SimpleMetricSet("errors", "", "The errors returned", this);
errorSum = new SumMetric("total", "", "Total number of errors", errors);
}
public MessageTypeMetricSet(MessageTypeMetricSet source, CopyType copyType, MetricSet owner, boolean includeUnused) {
super(source, copyType, owner, includeUnused);
msgName = source.msgName;
}
public String getMessageName() {
return msgName;
}
public void addReply(Reply r) {
if (!r.hasErrors() || onlyTestAndSetConditionFailed(r.getErrors())) {
updateSuccessMetrics(r);
} else {
updateFailureMetrics(r);
}
}
private void updateFailureMetrics(Reply r) {
String error = DocumentProtocol.getErrorName(r.getError(0).getCode());
CountMetric s = (CountMetric)errors.getMetric(error);
if (s == null) {
s = new ErrorMetric(error, errors);
errorSum.addMetricToSum(s);
}
s.inc();
}
private void updateSuccessMetrics(Reply r) {
if (!(r instanceof DocumentIgnoredReply)) {
if (r.getMessage().getTimeReceived() != 0) {
latency.addValue(SystemTimer.INSTANCE.milliTime() - r.getMessage().getTimeReceived());
}
count.inc();
} else {
ignored.inc();
}
}
@Override
public Metric clone(CopyType type, MetricSet owner, boolean includeUnused)
{ return new MessageTypeMetricSet(this, type, owner, includeUnused); }
/**
* Returns true if every error in a stream is a test and set condition failed
*/
private static boolean onlyTestAndSetConditionFailed(Stream<Error> errors) {
return errors.allMatch(e -> e.getCode() == DocumentProtocol.ERROR_TEST_AND_SET_CONDITION_FAILED);
}
}
|