Pub/Sub in Node.js

Use the installation instructions below if you're installing Helios in a Node.js microservice using Google Pub/Sub.

pull-subscriptions

When using pubsub with the pull-subscription type, you will need to wrap your request handlers with the following snippet to prevent Trace data cut off:

const { context, trace } = require('@opentelemetry/api');

// Assuming "app" is an Express app
app.use((req, res, next) => {
    const spanContext = req?.body?.message?.attributes?.googclient_OpenTelemetrySpanContext;
    if (!spanContext) {
        return next();
    }

    return context.with(trace.setSpanContext(context.active(), JSON.parse(spanContext)), () => {
        return next();
    });
});
import { context, trace } from '@opentelemetry/api';

// Assuming "app" is an Express app
app.use((req: express.Request, res: express.Response, next: express.NextFunction) => {
    const spanContext = req?.body?.message?.attributes?.googclient_OpenTelemetrySpanContext;
    if (!spanContext) {
        return next();
    }

    return context.with(trace.setSpanContext(context.active(), JSON.parse(spanContext)), () => {
        return next();
    });
});

Did this page help you?