1 |
torben |
2569 |
package dk.thoerup.circuitbreaker.statistics;
|
2 |
|
|
|
3 |
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
4 |
|
|
import java.util.concurrent.atomic.AtomicLong;
|
5 |
|
|
|
6 |
|
|
import dk.thoerup.circuitbreaker.Event;
|
7 |
|
|
|
8 |
|
|
public class AccountingStatistics implements Statistics {
|
9 |
|
|
|
10 |
|
|
|
11 |
|
|
private AtomicInteger tripCount = new AtomicInteger(0); // how many times ahs the CB tripped
|
12 |
|
|
private AtomicInteger retripCount = new AtomicInteger(0);
|
13 |
|
|
private AtomicInteger blockCount = new AtomicInteger(0); //how many times has this CB blocked a call that would otherwise go to the backend
|
14 |
|
|
private AtomicInteger totalFailureCount = new AtomicInteger(0); //how many times has the backend thrown an exception
|
15 |
|
|
private AtomicInteger totalCallCount = new AtomicInteger(0);
|
16 |
|
|
|
17 |
|
|
private AtomicLong lastTrip = new AtomicLong(0);
|
18 |
|
|
private AtomicLong lastRetrip = new AtomicLong(0);
|
19 |
|
|
private AtomicLong lastFailure = new AtomicLong(0);
|
20 |
|
|
private AtomicLong lastReset = new AtomicLong(0);
|
21 |
|
|
|
22 |
|
|
private long lastResetCounters = 0;
|
23 |
|
|
|
24 |
|
|
|
25 |
|
|
@Override
|
26 |
|
|
public void addStatistics(Event event) {
|
27 |
|
|
switch(event) {
|
28 |
|
|
case BreakerAttemptReset:
|
29 |
|
|
break;
|
30 |
|
|
case BreakerReset:
|
31 |
|
|
lastReset.set( System.currentTimeMillis());
|
32 |
|
|
break;
|
33 |
|
|
case BreakerRetripped:
|
34 |
|
|
retripCount.incrementAndGet();
|
35 |
|
|
lastRetrip.set( System.currentTimeMillis() );
|
36 |
|
|
break;
|
37 |
|
|
case BreakerTripped:
|
38 |
|
|
tripCount.incrementAndGet();
|
39 |
|
|
lastTrip.set( System.currentTimeMillis() );
|
40 |
|
|
break;
|
41 |
|
|
case InvocationBlocked:
|
42 |
|
|
blockCount.incrementAndGet();
|
43 |
|
|
break;
|
44 |
|
|
case InvocationFailure:
|
45 |
|
|
totalFailureCount.incrementAndGet();
|
46 |
|
|
lastFailure.set( System.currentTimeMillis() );
|
47 |
|
|
break;
|
48 |
|
|
case Invocation:
|
49 |
|
|
totalCallCount.incrementAndGet();
|
50 |
|
|
break;
|
51 |
|
|
}
|
52 |
|
|
}
|
53 |
|
|
|
54 |
|
|
public void resetCounters() {
|
55 |
|
|
tripCount.set(0);
|
56 |
|
|
retripCount.set(0);
|
57 |
|
|
blockCount.set(0);
|
58 |
|
|
totalFailureCount.set(0);
|
59 |
|
|
totalCallCount.set(0);
|
60 |
|
|
lastTrip.set(0);
|
61 |
|
|
lastRetrip.set(0);
|
62 |
|
|
lastReset.set(0);
|
63 |
|
|
lastFailure.set(0);
|
64 |
|
|
|
65 |
|
|
lastResetCounters = System.currentTimeMillis();
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
public int getTripCount() {
|
69 |
|
|
return tripCount.get();
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
public int getRetripCount() {
|
73 |
|
|
return retripCount.get();
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
public int getBlockCount() {
|
77 |
|
|
return blockCount.get();
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
public int getTotalFailureCount() {
|
81 |
|
|
return totalFailureCount.get();
|
82 |
|
|
}
|
83 |
|
|
|
84 |
|
|
public long getLastResetCounters() {
|
85 |
|
|
return lastResetCounters;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
public long getLastTrip() {
|
89 |
|
|
return lastTrip.get();
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
public long getLastRetrip() {
|
93 |
|
|
return lastRetrip.get();
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
public long getLastFailure() {
|
97 |
|
|
return lastFailure.get();
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
public long getLastReset() {
|
101 |
|
|
return lastReset.get();
|
102 |
|
|
}
|
103 |
|
|
|
104 |
|
|
public int getTotalCallCount() {
|
105 |
|
|
return totalCallCount.get();
|
106 |
|
|
}
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
}
|