MoleculerJs with Jaeger tracing: how to trace follow up action calls (new spans) in one trace

871 views Asked by At

I would like to display all my traces like in the examples from the moleculer-jaeger package: What i would like to have

But what i get is something like this: What i get with my current config All spans you can see in this picture should be within the main trace (gateway).

Here is my moleculer.config:

tracing: {
        enabled   : true,
        stackTrace: true,
        actions   : true,
        exporter  : {
            type   : 'Jaeger',
            options: {
                // HTTP Reporter endpoint. If set, HTTP Reporter will be used.
                endpoint: 'http://jaeger:14268/api/traces',
                // UDP Sender host option.
                host    : 'api.dev.host.com',
                // UDP Sender port option.
                port    : 6832,
                // Jaeger Sampler configuration.
                sampler : {
                    // Sampler type. More info: https://www.jaegertracing.io/docs/1.14/sampling/#client-sampling-configuration
                    type   : 'Const',
                    // Sampler specific options.
                    options: {
                    }
                },
                // Additional options for `Jaeger.Tracer`
                tracerOptions: {},
                // Default tags. They will be added into all span tags.
                defaultTags  : {
                    appName: 'core',
                }
            }
        },
        tags: {
            action: {
                // Always add the response
                response: true,
            }, 
        },
},

My jaeger-service is just one of the examples:

const JaegerService = require('moleculer-jaeger')

module.exports = {
    mixins  : [ JaegerService ],
    settings: {
        host   : 'jaeger-server',
        port   : 6832,
        sampler: {
            type   : 'Const',
            options: {
                decision: 1
            }
        }
    }
}

I tried several different configurations for sampling but nothing worked the way i would like it to have.

Here is some code where you can see the action calls i do:

// serviceX endpoint:
resolveByName: {
    rest  : 'GET resolve/name/:name',
    params: {
        name: { type: 'string' }
    },
    handler(ctx) {
        return resolveByNameHandler(this.broker, ctx.params, 'serviceY')
    }
},


// handler code
// please do not focus on the code itself. What i wanted to show is how i call the other
// services. 
const { NotFoundError } = require(`${process.env.INIT_CWD}/util/error`)

module.exports = (broker, params, dataSource) =>  {
    const { name } = params
    const query = { name: name }
    
    const rejectRequest = (name, data) => Promise.reject(new NotFoundError(name, data))

    const getSourceData = result => broker.call(`${dataSource}.find`, { query: { id: result[0].ownerId } })
        .then(sourceData => sourceData.length === 0
            ? rejectRequest(dataSource, sourceData)
            : mergeResult(sourceData, result))

    const mergeResult = (sourceData, result) => ({ ...sourceData[0], origin: { ...result[0], source: 'serviceX' } })

    return broker.call('serviceX.find', { query: query })
        .then(result => result.length === 0 ? rejectRequest('serviceX', query): result)
        .then(result => result[0].ownerId ? getSourceData(result) : rejectRequest('noOwnerId', query))
}

What i also tried is to not use the moleculer-jaeger package but to use the jaeger all-in-one docker image. Same results though...

# from the docker-compose.yml
  jaeger-server:
    image: jaegertracing/all-in-one:latest
    ports:
      - 5775:5775/udp
      - 6831:6831/udp
      - 6832:6832/udp
      - 5778:5778
      - 16686:16686
      - 14268:14268
      - 9411:9411
    networks:
      - internal

What i do not want to do is to set the spans manually in every service. I have tried it already but it did not work at all, so if this would be the only solution i would be very happy to see an example.

Thanks in advance!

*edit: The Versions i use:

{
    "jaeger-client": "^3.18.1",
    "moleculer": "^0.14.13",
    "moleculer-db": "^0.8.12",
    "moleculer-db-adapter-mongoose": "^0.8.9",
    "moleculer-jaeger": "^0.2.3",
    "moleculer-web": "^0.9.1",
    "mongoose": "^5.12.5",
}
1

There are 1 answers

1
Ivan Zhuravlev On BEST ANSWER
  1. This version already has a built-in jager tracer, see the documentation.
  2. In order for the events to be nested, it is necessary to transfer the context inside the actions, use ctx.call calls instead of broker.call, so they will be nested.
  3. To quickly receive support for the moleculer, join us in discord!