microsoft / ApplicationInsights-ServiceFabric

ApplicationInsights SDK for ServiceFabric projects
MIT License
63 stars 26 forks source link

Dependencies not being tracked #76

Closed jstawski closed 6 years ago

jstawski commented 6 years ago

We have a stateless service that is running some background process when a service bus message is received. We are referencing Microsoft.ApplicationInsights.ServiceFabric.Native 2.1.1 nuget package.

In the constructor of the StatelessService we are setting the insights key and initializing the telemetry as follows:

var telemetryConfig = TelemetryConfiguration.Active;
telemetryConfig.InstrumentationKey = key;
abricTelemetryInitializerExtension.CreateFabricTelemetryInitializer(context);

We want to track every new message as a request, so when we process the message we create a new RequestTelemetry:

public RequestTelemetry StartRequest(string name)
 {
   var request = new RequestTelemetry { Name = name, Timestamp = DateTime.UtcNow };
   return request;
}

We then Trace the request at the end:

public void TraceRequest(RequestTelemetry request, bool success, string responseCode = null)
{
  request.Duration = DateTime.UtcNow - request.Timestamp;
  request.Success = success;
  request.ResponseCode = responseCode ?? (success ? "200" : "500");
  _client.TrackRequest(request);
 }

In between there are 3 dependecy calls: 1 to SQL Azure, another to Postgres, and the last one to Azure Search Services.

We do see the request on the VS Application Insights tab, but we do not see any dependencies related to the requests or by themselves.

Are we missing any configuration?

brahmnes commented 6 years ago

Hi @jstawski

Please take a look at this article: https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-end-to-end-tracing#tracking-with-azure-application-insights

You probably need to use a very recent version of ServiceBus library, and the message handler pattern mentioned in that doc.

jstawski commented 6 years ago

hi @brahmnes, thanks for the quick reply. We are using the latest version of service bus client and are also using the message handler pattern. I first removed any custom request operation to see if anything would be tracked automagically, but nothing was tracked, even though the document says it would automatically do so. Then I implemented telemetryClient.StartOperation and it did log the request, but it was still missing the dependencies. I manually added a trace operation and it logged correctly, correlating it to the parent operation. I forgot to mention before that we are using .NET Core. Am I missing any configuration?

brahmnes commented 6 years ago

Can you also confirm if this is a .NET or .NET core app? Since this is a stateless service, I suspect this is more of a console app and not an ASP.NET web app? Which version of AppInsight SDK you are using and how are you enabling AppInsights?

jstawski commented 6 years ago

This is a .NET Core App and it is not an ASP.NET Web App. Using version 2.6.4 of App Insights SDK

brahmnes commented 6 years ago

Since this is not a ASP.NET core app, I suspect you may need to do extra setup to get the automatic behavior for AppInsights.

In an ASP.NET core app, there is a UseApplicationInsights() method allowing AppInsights to setup the automatic listeners. https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Getting-Started-with-Application-Insights-for-ASP.NET-Core

With a vanilla .NET core app in your case, you need to setup a DependencyTrackingTelemetryModule with your TelemetryConfiguration. This should give you the dependencies. For getting the request telemetry, I don't know how to setup the "handshake" between service bus SDK and AppInsights, other than just calling StartOperation. @lmolkova, can you provide some insights on setting up for a console style .NET core app?

lmolkova commented 6 years ago

@jstawski, with the console app you have an option to use ApplcaitonInsights.config or configure ApplicationInsights components one by one programmatically. You can find details and examples in this article: https://docs.microsoft.com/en-us/azure/application-insights/application-insights-console. If you need to enable some other scenario not covered by the article, feel free to create an issue

jstawski commented 6 years ago

@lmolkova and @brahmnes thank you for your help. I was able to track the dependecies by pulling information from multiple sources. Some feedback I have in general with Azure is the lack of good/advanced documentation. This was one of those cases. Service Fabric is a major product within Azure, why isn't there a well established documentation explaining how to set it up. Furthermore, there are two NUGET packages specifically for Service Fabric, but almost no documentation for it. I was expecting for it to work exactly like the ASP.NET Service Fabric package where I would install it and things will magically start working. I even went through the Add Application Insights wizard in Visual Studio 2017. The weird part is that I got it to work without those Service Fabric packages and I still don't understand why or what side effects it would have.

In short, I had to add Microsoft.ApplicationInsights version 2.6.4 and Microsoft.ApplicationInsights.DependencyCollector version 2.6.4. I wasn't able to initialize it declaratively through ApplicationInsights.config, but instead I had to initialize everything via code like specified here.

yantang-msft commented 6 years ago

@jstawski Thank you for your valuable feedbacks! The nuget package specifically for Service Fabric is mainly for the Context Problem, and correlation when communicating through SF remoting.

@dkkapur Are we able to improve the documentation for Service Fabric diagnostics? As @jstawski mentioned, there is no documentation for these two SF nuget packages. And the "Add Application Insights" wizard in VS does not include these libraries. Also, the AI configuration story varies based on the project types, we probably want to add a link specifically for the configuration.