Skip to content

[BUG]: Moleculer js called action context is lost #6262

@andymihaja01

Description

@andymihaja01

Tracer Version(s)

5.24.0 => 5.62.0

Node.js Version(s)

20.10.0

Bug Report

Moleculer js has a feature where you can set and send back the meta from the called action to the caller , as said in the docs, but dd trace breaks that feature.

Reproduction Code

With these actions as an example

async first(ctx) {
            await ctx.call("test.second", null, { meta: {
                a: "John"
            }});

            console.log(ctx.meta);
            // Prints: { a: "Doe" }
        },
        second(ctx) {
            // Modify meta
            ctx.meta.a = "Doe";
        }

Without datadog tracer we get Doe as the value in first action, but when we use dd-trace we still get John.

The reason is the way moleculer js implements the context is they add a ctx property to the returned promise (not value) of the broker call handler result and then the context of the called action is merged with the caller's context.

In our code, patching this code https://github.com/DataDog/dd-trace-js/blob/master/packages/datadog-instrumentations/src/moleculer/client.js#L24 to look like this solves the issue of context being lost, although it is just a hotfix not a proper solution:

      promise
        .then(
          result => {
            finishChannel.publish({ broker, ctx })
            return result
          },
          error => {
            errorChannel.publish(error)
            finishChannel.publish({ broker, ctx })
            throw error
          }
        )
        return promise

instead of (original not working code)

return promise
        .then(
          result => {
            finishChannel.publish({ broker, ctx })
            return result
          },
          error => {
            errorChannel.publish(error)
            finishChannel.publish({ broker, ctx })
            throw error
          }
        )

Error Logs

No response

Tracer Config

No response

Operating System

No response

Bundling

Unsure

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions