-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OpenTelemetry library and expose metrics for Witsml requests (#2291)
- Loading branch information
Showing
7 changed files
with
204 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace Witsml.Metrics; | ||
|
||
public enum WitsmlMethod | ||
{ | ||
GetFromStore, | ||
AddToStore, | ||
UpdateInStore, | ||
DeleteFromStore, | ||
GetCap, | ||
GetVersion | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.Metrics; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
|
||
using Serilog; | ||
|
||
using Witsml.ServiceReference; | ||
|
||
namespace Witsml.Metrics; | ||
|
||
internal sealed class WitsmlMetrics | ||
{ | ||
#region Singleton | ||
// We still want to collect unified metrics in case there are multiple WitsmlClient instances used | ||
|
||
private static readonly Lazy<WitsmlMetrics> LazyInstance = | ||
new(() => new WitsmlMetrics()); | ||
|
||
public static WitsmlMetrics Instance { get { return LazyInstance.Value; } } | ||
|
||
private WitsmlMetrics() { } | ||
|
||
#endregion | ||
|
||
private static readonly AssemblyName AssemblyName = | ||
typeof(WitsmlMetrics).Assembly.GetName(); | ||
|
||
internal static readonly string MeterName = AssemblyName.Name; | ||
|
||
private static readonly Meter MeterInstance = new(MeterName, AssemblyName.Version!.ToString()); | ||
|
||
private readonly Histogram<long> _requestDuration = MeterInstance.CreateHistogram<long>( | ||
"witsml.requests.duration", | ||
unit: "s", | ||
description: "Time spent during requests to a Witsml server"); | ||
|
||
private readonly UpDownCounter<int> _activeRequests = | ||
MeterInstance.CreateUpDownCounter<int>( | ||
"witsml.requests.active", | ||
description: "Number of active requests"); | ||
|
||
internal async Task<TResponseType> MeasureQuery<TResponseType>(Uri serverUri, WitsmlMethod method, string witsmlType, Task<TResponseType> wmlsTask) | ||
where TResponseType : IWitsmlResponse | ||
{ | ||
var tagList = new TagList | ||
{ | ||
{ "host", serverUri.Host }, | ||
{ "method", Enum.GetName(method) }, | ||
{ "objectType", witsmlType }, | ||
}; | ||
|
||
Stopwatch timer = null; | ||
TResponseType response; | ||
try | ||
{ | ||
_activeRequests.Add(1, tagList); | ||
timer = Stopwatch.StartNew(); | ||
response = await wmlsTask; | ||
} | ||
finally | ||
{ | ||
timer?.Stop(); | ||
_activeRequests.Add(-1, tagList); | ||
} | ||
|
||
tagList.Add("resultCode", response.GetResultCode()); | ||
|
||
var elapsedSeconds = timer.ElapsedMilliseconds / 1000; | ||
_requestDuration.Record(elapsedSeconds, tagList); | ||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using OpenTelemetry.Metrics; | ||
|
||
namespace Witsml.Metrics; | ||
|
||
public static class WitsmlMetricsExtensions | ||
{ | ||
// ReSharper disable once UnusedMember.Global (For usage by external applications) | ||
public static MeterProviderBuilder AddWitsmlInstrumentation(this MeterProviderBuilder builder) | ||
{ | ||
builder.AddMeter(WitsmlMetrics.MeterName); | ||
builder.AddView("witsml.requests.duration", new ExplicitBucketHistogramConfiguration() | ||
{ | ||
Boundaries = [0.5, 1, 3, 5, 10, 15, 30, 60] | ||
}); | ||
return builder; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,44 @@ | ||
// ReSharper disable ClassNeverInstantiated.Global | ||
// ReSharper disable InconsistentNaming | ||
namespace Witsml.ServiceReference | ||
{ | ||
// ReSharper disable once ClassNeverInstantiated.Global | ||
// ReSharper disable once InconsistentNaming | ||
public partial class WMLS_GetFromStoreResponse | ||
internal interface IWitsmlResponse | ||
{ | ||
public string GetResultCode(); | ||
} | ||
|
||
public partial class WMLS_GetFromStoreResponse : IWitsmlResponse | ||
{ | ||
public string GetResultCode() => Result.ToString(); | ||
|
||
public override string ToString() | ||
{ | ||
return $"Result: {Result}\nXMLout: {XMLout}\nSuppMsgOut: {SuppMsgOut}"; | ||
} | ||
} | ||
|
||
public partial class WMLS_AddToStoreResponse : IWitsmlResponse | ||
{ | ||
public string GetResultCode() => Result.ToString(); | ||
} | ||
|
||
public partial class WMLS_UpdateInStoreResponse : IWitsmlResponse | ||
{ | ||
public string GetResultCode() => Result.ToString(); | ||
} | ||
|
||
public partial class WMLS_DeleteFromStoreResponse : IWitsmlResponse | ||
{ | ||
public string GetResultCode() => Result.ToString(); | ||
} | ||
|
||
public partial class WMLS_GetCapResponse : IWitsmlResponse | ||
{ | ||
public string GetResultCode() => Result.ToString(); | ||
} | ||
|
||
public partial class WMLS_GetVersionResponse : IWitsmlResponse | ||
{ | ||
public string GetResultCode() => Result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters