0

I am trying to integrate Azure App insights service into the service fabric app for logging and instrumentation. I am running fabric code on my local VM. I exactly followed the document here [scenario 2]. Other resources on learn.microsoft.com also seem to indicate the same steps. [ex: https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-diagnostics-event-aggregation-eventflow For some reason, I don’t see any event entries in App insights. No errors in code when I do this:

ServiceEventSource.Current.ProcessedCountMetric("synced",sw.ElapsedMilliseconds, crc.DataTable.Rows.Count);

eventflowconfig.json contents


    {
      "inputs": [
        {
          "type": "EventSource",
          "sources": [
            { "providerName": "Microsoft-ServiceFabric-Services" },
            { "providerName": "Microsoft-ServiceFabric-Actors" },        
            { "providerName": "mystatefulservice" }
          ]
        }
      ],
      "filters": [
        {
          "type": "drop",
          "include": "Level == Verbose"
        }
      ],
      "outputs": [
        {
          "type": "ApplicationInsights",
          // (replace the following value with your AI resource's instrumentation key)
          "instrumentationKey": "XXXXXXXXXXXXXXXXXXXXXX",
          "filters": [
            {
              "type": "metadata",
              "metadata": "metric",
              "include": "ProviderName == mystatefulservice && EventName == ProcessedCountMetric",
              "operationProperty": "operation",
              "elapsedMilliSecondsProperty": "elapsedMilliSeconds",
              "recordCountProperty": "recordCount"
            }
          ]
        }
      ],
      "schemaVersion": "2016-08-11"
    }

In ServiceEventSource.cs

[Event(ProcessedCountMetricEventId, Level = EventLevel.Informational)]
    public void ProcessedCountMetric(string operation, long     elapsedMilliSeconds, int recordCount)
    {
        if (IsEnabled())
            WriteEvent(ProcessedCountMetricEventId, operation, elapsedMilliSeconds, recordCount);
    }

EDIT Adding diagnostics pipeline code from "Program.cs in fabric stateful service

using (var diagnosticsPipeline =
                ServiceFabricDiagnosticPipelineFactory.CreatePipeline($"{ServiceFabricGlobalConstants.AppName}-mystatefulservice-DiagnosticsPipeline")
            )
            {
                ServiceRuntime.RegisterServiceAsync("mystatefulserviceType",
                    context => new mystatefulservice(context)).GetAwaiter().GetResult();

                ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id,
                    typeof(mystatefulservice).Name);

                // Prevents this host process from terminating so services keep running.
                Thread.Sleep(Timeout.Infinite);
            }
Diego Mendes
  • 10,631
  • 2
  • 32
  • 36
teeboy
  • 408
  • 3
  • 13
  • Are there any logs or anything that can help guide us? – Jeff Breadner Mar 07 '18 at 04:43
  • In VS, Under View -> Other Windows select Diagnostic Events. This is a local ETW (EventSource) listener. You can check that events are getting emitted correctly. – Matt Trower - MSFT Mar 07 '18 at 06:06
  • Yes, Matt. I do see see the event in local ETW viewer. But, I want the events to show up in Azure app insights. Not local. Jeff, I don't see any logs on local that indicate errors in transmission to azure. – teeboy Mar 07 '18 at 11:38
  • That event source & event flow config all looks correct to me, however event sources can be finnicky. I've had good luck using [this package](https://www.nuget.org/packages/EnterpriseLibrary.SemanticLogging.EventSourceAnalyzer/) in unit testing to help find issues with event sources. Once you've verified that the event source is properly configured and being called, I would suggest playing with that filter on the app insights output and see if that's potentially your issue. I've had experience where filters don't always work like one would expect. – pdylanross Mar 07 '18 at 14:01
  • @ pdylanross: The eventsourceconfig looks good. Do you think this is probably a firewall issue of some kind on Azure? Do I have to open ports or something for on-prem events to show up on Azure App insights? – teeboy Mar 07 '18 at 16:22
  • I think we can rule out EventSource issues if you are seeing the events in Diagnostic Events. Did you initialize EventFlow within your process? I don't see any mention of you adding that code. – Matt Trower - MSFT Mar 07 '18 at 19:34
  • Yes, I do Matt. Please see edit to my original post – teeboy Mar 08 '18 at 00:56
  • EventFlow uses Service Fabric health subsystem to report warnings/errors in SF environment so check the SF dashboard to see if your services are healthy and whether they report any errors. Also, if you run your app under the debugger, the AppInsights SDK will output traces it tries to send to the backend into the Debug window. Lack of traces there suggests that something is not configured right--most likely the instrumentation key is not being read from the configuration, or is invalid/points to non-existent resource. – Karol Z Mar 08 '18 at 17:17
  • I do see the traces. I see the "Application insights telemetry data" in the output window along with the full payload. I also see the trace output in the "Diagnostics window" in Visual studio. The event is never posted over to "Azure App insights". Btw, I am running this SF app on my local dev machine inside corporate intranet. Double checked the "instrumentation key". The log messages are viewable in "App insights" -> "Overview" -> "search" tab, correct? – teeboy Mar 08 '18 at 18:44
  • If it is a _metric_ you expect to get, I would go to Metric Explorer, Metrics (preview) or Search | Analytics and do a query against "customMetrics". The last will give you access to raw data and is probably the best for troubleshooting. – Karol Z Mar 08 '18 at 20:24

2 Answers2

0

Event Source is a tricky technology, I have been working with it for a while and always have problems. The configuration looks good, It is very hard to investigate without access to the environments, so I will make my suggestions.

There are a few catches you must be aware of:

  • If you are listening etw events from a different process, your process must be running with a user with permissions on 'Performance Log Users. Check which identity your service is running on and if it is part of performance log users, who has permissions to create event sessions to listen for these events.

  • Ensure the events are being emitted correctly and you can listen to them from Diagnostics Events Window, if it is not showing there, there is a problem in the provider.

  • For testing purposes, comment out the line if (IsEnabled()). it is an internal check to validate if your events should be emitted. I had situations where it is always false and skip the emit of events, probably it cache the results for a while, the docs are not clear how it should work.

  • Whenever possible, use the EventSource from the nuget package instead of the framework one, the framework version is full of bugs and lack fixes found in the nuget version.

  • Application Insights are not RealTime, sometimes it might take a few minutes to process your events, I would recommend to output the events to a console or file and check if it is listening correctly, afterwards, enable the AppInsights.

Diego Mendes
  • 10,631
  • 2
  • 32
  • 36
  • Sorry, but this answer has some good advice, but also some advice that is simply not true. The membership in "Performance Log Users" is only necessary if one wants to capture ETW (Event Tracing for Windows) events from different processes. If you are just using EventSource/EventListener infrastructure, this is not necessary. – Karol Z Mar 08 '18 at 17:05
  • Also, the framework EventSource "System.Diagnostic.Tracing.EventSource" is almost always the right thing to use. This is the one that all .NET Frameworks are using, including Service Fabric, so if you want to combine traces from the frameworks and from your code, the framework EventSource/EventListener make it possible. The NuGet version should be used only if you know about _specific feature or bug_ that is missing from/present in the framework version of the EventSource and the NuGet version unblocks you. – Karol Z Mar 08 '18 at 17:12
  • Thanks Karol. I double checked *all* suggestions. "IsEnabled()" in "WriteEvent" does return true. I am also able to see the event in local Visaul studio "Diagnostics window". It's just that the event never makes it's way to App Insights in the cloud. Grasping at straws.. – teeboy Mar 08 '18 at 18:40
  • @KarolZ, thanks for your comments, I have updated the answer to make it clear about the user membership. Regarding the second point, I have had many problems with old versions of EventSource and once moved to the package version never used the framework version anymore, but I trust your feedback. – Diego Mendes Mar 09 '18 at 17:14
  • Looks like this could be due to firewall blocks from inside corporate network per https://learn.microsoft.com/en-us/azure/application-insights/app-insights-ip-addresses . Trying the same code from home worked!. so, will work with corporate and see if they can open up the firewall ports. – teeboy Mar 22 '18 at 20:53
0

The link you provide is quite outdated and there's actually a much better way to log application error and exception info to Application insights. For example, the above won't help with tracking the call hierarchy of an incoming request between multiple services.

Have a look at the Microsoft App Insights Service Fabric nuget packages. It works great for:

  • Sending error and exception info
  • Populating the application map with all your services and their dependencies (including database)
  • Reporting on app performance metrics,
  • Tracing service call dependencies end-to-end,
  • Integrating with native as well as non-native SF applications
Adriaan de Beer
  • 1,136
  • 13
  • 23