Set Up Metrics for UnityNEW
Metrics allow you to send, view and query counters, gauges and distributions from your Unity game to track application health and drill down into related traces, logs, and errors.
With Sentry's Application Metrics, you can send counters, gauges, and distributions from your Unity game to Sentry. Once in Sentry, these metrics can be viewed alongside relevant errors, and searched using their individual attributes.
Metrics for Unity are supported in Sentry SDK version 4.2.0 and above.
Metrics are enabled by default. Once the SDK is initialized, no additional setup is required before you can start emitting metrics.
To turn metrics off, uncheck Enable Metrics under Tools > Sentry > Advanced > Metrics in the editor, or set EnableMetrics to false programmatically. See Options below.
Once you initialize the SDK, you can send metrics using the SentrySdk.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:
// Record five total player interactions
SentrySdk.Metrics.EmitCounter("player_interaction", 5,
new Dictionary<string, object> { ["scene"] = "MainMenu", ["app_version"] = "1.0.0" });
// Record five total player interactions
SentrySdk.Metrics.EmitCounter("player_interaction", 5,
new Dictionary<string, object> { ["scene"] = "MainMenu", ["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:
// Add '15.0' to a distribution used for tracking the loading times per scene.
SentrySdk.Metrics.EmitDistribution("scene_load", 15.0, MeasurementUnit.Duration.Millisecond,
new Dictionary<string, object> { ["scene"] = "Level1" });
// Add '15.0' to a distribution used for tracking the loading times per scene.
SentrySdk.Metrics.EmitDistribution("scene_load", 15.0, MeasurementUnit.Duration.Millisecond,
new Dictionary<string, object> { ["scene"] = "Level1" });
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:
// Add '15.0' to a gauge used for tracking the loading times for a scene.
SentrySdk.Metrics.EmitGauge("scene_load", 15.0, MeasurementUnit.Duration.Millisecond,
new Dictionary<string, object> { ["scene"] = "Level1" });
// Add '15.0' to a gauge used for tracking the loading times for a scene.
SentrySdk.Metrics.EmitGauge("scene_load", 15.0, MeasurementUnit.Duration.Millisecond,
new Dictionary<string, object> { ["scene"] = "Level1" });
Attributes let you slice a metric by things like the active scene, level, or platform in Sentry, instead of emitting a separate metric per variant. Every Emit method accepts a collection of KeyValuePair<string, object> as attributes, so any Dictionary<string, object> or array works:
var attributes = new Dictionary<string, object>
{
["scene"] = SceneManager.GetActiveScene().name,
["level"] = 42,
["is_premium"] = true,
};
SentrySdk.Metrics.EmitCounter("enemy_defeated", 1, attributes);
SentrySdk.Metrics.EmitDistribution("scene_load", 15.0, MeasurementUnit.Duration.Millisecond, attributes);
SentrySdk.Metrics.EmitGauge("active_players", 12, MeasurementUnit.None, attributes);
var attributes = new Dictionary<string, object>
{
["scene"] = SceneManager.GetActiveScene().name,
["level"] = 42,
["is_premium"] = true,
};
SentrySdk.Metrics.EmitCounter("enemy_defeated", 1, attributes);
SentrySdk.Metrics.EmitDistribution("scene_load", 15.0, MeasurementUnit.Duration.Millisecond, attributes);
SentrySdk.Metrics.EmitGauge("active_players", 12, MeasurementUnit.None, attributes);
Supported attribute types are string, char, bool, integers up to 64-bit signed, and floating-point numbers up to 64-bit. Other types are converted to string via ToString().
To attach an attribute to every metric the SDK emits, set it once in the SetBeforeSendMetric callback described under Options instead of repeating it at each call site.
Metrics are enabled by default. Set to false to stop the SDK from sending metrics emitted through the SentrySdk.Metrics APIs. You can also toggle this in the editor under Tools > Sentry > Advanced > Metrics.
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.
public override void Configure(SentryUnityOptions options)
{
options.SetBeforeSendMetric(static metric =>
{
if (metric.Name == "removed-metric")
{
return null;
}
metric.SetAttribute("extra", "foo");
return metric;
});
}
public override void Configure(SentryUnityOptions options)
{
options.SetBeforeSendMetric(static metric =>
{
if (metric.Name == "removed-metric")
{
return null;
}
metric.SetAttribute("extra", "foo");
return metric;
});
}
or if you're manually initializing the SDK:
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.SetBeforeSendMetric(static metric =>
{
if (metric.Name == "removed-metric")
{
return null;
}
metric.SetAttribute("extra", "foo");
return metric;
});
});
SentrySdk.Init(options =>
{
options.Dsn = "___PUBLIC_DSN___";
options.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, char, 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 Sentry SDK for Unity 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 assentry.environment.release: The release set in the SDK if defined. This is sent from the SDK assentry.release.sdk.name: The name of the SDK that sent the metric. This is sent from the SDK assentry.sdk.name.sdk.version: The version of the SDK that sent the metric. This is sent from the SDK assentry.sdk.version.
server.address: The address of the server that sent the metric. Equivalent toserver_namethat 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.
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").