Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Fixed `OnEventWritten` processing events from EventSources that were not
configured via `AddEventSources`.
([#4031](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/4031))

* Updated OpenTelemetry core component version(s) to `1.15.1`.
([#4020](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/4020))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ protected override void OnEventWritten(EventWrittenEventArgs eventData)

var eventSourceName = eventData.EventSource.Name;

if (!this.options.ShouldListenToSource(eventSourceName))
{
return;
}

if (eventData.EventName != "EventCounters")
{
EventCountersInstrumentationEventSource.Log.IgnoreNonEventCountersName(eventSourceName);
Expand Down Expand Up @@ -190,9 +195,7 @@ private static string GetInstrumentName(string sourceName, string eventName)
}

private void EnableEvents(EventSource eventSource)
{
this.EnableEvents(eventSource, EventLevel.Critical, EventKeywords.None, GetEnableEventsArguments(this.options));
}
=> this.EnableEvents(eventSource, EventLevel.Critical, EventKeywords.None, GetEnableEventsArguments(this.options));

private void UpdateInstrumentWithEvent(bool isGauge, string eventSourceName, string name, double value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ public class EventCountersMetricsTests
public void EventCounter()
{
// Arrange
List<Metric> metricItems = [];
List<Metric> exported = [];
using EventSource source = new("a");
using EventCounter counter = new("c", source);

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options =>
{
options.AddEventSources(source.Name);
})
.AddInMemoryExporter(metricItems)
.Build()!;
Metric? metric;

// Act
counter.WriteMetric(1997.0202);
var metric = AwaitExport(meterProvider, metricItems, expectedInstrumentName: "ec.a.c");
using (var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options => options.AddEventSources(source.Name))
.AddInMemoryExporter(exported)
.Build())
{
// Act
counter.WriteMetric(1997.0202);
metric = AwaitExport(meterProvider, exported, expectedInstrumentName: "ec.a.c");
}

// Assert
Assert.NotNull(metric);
Expand All @@ -43,19 +43,20 @@ public void IncrementingEventCounter()
using EventSource source = new("b");
using IncrementingEventCounter incCounter = new("inc-c", source);

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options =>
{
options.AddEventSources(source.Name);
})
Metric? metric;

using (var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options => options.AddEventSources(source.Name))
.AddInMemoryExporter(metricItems)
.Build()!;
.Build())
{
// Act
incCounter.Increment(1);
incCounter.Increment(1);
incCounter.Increment(1);

// Act
incCounter.Increment(1);
incCounter.Increment(1);
incCounter.Increment(1);
var metric = AwaitExport(meterProvider, metricItems, expectedInstrumentName: "ec.b.inc-c");
metric = AwaitExport(meterProvider, metricItems, expectedInstrumentName: "ec.b.inc-c");
}

// Assert
Assert.NotNull(metric);
Expand All @@ -72,16 +73,16 @@ public void PollingCounter()
using EventSource source = new("c");
using PollingCounter pollCounter = new("poll-c", source, () => ++i * 10);

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options =>
{
options.AddEventSources(source.Name);
})
.AddInMemoryExporter(metricItems)
.Build()!;
Metric? metric;

// Act
var metric = AwaitExport(meterProvider, metricItems, expectedInstrumentName: "ec.c.poll-c");
using (var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options => options.AddEventSources(source.Name))
.AddInMemoryExporter(metricItems)
.Build())
{
// Act
metric = AwaitExport(meterProvider, metricItems, expectedInstrumentName: "ec.c.poll-c");
}

// Assert
Assert.NotNull(metric);
Expand All @@ -98,16 +99,16 @@ public void IncrementingPollingCounter()
using EventSource source = new("d");
using IncrementingPollingCounter incPollCounter = new("inc-poll-c", source, () => i++);

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options =>
{
options.AddEventSources(source.Name);
})
.AddInMemoryExporter(metricItems)
.Build()!;
Metric? metric;

// Act
var metric = AwaitExport(meterProvider, metricItems, expectedInstrumentName: "ec.d.inc-poll-c");
using (var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options => options.AddEventSources(source.Name))
.AddInMemoryExporter(metricItems)
.Build())
{
// Act
metric = AwaitExport(meterProvider, metricItems, expectedInstrumentName: "ec.d.inc-poll-c");
}

