|
| 1 | +--- |
| 2 | +title: "StatsD" |
| 3 | +draft: false |
| 4 | +weight: 4 |
| 5 | +icon: "/images/statsd.png" |
| 6 | +--- |
| 7 | + |
| 8 | +## StatsD |
| 9 | + |
| 10 | +The [App.Metrics.StatsD](https://www.nuget.org/packages/App.Metrics.StatsD/) nuget package reports metrics to [StatsD](https://github.com/statsd/statsd) using the [App.Metrics.Formatting.StatsD](https://www.nuget.org/packages/App.Metrics.Formatting.StatsD/) and [App.Metrics.Reporting.StatsD](https://www.nuget.org/packages/App.Metrics.Reporting.StatsD/) nuget packages to format and report metrics. |
| 11 | + |
| 12 | +### Getting started |
| 13 | + |
| 14 | +<i class="fa fa-hand-o-right"></i> To use the Datadog HTTP reporter, first install the [nuget package](https://www.nuget.org/packages/App.Metrics.Reporting.StatsD/): |
| 15 | + |
| 16 | +```console |
| 17 | +nuget install App.Metrics.StatsD |
| 18 | +``` |
| 19 | + |
| 20 | +<i class="fa fa-hand-o-right"></i> Then enable the reporter using `Report.ToStatsDTcp(...)`: |
| 21 | + |
| 22 | +```csharp |
| 23 | +var metrics = new MetricsBuilder() |
| 24 | + .Report.ToStatsDTcp(options => {}) |
| 25 | + .Build(); |
| 26 | +``` |
| 27 | + |
| 28 | +<i class="fa fa-hand-o-right"></i> or using `Report.ToStatsDUdp(...)`: |
| 29 | + |
| 30 | +```csharp |
| 31 | +var metrics = new MetricsBuilder() |
| 32 | + .Report.ToStatsDUdp(options => {}) |
| 33 | + .Build(); |
| 34 | +``` |
| 35 | + |
| 36 | +{{% notice info %}} |
| 37 | +<i class="fa fa-hand-o-right"></i> See [Reporting]({{< ref "web-monitoring/aspnet-core/reporting.md" >}}) for details on configuring metric report scheduling. |
| 38 | +{{% /notice %}} |
| 39 | + |
| 40 | +### Variants |
| 41 | + |
| 42 | +There are several flavors of StatsD available. `App.Metrics.StatsD` currently supports the original Etsy and DogStatsD variants of StatsD, DogStatsD is a StatsD variant that is used to report StatsD metrics to DataDog; by default, the Etsy variant is used. |
| 43 | + |
| 44 | +#### Supported Metrics |
| 45 | + |
| 46 | +- __Gauge__ metrics will be formatted as StatsD gauge "__g__" |
| 47 | +- __Count__ metrics will be formatted as StatsD count "__c__" |
| 48 | +- __Histogram__ metrics will be formatted as StatsD histogram "__h__" |
| 49 | +- __Meter__ metrics will be formatted as StatsD meter "__m__" |
| 50 | +- __Timer__ metrics will be formatted as StatsD timer "__ms__" |
| 51 | + |
| 52 | +{{% notice warning %}} |
| 53 | +<i class="fa fa-hand-o-right"></i> Timer metrics are not natively supported by DogStatsD. Replace timer metric with histogram if you have to send a timer-like StatsD metric to DogStatsD ingress endpoint. |
| 54 | +{{% /notice %}} |
| 55 | + |
| 56 | +#### Formatting |
| 57 | + |
| 58 | +To change the default formatter to DogStatsD, change the formatter in the options: |
| 59 | + |
| 60 | +```csharp |
| 61 | +var metrics = new MetricsBuilder() |
| 62 | + .Report.ToStatsDTcp( |
| 63 | + options => { |
| 64 | + options.StatsDOptions.MetricNameFormatter = new DefaultDogStatsDMetricStringSerializer(); |
| 65 | + }) |
| 66 | + .Build(); |
| 67 | +``` |
| 68 | + |
| 69 | +### Configuration |
| 70 | + |
| 71 | +Configuration options are provided as a setup action used with `ToStatsDTcp(...)` and `ToStatsDUdp(...)`. |
| 72 | + |
| 73 | +<i class="fa fa-hand-o-right"></i> To configure StatsD reporting options: |
| 74 | + |
| 75 | +```csharp |
| 76 | +var filter = new MetricsFilter().WhereType(MetricType.Timer); |
| 77 | +var metrics = new MetricsBuilder() |
| 78 | + .Report.ToStatsDTcp( |
| 79 | + options => { |
| 80 | + options.StatsDOptions.MetricNameFormatter = new DefaultStatsDMetricStringSerializer(); |
| 81 | + options.StatsDOptions.DefaultSampleRate = 1.0; |
| 82 | + options.StatsDOptions.TagMarker = '#'; |
| 83 | + options.SocketPolicy.BackoffPeriod = TimeSpan.FromSeconds(30); |
| 84 | + options.SocketPolicy.FailuresBeforeBackoff = 5; |
| 85 | + options.SocketPolicy.Timeout = TimeSpan.FromSeconds(10); |
| 86 | + options.SocketSettings.Address = "localhost"; |
| 87 | + options.SocketSettings.Port = 8125; |
| 88 | + options.Filter = filter; |
| 89 | + }) |
| 90 | + .Build(); |
| 91 | +``` |
| 92 | + |
| 93 | +<i class="fa fa-hand-o-right"></i> The configuration options provided are: |
| 94 | + |
| 95 | +|Option|Description| |
| 96 | +|------|:--------| |
| 97 | +|StatsDOptions.MetricNameFormatter|The metric payload formatter used when reporting StatsD metrics. There are 2 built-in formatter provided, `DefaultStatsDMetricStringSerializer` and `DefaultDogStatsDMetricStringSerializer` |
| 98 | +|StatsDOptions.DefaultSampleRate|The sample rate used to send metrics that supports them. |
| 99 | +|StatsDOptions.TagMarker|The character used to mark the start of tag section for reporters that supports metric tagging. |
| 100 | +|Filter|The filter used to filter metrics just for this reporter. |
| 101 | +|SocketPolicy.BackoffPeriod|The `TimeSpan` to back-off when metrics are failing to report to the metrics ingress endpoint. |
| 102 | +|SocketPolicy.FailuresBeforeBackoff|The number of failures before backing-off when metrics are failing to report to the metrics ingress endpoint. |
| 103 | +|SocketPolicy.Timeout|The socket timeout duration when attempting to report metrics to the metrics ingress endpoint. |
| 104 | +|SocketSettings.Address|The socket address of the metrics ingress endpoint. |
| 105 | +|SocketSettings.Port|The socket port of the metrics ingress endpoint. |
0 commit comments