Required dependencies

Add the following NPM packages to your project by running:

npm install @opentelemetry/api

Creating Spans

To create a span, you need to use the tracer object. The tracer object is the entry point for the OpenTelemetry API.

You can create a tracer object using the getTracer method from the @opentelemetry/api package.

const opentelemetry = require('@opentelemetry/api');

const tracer = opentelemetry.trace.getTracer(
  'instrumentation-scope-name',
  'instrumentation-scope-version',
);

function my_function() {
  const span = tracer.startSpan('my_function');
  // Your code here
  span.end();
}

Make sure to replace instrumentation-scope-name and instrumentation-scope-version with the name and version of your instrumented file.

Additional information

For more use cases, see the OpenTelemetry JavaScript API documentation.