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

Correct way to use Application Insights in v4 and .Net 6 #760

Closed
emilssonn opened this issue Dec 16, 2021 · 44 comments
Closed

Correct way to use Application Insights in v4 and .Net 6 #760

emilssonn opened this issue Dec 16, 2021 · 44 comments

Comments

@emilssonn
Copy link

From reading different issues, discussions and documentation from the past months, it is still unclear to me how to correctly use/install Application Insights in a .Net isolated function running .Net 6 and v4.

I have tested with the nuget Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights. The absolute most basic features of application insights works with this nuget. But no dependency tracking etc.

Where can I find the correct documentation for using Application Insights with a .Net isolated function running .Net 6 and v4? Including depedency tracking, log levels filter, using TelemetryClient and TelemetryConfiguration in code.

@ghost ghost assigned brettsam Dec 16, 2021
@elsand
Copy link

elsand commented Dec 17, 2021

@emilssonn Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights seems to be non-working. https://github.com/Azure/azure-functions-applicationinsights-extension/tree/main/src/Worker is literally empty except for a csproj.

I'm not sure quite sure how to parse Azure/app-service-announcements#346 (comment), but it appears that Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights isn't supposed to be used at this point.

In any case we're experiencing the same issue as you. Rudimentary trace logging is performed for external calls, but they are not tracked as dependencies (without using Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights).

@elsand
Copy link

elsand commented Dec 17, 2021

@emilssonn It appears to work if you follow the steps at https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service#using-application-insights-sdk-for-worker-services, that is get the Microsoft.ApplicationInsights.WorkerService package and add services.AddApplicationInsightsTelemetryWorkerService(); to ConfigureServices (services being IServiceCollection).

@emilssonn
Copy link
Author

Microsoft.ApplicationInsights.WorkerService does indeed seem to work, but not fully. Dependencies are logged, but they are not connected to the requests/other telemetry.

I am 99.9% sure I saw some official documentation about Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights and that it was the official/correct way. But I can no longer find it and the app service announcement might explain that.

But I still cannot find any official documentation about .Net isolated functions running .Net 6 and v4.
https://docs.microsoft.com/en-us/azure/azure-monitor/app/worker-service#using-application-insights-sdk-for-worker-services makes no mention of Azure Functions.

@fabiocav
Copy link
Member

fabiocav commented Jan 4, 2022

Leaving this assigned to @brettsam as he is driving the logging enhancements activities.

@ranouf
Copy link

ranouf commented Jan 19, 2022

Hi, so finally what happened with Application Insights for Azure Function V4 and .net 6?

@SeanFeldman
Copy link
Contributor

I'm facing a migration but hesitant until know this issue is resolved.
Looking forward to @brettsam's follow-up comments.

@feverline-ob
Copy link

+1 on this. I'm facing similar issues with dependency telemetry in dotnet-isolated in functions v4, .net 6.

@brettsam
Copy link
Member

Microsoft.Azure.Functions.Worker.Extensions.ApplicationInsights is deprecated now and we should hide it in nuget. I will follow up on that.

In short -- we abandoned that approach due to startup performance and I don't believe it would have given you everything you were looking for here anyway.

When you enable Application Insights from the portal (or add the APPLICATIONINSIGHTS_CONNECTION_STRING app setting), that turns on logging from the Functions host. It also will enable you to log via the ILogger that is passed into your function context in the isolated worker (this sends the logs back to the host to log for you).

However, there are some other scenarios that this does not cover -- dependency tracking in the worker; access to TelemetryClient, etc.

Ideally, the Microsoft.ApplicationInsights.WorkerService would "just work" when you used it in the dotnet-isolated worker. We're investigating whether that's possible and will update this issue with our findings. There's a set of logging issues that we're working through right now and this is one of the most important ones.

@wesselkranenborg
Copy link

+1 on this. We are also facing this issue. We use the Microsoft.ApplicationInsights.WorkerService but it's not correlating the dependencies. This is making us hesitant in rolling out isolated worker functions further.

@emilssonn
Copy link
Author

@brettsam Apart from dependency tracking in the worker and access to TelemetryClient scenarios (which almost work when using Microsoft.ApplicationInsights.WorkerService, apart from dependencies not correlating to other telemetry). What are the sets of logging issues exactly?
From what I can see the main issue is that ILogger categories are not honored, they are changed when logging to Application Insights. They are always changed to Function.<functionname>.User. Which makes it impossible to configure logging in any good way. For example if I want some of my code to use Information and some Warning etc. This includes framework code aswell. For example if Information logging is enabled, even HttpClient logs are written with category Function.<functionname>.User. Is this by design and working as intended? Or are one of the logging issues to make log levels and categories work as they do in other .Net scenarios?

