Documentation

7.4. Configure Instrumentation

In this chapter, you'll learn about observability in Medusa and how to configure instrumentation with OpenTelemetry.

Observability with OpenTelemtry#

Medusa uses OpenTelemetry for instrumentation and reporting. When configured, it reports traces for:

  • HTTP requests
  • Workflow executions
  • Query usages
  • Database queries and operations

How to Configure Instrumentation in Medusa?#

Install Dependencies#

Start by installing the following OpenTelemetry dependencies in your Medusa project:

Also, install the dependencies relevant for the exporter you use. If you're using Zipkin, install the following dependencies:

Add instrumentation.js#

Next, create the file instrumentation.js with the following content:

instrumentation.js
1const { registerOtel } = require("@medusajs/medusa")2// If using an exporter other than Zipkin, require it here.3const { ZipkinExporter } = require("@opentelemetry/exporter-zipkin")4
5// If using an exporter other than Zipkin, initialize it here.6const exporter = new ZipkinExporter({7  serviceName: "my-medusa-project",8})9
10export function register() {11  registerOtel({12    serviceName: "medusajs",13    // pass exporter14    exporter,15    instrument: {16      http: true,17      workflows: true,18      remoteQuery: true,19    },20  })21}

In the instrumentation.js file, you export a register function that uses Medusa's registerOtel utility function.

You also initialize an instance of the exporter, such as Zipkin, and pass it to the registerOtel function.

The registerOtel utility function accepts an object having the following properties:

serviceNamestring
The name of the service traced.
exporterSpanExporter
An instance of an exporter, such as Zipkin.
instrumentobjectOptional
Options specifying what to trace.
instrumentationsInstrumentation[]Optional
Additional instrumentation options that OpenTelemetry accepts.

Test it Out#

To test it out, start your exporter, such as Zipkin.

Then, start your Medusa application:

Try to open the Medusa Admin or send a request to an API route.

If you check traces in your exporter, you'll find new traces reported.

Trace Span Names#

Trace span names start with the following keywords based on what it's reporting:

  • {methodName} {URL} when reporting HTTP requests, where {methodName} is the HTTP method, and {URL} is the URL the request is sent to.
  • route: when reporting route handlers running on an HTTP requests.
  • middleware: when reporting a middleware running on an HTTP request.
  • workflow: when reporting a workflow execution.
  • step: when reporting a step in a workflow execution.
  • query.graph: when reporting Query usages.
  • pg.query: when reporting database queries and operations.
Was this chapter helpful?
Edit this page