Setup Sentry Metrics in any project. Use this when asked to add Sentry metrics, track custom metrics, setup counters/gauges/distributions, or instrument application performance metrics. Supports JavaScript, TypeScript, Python, React, Next.js, and Node.js.
Setup Sentry Metrics in any project. Use this when asked to add Sentry metrics, track custom metrics, setup counters/gauges/distributions, or instrument application performance metrics. Supports JavaScript, TypeScript, Python, React, Next.js, and Node.js.
Setup Sentry Metrics
This skill helps configure Sentry's custom metrics feature to track counters, gauges, and distributions across your applications.
When to Use This Skill
Invoke this skill when:
User asks to "setup Sentry metrics" or "add custom metrics"
User wants to "track metrics in Sentry"
User requests "counters", "gauges", or "distributions" with Sentry
User mentions they want to track business KPIs or application health
User asks about Sentry.metrics or sentry_sdk.metrics
Python (SDK 2.44.0+): Django, Flask, FastAPI, general Python
Note: Ruby does not currently have dedicated metrics support in the Sentry SDK.
Metric Types Overview
Before setup, explain the three metric types to the user:
Type
Purpose
Use Cases
Aggregations
Counter
Track cumulative occurrences
Button clicks, API calls, errors
sum, per_second, per_minute
Gauge
Point-in-time snapshots
Queue depth, memory usage, connections
min, max, avg
Distribution
Statistical analysis of values
Response times, cart amounts, query duration
p50, p75, p95, p99, avg, min, max
Platform Detection
JavaScript/TypeScript Detection
Check for these files:
package.json - Read to identify framework and Sentry SDK version
Look for @sentry/nextjs, @sentry/react, @sentry/node, @sentry/browser
Check if SDK version is 10.25.0+ (required for metrics)
Python Detection
Check for:
requirements.txt, pyproject.toml, setup.py, or Pipfile
Look for sentry-sdk version 2.44.0+ (required for metrics)
Required Information
Ask the user:
I'll help you set up Sentry Metrics. First, let me check your project setup.
After detecting the platform:
1. **What metrics do you want to track?**
- Counters: Event counts (clicks, API calls, errors)
- Gauges: Point-in-time values (queue depth, memory)
- Distributions: Value analysis (response times, amounts)
2. **Do you need metric filtering?**
- Yes: Configure beforeSendMetric to filter/modify metrics
- No: Send all metrics as-is
React: src/index.tsx, src/main.tsx, or dedicated sentry config file
Node.js: Entry point file or dedicated sentry config
Browser: Entry point or config file
Optional: Explicitly enable (not required):
import * asSentryfrom"@sentry/nextjs"; // or @sentry/react, @sentry/nodeSentry.init({
dsn: "YOUR_DSN_HERE",
// Metrics enabled by default, but can be explicitenableMetrics: true,
// ... other existing config
});
Step 3: Add Metric Filtering (Optional)
If user wants to filter metrics before sending:
Sentry.init({
dsn: "YOUR_DSN_HERE",
beforeSendMetric: (metric) => {
// Drop metrics with sensitive attributesif (metric.attributes?.sensitive === true) {
returnnull;
}
// Remove specific attribute before sendingif (metric.attributes?.internal) {
delete metric.attributes.internal;
}
return metric;
},
});
// Force pending metrics to send immediatelyawaitSentry.flush();
// Useful before process exit or after critical operations
process.on("beforeExit", async () => {
awaitSentry.flush();
});
Python Configuration
Minimum SDK Version
sentry-sdk version 2.44.0+
Step 1: Verify SDK Version
pip show sentry-sdk | grep Version
If version is below 2.44.0:
pip install --upgrade sentry-sdk
Step 2: Verify Metrics Are Enabled
Metrics are enabled by default in SDK 2.44.0+. No changes required unless filtering is needed.
Common init locations:
Django: settings.py
Flask: app.py or __init__.py
FastAPI: main.py
General: Entry point or dedicated config file
Step 3: Add Metric Filtering (Optional)
import sentry_sdk
defbefore_send_metric(metric, hint):
# Drop metrics with specific attributesif metric.get("attributes", {}).get("sensitive"):
returnNone# Modify metric before sendingif metric.get("attributes", {}).get("internal"):
del metric["attributes"]["internal"]
return metric
sentry_sdk.init(
dsn="YOUR_DSN_HERE",
before_send_metric=before_send_metric,
)
Python Metrics API Examples
Counter Examples
import sentry_sdk
# Basic counter
sentry_sdk.metrics.count("button_click", 1)
# Counter with attributes
sentry_sdk.metrics.count(
"api_call",
1,
attributes={
"endpoint": "/api/users",
"method": "GET",
"status_code": 200,
}
)
# Counter for errors
sentry_sdk.metrics.count(
"checkout_error",
1,
attributes={
"error_type": "payment_declined",
"payment_provider": "stripe",
}
)
# Business event counter
sentry_sdk.metrics.count(
"email_sent",
5, # Can increment by more than 1
attributes={
"template": "newsletter",
"batch_id": "batch_123",
}
)