DogStatsD-CSharp-Client 9.1.0

dotnet add package DogStatsD-CSharp-Client --version 9.1.0
                    
NuGet\Install-Package DogStatsD-CSharp-Client -Version 9.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DogStatsD-CSharp-Client" Version="9.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DogStatsD-CSharp-Client" Version="9.1.0" />
                    
Directory.Packages.props
<PackageReference Include="DogStatsD-CSharp-Client" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DogStatsD-CSharp-Client --version 9.1.0
                    
#r "nuget: DogStatsD-CSharp-Client, 9.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DogStatsD-CSharp-Client@9.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DogStatsD-CSharp-Client&version=9.1.0
                    
Install as a Cake Addin
#tool nuget:?package=DogStatsD-CSharp-Client&version=9.1.0
                    
Install as a Cake Tool

DogStatsD client library for .NET

Build status

A .NET DogStatsD client. DogStatsD is an extension of the StatsD metric server for Datadog.

See CHANGELOG for details.

Installation

Grab the package from NuGet, or get the source from here and build it yourself.

Platforms

DogStatsD-CSharp-Client supports the following platforms:

  • .NET Standard 2.0 or greater
  • .NET Core 2.0 or greater
  • .NET Framework 4.6.1 or greater

Configuration

During application startup, configure an instance of DogStatsdService class like this:

// The code is located under the StatsdClient namespace
using StatsdClient;

// ...

var dogstatsdConfig = new StatsdConfig
{
    StatsdServerName = "127.0.0.1",
    StatsdPort = 8125,
};

using (var service = new DogStatsdService())
{
    if (!service.Configure(dogstatsdConfig))
    {
        throw new InvalidOperationException("Cannot initialize DogStatsD. Set optionalExceptionHandler argument in the `Configure` method for more information.");
    }
}

See the full list of available DogStatsD Client instantiation parameters.

Supported environment variables:

  • The client can use the DD_AGENT_HOST and (optionally) the DD_DOGSTATSD_PORT environment variables to build the target address if the StatsdServerName and/or StatsdPort parameters are empty.
  • If the DD_ENTITY_ID environment variable is found, its value will be injected as a global dd.internal.entity_id tag. This tag will be used by the Datadog Agent to insert container tags to the metrics.

Where StatsdServerName is the hostname or address of the StatsD server, StatsdPort is the optional DogStatsD port number, and Prefix is an optional string that is prepended to all metrics.

Usage via the DogStatsdService class or the static DogStatsd class

For usage of DogStatsD metrics, events, and Service Checks the Agent must be running and available.

Here is an example to submit different kinds of metrics with DogStatsdService.

// The code is located under the StatsdClient namespace
using StatsdClient;

// ...

var dogstatsdConfig = new StatsdConfig
{
    StatsdServerName = "127.0.0.1",
    StatsdPort = 8125,
};

using (var service = new DogStatsdService())
{
    if (!service.Configure(dogstatsdConfig))
        throw new InvalidOperationException("Cannot initialize DogStatsD. Set optionalExceptionHandler argument in the `Configure` method for more information.");

    service.Increment("example_metric.increment", tags: new[] { "environment:dev" });
    service.Decrement("example_metric.decrement", tags: new[] { "environment:dev" });
    service.Counter("example_metric.count", 2, tags: new[] { "environment:dev" });

    var random = new Random(0);

    for (int i = 0; i < 10; i++)
    {
        service.Gauge("example_metric.gauge", i, tags: new[] { "environment:dev" });
        service.Set("example_metric.set", i, tags: new[] { "environment:dev" });
        service.Histogram("example_metric.histogram", random.Next(20), tags: new[] { "environment:dev" });
        System.Threading.Thread.Sleep(random.Next(10000));
    }
}

Here is another example to submit different kinds of metrics with DogStatsd.

// The code is located under the StatsdClient namespace
using StatsdClient;

// ...

var dogstatsdConfig = new StatsdConfig
{
    StatsdServerName = "127.0.0.1",
    StatsdPort = 8125,
};

if (!DogStatsd.Configure(dogstatsdConfig))
{
    throw new InvalidOperationException("Cannot initialize DogStatsD. Set optionalExceptionHandler argument in the `Configure` method for more information.");
}

DogStatsd.Increment("example_metric.increment", tags: new[] { "environment:dev" });
DogStatsd.Decrement("example_metric.decrement", tags: new[] { "environment:dev" });
DogStatsd.Counter("example_metric.count", 2, tags: new[] { "environment:dev" });

var random = new Random(0);

for (int i = 0; i < 10; i++)
{
    DogStatsd.Gauge("example_metric.gauge", i, tags: new[] { "environment:dev" });
    DogStatsd.Set("example_metric.set", i, tags: new[] { "environment:dev" });
    DogStatsd.Histogram("example_metric.histogram", random.Next(20), tags: new[] { "environment:dev" });
    System.Threading.Thread.Sleep(random.Next(10000));
}

DogStatsd.Dispose(); // Flush all metrics not yet sent

Metrics

After the client is created, you can start sending custom metrics to Datadog. See the dedicated Metric Submission: DogStatsD documentation to see how to submit all supported metric types to Datadog with working code examples:

Some options are supported when submitting metrics, like applying a Sample Rate to your metrics or Tagging your metrics with your custom Tags.

Events

