all files / src/ tchannel_bridge.js

97.44% Statements 76/78
70% Branches 21/30
100% Functions 12/12
96.77% Lines 60/62
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                                               32×         32× 32×                       16× 16× 16× 16×     16× 16× 16× 16× 16×   16× 16×       16×     16× 16× 28× 28× 28×       16× 16×         16× 16× 16× 16×     16× 16× 16×     16×   16×             16×   16× 16× 16×                     16× 16×     16×   16× 16×       16×                 16× 16× 16× 16×       16× 16×      
EEE// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
import * as constants from './constants';
import Span from './span';
import SpanContext from './span_context';
import Utils from './util';
import opentracing from 'opentracing';
import Tracer from '../src/tracer';
import TextMapCodec from '../src/propagators/text_map_codec';
 
let TCHANNEL_TRACING_PREFIX = '$tracing$';
 
export default class TChannelBridge {
    _tracer: Tracer;
    _codec: TextMapCodec;
 
    constructor(tracer: Tracer) {
        this._tracer = tracer;
        this._codec = new TextMapCodec({
            urlEncoding: false,
            contextKey: TCHANNEL_TRACING_PREFIX + constants.TRACER_STATE_HEADER_NAME,
            baggagePrefix: TCHANNEL_TRACING_PREFIX + constants.TRACER_BAGGAGE_HEADER_PREFIX
        });
    }
 
    _tchannelCallbackWrapper(span, callback, err, res) {
        if (Ierr) {
            span.setTag(opentracing.Tags.ERROR, true);
            span.log('error_msg', err);
        }
 
        span.finish();
        return callback(err, res);
    }
 
    /**
     * Wraps a tchannel handler, and takes a context in order to populate the incoming context
     * with a span.
     *
     * @param {Function} [handlerFunc] - a tchannel handler function that responds to an incoming request.
     * @param {Object} [options] - options to be passed to a span on creation.
     * @returns {Function} - a function that wrapps the handler in order to automatically populate
     * a the handler's context with a span.
     **/
    tracedHandler(handlerFunc: any, options: startSpanArgs = {}): Function {
        return (context, request, headers, body, callback) => {
            let operationName = options.operationName || request.arg1;
            let span = this._extractSpan(operationName, headers);
 
            // set tags
            span.setTag(opentracing.Tags.PEER_SERVICE, request.callerName);
            let hostPort = request.remoteAddr.split(':');
            if (EhostPort.length == 2) {
                span.setTag(opentracing.Tags.PEER_HOST_IPV4, Utils.ipToInt(hostPort[0]));
                span.setTag(opentracing.Tags.PEER_PORT, parseInt(hostPort[1]));
            }
            if (Erequest.headers && request.headers.as) {
                span.setTag('as', request.headers.as);
            }
 
            // In theory may overwrite tchannel span, but thats what we want anyway.
            context.openTracingSpan = span;
 
            // remove headers prefixed with $tracing$
            let headerKeys = Object.keys(headers);
            for (let i = 0; i < headerKeys.length; i++) {
                let key = headerKeys[i];
                if (Eheaders.hasOwnProperty(key) && Utils.startsWith(key, TCHANNEL_TRACING_PREFIX)) {
                    delete headers[key];
                }
            }
 
            let wrappingCallback = this._tchannelCallbackWrapper.bind(null, span, callback);
            handlerFunc(context, request, headers, body, wrappingCallback);
        };
    }
 
    _wrapTChannelSend(wrappedSend, channel, req, endpoint, headers, body, callback) {
        headers = headers || {};
        let context = req.context || {};
        let childOf = context.openTracingSpan;
        let clientSpan = this._tracer.startSpan(endpoint, {
            childOf: childOf // ok if null, will start a new trace
        });
        clientSpan.setTag(opentracing.Tags.PEER_SERVICE, req.serviceName);
        clientSpan.setTag(opentracing.Tags.SPAN_KIND, opentracing.Tags.SPAN_KIND_RPC_CLIENT);
        this._codec.inject(clientSpan.context(), headers);
 
        // wrap callback so that span can be finished as soon as the response is received
        let wrappingCallback = this._tchannelCallbackWrapper.bind(null, clientSpan, callback);
 
        return wrappedSend.call(channel, req, endpoint, headers, body, wrappingCallback);
    }
 
    _wrapTChannelRequest(channel, wrappedRequestMethod, requestOptions) {
        // We set the parent to a span with trace_id zero, so that tchannel's
        // outgoing tracing frame also has a trace id of zero.
        // This forces other tchannel implementations to rely on the headers for the trace context.
        requestOptions.parent = { span: TChannelBridge.makeFakeTChannelParentSpan() };
 
        let tchannelRequest = wrappedRequestMethod.call(channel, requestOptions);
        tchannelRequest.context = requestOptions.context;
        return tchannelRequest;
    }
 
    /**
     * A function that wraps a json, or thrift encoded channel, in order to populate
     * the outgoing headers with trace context, and baggage information.
     *
     * @param {Object} channel - the encoded channel to be wrapped for tracing.
     * @returns {Object} channel - the trace wrapped channel.
     * */
    tracedChannel(channel: any): any {
        let wrappedSend = channel.send;
        let wrappedRequestMethod = channel.channel.request;
 
        // We are patching the top level channel request method, not the encoded request method.
        channel.channel.request = this._wrapTChannelRequest.bind(this, channel.channel, wrappedRequestMethod);
 
        channel.send = this._wrapTChannelSend.bind(this, wrappedSend, channel);
        return channel;
    }
 
    static makeFakeTChannelParentSpan(): TChannelSpan {
        return {
            id: [0, 0],
            traceid: [0, 0],
            parentid: [0, 0],
            flags: 0
        };
    }
 
    _extractSpan(operationName: string, headers: any): Span {
        let traceContext: ?SpanContext = this._codec.extract(headers);
        let tags = {};
        tags[opentracing.Tags.SPAN_KIND] = opentracing.Tags.SPAN_KIND_RPC_SERVER;
        let options = {
            childOf: traceContext,
            tags: tags
        }
        let span = this._tracer.startSpan(operationName, options);
        return span;
    }
}