These issues have been discussed in other github issues aswell. The logging issues are a major problem

@arthurzennig07
Copy link

In my case, Logs were showing in Trace and locally. But the Function's Monitor wasn't logging.
We recently moved the function from dotnet3.1 to dotnet6 and added dependency injection (including log).
It started showing logs in Monitor once we added the following configuration in function's startup.cs:
.ConfigureLogging((ctx,logging) => { if (ctx.HostingEnvironment.IsDevelopment()) { logging.AddDebug(); } logging.AddConsole(); logging.Services.Configure<AzureFileLoggerOptions>(options => { options.FileName = "az.log"; options.FileSizeLimit = 5 * 1024; options.RetainedFileCountLimit = 3; }

@vinhent
Copy link

vinhent commented Mar 18, 2022

We are having the same issue using Microsoft.ApplicationInsights.WorkerService . The dependencies are collected but they are not being correlated. We can see them in App Insights, but they are not showing up underneath the request. While this is being fixed, is there any workaround that could be use to "restore" the operation context before sending the dependency telemetry, maybe with an ITelemetryProcessor?

@pmonte
Copy link

pmonte commented Mar 30, 2022

From what we can see from our tests _logger.BeginScope("MyScope") doesn't work in Function, i.e. the log appears in Application Insight but Scope is not set in customDimensions.

@af4jm
Copy link

af4jm commented Apr 6, 2022

we can't get Azure pipelines to build correctly with Microsoft.ApplicationInsights.WorkerService ... for .NET 5 projects the work-around is to have the pipeline download the 3.1.x SDK, then 5.0.x ... but with .NET 6 the NuGet restore fails, whether the SDK 3.1 step is there or not, so we're stuck on 5 until this is resolved (we're running isolated process, in case it's relevant)

in both cases the error is Failed to download package 'Microsoft.NETCore.Targets.3.0.0' referenced by WorkerExtensions.csproj

@AieatAssam
Copy link

Are there any updates on this issue? Lack of correlated dependencies is a major concern as is lack of documentation on enabling this in general.

@xtellurian
Copy link

I've just run across this issue as I'm digging into my Azure Function application insights telemetry.

We're running functions v4 (dotnet-isolated) and .net6.0. To be honest, I would expect this to be resolved already, since Az Functions v4 has gone GA

@brettsam can you give us an update on when we'll have access to TelemetryClient etc?

@rhythmnewt
Copy link

Considering that .net core 3.1 is end of life in December 2022, and .net core 5 is already end of life, this needs to be pushed to the forefront. Requiring organizations to migrate/upgrade to v4/core 6 but losing basic functionality is just terrible.

@vc4u2c
Copy link

vc4u2c commented May 17, 2022

Been having this issue for some time now. We are on Functions v4 and .NET 6. Please resolve this quickly.

@bbieniek
Copy link

We have used this way: #822 (comment)
and it seems to work fine

@oatsoda
Copy link

oatsoda commented May 26, 2022

This is really starting to become very problematic, to the point we're considering ditching Application Insights and using something else.

We're in a situation where we cannot investigate/analyse live data.

Running .net 6 in Isolated Process functions.

These are the things causing problems:

An update on when all of this is going to be sorted would be appreciated as it's getting to the point where we have to seek alternatives.

@NeilMacMullen
Copy link

I've just spent 3.5 months going round in circles with customer support on this only to finally discover app-insight dependencies are "not supported" for isolated functions :-( Could we at least get the documentation improved to make it obvious this is a limitation?

@SeanFeldman
Copy link
Contributor

Many have already started the obvious - for a GA-ed product this SDK is in a poor state. With serverless that is hosted and managed by Azure, logging and telemetry is a vital tool for troubleshooting. Without it functioning properly clients are running with blinders on. Detailed documentation, functioning Appinsights integration, and fixed related issues should keep the team up at night. Untill then, it's their customers are the ones that are staying up.

@fabiocav
Copy link
Member

Design for this is in process. We'll be sharing details here once that is reviewed. Moving to 125 to track the completion of this work.

@truptisatardekar
Copy link

+1 for facing the same issue. In my Azure function (dotnet-isolated version 4.0), I used Opentelemetry to track postgresql dependency calls, which show up correctly in AppInsights however they aren't correlated with the parent request.

I tried the below code in Azure Function -> Program.cs -> main -> ConfigureServices

Usings
using Npgsql;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

services.AddLogging(c =>
            {
           c.AddSerilog(
                new LoggerConfiguration()
                  .ReadFrom.Configuration(context.Configuration)
                  .WriteTo.ApplicationInsights(TelemetryConfiguration.CreateDefault(), TelemetryConverter.Traces, Serilog.Events.LogEventLevel.Information)
                  .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
                  .Enrich.WithCorrelationId()
                  .CreateLogger()
              );

              // Enable Tracing for postgresql with OpenTelemetry
              var openTelemetryResourceBuilder = ResourceBuilder.CreateDefault().AddService(serviceName: System.Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME").ToLower());
              var openTelemetryTracerProvider = Sdk.CreateTracerProviderBuilder()
                  .SetResourceBuilder(openTelemetryResourceBuilder)
                  .AddSource(System.Environment.GetEnvironmentVariable("WEBSITE_SITE_NAME").ToLower())
                  .AddNpgsql()
                  .AddAzureMonitorTraceExporter(o =>
                    {
                      o.ConnectionString = context.Configuration.GetValue<string?>("APPLICATIONINSIGHTS_CONNECTION_STRING");
                    })
                  .Build();
              services.AddSingleton(openTelemetryTracerProvider);
            });

            services.AddHttpContextAccessor();

@Tinathnath

This comment was marked as off-topic.

@brettsam
Copy link
Member

The preview packages are now available: https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.ApplicationInsights.

For usage and explanation, see the PR here (official docs will come when this is out of preview): #944.

If you have feedback, bugs, etc, please create a new issue with the details and we'll take a look.

@LenaVinogradov
Copy link

LenaVinogradov commented Aug 19, 2022

@brettsam I have used preview package and sample code provided in the PR link today. I still see all dependencies not linked to the parent. The parentId set in dependencies does not correspond to any telemetry objects logged in our appinsights.

@brettsam
Copy link
Member

@LenaVinogradov -- can you open a separate issue with exactly what you're seeing and what you expect to see? That'd help us narrow it down. Thanks!

@LockTar
Copy link

LockTar commented Aug 30, 2022

The preview packages are now available: https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.ApplicationInsights.

So I'm trying this new preview package locally and in Azure with a .Net 6 function v4 isolated test project. So everything is brand new/green field/new project template. I installed the package and changed the Program.cs to:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(builder =>
    {
        builder
            .AddApplicationInsights()
            .AddApplicationInsightsLogger();
    })
    .Build();

host.Run();

The azure portal generated application insights for me and configured the function app.
But it's still unclear to me why logging Debug and Trace messages isn't working.
What do I need to change? The documentation is not clear.

@vasylstrogushr
Copy link

The preview packages are now available: https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.ApplicationInsights.

So I'm trying this new preview package locally and in Azure with a .Net 6 function v4 isolated test project. So everything is brand new/green field/new project template. I installed the package and changed the Program.cs to:

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults(builder =>
    {
        builder
            .AddApplicationInsights()
            .AddApplicationInsightsLogger();
    })
    .Build();

host.Run();

The azure portal generated application insights for me and configured the function app. But it's still unclear to me why logging Debug and Trace messages isn't working. What do I need to change? The documentation is not clear.

Hi,
same behavior for me.
After use this package (Microsoft.Azure.Functions.Worker.ApplicationInsights) I don't have any traces or information logs. I had them when used Microsoft.ApplicationInsights.WorkerService but they were not linked in dependency, now nothing at all.

Azure Function v4, .Net 6 isolated.

@vasylstrogushr
Copy link

vasylstrogushr commented Aug 31, 2022

Microsoft.Azure.Functions.Worker.ApplicationInsights looks work now for me.
But still not all dependencies are loaded.
For example(screenshot), call to last service has many internal dependencies, but no one is shown here. But traces and information are from all services. This service is gRPC service. Maybe new update will able to grab everything
image

@lund76
Copy link

lund76 commented Sep 14, 2022

Hello,

I'm also struggling like cracy to get the trace and debug log work:

TL;DR:

Using an in process function. The out the box logging works fine (even structures the log a bit). But in isolated I don't get any structure, and Traces and Debugs are not logged. Not in console - not in application insight. With a few config in progam.cs I can get a structured log going - but no way traces and debug.

The simplest possible solutions:

I've created a repo with what works, and what doesn't

In Process Example that works

The host file log level it default set to trace

The HttpExample.cs logs both information, trace and debug:

           log.LogInformation("Log Request number {InfoLogCount}", 1);
            log.LogTrace("A little trace in the middle");
            log.LogDebug("A littel debug info in the middle");
            log.LogInformation("Log Request number {InfoLogCount}", 2);

And this results in expected logs in both "console"
image

And application insights:
image

And even structures the log somewhat:
image

Now using an isolated approach

And with the same log events, the trace and debug doesn't get logged:

Not in console:
image

and not in Application insights:
image

The base implementation doesn't handle the structured logging:
image

Adding reference to Microsoft.Extensions.Logging.ApplicationInsights
we get the structure back:
image
And with the existing providers cleared . The dublicate entries that this would give are gone.

@olandese
Copy link

olandese commented Oct 1, 2022

At the moment it's a mess. It is totally unclear using a function in Isolated Mode what is the right/correct way to log to Application Insights.
Which packages and version do we need?
What configuration needs to be made?
What code changes we need to apply and where?

@Azure Azure locked and limited conversation to collaborators Oct 13, 2022
@brettsam
Copy link
Member

Hey everyone -- I'm going to lock this issue down as it's getting hard to track the individual issues. I'll respond to everything that's here now and spin off new issues if needed, but please create a new issue with your questions/bugs from now on.

Thanks for all the feedback!

@brettsam
Copy link
Member

Reply to: #760 (comment)

The azure portal generated application insights for me and configured the function app. But it's still unclear to me why logging Debug and Trace messages isn't working. What do I need to change? The documentation is not clear.

@LockTar:

The worker should behave just like any other Console application in .NET. You can adjust logging levels and filters via code, appsettings.json, etc -- https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-6.0#configure-logging.

Maybe the confusing thing here is that there is now two parts to logging configuration: the host and the worker. The host is controlled by host.json and the worker is controlled by the normal .NET mechanisms mentioned above. I called this out here in my initial "docs" (see the last bullet), but we're welcoming feedback: #944 (comment).

Please open a new issue with details if this doesn't help -- or if you don't like it :-) I can go into some reasoning there, if needed.

@brettsam
Copy link
Member

Reply to: #760 (comment)

@vasylstrogushr -- can you share a repro in a new issue? We're not doing anything special when we add the App Insights Logger, so I'd expect it to behave exactly the same way.

One thing that I did realize (and will update shortly) is that the App Insights logger injects an extra filter into the logging system to log only at Warning and above. I spent a while trying to figure out why my Information logs didn't appear anywhere, too:

@brettsam
Copy link
Member

Reply to: #760 (comment)

@vasylstrogushr -- Can you open a new issue with a small repro to explain the scenario? I can get the right App Insights people to help out there. Are you saying that logs from the separate service are not being correctly correlated?

@brettsam
Copy link
Member

Reply to: #760 (comment)

@lund76 -- you may be hitting the same thing that I mentioned above in #760 (comment) (that host.json only applies to the host). The worker is a separate Console application and it's completely configurable like any other Console application.

Please feel free to file a new issue if this doesn't help or you have feedback.

I'll give a little overview of our reasoning here: The host.json logging settings are very .NET-specific in their structure. We could load them directly and apply them to the dotnet-isolated application with no problem. But that isn't a great solution for any other language, so we're experimenting with the notion of having the worker control its own logging in its "natural" way and the host being controlled separately via host.json (it is called host configuration, after all). This allows you to write filtering code in .NET, apply different log levels to things in host vs worker, etc. And you don't have to think about how two conflicting settings between the host and worker will compose together... the worker ignores host.json completely.

As mentioned above, we welcome feedback here, so feel free to file a new issue. We're trying to rationalize the logging configuration for out-of-proc workers and realizes this can be confusing for those coming from the in-proc world.

@brettsam
Copy link
Member

brettsam commented Oct 13, 2022

Reply to: #760 (comment)

@olandese -- see the "preview doc" at #944 (comment). I think it answers all of your questions. If not, please file a new issue with your questions that are unanswered, and I can help out there.

@brettsam
Copy link
Member

Closing this now as the preview package is released and it's tough to track the one-off comments here. If you run into issues, have questions, or general feedback, please file a new issue.

Package: https://www.nuget.org/packages/Microsoft.Azure.Functions.Worker.ApplicationInsights
Preview "docs": #944 (comment)

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

No branches or pull requests