Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Context-based handlers #64

Merged
merged 14 commits into from
Feb 20, 2023
Merged

Context-based handlers #64

merged 14 commits into from
Feb 20, 2023

Conversation

icebob
Copy link
Member

@icebob icebob commented Feb 19, 2023

This PR adds Context-based functionality:

  • transferring ctx.meta and tracing information from Context
  • create a Context and pass to channel handlers where ctx.params contains the payload and ctx.meta contains the parent ctx.meta. The modified ctx.meta is not transferred back to the caller.

This functions works with all adapters.

Enable the function globally for all channel handlers

// moleculer.config.js
const ChannelsMiddleware = require("@moleculer/channels").Middleware;

module.exports = {
    logger: true,

    middlewares: [
        ChannelsMiddleware({
            adapter: "redis://localhost:6379",
            // Enable context in all channel handlers 
            context: true
        })
    ]
};

Call with context:

broker.createService({
    name: "publisher",
    actions: {
        async publish(ctx) {
            await broker.sendToChannel("my.topic", { a: 5, b: "John" }, {
                ctx,
                headers: { myHeader: "123" }
            });
        }
    }
});

Context in handler

The Context is always created regardless of whether it was passed at sendToChannel

module.exports = {
    name: "sub1",
    channels: {
        "my.topic": {
            context: true, // Unless not enabled it globally
            async handler(ctx, raw) {
                // Original `msg` in `ctx.params`
                this.logger.info("Processing...", ctx.params, ctx.meta);
            }
        }
    }
}

Tracing

Register channel tracing middleware

//moleculer.config.js
const TracingMiddleware = require("@moleculer/channels").Tracing;

module.exports = {
    logger: true,

    middlewares: [
        ChannelsMiddleware({
            adapter: "redis://localhost:6379",
            // Enable context in all channel handlers 
            context: true
        }),
        TracingMiddleware()
    ]
};

You can fine-tuning tracing tags and span name in tracing channel property similar to actions.

Customize tags and span name

broker.createService({
    name: "sub1",
    channels: {
        "my.topic": {
            context: true,
            tracing: {
                spanName: ctx => `My custom span: ${ctx.params.id}`
                tags: {
                    params: true,
                    meta: true
                }
            },
            async handler(ctx, raw) {
                // ...
            }
        }
    }
});

@icebob icebob mentioned this pull request Feb 19, 2023
@icebob icebob requested a review from AndreMaz February 19, 2023 16:23
@icebob icebob linked an issue Feb 19, 2023 that may be closed by this pull request
@icebob icebob merged commit edf022b into master Feb 20, 2023
@icebob icebob deleted the context branch February 20, 2023 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sendToChannel with Context
2 participants