1 import { Prerequisites, TypeList } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Configure Instrumentation`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you'll learn about observability in Medusa and how to configure instrumentation with OpenTelemetry.
10
11 ## What is Instrumentation?
12
13 Instrumentation is the collection of data about your application's performance and errors. It helps you debug issues, monitor performance, and gain insights into how your application behaves in production.
14
15 Instrumentation and observability are crucial as you build customizations in your application. They allow you to optimize performance, identify bottlenecks, and ensure your application runs smoothly.
16
17 ---
18
19 ## Instrumentation and Observability with OpenTelemetry
20
21 Medusa uses [OpenTelemetry](https://opentelemetry.io/) for instrumentation and reporting. When configured, it reports traces for:
22
23 - HTTP requests
24 - Workflow executions
25 - Query usages
26 - Database queries and operations
27 - Cache operations
28
29 ---
30
31 ## How to Configure Instrumentation in Medusa?
32
33 <Prerequisites
34 items={[
35 {
36 text: "An exporter to visualize your application's traces, such as Zipkin.",
37 link: "https://zipkin.io/pages/quickstart.html"
38 }
39 ]}
40 />
41
42 ### Install Dependencies
43
44 <Note>
45
46 As of [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0), OpenTelemetry dependencies are installed by default in new Medusa projects. If you're using an older version of Medusa, you need to install the `@opentelemetry/sdk-node`, `@opentelemetry/resources`, `@opentelemetry/sdk-trace-node`, and `@opentelemetry/instrumentation-pg` dependencies.
47
48 </Note>
49
50 Before you start, you must install the dependencies relevant for the exporter you use. If you're using Zipkin, install the following dependencies:
51
52 ```bash npm2yarn
53 npm install @opentelemetry/exporter-zipkin
54 ```
55
56 ### Add instrumentation.ts
57
58 Next, create the file `instrumentation.ts` with the following content:
59
60 ```ts title="instrumentation.ts"
61 import { registerOtel } from "@medusajs/medusa"
62 import { ZipkinExporter } from "@opentelemetry/exporter-zipkin"
63
64 // If using an exporter other than Zipkin, initialize it here.
65 const exporter = new ZipkinExporter({
66 serviceName: "my-medusa-project",
67 })
68
69 export function register() {
70 registerOtel({
71 serviceName: "medusajs",
72 // pass exporter
73 exporter,
74 instrument: {
75 http: true,
76 workflows: true,
77 query: true,
78 db: true,
79 cache: true,
80 },
81 })
82 }
83 ```
84
85 In the `instrumentation.ts` 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.
86
87 `registerOtel` accepts an object where you can pass any [NodeSDKConfiguration](https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_sdk-node.NodeSDKConfiguration.html) property along with the following properties:
88
89 <Note>
90
91 The `NodeSDKConfiguration` properties are accepted since Medusa v2.5.1.
92
93 </Note>
94
95 <TypeList
96 types={[
97 {
98 name: "serviceName",
99 type: "`string`",
100 description: "The name of the service traced.",
101 optional: false
102 },
103 {
104 name: "exporter",
105 type: "[SpanExporter](https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_sdk-node.node.SpanExporter.html)",
106 description: "An instance of an exporter, such as Zipkin."
107 },
108 {
109 name: "instrument",
110 type: "`object`",
111 description: "Options specifying what to trace.",
112 optional: true,
113 children: [
114 {
115 name: "http",
116 type: "`boolean`",
117 description: "Whether to trace HTTP requests."
118 },
119 {
120 name: "query",
121 type: "`boolean`",
122 description: "Whether to trace Query usages."
123 },
124 {
125 name: "workflows",
126 type: "`boolean`",
127 description: "Whether to trace Workflow executions."
128 },
129 {
130 name: "db",
131 type: "`boolean`",
132 description: "Whether to trace database queries and operations."
133 },
134 {
135 name: "cache",
136 type: "`boolean`",
137 description: "Whether to trace cache operations."
138 },
139 ]
140 },
141 {
142 name: "instrumentations",
143 type: "[Instrumentation[]](https://open-telemetry.github.io/opentelemetry-js/interfaces/_opentelemetry_instrumentation.Instrumentation.html)",
144 description: "Additional instrumentation options that OpenTelemetry accepts.",
145 optional: true
146 }
147 ]}
148 />
149
150 ---
151
152 ## Test it Out
153
154 To test it out, start your exporter, such as Zipkin.
155
156 Then, start your Medusa application:
157
158 ```bash npm2yarn
159 npm run dev
160 ```
161
162 Try to open the Medusa Admin or send a request to an API route.
163
164 If you check traces in your exporter, you'll find new traces reported.
165
166 ### Trace Span Names
167
168 Trace span names start with the following keywords based on what it's reporting:
169
170 - `{methodName} {URL}` when reporting HTTP requests, where `{methodName}` is the HTTP method, and `{URL}` is the URL the request is sent to.
171 - `route:` when reporting route handlers running on an HTTP request.
172 - `middleware:` when reporting a middleware running on an HTTP request.
173 - `workflow:` when reporting a workflow execution.
174 - `step:` when reporting a step in a workflow execution.
175 - `query.graph:` when reporting Query usages.
176 - `pg.query:` when reporting database queries and operations.
177
178 ---
179
180 ## Useful Links
181
182 - [Integrate Sentry with Medusa](!resources!/integrations/guides/sentry)