Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 sent from your applications within Sentry."
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`):

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


### Browser Attributes

The SDK will optionally attach browser information as attributes:

1. `browser.name`
2. `browser.version`
3. `sentry.replay_id`: The replay id of the replay that was active when the metric was collected.
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.tags = {
...metric.tags,
processed: true,
};

return metric;
},
});
```

### Disabling Metrics

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

```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` (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