examples/node/index.js
function httpGet(parentSpan, urlString, callback) {
    var span = opentracing.globalTracer().startSpan('http.get', { childOf : parentSpan });
    var callbackWrapper = function (err, data) {
        span.finish();
        callback(err, data);
    };

    try {
        var carrier = {};
        opentracing.globalTracer().inject(span, opentracing.FORMAT_TEXT_MAP, carrier);

        var dest = url.parse(urlString);
        var options = {
            host : PROXY_HOST,
            path : dest.path,
            port : PROXY_PORT,
            headers: {
                // User-Agent is required by the GitHub APIs
                'User-Agent': 'LightStep Example',
            }
        };
        for (var key in carrier) {
            options.headers[key] = carrier[key];
        }

        // Create a span representing the https request
        span.setTag('url', urlString);
        span.log({
            event : 'options',
            'options': options,
        });
        return http.get(options, function(response) {
            var bodyBuffer = '';
            response.on('data', function(chunk) {
                bodyBuffer += chunk;
            });
            response.on('end', function() {
                span.log({
                    body   : bodyBuffer,
                    length : bodyBuffer.length,
                });

                var parsedJSON, err;
                try {
                    parsedJSON = JSON.parse(bodyBuffer);
                } catch (exception) {
                    err = {
                        buffer    : bodyBuffer,
                        exception : exception,
                    };
                    span.setTag('error', true);
                    span.log({
                        event : 'error',
                        'error.object': err,
                    });
                }

                callbackWrapper(err, parsedJSON);
            });
        });
    } catch (exception) {
        span.setTag('error', true);
        span.log({
            event : 'error',
            'error.object': exception,
        });
        callbackWrapper(exception, null);
    }
}
function httpGet(parentSpan, urlString, callback) {
    var span = opentracing.globalTracer().startSpan('http.get', { childOf : parentSpan });
    var callbackWrapper = function (err, data) {
        span.finish();
        callback(err, data);
    };

    try {
        var carrier = {};
        opentracing.globalTracer().inject(span, opentracing.FORMAT_TEXT_MAP, carrier);

        var dest = url.parse(urlString);
        var options = {
            host : PROXY_HOST,
            path : dest.path,
            port : PROXY_PORT,
            headers: {
                // User-Agent is required by the GitHub APIs
                'User-Agent': 'LightStep Example',
            }
        };
        for (var key in carrier) {
            options.headers[key] = carrier[key];
        }

        // Create a span representing the https request
        span.setTag('url', urlString);
        span.log({
            event : 'options',
            'options': options,
        });
        return http.get(options, function(response) {
            var bodyBuffer = '';
            response.on('data', function(chunk) {
                bodyBuffer += chunk;
            });
            response.on('end', function() {
                span.log({
                    body   : bodyBuffer,
                    length : bodyBuffer.length,
                });

                var parsedJSON, err;
                try {
                    parsedJSON = JSON.parse(bodyBuffer);
                } catch (exception) {
                    err = {
                        buffer    : bodyBuffer,
                        exception : exception,
                    };
                    span.setTag('error', true);
                    span.log({
                        event : 'error',
                        'error.object': err,
                    });
                }

                callbackWrapper(err, parsedJSON);
            });
        });
    } catch (exception) {
        span.setTag('error', true);
        span.log({
            event : 'error',
            'error.object': exception,
        });
        callbackWrapper(exception, null);
    }
}
function httpGet ( parentSpan , urlString , callback ) { var span = opentracing . globalTracer ( ) . startSpan ( 'http.get' , { childOf : parentSpan } ) ; var callbackWrapper = function ( err , data ) { span . finish ( ) ; callback ( err , data ) ; } ; try { var carrier = { } ; opentracing . globalTracer ( ) . inject ( span , opentracing . FORMAT_TEXT_MAP , carrier ) ; var dest = url . parse ( urlString ) ; var options = { host : PROXY_HOST , path : dest . path , port : PROXY_PORT , headers : { // User-Agent is required by the GitHub APIs 'User-Agent' : 'LightStep Example' , } } ; for ( var key in carrier ) { options . headers [ key ] = carrier [ key ] ; } // Create a span representing the https request span . setTag ( 'url' , urlString ) ; span . log ( { event : 'options' , 'options' : options , } ) ; return http . get ( options , function ( response ) { var bodyBuffer = '' ; response . on ( 'data' , function ( chunk ) { bodyBuffer += chunk ; } ) ; response . on ( 'end' , function ( ) { span . log ( { body : bodyBuffer , length : bodyBuffer . length , } ) ; var parsedJSON , err ; try { parsedJSON = JSON . parse ( bodyBuffer ) ; } catch ( exception ) { err = { buffer : bodyBuffer , exception : exception , } ; span . setTag ( 'error' , true ) ; span . log ( { event : 'error' , 'error.object' : err , } ) ; } callbackWrapper ( err , parsedJSON ) ; } ) ; } ) ; } catch ( exception ) { span . setTag ( 'error' , true ) ; span . log ( { event : 'error' , 'error.object' : exception , } ) ; callbackWrapper ( exception , null ) ; } }
Helper function to make a GET request and return parsed JSON data.
Helper function to make a GET request and return parsed JSON data .