use of com.netflix.zuul.Attrs in project zuul by Netflix.
the class ConnCounterTest method record.
@Test
public void record() {
EmbeddedChannel chan = new EmbeddedChannel();
Attrs attrs = Attrs.newInstance();
chan.attr(Server.CONN_DIMENSIONS).set(attrs);
Registry registry = new DefaultRegistry();
ConnCounter counter = ConnCounter.install(chan, registry, registry.createId("foo"));
counter.increment("start");
counter.increment("middle");
Attrs.newKey("bar").put(attrs, "baz");
counter.increment("end");
Gauge meter1 = registry.gauge(registry.createId("foo.start", "from", "nascent"));
assertNotNull(meter1);
assertEquals(1, meter1.value(), 0);
Gauge meter2 = registry.gauge(registry.createId("foo.middle", "from", "start"));
assertNotNull(meter2);
assertEquals(1, meter2.value(), 0);
Gauge meter3 = registry.gauge(registry.createId("foo.end", "from", "middle", "bar", "baz"));
assertNotNull(meter3);
assertEquals(1, meter3.value(), 0);
}
use of com.netflix.zuul.Attrs in project zuul by Netflix.
the class ConnCounterTest method activeConnsCount.
@Test
public void activeConnsCount() {
EmbeddedChannel channel = new EmbeddedChannel();
Attrs attrs = Attrs.newInstance();
channel.attr(Server.CONN_DIMENSIONS).set(attrs);
Registry registry = new DefaultRegistry();
ConnCounter.install(channel, registry, registry.createId("foo"));
// Dedup increments
ConnCounter.from(channel).increment("active");
ConnCounter.from(channel).increment("active");
assertEquals(1, ConnCounter.from(channel).getCurrentActiveConns(), 0);
}
use of com.netflix.zuul.Attrs in project zuul by Netflix.
the class HAProxyMessageChannelHandlerTest method setClientDestPortForHAPM.
@Test
public void setClientDestPortForHAPM() {
EmbeddedChannel channel = new EmbeddedChannel();
// This is normally done by Server.
channel.attr(Server.CONN_DIMENSIONS).set(Attrs.newInstance());
// This is to emulate `ElbProxyProtocolChannelHandler`
channel.pipeline().addLast(HAProxyMessageDecoder.class.getSimpleName(), new HAProxyMessageDecoder()).addLast(HAProxyMessageChannelHandler.class.getSimpleName(), new HAProxyMessageChannelHandler());
ByteBuf buf = Unpooled.wrappedBuffer("PROXY TCP4 192.168.0.1 124.123.111.111 10008 443\r\n".getBytes(StandardCharsets.US_ASCII));
channel.writeInbound(buf);
Object result = channel.readInbound();
assertNull(result);
InetSocketAddress destAddress = channel.attr(SourceAddressChannelHandler.ATTR_PROXY_PROTOCOL_DESTINATION_ADDRESS).get();
InetSocketAddress srcAddress = (InetSocketAddress) channel.attr(SourceAddressChannelHandler.ATTR_REMOTE_ADDR).get();
assertEquals("124.123.111.111", destAddress.getHostString());
assertEquals(443, destAddress.getPort());
assertEquals("192.168.0.1", srcAddress.getHostString());
assertEquals(10008, srcAddress.getPort());
Attrs attrs = channel.attr(Server.CONN_DIMENSIONS).get();
Integer port = HAProxyMessageChannelHandler.HAPM_DEST_PORT.get(attrs);
assertEquals(443, port.intValue());
String sourceIpVersion = HAProxyMessageChannelHandler.HAPM_SRC_IP_VERSION.get(attrs);
assertEquals("v4", sourceIpVersion);
String destIpVersion = HAProxyMessageChannelHandler.HAPM_DEST_IP_VERSION.get(attrs);
assertEquals("v4", destIpVersion);
}
use of com.netflix.zuul.Attrs in project zuul by Netflix.
the class ConnTimer method record.
public void record(Long now, String event, Attrs extraDimensions) {
if (timings.containsKey(event)) {
return;
}
Objects.requireNonNull(now);
Objects.requireNonNull(event);
Objects.requireNonNull(extraDimensions);
Attrs connDims = chan.attr(Server.CONN_DIMENSIONS).get();
Map<String, String> dimTags = new HashMap<>(connDims.size() + extraDimensions.size());
connDims.forEach((k, v) -> dimTags.put(k.name(), String.valueOf(v)));
extraDimensions.forEach((k, v) -> dimTags.put(k.name(), String.valueOf(v)));
// Note: this is effectively O(n^2) because it will be called for each event in the connection
// setup. It should be bounded to at most 10 or so.
timings.forEach((from, stamp) -> {
long durationNanos = now - stamp;
if (durationNanos == 0) {
// it.
return;
}
registry.timer(buildId(metricBase, from, event, dimTags)).record(durationNanos, TimeUnit.NANOSECONDS);
if (preciseMetricBase != null) {
PercentileTimer.builder(registry).withId(buildId(preciseMetricBase, from, event, dimTags)).withRange(MIN_CONN_TIMING, MAX_CONN_TIMING).build().record(durationNanos, TimeUnit.NANOSECONDS);
}
});
timings.put(event, now);
}
use of com.netflix.zuul.Attrs in project zuul by Netflix.
the class HAProxyMessageChannelHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HAProxyMessage) {
HAProxyMessage hapm = (HAProxyMessage) msg;
Channel channel = ctx.channel();
channel.attr(ATTR_HAPROXY_MESSAGE).set(hapm);
ctx.channel().closeFuture().addListener((ChannelFutureListener) future -> hapm.release());
channel.attr(ATTR_HAPROXY_VERSION).set(hapm.protocolVersion());
// Get the real host and port that the client connected to ELB with.
String destinationAddress = hapm.destinationAddress();
if (destinationAddress != null) {
channel.attr(SourceAddressChannelHandler.ATTR_LOCAL_ADDRESS).set(destinationAddress);
SocketAddress addr;
out: {
switch(hapm.proxiedProtocol()) {
case UNKNOWN:
throw new IllegalArgumentException("unknown proxy protocl" + destinationAddress);
case TCP4:
case TCP6:
InetSocketAddress inetAddr = new InetSocketAddress(InetAddresses.forString(destinationAddress), hapm.destinationPort());
addr = inetAddr;
// setting PPv2 explicitly because SourceAddressChannelHandler.ATTR_LOCAL_ADDR could be PPv2 or not
channel.attr(SourceAddressChannelHandler.ATTR_PROXY_PROTOCOL_DESTINATION_ADDRESS).set(inetAddr);
Attrs attrs = ctx.channel().attr(Server.CONN_DIMENSIONS).get();
if (inetAddr.getAddress() instanceof Inet4Address) {
HAPM_DEST_IP_VERSION.put(attrs, "v4");
} else if (inetAddr.getAddress() instanceof Inet6Address) {
HAPM_DEST_IP_VERSION.put(attrs, "v6");
} else {
HAPM_DEST_IP_VERSION.put(attrs, "unknown");
}
HAPM_DEST_PORT.put(attrs, hapm.destinationPort());
break out;
// TODO: implement
case UNIX_STREAM:
case UDP4:
case UDP6:
case UNIX_DGRAM:
throw new IllegalArgumentException("unknown proxy protocol" + destinationAddress);
}
throw new AssertionError(hapm.proxiedProtocol());
}
channel.attr(SourceAddressChannelHandler.ATTR_LOCAL_ADDR).set(addr);
}
// Get the real client IP from the ProxyProtocol message sent by the ELB, and overwrite the SourceAddress
// channel attribute.
String sourceAddress = hapm.sourceAddress();
if (sourceAddress != null) {
channel.attr(SourceAddressChannelHandler.ATTR_SOURCE_ADDRESS).set(sourceAddress);
SocketAddress addr;
out: {
switch(hapm.proxiedProtocol()) {
case UNKNOWN:
throw new IllegalArgumentException("unknown proxy protocl" + sourceAddress);
case TCP4:
case TCP6:
InetSocketAddress inetAddr;
addr = inetAddr = new InetSocketAddress(InetAddresses.forString(sourceAddress), hapm.sourcePort());
Attrs attrs = ctx.channel().attr(Server.CONN_DIMENSIONS).get();
if (inetAddr.getAddress() instanceof Inet4Address) {
HAPM_SRC_IP_VERSION.put(attrs, "v4");
} else if (inetAddr.getAddress() instanceof Inet6Address) {
HAPM_SRC_IP_VERSION.put(attrs, "v6");
} else {
HAPM_SRC_IP_VERSION.put(attrs, "unknown");
}
break out;
// TODO: implement
case UNIX_STREAM:
case UDP4:
case UDP6:
case UNIX_DGRAM:
throw new IllegalArgumentException("unknown proxy protocol" + sourceAddress);
}
throw new AssertionError(hapm.proxiedProtocol());
}
channel.attr(SourceAddressChannelHandler.ATTR_REMOTE_ADDR).set(addr);
}
// TODO - fire an additional event to notify interested parties that we now know the IP?
// Remove ourselves (this handler) from the channel now, as no more work to do.
ctx.pipeline().remove(this);
// Do not continue propagating the message.
return;
}
}
Aggregations