skip to Main Content

I am trying to implement opentelemetry tracing in loopback 4.

As per the auto instrument libraries from opentelemetry for HTTP, express, redis, psotgres all opentelemetry modules required to be loaded before any of such application module are loaded. But when I try to do so within the app it always gives a message which says:

Some modules (redis, express) were already required when their respective plugin was loaded, some plugins might not work. Make sure the SDK is setup before you require in other modules.

Below is the sample code which I have tried.

Install the following packages:

@opentelemetry/node
@opentelemetry/tracing
@opentelemetry/exporter-jaeger
@opentelemetry/plugin-http
@opentelemetry/plugin-https
@opentelemetry/plugin-express

AT the top of index.ts

import { JaegerExporter } from '@opentelemetry/exporter-jaeger';
import { NodeTracerProvider } from '@opentelemetry/node';
import { SimpleSpanProcessor, ConsoleSpanExporter } from '@opentelemetry/tracing';

Following at the top of the main function in index.ts file

  const option = {
    serviceName: 'basic-service',
    tags: [], // optional
    // You can use the default UDPSender
    host: 'localhost', // optional
    port: 6832, // optional
    // OR you can use the HTTPSender as follows
    // endpoint: 'http://localhost:14268/api/traces',
  }

  // Configure span processor to send spans to the exporter
  const exporter = new JaegerExporter(option);
  provider.addSpanProcessor(new SimpleSpanProcessor(exporter));
  provider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
  provider.register();

Then import the following in the application.ts file:

import openTelemetry, { Span, Tracer } from '@opentelemetry/api';

Then I added this middleware in the application.ts

const middle: Middleware = async (ctx, next) => {
  const trace = openTelemetry.trace.getTracer('loopback:tracer');
  let span: Span;
  span = trace.startSpan(ctx.request.url);
  span.setAttribute('key', 'value');
  span.addEvent('invoking next');
  const res = await next();
  return res;
}

Then bind this middleware in the constructor in the application.ts:

this.middleware(middle);

Another thing if I use Axios to make an HTTP request then context propagation happens properly but if I use the recommended way of loopback by creating a rest connector then it doesn’t happen. I have installed all the required modules in the app.

I have a microservice based architecture where a request can go from one service to another and I need to keep the trace of the complete request cycle.

Any help will be appreciated here.

2

Answers


  1. Chosen as BEST ANSWER

    Figured out the issues and made them work as well. All the details of the same are in the following link:

    https://github.com/strongloop/loopback-next/issues/6791

    A simple guideline is given in the link and following that one can very easily setup the tracing using opentelemetry in loopback 4.


  2. You have to provide config for NodeTracerProvider and registerInstrumentation in 0.15.0 as follows

    // Enable OpenTelemetry exporters to export traces to Grafan Tempo.
    const provider = new NodeTracerProvider ({
        plugins: {
            express: {
              enabled: true,
              path: '@opentelemetry/plugin-express',
            },
            http: {
                enabled: true,
                path: '@opentelemetry/plugin-http',
            },
            redis: {
                enabled: true,
                // You may use a package name or absolute path to the file.
                path: '@opentelemetry/plugin-redis',
            },
        },
        logLevel: LogLevel.ERROR,      
    });
    
    registerInstrumentations({
        tracerProvider: provider
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search