Audiu.YetAnotherStatsDClient 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Audiu.YetAnotherStatsDClient --version 1.0.0
NuGet\Install-Package Audiu.YetAnotherStatsDClient -Version 1.0.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="Audiu.YetAnotherStatsDClient" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Audiu.YetAnotherStatsDClient --version 1.0.0
#r "nuget: Audiu.YetAnotherStatsDClient, 1.0.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.
// Install Audiu.YetAnotherStatsDClient as a Cake Addin
#addin nuget:?package=Audiu.YetAnotherStatsDClient&version=1.0.0

// Install Audiu.YetAnotherStatsDClient as a Cake Tool
#tool nuget:?package=Audiu.YetAnotherStatsDClient&version=1.0.0

YetAnotherStatsDClient

Yet Another StatsD .NET Client

What is this?

As described, this is Yet Another StatsD .NET Client.

Why? At Audiu and RepostExchange, our infrastructure is spread far and wide. This gives us an unfortunate situation where we end up with UDP packets with statistics being sent over the internet between datacenters.

There are a few options on the table, such as using relays within the DCs to pass the statistics on, but for us, I chucked together a version which uses a TCP connection on its own thread, and stats are buffered in there, and sent as and when they can.

If there is a disruption, when it reconnects it will drop all the previous statistics on the floor.

It's designed to be used within DI containers (no statics!)

How do we use it?

This is a quick example of our setup using the wonderful SimpleInjector library

var statsdHostName = ConfigurationManager.AppSettings["statsd.hostname"];
var statsdPort = int.Parse(ConfigurationManager.AppSettings["statsd.port"]);
var statsdPrefix = ConfigurationManager.AppSettings["statsd.prefix"];

var statsClient = new StatsClient(
    new StatsConfig
    {
        Host = statsdHostName,
        Port = statsdPort,
        Prefix = statsdPrefix
    });

var statsNoOpClient = new StatsNoOpClient();

container.Register<IStatsClient>(
    () =>
    {
        // We use feature flags, but feel free just to register statsClient as a singleton
        if (iShouldBeUsingStatsD) 
        {
            return statsClient;
        }

        return statsNoOpClient;

    }, Lifestyle.Scoped);

Inject IStatsClient wherever you might need it

Then invoke something like:

_statsClient.Count("mycounter.count");
_statsClient.Count("mycounter.count", 10);
_statsClient.Gauge("mygauge.value", 10.25);

var timer = Stopwatch.StartNew();
// Do something
timer.Stop();
_statsClient.Timing("somefunction.timing", timer.ElapsedMilliseconds);

Using disposible timers is also super useful:

using (_statsClient.StartTimer("somefunction.timing")) 
{
    myFunc();
}

And functional styles in sync and async style:

_statsClient.Time("somefunction.timing", t => myFunc());

var myValue = _statsClient.Time("somefunction.timing", t => myFunc());

await _statsClient.Time("asyncfunction.timing", async t => await myAsyncFunc());

var myValue = await _statsClient.Time("asyncfunction.timing", async t => await myAsyncFunc());

Credits

I have taken bits of code from the following great OS libs: statsd-csharp-client, JustEat.StatsD and StatsN

Product Compatible and additional computed target framework versions.
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 26,094 8/31/2018
1.0.1 928 3/19/2018
1.0.0 1,022 1/14/2018

Initial relase