Search in sources :

Example 1 with Attrs

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);
}
Also used : Attrs(com.netflix.zuul.Attrs) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) Registry(com.netflix.spectator.api.Registry) Gauge(com.netflix.spectator.api.Gauge) Test(org.junit.Test)

Example 2 with Attrs

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);
}
Also used : Attrs(com.netflix.zuul.Attrs) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) DefaultRegistry(com.netflix.spectator.api.DefaultRegistry) Registry(com.netflix.spectator.api.Registry) Test(org.junit.Test)

Example 3 with Attrs

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);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HAProxyMessageDecoder(io.netty.handler.codec.haproxy.HAProxyMessageDecoder) Attrs(com.netflix.zuul.Attrs) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 4 with Attrs

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);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Attrs(com.netflix.zuul.Attrs)

Example 5 with Attrs

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;
    }
}
Also used : SourceAddressChannelHandler(com.netflix.netty.common.SourceAddressChannelHandler) AttributeKey(io.netty.util.AttributeKey) Attrs(com.netflix.zuul.Attrs) SocketAddress(java.net.SocketAddress) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) HAProxyMessage(io.netty.handler.codec.haproxy.HAProxyMessage) Inet6Address(java.net.Inet6Address) HAProxyProtocolVersion(io.netty.handler.codec.haproxy.HAProxyProtocolVersion) ChannelFutureListener(io.netty.channel.ChannelFutureListener) VisibleForTesting(com.google.common.annotations.VisibleForTesting) InetAddresses(com.google.common.net.InetAddresses) Server(com.netflix.zuul.netty.server.Server) Inet4Address(java.net.Inet4Address) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) Attrs(com.netflix.zuul.Attrs) Inet6Address(java.net.Inet6Address) HAProxyMessage(io.netty.handler.codec.haproxy.HAProxyMessage) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress)

Aggregations

Attrs (com.netflix.zuul.Attrs)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 Test (org.junit.Test)4 DefaultRegistry (com.netflix.spectator.api.DefaultRegistry)3 Registry (com.netflix.spectator.api.Registry)3 Gauge (com.netflix.spectator.api.Gauge)2 InetSocketAddress (java.net.InetSocketAddress)2 HashMap (java.util.HashMap)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 InetAddresses (com.google.common.net.InetAddresses)1 SourceAddressChannelHandler (com.netflix.netty.common.SourceAddressChannelHandler)1 Id (com.netflix.spectator.api.Id)1 PercentileTimer (com.netflix.spectator.api.histogram.PercentileTimer)1 Server (com.netflix.zuul.netty.server.Server)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)1 HAProxyMessage (io.netty.handler.codec.haproxy.HAProxyMessage)1