Set Up Metrics

Metrics allow you to send, view and query counters, gauges and measurements sent from your applications within Sentry.

Sentry Agent Skills

Install Sentry's agent skills to teach your AI coding assistant how to set up metrics in your .NET application.

Copied
npx @sentry/dotagents add getsentry/sentry-agent-skills --name sentry-setup-metrics

See the full list of available skills and installation docs for more details.

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.

Metrics for .NET are supported in Sentry .NET SDK version 6.1.0 and above.

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.

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.

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:

Copied
// 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")]);

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:

Copied
// 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")]);

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:

Copied
// 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")]);

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

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.

Copied
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:

MemberTypeDescription
TimestampDateTimeOffsetTimestamp indicating when the metric was emitted.
TraceIdSentryIdThe trace ID of the trace this metric belongs to.
TypeSentryMetricTypeThe type of metric. One of Counter, Gauge, Distribution.
NamestringThe name of the metric.
SpanIdSpanId?The span ID of the span that was active when the metric was emitted.
Unitstring?The unit of measurement for the metric value. Applies to Gauge and Distribution only.
TryGetValue<TValue>(out TValue value)MethodGets 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)MethodGets 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)MethodSets 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.

The .NET SDK automatically sets several default attributes on all metrics to provide context and improve debugging:

  • environment: The environment set in the SDK if defined. This is sent from the SDK as sentry.environment.
  • release: The release set in the SDK if defined. This is sent from the SDK as sentry.release.
  • sdk.name: The name of the SDK that sent the metric. This is sent from the SDK as sentry.sdk.name.
  • sdk.version: The version of the SDK that sent the metric. This is sent from the SDK as sentry.sdk.version.

  • server.address: The address of the server that sent the metric. Equivalent to server_name that gets attached to Sentry errors.

If user information is available in the current scope, the following attributes are added to the metric:

  • user.id: The user ID.
  • user.name: The username.
  • user.email: The email address.
Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").