Skip to content

Commit

Permalink
remove duplicate checks sdk already does
Browse files Browse the repository at this point in the history
  • Loading branch information
mic-max committed Oct 13, 2022
1 parent 556b65e commit 71560ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,33 @@ internal sealed class EventCountersInstrumentationEventSource : EventSource
{
public static readonly EventCountersInstrumentationEventSource Log = new();

[Event(1, Level = EventLevel.Warning, Message = "Duplicate instrument name detected: {0}.")]
internal void DuplicateInstrumentNameWarning(string instrumentName)
{
this.WriteEvent(1, instrumentName);
}

[Event(2, Level = EventLevel.Warning, Message = "Instrument not created. Invalid instrument name: {0}.")]
internal void InvalidInstrumentNameWarning(string instrumentName)
{
this.WriteEvent(2, instrumentName);
}

[Event(3, Level = EventLevel.Warning, Message = "Error while writing event from source: {0} - {1}.")]
[Event(1, Level = EventLevel.Warning, Message = "Error while writing event from source: {0} - {1}.")]
internal void ErrorWhileWritingEvent(string eventSourceName, string exceptionMessage)
{
this.WriteEvent(3, eventSourceName, exceptionMessage);
this.WriteEvent(1, eventSourceName, exceptionMessage);
}

[Event(4, Level = EventLevel.Warning, Message = "Event data payload not parseable from source: {0}.")]
[Event(2, Level = EventLevel.Warning, Message = "Event data payload not parseable from source: {0}.")]
internal void IgnoreEventWrittenEventArgsPayloadNotParseable(string eventSourceName)
{
this.WriteEvent(4, eventSourceName);
this.WriteEvent(2, eventSourceName);
}

[Event(5, Level = EventLevel.Warning, Message = "Event data has no name from source: {0}.")]
[Event(3, Level = EventLevel.Warning, Message = "Event data has no name from source: {0}.")]
internal void IgnoreEventWrittenEventArgsWithoutName(string eventSourceName)
{
this.WriteEvent(5, eventSourceName);
this.WriteEvent(3, eventSourceName);
}

[Event(6, Level = EventLevel.Warning, Message = "Event data payload problem with values of Mean, Increment from source: {0}.")]
[Event(4, Level = EventLevel.Warning, Message = "Event data payload problem with values of Mean, Increment from source: {0}.")]
internal void IgnoreMeanIncrementConflict(string eventSourceName)
{
this.WriteEvent(6, eventSourceName);
this.WriteEvent(4, eventSourceName);
}

[Event(7, Level = EventLevel.Warning, Message = "Event data has name other than 'EventCounters' from source: {0}.")]
[Event(5, Level = EventLevel.Warning, Message = "Event data has name other than 'EventCounters' from source: {0}.")]
internal void IgnoreNonEventCountersName(string eventSourceName)
{
this.WriteEvent(7, eventSourceName);
this.WriteEvent(5, eventSourceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,20 @@
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Diagnostics.Tracing;
using System.Reflection;
using System.Text.RegularExpressions;

namespace OpenTelemetry.Instrumentation.EventCounters;

/// <summary>
/// .NET EventCounters Instrumentation.
/// </summary>
internal class EventCountersMetrics : EventListener
internal sealed class EventCountersMetrics : EventListener
{
internal static readonly Meter MeterInstance = new(AssemblyName.Name, AssemblyName.Version.ToString());
private static readonly AssemblyName AssemblyName = typeof(EventCountersMetrics).Assembly.GetName();
private static readonly Regex InstrumentNameRegex = new(
@"^[a-zA-Z][-.\w]{0,62}", RegexOptions.IgnoreCase | RegexOptions.Compiled);
internal static readonly Meter MeterInstance = new(typeof(EventCountersMetrics).Assembly.GetName().Name, typeof(EventCountersMetrics).Assembly.GetName().Version.ToString());

private readonly EventCountersInstrumentationOptions options;
private readonly ConcurrentQueue<EventSource> preInitEventSources = new();
private readonly ConcurrentDictionary<(string, string), Instrument> instruments = new();
private readonly ConcurrentDictionary<(string, string), double> values = new();
private readonly ConcurrentDictionary<string, byte> instrumentNames = new();

/// <summary>
/// Initializes a new instance of the <see cref="EventCountersMetrics"/> class.
Expand Down Expand Up @@ -86,13 +80,12 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
return;
}

if (eventData.Payload == null || eventData.Payload.Count == 0 || eventData.Payload[0] is not IDictionary<string, object>)
if (eventData.Payload == null || eventData.Payload.Count == 0 || eventData.Payload[0] is not IDictionary<string, object> payload)
{
EventCountersInstrumentationEventSource.Log.IgnoreEventWrittenEventArgsPayloadNotParseable(eventSourceName);
return;
}

var payload = eventData.Payload[0] as IDictionary<string, object>;
var hasName = payload.TryGetValue("Name", out var nameObj);

if (!hasName)
Expand All @@ -119,9 +112,6 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)
private static Dictionary<string, string> GetEnableEventsArguments(EventCountersInstrumentationOptions options) =>
new() { { "EventCounterIntervalSec", options.RefreshIntervalSecs.ToString() } };

private static bool IsValidInstrumentName(string name) =>
!string.IsNullOrWhiteSpace(name) && InstrumentNameRegex.IsMatch(name);

private void UpdateInstrumentWithEvent(bool isGauge, string eventSourceName, string name, double value)
{
try
Expand All @@ -131,23 +121,12 @@ private void UpdateInstrumentWithEvent(bool isGauge, string eventSourceName, str

var instrumentName = $"EventCounters.{eventSourceName}.{name}";

if (!IsValidInstrumentName(instrumentName))
{
EventCountersInstrumentationEventSource.Log.InvalidInstrumentNameWarning(instrumentName);
return;
}

if (!this.instruments.ContainsKey(metricKey))
{
Instrument instrument = isGauge
? MeterInstance.CreateObservableGauge(instrumentName, () => this.values[metricKey])
: MeterInstance.CreateObservableCounter(instrumentName, () => this.values[metricKey]);
_ = this.instruments.TryAdd(metricKey, instrument);

if (!this.instrumentNames.TryAdd(instrumentName, 0))
{
EventCountersInstrumentationEventSource.Log.DuplicateInstrumentNameWarning(instrumentName);
}
}
}
catch (Exception ex)
Expand Down

0 comments on commit 71560ed

Please sign in to comment.