|
| 1 | +#### EnableMetrics |
| 2 | + |
| 3 | +Set to `false` in order to disable the `SentrySdk.Experimental.Metrics` APIs. |
| 4 | + |
| 5 | +#### SetBeforeSendMetric |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +```csharp |
| 10 | +public override void Configure(SentryUnityOptions options) |
| 11 | +{ |
| 12 | + options.Experimental.SetBeforeSendMetric(static metric => |
| 13 | + { |
| 14 | + if (metric.Name == "removed-metric") |
| 15 | + { |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
| 19 | + metric.SetAttribute("extra", "foo"); |
| 20 | + |
| 21 | + return metric; |
| 22 | + }); |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +or if you're manually initializing the SDK: |
| 27 | + |
| 28 | +```csharp |
| 29 | +SentrySdk.Init(options => |
| 30 | +{ |
| 31 | + options.Dsn = "___PUBLIC_DSN___"; |
| 32 | + options.Experimental.SetBeforeSendMetric(static metric => |
| 33 | + { |
| 34 | + if (metric.Name == "removed-metric") |
| 35 | + { |
| 36 | + return null; |
| 37 | + } |
| 38 | + |
| 39 | + metric.SetAttribute("extra", "foo"); |
| 40 | + |
| 41 | + return metric; |
| 42 | + }); |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +The metric object of type `SentryMetric` has the following members: |
| 49 | + |
| 50 | +| Member | Type | Description | |
| 51 | +|--------|------|-------------| |
| 52 | +| `Timestamp` | `DateTimeOffset` | Timestamp indicating when the metric was emitted. | |
| 53 | +| `TraceId` | `SentryId` | The trace ID of the trace this metric belongs to. | |
| 54 | +| `Type` | `SentryMetricType` | The type of metric. One of `Counter`, `Gauge`, `Distribution`. | |
| 55 | +| `Name` | `string` | The name of the metric. | |
| 56 | +| `SpanId` | `SpanId?` | The span ID of the span that was active when the metric was emitted. | |
| 57 | +| `Unit` | `string?` | The unit of measurement for the metric value. Applies to `Gauge` and `Distribution` only. | |
| 58 | +| `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`. | |
| 59 | +| `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`. | |
| 60 | +| `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. | |
| 61 | + |
| 62 | +The numeric value of `SentryMetric` has the same numeric type that the metric was emitted with. |
| 63 | +The supported numeric types are `byte`, `short`, `int`, `long`, `float`, and `double`. |
0 commit comments