This is an Instrumentation Library , which republishes EventCounters using OpenTelemetry Metrics API.
You can view an example project using EventCounters at
/examples/event-counters/Examples.EventCounters
.
Add a reference to the
OpenTelemetry.Instrumentation.EventCounters
package.
dotnet add package OpenTelemetry.Instrumentation.EventCounters --prerelease
EventCounters instrumentation should be enabled at application startup using the
AddEventCountersInstrumentation
extension on the MeterProviderBuilder
:
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddEventCountersInstrumentation(options => {
options.RefreshIntervalSecs = 1;
options.AddEventSources("MyEventSource");
})
.AddPrometheusHttpListener()
.Build();
Additionally, the above snippet sets up the OpenTelemetry Prometheus exporter, which
requires adding the package
OpenTelemetry.Exporter.Prometheus
to the application.
Learn about EventCounters in .NET .
EventSource eventSource = new("MyEventSource");
EventCounter eventCounter = new("MyEventCounterName", eventSource);
eventCounter.WriteMetric(0);
eventCounter.WriteMetric(1000);
PollingCounter pollingCounter = new("MyPollingCounterName", eventSource, () => new Random().NextDouble());
The metrics will only be available after EventCounterIntervalSec
seconds.
Before that nothing will be exported, if anything is present at the Prometheus
metrics endpoint it is from a prior execution. This is more evident when using
longer polling intervals.