forked from apache/bookkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodahaleStatsLogger.java
136 lines (117 loc) · 4.21 KB
/
CodahaleStatsLogger.java
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.apache.bookkeeper.stats.codahale;
import static com.codahale.metrics.MetricRegistry.name;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import org.apache.bookkeeper.stats.Counter;
import org.apache.bookkeeper.stats.Gauge;
import org.apache.bookkeeper.stats.OpStatsLogger;
import org.apache.bookkeeper.stats.StatsLogger;
import java.util.concurrent.TimeUnit;
/**
* A {@link StatsLogger} implemented based on <i>Codahale</i> metrics library.
*/
public class CodahaleStatsLogger implements StatsLogger {
protected final String basename;
final MetricRegistry metrics;
CodahaleStatsLogger(MetricRegistry metrics, String basename) {
this.metrics = metrics;
this.basename = basename;
}
@Override
public OpStatsLogger getOpStatsLogger(String statName) {
Timer success = metrics.timer(name(basename, statName));
Timer failure = metrics.timer(name(basename, statName + "-fail"));
return new CodahaleOpStatsLogger(success, failure);
}
@Override
public Counter getCounter(String statName) {
final com.codahale.metrics.Counter c = metrics.counter(name(basename, statName));
return new Counter() {
@Override
public synchronized void clear() {
long cur = c.getCount();
c.dec(cur);
}
@Override
public Long get() {
return c.getCount();
}
@Override
public void inc() {
c.inc();
}
@Override
public void dec() {
c.dec();
}
@Override
public void addCount(long delta) {
c.inc(delta);
}
@Override
public void addLatency(long eventLatency, TimeUnit unit) {
long valueMillis = unit.toMicros(eventLatency) / 1000;
c.inc(valueMillis);
}
};
}
@Override
public <T extends Number> void registerGauge(final String statName, final Gauge<T> gauge) {
String metricName = name(basename, statName);
metrics.remove(metricName);
metrics.register(metricName, new com.codahale.metrics.Gauge<T>() {
@Override
public T getValue() {
return gauge.getSample();
}
});
}
@Override
public <T extends Number> void unregisterGauge(String statName, Gauge<T> gauge) {
// do nothing right now as the Codahale doesn't support conditional removal
}
@Override
public StatsLogger scope(String scope) {
String scopeName;
if (basename == null || 0 == basename.length()) {
scopeName = scope;
} else {
scopeName = name(basename, scope);
}
return new CodahaleStatsLogger(metrics, scopeName);
}
@Override
public void removeScope(String name, StatsLogger statsLogger) {
// no-op. the codahale stats logger doesn't have the references for stats logger.
}
/**
Thread-scoped stats not currently supported.
*/
@Override
public OpStatsLogger getThreadScopedOpStatsLogger(String name) {
return getOpStatsLogger(name);
}
/**
Thread-scoped stats not currently supported.
*/
@Override
public Counter getThreadScopedCounter(String name) {
return getCounter(name);
}
}