From 71560ed15ec240ccbb9e3b3fb658f7199300cdd3 Mon Sep 17 00:00:00 2001 From: Michael Maxwell Date: Wed, 12 Oct 2022 17:37:06 -0700 Subject: [PATCH] remove duplicate checks sdk already does --- ...EventCountersInstrumentationEventSource.cs | 32 ++++++------------- .../EventCountersMetrics.cs | 27 ++-------------- 2 files changed, 13 insertions(+), 46 deletions(-) diff --git a/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersInstrumentationEventSource.cs b/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersInstrumentationEventSource.cs index 207507be82..071f963352 100644 --- a/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersInstrumentationEventSource.cs +++ b/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersInstrumentationEventSource.cs @@ -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); } } diff --git a/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersMetrics.cs b/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersMetrics.cs index 8a15bb553f..e01ce8b968 100644 --- a/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersMetrics.cs @@ -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; /// /// .NET EventCounters Instrumentation. /// -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 preInitEventSources = new(); private readonly ConcurrentDictionary<(string, string), Instrument> instruments = new(); private readonly ConcurrentDictionary<(string, string), double> values = new(); - private readonly ConcurrentDictionary instrumentNames = new(); /// /// Initializes a new instance of the class. @@ -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) + if (eventData.Payload == null || eventData.Payload.Count == 0 || eventData.Payload[0] is not IDictionary payload) { EventCountersInstrumentationEventSource.Log.IgnoreEventWrittenEventArgsPayloadNotParseable(eventSourceName); return; } - var payload = eventData.Payload[0] as IDictionary; var hasName = payload.TryGetValue("Name", out var nameObj); if (!hasName) @@ -119,9 +112,6 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData) private static Dictionary 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 @@ -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)