- Why did you create yet another statsd client especially for Splunk?
Splunk supports expanded statsd metrics. This library supports dimensions and sample rates.
The statsd bucket consists of 6 parts
- The first and second are the feature and action eg order-processor.orders, ingestion.entities
- The sixth is the event (and should be defined in the past as a fact) eg succeeded, failed, processed, etc
The following are examples of the full statsd string generated if you follow this guideline:
order-processor.orders.processed
ingestion.entities.handled
- Install the Nuget package Splunk.Metrics.Abstractions
Install-Package Splunk.Metrics.Abstractions
- Inject IStatsPublisher from the installed library and call the methods for counts, gauges and timing
public class Foo
{
Foo(IStatsPublisher statsPublisher)
{
this.statsPublisher = statsPublisher;
}
public async Task DoSomething()
{
await statsPublisher.IncrementAsync("order-processor.orders.processed");
}
}
- Install the Nuget package Splunk.Metrics.Statsd
Install-Package Splunk.Metrics.Statsd
- In your startup or bootstrapper:
serviceCollection.AddTransient<IStatsPublisher, StatsPublisher>();
serviceCollection.Configure<MetricsConfiguration>(configuration.GetSection("Stats"));
- In your appsettings file, define the namespace
"Stats": {
"Prefix": "some-product.some-service"
}
- Install the Nuget package Splunk.Metrics.Statsd
Install-Package Splunk.Metrics.Statsd
- In your startup or bootstrapper:
container.RegisterType<IStatsPublisher, StatsPublisher>();
container.RegisterInstance(new StatsConfiguration(...)));
- In your appsettings file, define the namespace
"Stats": {
"Prefix": "some-product.some-service"
}
- Install the Nuget package Splunk.Metrics.Http
Install-Package Splunk.Metrics.Http
- In your startup, add the HTTP metrics middleware to the OWIN pipeline. Note that this should be one of the first middlewares in the pipeline to effectively capture metrics for all requests:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHttpMetrics();
app.UseMvc();
}
You can optionally add dimensions to your http metrics from your controller without accessing the stats publisher as follows:
Request.HttpContext.SetDimensionForHttpMetrics("some-dimension", "some-dimension-value");
- Install the Nuget package Splunk.Metrics.WebApi
Install-Package Splunk.Metrics.WebApi
- In your startup, add the filter
HttpMetricsFilter
to the Http Configuration and the HTTP metrics middleware to the OWIN pipeline. Note that this should be one of the first middlewares in the pipeline to effectively capture metrics for all requests:
\\ Startup.cs
public void Configuration(IAppBuilder app)
{
var httpConfiguration = new HttpConfiguration();
httpConfiguration.Filters.Add(new HttpMetricsFilter());
\\ Additional config...
app.UseHttpMetrics(_statsConfiguration)
.UseWebApi(httpConfiguration);
}
You can optionally add dimensions to your http metrics from your controller without accessing the stats publisher as follows:
Request.GetOwinContext().SetDimensionForHttpMetrics("some-dimension", some-dimension-value);