Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventCounterCollectionModule reports unexpected metric values for custom event source #2911

Open
mortenbock opened this issue Oct 29, 2024 · 2 comments
Labels

Comments

@mortenbock
Copy link

  • Nuget package: Microsoft.ApplicationInsights.AspNetCore v 2.22.0
  • Runtime version: net8.0
  • Hosting environment: Windows 11, Visual Studio 2022

Describe the bug

I want to create a metric that describes how many times something happened in my application. For example, when people place an order. So if 3 orders are placed within a minute, I expect the "Sum" of the metric to be 3 for that minute.

When I try to track this metric via an EventSource using the EventCounter or IncrementingEventCounter, the metrics are not adding up, but looking more like an "Orders per minute" statistic.

When using the _telemetryClient.GetMetric() approach, the metric is tracked exactly as I want it, but I would like to instrument my code using the EventSource instead.

To Reproduce

Using the default aspnet mvc template project I did the following:

Created an event source with two types of counters:

[EventSource(Name = "My.App")]
internal sealed class MyEventSource : EventSource
{
    public static MyEventSource Log = new();

    private readonly EventCounter _eventCounter;
    private readonly IncrementingEventCounter _incrementingCounter;

    public MyEventSource()
    {
        _eventCounter = new EventCounter("test-eventcounter", this)
        {
            DisplayName = "test-eventcounter"
        };

        _incrementingCounter = new IncrementingEventCounter("test-incrementing", this)
        {
            DisplayName = "test-incrementing"
        };
    }

    [Event(1)]
    public void HitEventCounter()
    {
        WriteEvent(1);
        _eventCounter?.WriteMetric(1);
    }

    [Event(2)]
    public void HitIncrementingCounter()
    {
        WriteEvent(2);
        _incrementingCounter?.Increment();
    }
}

Configured the EventCounterCollectionModule like this:

var builder = WebApplication.CreateBuilder(args);

builder.Services.ConfigureTelemetryModule<EventCounterCollectionModule>((module, options) =>
{
    module.UseEventSourceNameAsMetricsNamespace = true;
    module.Counters.Add(new EventCounterCollectionRequest("My.App", "test-eventcounter"));
    module.Counters.Add(new EventCounterCollectionRequest("My.App", "test-incrementing"));
});
builder.Services.AddApplicationInsightsTelemetry();

Calling three different ways of tracking the metric in my Home controller:

public IActionResult Index()
{
    var metric = _telemetryClient.GetMetric("test-sdk");
    metric.TrackValue(1);

    MyEventSource.Log.HitEventCounter();
    MyEventSource.Log.HitIncrementingCounter();

    return View();
}

The resulting data in the customMetrics table in Application Insights:

Image

I would expect the EventSource based metrics to work the same way as the SDK metric, creating a correct "Sum" for the number of events.

@mortenbock mortenbock added the bug label Oct 29, 2024
@cijothomas
Copy link
Contributor

but I would like to instrument my code using the EventSource instead.

It is not recommended to leverage EventCounters. They are somewhat legacy, and you should use https://learn.microsoft.com/en-us/dotnet/core/diagnostics/metrics-instrumentation instead. It integrates well with OpenTelemetry too.

This is also called out here: https://learn.microsoft.com/en-us/dotnet/core/diagnostics/event-counters

@mortenbock
Copy link
Author

@cijothomas I appreciate that, however:

  • Application Insights SDK does not support that Metrics Api
  • The App Insigths OTEL package does not support Adaptive Sampling, among many other things

So I have to pick my poison here, and this seemed like the middle ground, to instrument the code without referencing the Insights SDK directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants