Skip to content
33 changes: 33 additions & 0 deletions docs/platforms/javascript/common/metrics/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Set Up Metrics
sidebar_title: Metrics
description: "Metrics allow you to send, view and query counters, gauges and measurements from your Sentry-configured apps to track application health and drill down into related traces, logs, and errors."
sidebar_order: 5755
---

With Sentry Metrics, you can send counters, gauges, distributions, and sets from your applications to Sentry. Once in Sentry, these metrics can be viewed alongside relevant errors, and searched using their individual attributes.

<Alert>
This feature is currently in limited beta. Please reach out on [GitHub](https://github.com/getsentry/sentry-javascript/discussions/18055) if you have feedback or questions. Features in beta are still in-progress and may have bugs. We recognize the irony.
</Alert>

## Requirements

<PlatformContent includePath="metrics/requirements" />

## Setup

<PlatformContent includePath="metrics/setup" />

## Usage

<PlatformContent includePath="metrics/usage" />

## Options

<PlatformContent includePath="metrics/options" />

## Default Attributes

<PlatformContent includePath="metrics/default-attributes" />

23 changes: 23 additions & 0 deletions platform-includes/metrics/default-attributes/javascript.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
By default the SDK will attach the following attributes to a metric:

1. `sentry.environment`: The environment set in the SDK if defined.
2. `sentry.release`: The release set in the SDK if defined.
3. `sentry.sdk.name`: The name of the SDK that sent the metric.
4. `sentry.sdk.version`: The version of the SDK that sent the metric.

### User Attributes

The SDK will optionally attach user information as attributes (guarded by [`sendDefaultPii`](/platforms/javascript/configuration/options/#sendDefaultPii)):

1. `user.id`
2. `user.name`
3. `user.email`


### Browser Attributes

The SDK will optionally attach browser information as attributes:

1. `browser.name` (added during ingestion, guarded by [`sendDefaultPii`](/platforms/javascript/configuration/options/#sendDefaultPii))
2. `browser.version` (added during ingestion, guarded by [`sendDefaultPii`](/platforms/javascript/configuration/options/#sendDefaultPii))
3. `sentry.replay_id`: The replay id of the replay that was active when the metric was collected. (added during ingestion by Relay)
56 changes: 56 additions & 0 deletions platform-includes/metrics/options/javascript.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
The Sentry JavaScript SDK provides several options to configure how metrics are captured and sent to Sentry.

### Filtering and Modifying Metrics

Use the `beforeSendMetric` callback to filter or modify metrics before they're sent to Sentry. This is useful for:

- Removing sensitive data from metric attributes
- Dropping metrics you don't want to send
- Adding or modifying attributes

The callback receives a metric object and must return either a modified metric or `null` to drop it.

```javascript
Sentry.init({
// ...
beforeSendMetric: (metric) => {
// Drop metrics with specific attributes
if (metric.attributes?.dropMe === true) {
return null;
}

// Modify metric attributes
metric.attributes = {
...metric.attributes,
processed: true,
};

return metric;
},
});
```

### Disabling Metrics

If you want to disable metrics collection entirely, you can do so by disabling the `_experimental.enableMetrics` flag:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused by this; since this is an experimental feature that you need to manually turn on with enableMetrics: true I would assume it's false' by default, so is explicitly setting it to false` necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left this in here since we'll change this on monday to the top-level enableMetrics option


```javascript
Sentry.init({
dsn: "___PUBLIC_DSN___",
_experiments: {
enableMetrics: false,
}
});
```


### Flushing Metrics

By default, metrics are buffered and flushed depending on buffer size and time. If you need to manually flush metrics before the automatic interval, you can use the `flush` method:

```javascript
await Sentry.flush();
```

This will flush all pending metrics and events to Sentry.

1 change: 1 addition & 0 deletions platform-includes/metrics/requirements/javascript.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Metrics are supported in all Sentry JavaScript SDKs version `10.22.0` and above.
11 changes: 11 additions & 0 deletions platform-includes/metrics/setup/javascript.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Metrics are gated by an experimental option, `_experiments.enableMetrics` (this will not be required in future versions of the SDK).

```javascript
Sentry.init({
dsn: "___PUBLIC_DSN___",
_experiments: {
enableMetrics: true,
},
});
```

66 changes: 66 additions & 0 deletions platform-includes/metrics/usage/javascript.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
Once the feature is enabled and the SDK is initialized, you can send metrics using the `Sentry.metrics` APIs.

The `metrics` namespace exposes three methods that you can use to capture different types of metric information: `count`, `gauge` and `distribution`.

### Counter

Use `count` to track an incrementing value, such as the number of times a button was clicked or a function was called.

```javascript
Sentry.metrics.count("button_click", 1);
```

### Gauge

Use `gauge` to track a value that can go up and down, such as the current memory usage or the number of items in a queue.

```javascript
Sentry.metrics.gauge("queue_depth", 42);
```

### Distribution

Use `distribution` to track the distribution of a value, such as the response time of a request.

```javascript
Sentry.metrics.distribution("response_time", 187.5);
```

### Adding Attributes

You can also pass additional attributes to any of the metric methods via the `attributes` option. Attributes allow you to filter and group metrics.

```javascript
Sentry.metrics.count(
"button_click",
1,
{
tags: {
browser: "Firefox",
app_version: "1.0.0",
},
}
);
```

### Specifying Units

For `gauge` and `distribution` metrics, you can specify a unit using the `unit` option. This helps Sentry display the metric value in a human-readable format.

```javascript
Sentry.metrics.distribution(
"response_time",
187.5,
{
unit: "millisecond",
}
);

Sentry.metrics.gauge(
"memory_usage",
1024,
{
unit: "byte",
}
);
```
8 changes: 0 additions & 8 deletions redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -1080,14 +1080,6 @@ const userDocsRedirects = [
source: '/platforms/javascript/tracing/trace-propagation/:path*',
destination: '/platforms/javascript/tracing/distributed-tracing/:path*',
},
{
source: '/platforms/javascript/metrics/:path*',
destination: '/platforms/javascript/tracing/span-metrics/:path*',
},
{
source: '/platforms/javascript/guides/:guide/metrics/:path*',
destination: '/platforms/javascript/guides/:guide/tracing/span-metrics/:path*',
},
{
source: '/platforms/javascript/tracing/instrumentation/performance-metrics/',
destination: '/platforms/javascript/tracing/span-metrics/',
Expand Down
Loading