diff --git a/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersMetrics.cs b/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersMetrics.cs index e01ce8b968..f4c92ad5c2 100644 --- a/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersMetrics.cs +++ b/src/OpenTelemetry.Instrumentation.EventCounters/EventCountersMetrics.cs @@ -30,7 +30,7 @@ internal sealed class EventCountersMetrics : EventListener 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 List preInitEventSources = new(); private readonly ConcurrentDictionary<(string, string), Instrument> instruments = new(); private readonly ConcurrentDictionary<(string, string), double> values = new(); @@ -40,27 +40,35 @@ internal sealed class EventCountersMetrics : EventListener /// The options to define the metrics. public EventCountersMetrics(EventCountersInstrumentationOptions options) { - this.options = options; - - while (this.preInitEventSources.TryDequeue(out EventSource eventSource)) + lock (this.preInitEventSources) { - if (this.options.ShouldListenToSource(eventSource.Name)) + this.options = options; + + foreach (EventSource eventSource in this.preInitEventSources) { - this.EnableEvents(eventSource, EventLevel.LogAlways, EventKeywords.None, GetEnableEventsArguments(this.options)); + if (this.options.ShouldListenToSource(eventSource.Name)) + { + this.EnableEvents(eventSource); + } } + + this.preInitEventSources.Clear(); } } /// - protected override void OnEventSourceCreated(EventSource source) + protected override void OnEventSourceCreated(EventSource eventSource) { - if (this.options == null) + lock (this.preInitEventSources) { - this.preInitEventSources.Enqueue(source); - } - else if (this.options.ShouldListenToSource(source.Name)) - { - this.EnableEvents(source, EventLevel.LogAlways, EventKeywords.None, GetEnableEventsArguments(this.options)); + if (this.options == null) + { + this.preInitEventSources.Add(eventSource); + } + else if (this.options.ShouldListenToSource(eventSource.Name)) + { + this.EnableEvents(eventSource); + } } } @@ -112,6 +120,11 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData) private static Dictionary GetEnableEventsArguments(EventCountersInstrumentationOptions options) => new() { { "EventCounterIntervalSec", options.RefreshIntervalSecs.ToString() } }; + private void EnableEvents(EventSource eventSource) + { + this.EnableEvents(eventSource, EventLevel.Critical, EventKeywords.None, GetEnableEventsArguments(this.options)); + } + private void UpdateInstrumentWithEvent(bool isGauge, string eventSourceName, string name, double value) { try