Skip to content
Open
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
32 changes: 32 additions & 0 deletions docs/platforms/dotnet/common/metrics/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
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: 7
sidebar_section: features
beta: true
---

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

Sentry metrics help you pinpoint and solve issues that impact user experience and app performance by measuring the data points that are important to you. You can track things like processing time, event size, user signups, and conversion rates, then correlate them back to tracing data in order to get deeper insights and solve issues faster.

Once in Sentry, these metrics can be viewed alongside relevant errors, and searched using their individual attributes.

## Requirements

<PlatformContent includePath="metrics/requirements" />

## Usage

<PlatformContent includePath="metrics/usage" />

## Options

<PlatformContent includePath="metrics/options" />

## Default Attributes

<PlatformContent includePath="metrics/default-attributes" />
7 changes: 7 additions & 0 deletions platform-includes/metrics/default-attributes/dotnet.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
The .NET SDK automatically sets several default attributes on all metrics to provide context and improve debugging:

<Include name="metrics/default-attributes/core" />

<Include name="metrics/default-attributes/server" />

<Include name="metrics/default-attributes/user" />
44 changes: 44 additions & 0 deletions platform-includes/metrics/options/dotnet.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#### EnableMetrics

Set to `false` in order to disable the `SentrySdk.Experimental.Metrics` APIs.

#### SetBeforeSendMetric

To filter metrics, or update them before they are sent to Sentry, you can use the `SetBeforeSendMetric(Func<SentryMetric, SentryMetric?>)` option. If the callback returns `null`, the metric is not emitted. Attributes can also be updated in the callback delegate.

```csharp
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.Experimental.SetBeforeSendMetric(static metric =>
{
if (metric.Name == "removed-metric")
{
return null;
}

metric.SetAttribute("extra", "foo");

return metric;
});
});
```

The `beforeSendMetric` delegate receives a metric object, and should return the metric object if you want it to be sent to Sentry, or `null` if you want to discard it.

The metric object of type `SentryMetric` has the following members:

| Member | Type | Description |
|--------|------|-------------|
| `Timestamp` | `DateTimeOffset` | Timestamp indicating when the metric was emitted. |
| `TraceId` | `SentryId` | The trace ID of the trace this metric belongs to. |
| `Type` | `SentryMetricType` | The type of metric. One of `Counter`, `Gauge`, `Distribution`. |
| `Name` | `string` | The name of the metric. |
| `SpanId` | `SpanId?` | The span ID of the span that was active when the metric was emitted. |
| `Unit` | `string?` | The unit of measurement for the metric value. Applies to `Gauge` and `Distribution` only. |
| `TryGetValue<TValue>(out TValue value)` | Method | Gets the numeric value of the metric. Returns `true` if the metric value is of type `TValue`, otherwise `false`. Supported numeric value types are `byte`, `short`, `int`, `long`, `float`, and `double`. |
| `TryGetAttribute<TAttribute>(string key, out TAttribute value)` | Method | Gets the attribute value associated with the specified key. Returns `true` if the metric contains an attribute with the specified key and its value is of type `TAttribute`, otherwise `false`. |
| `SetAttribute<TAttribute>(string key, TAttribute value)` | Method | Sets a key-value pair of data attached to the metric. Supported types are `string`, `bool`, integers up to a size of 64-bit signed, and floating-point numbers up to a size of 64-bit. |

The numeric value of `SentryMetric` has the same numeric type that the metric was emitted with.
The supported numeric types are `byte`, `short`, `int`, `long`, `float`, and `double`.
Copy link

Choose a reason for hiding this comment

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

Duplicated numeric types text across two files

Low Severity

The exact text "The supported numeric types are byte, short, int, long, float, and double." appears verbatim in both usage/dotnet.mdx (line 6) and options/dotnet.mdx (lines 43-44). This duplication creates a maintenance burden if the supported types ever change.

Fix in Cursor Fix in Web

Copy link
Member Author

Choose a reason for hiding this comment

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

True ... but to be fair, there is two parts of the API:

  • the "production" part via Emit, whose methods are all generic, dictating the type of the numeric value
  • the "consumption" part via BeforeSendMetric, which is a callback receiving/returning an instance of SentryMetric, which does not expose the Value directly, but through a "Try-Get-Value" method

