19
19
import static com .netflix .netty .common .HttpLifecycleChannelHandler .CompleteEvent ;
20
20
import static com .netflix .netty .common .HttpLifecycleChannelHandler .CompleteReason ;
21
21
22
- import com .netflix .spectator .api .Counter ;
22
+ import com .netflix .spectator .api .Spectator ;
23
23
import com .netflix .zuul .netty .ChannelUtils ;
24
- import com .netflix .zuul .netty .SpectatorUtils ;
25
24
import com .netflix .zuul .origins .OriginName ;
26
25
import io .netty .channel .ChannelDuplexHandler ;
27
26
import io .netty .channel .ChannelHandler ;
30
29
import io .netty .handler .codec .http .HttpResponse ;
31
30
import io .netty .handler .ssl .SslCloseCompletionEvent ;
32
31
import io .netty .handler .timeout .IdleStateEvent ;
32
+ import java .util .Objects ;
33
33
import org .slf4j .Logger ;
34
34
import org .slf4j .LoggerFactory ;
35
35
42
42
public class ConnectionPoolHandler extends ChannelDuplexHandler {
43
43
private static final Logger LOG = LoggerFactory .getLogger (ConnectionPoolHandler .class );
44
44
45
- public static final String METRIC_PREFIX = "connectionpool" ;
46
-
45
+ private final ConnectionPoolMetrics metrics ;
47
46
private final OriginName originName ;
48
- private final Counter idleCounter ;
49
- private final Counter inactiveCounter ;
50
- private final Counter errorCounter ;
51
- private final Counter headerCloseCounter ;
52
- private final Counter sslCloseCompletionCounter ;
53
47
48
+
49
+ @ Deprecated
54
50
public ConnectionPoolHandler (OriginName originName ) {
55
- if (originName == null ) {
56
- throw new IllegalArgumentException ("Null originName passed to constructor!" );
57
- }
58
- this .originName = originName ;
59
- this .idleCounter = SpectatorUtils .newCounter (METRIC_PREFIX + "_idle" , originName .getMetricId ());
60
- this .inactiveCounter = SpectatorUtils .newCounter (METRIC_PREFIX + "_inactive" , originName .getMetricId ());
61
- this .errorCounter = SpectatorUtils .newCounter (METRIC_PREFIX + "_error" , originName .getMetricId ());
62
- this .headerCloseCounter = SpectatorUtils .newCounter (METRIC_PREFIX + "_headerClose" , originName .getMetricId ());
63
- this .sslCloseCompletionCounter =
64
- SpectatorUtils .newCounter (METRIC_PREFIX + "_sslClose" , originName .getMetricId ());
51
+ this (ConnectionPoolMetrics .create (Objects .requireNonNull (originName ), Spectator .globalRegistry ()));
52
+ }
53
+
54
+ public ConnectionPoolHandler (ConnectionPoolMetrics metrics ) {
55
+ this .originName = metrics .originName ();
56
+ this .metrics = metrics ;
65
57
}
66
58
67
59
@ Override
@@ -71,7 +63,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
71
63
72
64
if (evt instanceof IdleStateEvent ) {
73
65
// Log some info about this.
74
- idleCounter .increment ();
66
+ metrics . idleCounter () .increment ();
75
67
String msg = "Origin channel for origin - " + originName + " - idle timeout has fired. "
76
68
+ ChannelUtils .channelInfoForLogging (ctx .channel ());
77
69
closeConnection (ctx , msg );
@@ -88,7 +80,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
88
80
String msg = "Origin channel for origin - " + originName
89
81
+ " - completed because of expired keep-alive. "
90
82
+ ChannelUtils .channelInfoForLogging (ctx .channel ());
91
- headerCloseCounter .increment ();
83
+ metrics . headerCloseCounter () .increment ();
92
84
closeConnection (ctx , msg );
93
85
} else {
94
86
conn .setConnectionState (PooledConnection .ConnectionState .WRITE_READY );
@@ -101,7 +93,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
101
93
closeConnection (ctx , msg );
102
94
}
103
95
} else if (evt instanceof SslCloseCompletionEvent event ) {
104
- sslCloseCompletionCounter .increment ();
96
+ metrics . sslCloseCompletionCounter () .increment ();
105
97
String msg = "Origin channel for origin - " + originName + " - received SslCloseCompletionEvent " + event
106
98
+ ". " + ChannelUtils .channelInfoForLogging (ctx .channel ());
107
99
closeConnection (ctx , msg );
@@ -111,7 +103,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
111
103
@ Override
112
104
public void exceptionCaught (ChannelHandlerContext ctx , Throwable cause ) throws Exception {
113
105
// super.exceptionCaught(ctx, cause);
114
- errorCounter .increment ();
106
+ metrics . errorCounter () .increment ();
115
107
String mesg = "Exception on Origin channel for origin - " + originName + ". "
116
108
+ ChannelUtils .channelInfoForLogging (ctx .channel ()) + " - "
117
109
+ cause .getClass ().getCanonicalName ()
@@ -126,7 +118,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
126
118
@ Override
127
119
public void channelInactive (ChannelHandlerContext ctx ) throws Exception {
128
120
// super.channelInactive(ctx);
129
- inactiveCounter .increment ();
121
+ metrics . inactiveCounter () .increment ();
130
122
String msg = "Client channel for origin - " + originName + " - inactive event has fired. "
131
123
+ ChannelUtils .channelInfoForLogging (ctx .channel ());
132
124
closeConnection (ctx , msg );
0 commit comments