After the client is created, you can start sending events to your Datadog Event Stream. See the dedicated Event Submission: DogStatsD documentation to see how to submit an event to Datadog Event Stream.

Service Checks

After the client is created, you can start sending Service Checks to Datadog. See the dedicated Service Check Submission: DogStatsD documentation to see how to submit a Service Check to Datadog.

Usage via the Statsd class

Statsd has been removed in v6.0.0 because it is not thread safe and not efficient. Use DogStatsdService or DogStatsd instead:

  • Methods from DogStatsdService and DogStatsd do not block when called except for Flush and Dispose.
  • DogStatsdService and DogStatsd batch automatically several metrics in one datagram.

Unix domain socket support

The version 6 (and above) of the Agent accepts packets through a Unix Socket datagram connection. Details about the advantages of using UDS over UDP are available in the Datadog DogStatsD Unix Socket documentation.

You can use unix domain socket protocol by setting StatsdServerName property to unix://YOUR_FULL_PATH, for example unix:///tmp/dsd.socket. Note that there are three / as the path of the socket is /tmp/dsd.socket.

var dogstatsdConfig = new StatsdConfig
{
    StatsdServerName = "unix:///tmp/dsd.socket"
};

The property StatsdMaxUnixDomainSocketPacketSize of StatsdConfig defines the maximum size of the payload. Values higher than 8196 bytes are ignored.

The feature is not supported on Windows platform. Windows has support for Unix Domain Sockets, but not for Unix Domain Sockets of type Dgram (SocketType.Dgram).

On MacOS Mojave, setting more than 2048 bytes for StatsdMaxUnixDomainSocketPacketSize is experimental.

Client-side aggregation

By default, metrics for basic types (gauges, counts, sets) are aggregated before they are sent. For example, instead of sending my_metric:10|c|#tag1:value 3 times, the DogStatsD client sends my_metric:30|c|#tag1:value once. For more technical details about how client-side aggregation works see PR #134.

Enabling client-side aggregation has the benefit of reducing network usage and reducing the load for the DogStatsD server in the Datadog Agent.

When an application sends a lot of different contexts but each context appears with a very low frequency, enabling client-side aggregation may take more memory and more CPU. A context identifies a metric name, a tag set, and a metric type. The metric datadog.dogstatsd.client.aggregated_context reported by the DogStatsD .NET client counts the number of contexts in memory used for client-side aggregation. There is also the metric datadog.dogstatsd.client.metrics_by_type that represents the number of metrics submitted by the client before aggregation.

Configuration

The aggregation window is two seconds by default and can be changed using the FlushInterval. Note that the aggregation window on the Agent side is 10 seconds for DogStatsD metrics. For example, setting an aggregation window of 3s in the client produces a spike in your dashboard every 30s for counts metrics (as the third 10s bucket on the Agent is received 4 samples from the client).

To disable client-side aggregation set ClientSideAggregation to null.

Testing

  1. Restore packages
dotnet restore
  1. Run the tests
dotnet test tests/StatsdClient.Tests/

Feedback

To suggest a feature, report a bug, or general discussion, create a new issue in the GitHub repo.

Credits

dogstatsd-csharp-client is forked from Goncalo Pereira's original StatsD client.

Copyright (c) 2012 Goncalo Pereira and all contributors. See LICENSE.md for further details.

Thanks to Goncalo Pereira, Anthony Steele, Darrell Mozingo, Antony Denyer, and Tim Skauge for their contributions to the original client.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 is compatible. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.1

  • .NETFramework 4.6.1

  • .NETStandard 2.0

  • net6.0

NuGet packages (30)

Showing the top 5 NuGet packages that depend on DogStatsD-CSharp-Client:

Package Downloads
AspNetCore.HealthChecks.Publisher.Datadog

HealthChecks.Publisher.Datadog is the health check publisher for Datadog.

Serilog.Sinks.Datadog

Package Description

Akka.Monitoring.Datadog

Akka.Monitoring extension for reporting to Datadog

Ubiquitous.Metrics.Dogstatsd

Package Description

CL.Monitoring

Package Description

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on DogStatsD-CSharp-Client:

Repository Stars
Xabaril/AspNetCore.Diagnostics.HealthChecks
Enterprise HealthChecks for ASP.NET Core Diagnostics Package
PiotrJustyna/road-to-orleans
This repository illustrates the road to orleans with practical, real-life examples. From most basic, to more advanced techniques.
veldtech/miki-bot
Miki Discord Bot
Particular/docs.particular.net
All content for ParticularDocs
Version Downloads Last Updated
9.1.0 427 4/28/2026
9.0.0 1,882,671 8/18/2025
8.0.0 18,000,499 10/27/2022
7.0.1 2,028,808 6/28/2022
7.0.0 15,690,475 10/13/2021
6.0.0 8,225,541 11/23/2020
5.1.0 1,713,744 9/8/2020
5.0.2 1,496,187 5/29/2020
5.0.1 22,653 5/19/2020
5.0.0 35,915 5/13/2020
4.0.1 3,683,411 2/11/2020
4.0.0 1,576,340 1/3/2020
3.4.0 413,779 11/15/2019
3.3.0 3,131,099 4/5/2019
3.2.0 1,640,205 10/18/2018
3.1.0 2,142,269 11/16/2017
3.0.0 817,351 10/31/2016
2.2.1 77,974 10/13/2016
2.2.0 45,364 8/8/2016
Loading failed