We are not 100 % convinced that this is the best API shape, yet.
We are about to release this in v6.1.0 as an Experimental feature ... so we might be tweaking the API, and with that the documentation.
But for now, I'd like to be quite explicit ... and potentially duplicating.

And also ... I am quite sure that you will create a comment if future edits only update one part ... won't you 😉 ... and yes ... I am talking to a machine here right now ... it is getting late.

1 change: 1 addition & 0 deletions platform-includes/metrics/requirements/dotnet.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Metrics for .NET are supported in Sentry .NET SDK version `6.1.0` and above.
42 changes: 42 additions & 0 deletions platform-includes/metrics/usage/dotnet.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Metrics are enabled by default. Once you initialize the SDK, you can send metrics using the `SentrySdk.Experimental.Metrics` APIs.

The `SentryMetricEmitter` type exposes three method groups that you can use to capture different types of metric information: `Counter`, `Gauge`, and `Distribution`.
Copy link

Choose a reason for hiding this comment

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

Bug: The documentation refers to the SentryMetricEmitter type, but code examples use SentrySdk.Experimental.Metrics, which is confusing and inconsistent with other SDKs.
Severity: MEDIUM

Suggested Fix

Update the documentation to align the description with the code examples. Either state that metrics are sent via SentrySdk.Experimental.Metrics or clarify the relationship between SentrySdk.Experimental.Metrics and SentryMetricEmitter.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: platform-includes/metrics/usage/dotnet.mdx#L3

Potential issue: The documentation states that "The `SentryMetricEmitter` type exposes
three method groups," but the code examples show calls to
`SentrySdk.Experimental.Metrics.EmitCounter(...)`. This discrepancy is misleading
because the type `SentryMetricEmitter` never appears in the example code. A developer
following the text might search for `SentryMetricEmitter` and be unable to find it, or
try to instantiate it, leading to confusion. This is inconsistent with other Sentry SDKs
where the documented component name matches the code path used in examples.

Copy link
Member Author

Choose a reason for hiding this comment

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

But it is consistent with Logs for .NET:

Once the feature is enabled on the SDK and the SDK is initialized, you can send logs using the `SentrySdk.Logger` APIs.
The `SentryStructuredLogger` type exposes six method groups that you can use to log messages at different log levels: `Trace`, `Debug`, `Info`, `Warning`, `Error`, and `Fatal`.

And the type name is indeed SentryMetricEmitter.
And the SDK-API (SentrySdk.Experimental.Metrics) is mentioned just the line above.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's fine for the property to have a more user friendly and less descriptive name than the type. People will use SentrySdk.Experimental.Metrics to get at the metrics functionality for the SDK... under the hood, we handle that by emitting metrics for them, but the shorter and more generic Metrics name is going to be easier for SDK users to work with.


All methods are generic, where the provided type argument defines the numeric value type that the metric is emitted with.
The supported numeric types are `byte`, `short`, `int`, `long`, `float`, and `double`.

### Emit a Counter

Counters are one of the more basic types of metrics and can be used to count certain event occurrences.

To emit a counter, do the following:

```csharp
// Record five total button clicks
SentrySdk.Experimental.Metrics.EmitCounter("button_click", 5,
[new KeyValuePair<string, object>("browser", "Firefox"), new KeyValuePair<string, object>("app_version", "1.0.0")]);
```

### Emit a Distribution

Distributions help you get the most insights from your data by allowing you to obtain aggregations such as `p90`, `min`, `max`, and `avg`.

To emit a distribution, do the following:

```csharp
// Add '15.0' to a distribution used for tracking the loading times per page.
SentrySdk.Experimental.Metrics.EmitDistribution("page_load", 15.0, MeasurementUnit.Duration.Millisecond,
[new KeyValuePair<string, object>("page", "/home")]);
```

### Emit a Gauge

Gauges let you obtain aggregates like `min`, `max`, `avg`, `sum`, and `count`. They can be represented in a more space-efficient way than distributions, but they can't be used to get percentiles. If percentiles aren't important to you, we recommend using gauges.

To emit a gauge, do the following:

```csharp
// Add '15.0' to a gauge used for tracking the loading times for a page.
SentrySdk.Experimental.Metrics.EmitGauge("page_load", 15.0, MeasurementUnit.Duration.Millisecond,
[new KeyValuePair<string, object>("page", "/home")]);
```