opentracing-tutorial icon indicating copy to clipboard operation
opentracing-tutorial copied to clipboard

OpenTracing Tutorial - Node.js Lesson 3 RPC not updated yet ?

Open lwmxiaobei opened this issue 4 years ago • 6 comments

OpenTracing Tutorial - Node.js Lesson 3 RPC not updated yet ? I am more interested in rpc call, can you update it?

lwmxiaobei avatar Jul 16 '20 08:07 lwmxiaobei

@lwmxiaobei use the opentracing.FORMAT_TEXT_MAP

Example:

const jaeger = require('jaeger-client');
const opentracing = require('opentracing');

const jaegerLogger = {
    info(msg) {
        console.log(`[jaeger-info]${msg}`);
    },
    error(msg) {
        console.log(`[jaeger-error]${msg}`);
    },
};

function createConfig(name) {
    return {
        disable: false,
        serviceName: name,
        sampler: {
            type: 'const',
            param: 1,
        },
        reporter: {
            logSpans: true,
            collectorEndpoint: 'http://127.0.0.1:14268/api/traces'
        },
    }
}

const options = {
    tags: {
        version: 'v1.0.0',
    },
    logger: jaegerLogger,
};

const parentTracer = jaeger.initTracer(createConfig('parent-tracer'), options);
const childTracer = jaeger.initTracer(createConfig('child-tracer'), options);


function createSerializedSpanContext(myTracer, mySpan) {
    const obj = {};
    myTracer.inject(mySpan.context(), opentracing.FORMAT_TEXT_MAP, obj);
    return obj;
}

function createSpanContextFromSerializedParentContext(myTracer, obj) {
    const parentSpanContext = myTracer.extract(opentracing.FORMAT_TEXT_MAP, obj);
    return parentSpanContext;
}


const span = parentTracer.startSpan('parent service');
const serialized = createSerializedSpanContext(parentTracer, span);

console.log(`obj: ${JSON.stringify(serialized, null, 2)}`);

const context = createSpanContextFromSerializedParentContext(childTracer, serialized);
const childSpan = childTracer.startSpan('child service', {
    childOf: context,
});


span.finish();
childSpan.finish();

a1300 avatar Dec 08 '20 14:12 a1300

Http requests should use FORMAT_HTTP_HEADERS

yurishkuro avatar Dec 08 '20 15:12 yurishkuro

@yurishkuro you are right. Should I hide my comment?

I am using js-libp2p which uses no http and directly communicates over tcp. Therefore it worked for me with opentracing.FORMAT_TEXT_MAP. Can we add an example for opentracing.FORMAT_TEXT_MAP in the Lesson 3 (nodejs)?

a1300 avatar Dec 08 '20 16:12 a1300

If you are using non-HTTP protocol that does not impose header restrictions the way HTTP does, then you should use text map format.

I am not sure why we'd add examples for text map here since this tutorial uses only HTTP.

Overall the Node.js tutorial is under-developed, if you look at Go tutorial, its README files have a lot more narrative / explanations.

yurishkuro avatar Dec 08 '20 17:12 yurishkuro

@yurishkuro can you maybe create a ticket which things are missing for the node.js tutorial? I am ready to contribute. Or should I simply compare the node.js tutorial to e.g. the Go tutorial and then add the missing things?

a1300 avatar Dec 08 '20 17:12 a1300

yes, simply compare Node.js README's with other languages, you will see that it has a low less narrative/explanations.

yurishkuro avatar Dec 08 '20 17:12 yurishkuro