Skip to content

Commit

Permalink
Added OpenTelemetry Trace support.
Browse files Browse the repository at this point in the history
  • Loading branch information
houseofcat committed Mar 27, 2024
1 parent f38b745 commit 87be2de
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/HouseofCat.Metrics/IMetricsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using HouseofCat.Utilities;
using System;
using System;
using System.Collections.Generic;

namespace HouseofCat.Metrics;

Expand All @@ -13,6 +13,6 @@ public interface IMetricsProvider
void ObserveValueFluctuation(string name, double value, string unit = null, string description = null);
IDisposable Duration(string name, bool microScale = false, string unit = null, string description = null);
IDisposable Track(string name, string unit = null, string description = null);
MultiDispose TrackAndDuration(string name, bool microScale = false, string unit = null, string description = null);
MultiDispose Trace(string name, string unit = null, string description = null);
IDisposable TrackAndDuration(string name, bool microScale = false, string unit = null, string description = null, Dictionary<string, string> tags = null);
IDisposable Trace(string name, Dictionary<string, string> tags = null);
}
8 changes: 4 additions & 4 deletions src/HouseofCat.Metrics/NullMetricsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using HouseofCat.Utilities;
using System;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace HouseofCat.Metrics;
Expand Down Expand Up @@ -43,13 +43,13 @@ public IDisposable Track(string name, string unit = null, string description = n
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MultiDispose TrackAndDuration(string name, bool microScale = false, string unit = null, string description = null)
public IDisposable TrackAndDuration(string name, bool microScale = false, string unit = null, string description = null, Dictionary<string, string> tags = null)
{
return null;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MultiDispose Trace(string name, string unit = null, string description = null)
public IDisposable Trace(string name, Dictionary<string, string> tags = null)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using HouseofCat.Utilities;
using HouseofCat.Utilities.Errors;
using HouseofCat.Utilities.Errors;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Runtime.CompilerServices;

Expand All @@ -10,17 +11,17 @@ namespace HouseofCat.Metrics;
public class OpenTelemetryMetricsProvider : IMetricsProvider, IDisposable
{
private readonly IMeterFactory _factory;

private readonly Meter _meter;
private readonly string _meterName;
private readonly ActivitySource _activitySource;

public ConcurrentDictionary<string, object> Counters { get; } = new ConcurrentDictionary<string, object>();
public ConcurrentDictionary<string, object> Gauges { get; } = new ConcurrentDictionary<string, object>();
public ConcurrentDictionary<string, object> Histograms { get; } = new ConcurrentDictionary<string, object>();

private bool _disposedValue;

public OpenTelemetryMetricsProvider(IMeterFactory meterFactory, string meterName)
public OpenTelemetryMetricsProvider(IMeterFactory meterFactory, string meterName, string activitySourceName)
{
Guard.AgainstNull(meterFactory, nameof(meterFactory));
Guard.AgainstNullOrEmpty(meterName, nameof(meterName));
Expand All @@ -29,6 +30,7 @@ public OpenTelemetryMetricsProvider(IMeterFactory meterFactory, string meterName
_meterName = meterName;

_meter = _factory.Create(_meterName);
_activitySource = new ActivitySource(activitySourceName ?? "HouseofCat.Metrics");
}

public Counter<T> GetOrAddCounter<T>(string name, string unit = null, string description = null) where T : struct
Expand Down Expand Up @@ -136,15 +138,36 @@ public IDisposable Track(string name, string unit = null, string description = n
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MultiDispose TrackAndDuration(string name, bool microScale = false, string unit = null, string description = null)
public IDisposable TrackAndDuration(
string name,
bool microScale = false,
string unit = null,
string description = null,
Dictionary<string, string> tags = null)
{
return null;
return GetActivity(name, tags);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MultiDispose Trace(string name, string unit = null, string description = null)
public IDisposable Trace(string name, Dictionary<string, string> tags = null)
{
return null;
return GetActivity(name, tags);
}

private Activity GetActivity(string name, Dictionary<string, string> tags)
{
var activity = _activitySource.StartActivity(name);
if (activity is not null
&& tags is not null
&& activity.IsAllDataRequested)
{
foreach (var tag in tags)
{
activity.SetTag(tag.Key, tag.Value);
}
}

return activity;
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Prometheus.DotNetRuntime.Metrics.Producers;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace HouseofCat.Metrics;
Expand Down Expand Up @@ -185,14 +186,14 @@ public IDisposable Track(string name, string unit = null, string description = n
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public MultiDispose TrackAndDuration(string name, bool microScale = false, string unit = null, string description = null)
public IDisposable TrackAndDuration(string name, bool microScale = false, string unit = null, string description = null, Dictionary<string, string> tags = null)
{
var duration = Duration(name, microScale, description: description);
var track = Track(name, description: description);
return new MultiDispose(duration, track);
}

public MultiDispose Trace(string name, string unit = null, string description = null)
public IDisposable Trace(string name, Dictionary<string, string> tags = null)
{
return null;
}
Expand Down
6 changes: 3 additions & 3 deletions version.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<Version>3.1.3</Version>
<AssemblyVersion>3.1.3</AssemblyVersion>
<FileVersion>3.1.3</FileVersion>
<Version>3.2.0</Version>
<AssemblyVersion>3.2.0</AssemblyVersion>
<FileVersion>3.2.0</FileVersion>
</PropertyGroup>
</Project>

0 comments on commit 87be2de

Please sign in to comment.