microsoft / ApplicationInsights-dotnet

ApplicationInsights-dotnet
MIT License
566 stars 285 forks source link

Can I split metrics by application name? #2644

Open verdysh opened 2 years ago

verdysh commented 2 years ago

Hi! Assumed, there will be few different but related apps writing metrics, traces and logs to the same ApplicationInsights resource in Azure. It's okay if I see traces and logs in the one place. I even can filter them by cloud_RoleName or something else if I want to see logs by specific app. But for metrics I think it doesn't make sense to watch them all together. I would want to see metrics splitted by apps. Here is how I collect metrics:

var metrics = new Dictionary<string, List<string>>
{
    ["System.Runtime"] = new List<string>
    {
        "time-in-gc","alloc-rate", "cpu-usage", "exception-count",
        "gc-heap-size", "gen-0-gc-count", "gen-0-size", "gen-1-gc-count", "gen-1-size",
        "gen-2-gc-count", "gen-2-size", "loh-size", "poh-size", "gc-fragmentation",
        "monitor-lock-contention-count", "active-timer-count", "assembly-count",
        "threadpool-completed-items-count", "threadpool-queue-length", "threadpool-thread-count",
        "working-set", "il-bytes-jitted", "method-jitted-count", "gc-committed-bytes"
    }
};

builder.Services.ConfigureTelemetryModule<EventCounterCollectionModule>(
    (module, o) =>
    {
        foreach (var kv in metrics)            
            foreach(var counterName in kv.Value)                
                module.Counters.Add(new EventCounterCollectionRequest(kv.Key, counterName));
    }
);

Can I set namespace which I'll be able to filter by in Metrics explorer in Azure? image

verdysh commented 2 years ago

Question is still actual. If there is no answer to this question, how do you split metrics of different apps in Azure?

cijothomas commented 2 years ago

https://github.com/microsoft/ApplicationInsights-dotnet/blob/main/WEB/Src/EventCounterCollector/EventCounterCollector/EventCounterCollectionModule.cs#L52

verdysh commented 2 years ago

@cijothomas , thanks for the answer! This property doesn't help to achieve what I described in start question. What it really do - it decomposes metrics into namespaces according to metrics source name. For case in start post metrics look like this

image

But they are still added into one bucket and I have no idea how I can look to metrics of some specific app.

verdysh commented 2 years ago

Is it impossible? If so, how do you understand, using one instance of ApplicationInsights, what metrics belongs what apps?

verdysh commented 2 years ago

The question is still actual. If you use separate instances of Application Insights you can't see traces through all your services in one place. If you use one instances of Application Insights you get a mess of metrics. Doesn't this issue bother anyone else?

CodeBlanch commented 2 years ago

Sorry @verdysh there isn't a way to append app to the namespaces automatically. The options you have:

  1. Use separate ApplicationInsights resources. That will generate a unique scope for each app.
  2. Use custom namespaces including the app name when you define metrics in your code.
verdysh commented 2 years ago
  1. If I had chosen this way I would have lost my end-to-end transactions.
  2. If I define my own metric, this is solution. But I can't define or impact to what namespace will be used in case of using EventCounterCollectionModule which is part of this library.

Whatever way I choose I'll lost something. I thought the solution is on the surface and I missed some setting or option. I can't believe I'm first who have faced this issue. If I want to save end-to-end transactions and to see runtime metrics separately by apps, the only way is to implement runtime metrics collection by myself?

CodeBlanch commented 2 years ago

@cijothomas If we define a TelemetryInitializer and add the app name as a custom property on all the emitted telemetry would that allow us to filter/group as if it were a dimension kind of thing?

cijothomas commented 2 years ago

If you use separate instances of Application Insights you can't see traces through all your services in one place

^ This is not true. As long as you have access to the Application Insights resource, transaction view works.

verdysh commented 2 years ago

@cijothomas I run app1 locally, locally it writes telemetry to AplicationInsights-1 instance. I made http query to app2 which is run in Azure and writes telemetry to AplicationInsights-2 instance.

If I go to AplicationInsights-1 I see in end-to-end transaction the next: image

If I go to AplicationInsights-2 I see all what happened in app2 (but obviously I don't see what happened in app1 there). image

What I have to do in order to see complete end-to-end transaction tree in AplicationInsights-1 (or somewhere else)?

cijothomas commented 2 years ago

As the UI shows - you do not have access to the telemetry. If you think that is an error, please open Azure support ticket to get someone to investigate it.

verdysh commented 2 years ago

If you think that is an error, please open Azure support ticket to get someone to investigate it.

It's a way through the hell. Once I tried going this way, but couldn't pass it.

I just want to set my custom namespace (or namespace prefix) for metrics which are collected by this package. Is it impossible?

SEnglishSMP commented 1 year ago

@verdysh, we have multiple apps using Application Insights, each with their own AI instance on Azure. We always see the distributed call stack. In our case all of the AI instances (1) are part of the same subscription (2) have the same owner, and (3) have the same permissions.

From your screenshot, it's evident that you don't have the same permissions on each AI instance.

verdysh commented 1 year ago

@SEnglishSMP , thanks for the answer! What is "permissions" in this case? Where I can change them?

SEnglishSMP commented 1 year ago

@SEnglishSMP , thanks for the answer! What is "permissions" in this case? Where I can change them?

@verdysh In the Azure portal, select Application Insights instance and then select "Access Control (IAM)" on the left. From here you can edit which users have access to view and/or edit this instance. A user needs access to both instances in order to see calls from one correlated with calls to the other.

verdysh commented 1 year ago

@SEnglishSMP, thanks for the answer! I'll check it once again and will text soon.

verdysh commented 1 year ago

@SEnglishSMP , it seems it really works, I mean traces. I didn't change anything but it didn't work then, but now works. It's a great news! Thanks!

But the question how to split metrics by different apps inside one AI instance - it's still open question.

andradf commented 1 year ago

I have just ran into this and I have to say, it seems like a big design mistake to not allow splitting on cloud_roleName and cloud_RoleInstance. Especially cloud_RoleInstance. Or are we expected to have a single Application Insights resource per instance of a service? That would be prohibitive when considering applications with tens or hundreds of instances.