|
| 1 | +--- |
| 2 | +title: Set Up |
| 3 | +sidebar_order: 0 |
| 4 | +description: "Learn how to set up Sentry's Metrics feature using our supported SDKs." |
| 5 | +--- |
| 6 | + |
| 7 | +<Alert level="info"> |
| 8 | + Metrics is currently in beta. If you'd like access for JavaScript or Python, please comment with your org slug on [this GitHub discussion ](https://github.com/getsentry/sentry/discussions/102275) or contact us at [email protected]. |
| 9 | +</Alert> |
| 10 | + |
| 11 | +To set up Sentry Metrics, use the links below for supported SDKs. After it's been set up, you'll be able to send counters, gauges, and distributions from your code and view them in Sentry with direct links to related traces. |
| 12 | + |
| 13 | +## Supported SDKs |
| 14 | + |
| 15 | +### JavaScript |
| 16 | + |
| 17 | +Minimum SDK version: **10.20.0** |
| 18 | + |
| 19 | +- <LinkWithPlatformIcon |
| 20 | + platform="javascript.browser" |
| 21 | + label="Browser JavaScript" |
| 22 | + url="/platforms/javascript/metrics/" |
| 23 | + /> |
| 24 | +- <LinkWithPlatformIcon |
| 25 | + platform="javascript.angular" |
| 26 | + label="Angular" |
| 27 | + url="/platforms/javascript/guides/angular/metrics/" |
| 28 | + /> |
| 29 | +- <LinkWithPlatformIcon |
| 30 | + platform="javascript.astro" |
| 31 | + label="Astro" |
| 32 | + url="/platforms/javascript/guides/astro/metrics/" |
| 33 | + /> |
| 34 | +- <LinkWithPlatformIcon |
| 35 | + platform="javascript.aws-lambda" |
| 36 | + label="AWS Lambda" |
| 37 | + url="/platforms/javascript/guides/aws-lambda/metrics/" |
| 38 | + /> |
| 39 | +- <LinkWithPlatformIcon |
| 40 | + platform="javascript.azure-functions" |
| 41 | + label="Azure Functions" |
| 42 | + url="/platforms/javascript/guides/azure-functions/metrics/" |
| 43 | + /> |
| 44 | +- <LinkWithPlatformIcon |
| 45 | + platform="javascript.bun" |
| 46 | + label="Bun" |
| 47 | + url="/platforms/javascript/guides/bun/metrics/" |
| 48 | + /> |
| 49 | +- <LinkWithPlatformIcon |
| 50 | + platform="javascript.cloudflare" |
| 51 | + label="Cloudflare" |
| 52 | + url="/platforms/javascript/guides/cloudflare/metrics/" |
| 53 | + /> |
| 54 | +- <LinkWithPlatformIcon |
| 55 | + platform="javascript.connect" |
| 56 | + label="Connect" |
| 57 | + url="/platforms/javascript/guides/connect/metrics/" |
| 58 | + /> |
| 59 | +- <LinkWithPlatformIcon |
| 60 | + platform="javascript.electron" |
| 61 | + label="Electron" |
| 62 | + url="/platforms/javascript/guides/electron/metrics/" |
| 63 | + /> |
| 64 | +- <LinkWithPlatformIcon |
| 65 | + platform="javascript.ember" |
| 66 | + label="Ember" |
| 67 | + url="/platforms/javascript/guides/ember/metrics/" |
| 68 | + /> |
| 69 | +- <LinkWithPlatformIcon |
| 70 | + platform="javascript.express" |
| 71 | + label="Express" |
| 72 | + url="/platforms/javascript/guides/express/metrics/" |
| 73 | + /> |
| 74 | +- <LinkWithPlatformIcon |
| 75 | + platform="javascript.fastify" |
| 76 | + label="Fastify" |
| 77 | + url="/platforms/javascript/guides/fastify/metrics/" |
| 78 | + /> |
| 79 | +- <LinkWithPlatformIcon |
| 80 | + platform="javascript.gatsby" |
| 81 | + label="Gatsby" |
| 82 | + url="/platforms/javascript/guides/gatsby/metrics/" |
| 83 | + /> |
| 84 | +- <LinkWithPlatformIcon |
| 85 | + platform="javascript.gcp-functions" |
| 86 | + label="Google Cloud Functions" |
| 87 | + url="/platforms/javascript/guides/gcp-functions/metrics/" |
| 88 | + /> |
| 89 | +- <LinkWithPlatformIcon |
| 90 | + platform="javascript.hapi" |
| 91 | + label="Hapi" |
| 92 | + url="/platforms/javascript/guides/hapi/metrics/" |
| 93 | + /> |
| 94 | +- <LinkWithPlatformIcon |
| 95 | + platform="javascript.hono" |
| 96 | + label="Hono" |
| 97 | + url="/platforms/javascript/guides/hono/metrics/" |
| 98 | + /> |
| 99 | +- <LinkWithPlatformIcon |
| 100 | + platform="javascript.koa" |
| 101 | + label="Koa" |
| 102 | + url="/platforms/javascript/guides/koa/metrics/" |
| 103 | + /> |
| 104 | +- <LinkWithPlatformIcon |
| 105 | + platform="javascript.nestjs" |
| 106 | + label="Nest.js" |
| 107 | + url="/platforms/javascript/guides/nestjs/metrics/" |
| 108 | + /> |
| 109 | +- <LinkWithPlatformIcon |
| 110 | + platform="javascript.node" |
| 111 | + label="Node.js" |
| 112 | + url="/platforms/javascript/guides/node/metrics/" |
| 113 | + /> |
| 114 | +- <LinkWithPlatformIcon |
| 115 | + platform="javascript.nextjs" |
| 116 | + label="Next.js" |
| 117 | + url="/platforms/javascript/guides/nextjs/metrics/" |
| 118 | + /> |
| 119 | +- <LinkWithPlatformIcon |
| 120 | + platform="javascript.nuxt" |
| 121 | + label="Nuxt" |
| 122 | + url="/platforms/javascript/guides/nuxt/metrics/" |
| 123 | + /> |
| 124 | +- <LinkWithPlatformIcon |
| 125 | + platform="javascript.react" |
| 126 | + label="React" |
| 127 | + url="/platforms/javascript/guides/react/metrics/" |
| 128 | + /> |
| 129 | +- <LinkWithPlatformIcon |
| 130 | + platform="javascript.react-router" |
| 131 | + label="React Router" |
| 132 | + url="/platforms/javascript/guides/react-router/metrics/" |
| 133 | + /> |
| 134 | +- <LinkWithPlatformIcon |
| 135 | + platform="javascript.remix" |
| 136 | + label="Remix" |
| 137 | + url="/platforms/javascript/guides/remix/metrics/" |
| 138 | + /> |
| 139 | +- <LinkWithPlatformIcon |
| 140 | + platform="javascript.solid" |
| 141 | + label="Solid" |
| 142 | + url="/platforms/javascript/guides/solid/metrics/" |
| 143 | + /> |
| 144 | +- <LinkWithPlatformIcon |
| 145 | + platform="javascript.solidstart" |
| 146 | + label="SolidStart" |
| 147 | + url="/platforms/javascript/guides/solidstart/metrics/" |
| 148 | + /> |
| 149 | +- <LinkWithPlatformIcon |
| 150 | + platform="javascript.svelte" |
| 151 | + label="Svelte" |
| 152 | + url="/platforms/javascript/guides/svelte/metrics/" |
| 153 | + /> |
| 154 | +- <LinkWithPlatformIcon |
| 155 | + platform="javascript.sveltekit" |
| 156 | + label="SvelteKit" |
| 157 | + url="/platforms/javascript/guides/sveltekit/metrics/" |
| 158 | + /> |
| 159 | +- <LinkWithPlatformIcon |
| 160 | + platform="javascript.tanstackstart-react" |
| 161 | + label="TanStack Start" |
| 162 | + url="/platforms/javascript/guides/tanstackstart-react/metrics/" |
| 163 | + /> |
| 164 | +- <LinkWithPlatformIcon |
| 165 | + platform="javascript.vue" |
| 166 | + label="Vue" |
| 167 | + url="/platforms/javascript/guides/vue/metrics/" |
| 168 | + /> |
| 169 | +- <LinkWithPlatformIcon |
| 170 | + platform="javascript.wasm" |
| 171 | + label="Wasm" |
| 172 | + url="/platforms/javascript/guides/wasm/metrics/" |
| 173 | + /> |
| 174 | + |
| 175 | +### Python |
| 176 | + |
| 177 | +Minimum SDK version: **2.43.0** |
| 178 | + |
| 179 | +- <LinkWithPlatformIcon |
| 180 | + platform="python" |
| 181 | + label="Python" |
| 182 | + url="/platforms/python/metrics/" |
| 183 | + /> |
| 184 | +- <LinkWithPlatformIcon |
| 185 | + platform="python.django" |
| 186 | + label="Django" |
| 187 | + url="/platforms/python/guides/django/metrics/" |
| 188 | + /> |
| 189 | +- <LinkWithPlatformIcon |
| 190 | + platform="python.flask" |
| 191 | + label="Flask" |
| 192 | + url="/platforms/python/guides/flask/metrics/" |
| 193 | + /> |
| 194 | +- <LinkWithPlatformIcon |
| 195 | + platform="python.fastapi" |
| 196 | + label="FastAPI" |
| 197 | + url="/platforms/python/guides/fastapi/metrics/" |
| 198 | + /> |
| 199 | +- <LinkWithPlatformIcon |
| 200 | + platform="python.starlette" |
| 201 | + label="Starlette" |
| 202 | + url="/platforms/python/guides/starlette/metrics/" |
| 203 | + /> |
| 204 | +- <LinkWithPlatformIcon |
| 205 | + platform="python.celery" |
| 206 | + label="Celery" |
| 207 | + url="/platforms/python/guides/celery/metrics/" |
| 208 | + /> |
| 209 | +- <LinkWithPlatformIcon |
| 210 | + platform="python.aws-lambda" |
| 211 | + label="AWS Lambda" |
| 212 | + url="/platforms/python/guides/aws-lambda/metrics/" |
| 213 | + /> |
| 214 | +- <LinkWithPlatformIcon |
| 215 | + platform="python.gcp-functions" |
| 216 | + label="Google Cloud Functions" |
| 217 | + url="/platforms/python/guides/gcp-functions/metrics/" |
| 218 | + /> |
| 219 | + |
| 220 | +## Quick Start Examples |
| 221 | + |
| 222 | +### JavaScript Example |
| 223 | + |
| 224 | +First, enable metrics in your Sentry initialization: |
| 225 | + |
| 226 | +```javascript |
| 227 | +import * as Sentry from "@sentry/node"; // or your specific framework |
| 228 | + |
| 229 | +Sentry.init({ |
| 230 | + dsn: '<your-dsn>', |
| 231 | + _experiments: { |
| 232 | + enableMetrics: true |
| 233 | + }, |
| 234 | +}); |
| 235 | +``` |
| 236 | + |
| 237 | +Then use the metrics API to send counters, gauges, and distributions: |
| 238 | + |
| 239 | +```javascript |
| 240 | +// Counter — increment a metric (defaults to +1) |
| 241 | +Sentry.metrics.count('checkout.failed', 1, { |
| 242 | + attributes: { |
| 243 | + route: '/checkout', |
| 244 | + tenant: 'acme', |
| 245 | + provider: 'stripe', |
| 246 | + }, |
| 247 | +}); |
| 248 | + |
| 249 | +// Gauge — set a current value |
| 250 | +Sentry.metrics.gauge('queue.depth', 42, { |
| 251 | + unit: 'items', |
| 252 | + attributes: { |
| 253 | + queue: 'emails', |
| 254 | + region: 'us-east-1', |
| 255 | + }, |
| 256 | +}); |
| 257 | + |
| 258 | +// Distribution — record numeric values to compute p95, avg, sum, etc. |
| 259 | +Sentry.metrics.distribution('cart.amount_usd', 187.5, { |
| 260 | + unit: 'usd', |
| 261 | + attributes: { |
| 262 | + currency: 'USD', |
| 263 | + tenant: 'acme', |
| 264 | + }, |
| 265 | +}); |
| 266 | +``` |
| 267 | + |
| 268 | +Each metric event includes: |
| 269 | +- **name**: The metric identifier |
| 270 | +- **kind**: counter, gauge, or distribution |
| 271 | +- **value**: The numeric value |
| 272 | +- **attributes**: Custom key-value pairs for grouping and filtering |
| 273 | +- **trace_id, span_id**: Automatically attached to link with traces |
| 274 | +- **unit** (optional): Unit of measurement |
| 275 | +- **byte_size** (optional): Size information |
| 276 | + |
| 277 | +More details: [JavaScript SDK Metrics Discussion](https://github.com/getsentry/sentry-javascript/discussions/18055) |
| 278 | + |
| 279 | +### Python Example |
| 280 | + |
| 281 | +First, enable metrics in your Sentry initialization: |
| 282 | + |
| 283 | +```python |
| 284 | +import sentry_sdk |
| 285 | +from sentry_sdk import metrics |
| 286 | + |
| 287 | +sentry_sdk.init( |
| 288 | + dsn="___PUBLIC_DSN___", |
| 289 | + _experiments={ |
| 290 | + "enable_metrics": True, |
| 291 | + }, |
| 292 | +) |
| 293 | +``` |
| 294 | + |
| 295 | +Then use the metrics API to send counters, gauges, and distributions: |
| 296 | + |
| 297 | +```python |
| 298 | +# Counter — increment a metric (defaults to +1) |
| 299 | +metrics.count( |
| 300 | + "checkout.failed", |
| 301 | + 1, |
| 302 | + attributes={"route": "/checkout", "tenant": "acme"} |
| 303 | +) |
| 304 | + |
| 305 | +# Gauge — set a current value |
| 306 | +metrics.gauge( |
| 307 | + "queue.depth", |
| 308 | + 42, |
| 309 | + attributes={"queue": "emails"} |
| 310 | +) |
| 311 | + |
| 312 | +# Distribution — record numeric values to compute p95, avg, sum, etc. |
| 313 | +metrics.distribution( |
| 314 | + "cart.amount_usd", |
| 315 | + 187.5, |
| 316 | + attributes={"currency": "USD", "tenant": "acme"} |
| 317 | +) |
| 318 | +``` |
| 319 | + |
| 320 | +Each metric event includes: |
| 321 | +- **name**: The metric identifier |
| 322 | +- **kind**: counter, gauge, or distribution |
| 323 | +- **value**: The numeric value |
| 324 | +- **attributes**: Custom key-value pairs for grouping and filtering |
| 325 | +- **trace_id, span_id**: Automatically attached to link with traces |
| 326 | +- **unit** (optional): Unit of measurement |
| 327 | + |
| 328 | +More details: [Python SDK Metrics Discussion](https://github.com/getsentry/sentry-python/discussions/5042) |
| 329 | + |
| 330 | +## Best Practices |
| 331 | + |
| 332 | +### Naming Conventions |
| 333 | + |
| 334 | +Use descriptive, dot-separated names that indicate the metric's purpose: |
| 335 | + |
| 336 | +- **Good**: `checkout.failed`, `email.sent`, `queue.depth` |
| 337 | +- **Avoid**: `metric1`, `counter`, `x` |
| 338 | + |
| 339 | +### Attributes |
| 340 | + |
| 341 | +Add attributes for any dimension you want to group or filter by: |
| 342 | + |
| 343 | +```javascript |
| 344 | +Sentry.metrics.count('api.request', 1, { |
| 345 | + attributes: { |
| 346 | + endpoint: '/users', |
| 347 | + method: 'GET', |
| 348 | + status: '200', |
| 349 | + region: 'us-west', |
| 350 | + }, |
| 351 | +}); |
| 352 | +``` |
| 353 | + |
| 354 | +This allows you to query metrics like: |
| 355 | +- `sum(api.request)` grouped by `endpoint` |
| 356 | +- `sum(api.request)` where `status:500` |
| 357 | +- `sum(api.request)` grouped by `region` where `method:POST` |
| 358 | + |
| 359 | +### Units |
| 360 | + |
| 361 | +Always specify units for clarity: |
| 362 | + |
| 363 | +- Time: `ms`, `seconds` |
| 364 | +- Size: `bytes`, `kb`, `mb` |
| 365 | +- Currency: `usd`, `eur` |
| 366 | +- Count: `items`, `requests`, `users` |
| 367 | + |
| 368 | +### When to Instrument |
| 369 | + |
| 370 | +Add metrics at key decision points in your code: |
| 371 | + |
| 372 | +- **Before/after critical operations**: Track success and failure rates |
| 373 | +- **At service boundaries**: Monitor external API calls, database queries |
| 374 | +- **Business logic**: Capture important business events |
| 375 | +- **Resource usage**: Track queue depths, connection pools, cache sizes |
| 376 | + |
| 377 | +## Upcoming SDKs |
| 378 | + |
| 379 | +We're actively working on adding Metrics functionality to additional SDKs. If you don't see your platform listed above, please reach out to us on [GitHub ](https://github.com/getsentry/sentry/discussions/102275) or contact us at [[email protected]](mailto:[email protected]). |
| 380 | + |
0 commit comments