microsoft / ApplicationInsights-dotnet

ApplicationInsights-dotnet
MIT License
568 stars 285 forks source link

Best practice when using API and WorkerService with ApplicationInsights and LiveMetrics #1732

Closed davidallyoung closed 4 years ago

davidallyoung commented 4 years ago

Describe your environment. Describe any aspect of your environment relevant to the question:

What are you trying to achieve? I am curious what the best practice is around adding application insights when using aspnetcore API and strapping on a hosted service via services.AddHostedService<T>.

I'm currently only using services.AddApplicationInsightsTelemetry(instrumentationKey), and it seems to work okay, however it feels like when the API is no longer active the live metrics disconnects and I was curious if it's because I'm not also adding in the hosted service application insights.

services.AddApplicationInsightsTelemetryWorkerService("instrumentationkeyhere");

I have enabled Always On for the app service to make sure it stays up.

The context for the hostedservices I have is that they should always be alive for the life of the application. Once http requests start flowing it seems like the live metrics come back to life. It seems as though all of the telemetry I expect to see is being consistently processed, just not always being reflected by having a connected Live Metrics (says app may be offline or using an older sdk, when the app is not offline or restarted, based on application event logs, etc)

I feel like I should only hook up application insights once (like I am currently) so that the same telemetry client/processor flows are being used throughout the application, but wanted to ask as I'm new to hosting both together and could be overlooking something.

cijothomas commented 4 years ago

You should not need to use AddApplicationInsightsTelemetryWorkerService. The AddApplicationInsightsTelemetry is good enough. If you inject TelemetryClient to the HostedService, it'd be sharing same config as the rest of telemetry from the web api part.

Regd. Live metrics - I can't tell why you are seeing live metrics disappearing.. it could have been a backend issue with livemetrics service. Could you try this again, and let us know.

cijothomas commented 4 years ago

Btw - if you have AlwaysOn enabled, then AppService make a web request every 5min to keep the app running. That should have kept live metrics also running.

cijothomas commented 4 years ago

Closing - reopen if needed.