public void write()

in apm-sniffer/optional-plugins/netty-http-4.1.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/netty/http/handler/NettyHttpRequestEncoderTracingHandler.java [66:133]


    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
        try {
            if (!TypeUtils.isHttpRequest(msg)) {
                return;
            }

            AbstractSpan lastSpan = ctx.channel().attr(AttributeKeys.HTTP_CLIENT_SPAN).getAndSet(null);
            if (null != lastSpan) {
                ContextManager.stopSpan(lastSpan);
            }

            HttpRequest request = (HttpRequest) msg;
            HttpHeaders headers = request.headers();
            String uri = request.uri();
            InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
            String peer = address.getHostString() + ":" + address.getPort();
            String url = peer + uri;
            String method = request.method().toString();

            ContextCarrier contextCarrier = new ContextCarrier();
            AbstractSpan span = ContextManager.createExitSpan(NettyConstants.NETTY_HTTP_OPERATION_PREFIX + uri, contextCarrier, peer);

            for (CarrierItem item = contextCarrier.items(); item.hasNext(); ) {
                item = item.next();
                headers.add(AsciiString.of(item.getHeadKey()), item.getHeadValue());
            }

            ContextSnapshot contextSnapshot = ctx.channel().attr(AttributeKeys.CONTEXT_SNAPSHOT_ATTRIBUTE_KEY).get();
            if (contextSnapshot != null) {
                ContextManager.continued(contextSnapshot);
            }

            span.prepareForAsync();

            SpanLayer.asHttp(span);
            span.setPeer(peer);
            span.setComponent(ComponentsDefine.NETTY_HTTP);

            boolean sslFlag = ctx.channel().pipeline().context(SslHandler.class) != null;
            Tags.URL.set(span, sslFlag ? NettyConstants.HTTPS_PROTOCOL_PREFIX + url : NettyConstants.HTTP_PROTOCOL_PREFIX + url);
            Tags.HTTP.METHOD.set(span, request.method().name());

            if (NettyHttpPluginConfig.Plugin.NettyHttp.COLLECT_REQUEST_BODY) {
                if (TypeUtils.isLastHttpContent(msg)) {
                    HttpDataCollectUtils.collectHttpRequestBody(request.headers(), ((LastHttpContent) msg).content(), span);
                }
            }

            ContextManager.stopSpan(span);

            ctx.channel().attr(AttributeKeys.HTTP_CLIENT_SPAN).set(span);
        } catch (Exception e) {
            LOGGER.error("Fail to trace netty http request", e);
        } finally {
            try {
                ctx.write(msg, promise);
            } catch (Throwable throwable) {
                AbstractSpan span = ctx.channel().attr(AttributeKeys.HTTP_CLIENT_SPAN).getAndSet(null);
                if (span != null) {
                    span.errorOccurred();
                    span.log(throwable);
                    Tags.HTTP_RESPONSE_STATUS_CODE.set(span, 500);
                    span.asyncFinish();
                }
                throw throwable;
            }
        }
    }