// Assert
Assert.NotNull(metric);
Expand All @@ -121,10 +122,7 @@ public void ThrowExceptionForUnsupportedEventSources()
var ex = Assert.Throws<NotSupportedException>(() =>
{
Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options =>
{
options.AddEventSources("System.Runtime");
});
.AddEventCountersInstrumentation(options => options.AddEventSources("System.Runtime"));
});

Assert.Equal("Use the `OpenTelemetry.Instrumentation.Runtime` or `OpenTelemetry.Instrumentation.Process` instrumentations.", ex.Message);
Expand All @@ -146,17 +144,17 @@ public void EventSourceNameShortening(string sourceName, string eventName, strin
using EventSource source = new(sourceName);
using IncrementingEventCounter connections = new(eventName, source);

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options =>
{
options.AddEventSources(source.Name);
})
.AddInMemoryExporter(metricItems)
.Build()!;
Metric? metric;

// Act
connections.Increment(1);
var metric = AwaitExport(meterProvider, metricItems, expectedInstrumentName);
using (var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options => options.AddEventSources(source.Name))
.AddInMemoryExporter(metricItems)
.Build())
{
// Act
connections.Increment(1);
metric = AwaitExport(meterProvider, metricItems, expectedInstrumentName);
}

// Assert
Assert.NotNull(metric);
Expand All @@ -174,19 +172,17 @@ public async Task InstrumentNameTooLong()
var veryLongEventName = new string('e', 100);
using IncrementingEventCounter connections = new(veryLongEventName, source);

using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options =>
{
options.AddEventSources(source.Name);
})
using (var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options => options.AddEventSources(source.Name))
.AddInMemoryExporter(metricItems)
.Build();

// Act
connections.Increment(1);
.Build())
{
// Act
connections.Increment(1);

await Task.Delay(1800);
meterProvider.ForceFlush();
await Task.Delay(1800);
meterProvider.ForceFlush();
}

// Assert
foreach (var item in metricItems)
Expand All @@ -196,6 +192,48 @@ public async Task InstrumentNameTooLong()
}
}

[Fact]
public async Task OnlyConfiguredEventSourcesEmitMetrics()
{
// Arrange
List<Metric> metrics = [];

using var configuredSource = new EventSource("configured-source");
using var otherSource = new EventSource("other-source-not-configured");
using var configuredCounter = new IncrementingEventCounter("hits", configuredSource);
using var otherCounter = new IncrementingEventCounter("other-hits", otherSource);

// A standalone EventListener that enables the unconfigured source, simulating
// an external tool such as dotnet-counters enabling additional event sources.
using var externalListener = new TestEventListener();
externalListener.EnableEventSource(otherSource);

Metric? configuredMetric;

using (var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options =>
{
options.AddEventSources(configuredSource.Name);
options.RefreshIntervalSecs = 1;
})
.AddInMemoryExporter(metrics)
.Build())
{
// Act: write to both sources
configuredCounter.Increment(1);
otherCounter.Increment(99);

// Wait for the configured source to produce a metric
configuredMetric = AwaitExport(meterProvider, metrics, expectedInstrumentName: "ec.configured-source.hits");

meterProvider.ForceFlush();
}

// Assert: only the configured source should emit metrics
Assert.NotNull(configuredMetric);
Assert.DoesNotContain(metrics, m => m.Name.Contains("other-source-not-configured", StringComparison.Ordinal));
}

private static double GetActualValue(Metric metric)
{
double sum = 0;
Expand Down Expand Up @@ -226,4 +264,18 @@ private static double GetActualValue(Metric metric)

return metric;
}

private sealed class TestEventListener : EventListener
{
public void EnableEventSource(EventSource source) =>
this.EnableEvents(source, EventLevel.LogAlways, EventKeywords.All, new Dictionary<string, string?>
{
{ "EventCounterIntervalSec", "1" },
});

protected override void OnEventWritten(EventWrittenEventArgs eventData)
{
// No-op
}
}
}
Loading