Skip to content

Commit

Permalink
Adds support for counting
Browse files Browse the repository at this point in the history
Library has always had support for 'timing' subevents (i.e. granular timings [e.g. database query duration]), but didn't have support for simply counting (e.g. number of roundtrips to database)
  • Loading branch information
chris-peterson committed Oct 18, 2019
1 parent 27a48b4 commit 8ec62fd
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
[Bb]in/
[Oo]bj/

.idea/

# mstest test results
TestResults

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A structured logging framework for .NET that supports log aggregation, e.g. Splunk.

Handled over **155,000,000,000** production requests (and counting!)
Handled over **219,000,000,000** production requests (and counting!)

## Status

Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 5.1.{build}
version: 5.2.{build}
image: Visual Studio 2017
build_script:
- ps: >-
Expand Down
37 changes: 37 additions & 0 deletions src/Spiffy.Monitoring/EventContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ Assembly AssemblyFor<T>()
public Level Level { get; private set; }

readonly Dictionary<string, object> _values = new Dictionary<string, object>();
readonly Dictionary<string, uint> _counts = new Dictionary<string, uint>();
readonly Dictionary<string, AutoTimer> _timers = new Dictionary<string, AutoTimer>();

readonly object _valuesSyncObject = new object();
readonly object _countsSyncObject = new object();
readonly object _timersSyncObject = new object();

readonly DateTime _timestamp;
Expand All @@ -105,6 +107,21 @@ public IDisposable Time(string key)
return timer;
}

public void Count(string key)
{
lock (_countsSyncObject)
{
if (_counts.ContainsKey(key))
{
_counts[key]++;
}
else
{
_counts[key] = 1;
}
}
}

public object this[string key]
{
get
Expand Down Expand Up @@ -236,6 +253,11 @@ private string GetFormattedMessage()
kvp => GetValue(kvp.Value));
}

foreach (var kvp in GetCountValues())
{
kvps.Add(kvp.Key, kvp.Value);
}

foreach (var kvp in GetTimeValues())
{
kvps.Add(kvp.Key, kvp.Value);
Expand Down Expand Up @@ -333,6 +355,21 @@ private string GetSplunkFormattedTime()
return _timestamp.ToString("yyyy-MM-dd HH:mm:ss.fffK").WrappedInBrackets();
}

private IEnumerable<KeyValuePair<string, string>> GetCountValues()
{
var counts = new Dictionary<string, string>();

lock (_countsSyncObject)
{
foreach (var kvp in _counts)
{
counts[$"{kvp.Key}"] = kvp.Value.ToString();
}
}

return counts;
}

private IEnumerable<KeyValuePair<string, string>> GetTimeValues()
{
var times = new Dictionary<string, string>();
Expand Down
8 changes: 4 additions & 4 deletions src/Spiffy.Monitoring/Spiffy.Monitoring.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Copyright>2014-2018</Copyright>
<Copyright>2014-2019</Copyright>
<Authors>Chris Peterson</Authors>
<Description>A monitoring framework for .NET that supports IoC and modern targets, e.g. Splunk</Description>
<TargetFrameworks>netstandard1.3;net40</TargetFrameworks>
Expand All @@ -10,15 +10,15 @@
<PackageId>Spiffy.Monitoring</PackageId>
<PackageTags>monitoring;eventcontext;logging;structured logging;nlog;splunk</PackageTags>
<PackageReleaseNotes>
Bug fixes:
New features:

- https://github.com/chris-peterson/spiffy/pull/22
- Support for counting subevents (EventContext.Count)
</PackageReleaseNotes>
<PackageProjectUrl>http://github.com/chris-peterson/spiffy</PackageProjectUrl>
<PackageLicenseUrl>http://opensource.org/licenses/MIT</PackageLicenseUrl>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.3' ">1.6.0</NetStandardImplicitPackageVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>5.1.2</Version>
<Version>5.2.0</Version>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
Expand Down
29 changes: 29 additions & 0 deletions tests/UnitTests/EventContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public void Can_add_multiple_values_via_enumerable()
Then(The_context_contains_key_value_pairs);
}

[Scenario]
public void Can_count()
{
Given(An_event_context);
When(Adding_counts);
Then(The_context_contains_counts);
}

[Scenario]
public void Can_include_exception()
{
Expand Down Expand Up @@ -164,6 +172,19 @@ void The_context_contains_key_value_pairs()
context["key2"].Should().Be("value2");
}

void The_context_contains_counts()
{
string result = string.Empty;
var context = (EventContext) Context.EventContext;

Behavior.UseCustomLogging((level, msg) =>
result = msg);

context.Dispose();

result.Contains("foo=1").Should().BeTrue();
result.Contains("bar=2").Should().BeTrue();
}
void Including_an_exception()
{
((EventContext) Context.EventContext).IncludeException(new NullReferenceException());
Expand Down Expand Up @@ -209,6 +230,14 @@ void The_context_contains_structure_data()
context["Prefix_Data2"].Should().Be("foo");
}

void Adding_counts()
{
var context = (EventContext) Context.EventContext;
context.Count("foo");
context.Count("bar");
context.Count("bar");
}

private class NewlineRemovalContext : IDisposable
{
private readonly bool _oldValue;
Expand Down

0 comments on commit 8ec62fd

Please sign in to comment.