Expand my Community achievements bar.

Azure Application Insights Integration with my component

Avatar

Level 1
  • In  my pom.xml , I have added the below lines of code:

<dependency>

<groupId>com.microsoft.azure</groupId>

<artifactId>applicationinsights-web</artifactId>

<version>2.2.0</version>

     </dependency>

……………………………………………………………………………..

<Export-Package>  com.microsoft.applicationinsights.*,

</Export-Package>

  • Sample code Snippet: In my @component/@service/ Servlet class(all these are felix scr annotations) used in AEM 6.2 , I have given below lines of code.

import com.microsoft.applicationinsights.TelemetryClient;

import com.microsoft.applicationinsights.TelemetryConfiguration;

import com.microsoft.applicationinsights.telemetry.TelemetryContext;

………………………………………….

…………………………………….

private static TelemetryClient telemetry = new TelemetryClient();

…………………….

By some means I m setting the instrumentation key also.

…………………

Inside a method,

try {

       My custom logic………………………..

} catch (Exception e) {

telemetry.trackException(e);

LOGGER.error(e.getMessage());

}

So whenever i instantiate the TelemetryClient , the component/service/servlet  in which i do this, goes to "satisfied" state instead of "active" state. Fo example, i have used the above client in component - i get following error:

01.11.2018 07:16:46.773 *ERROR* [FelixDispatchQueue] org.apache.sling.auth.core FrameworkEvent ERROR (org.osgi.framework.ServiceException: Service factory returned null. (Component: Project.package.CustomHandler (id of the component: 12360)))

org.osgi.framework.ServiceException: Service factory returned null. (Component: Project.package.CustomHandler  (12360))

at org.apache.felix.framework.ServiceRegistrationImpl.getFactoryUnchecked(ServiceRegistrationImpl.java:380)

at org.apache.felix.framework.ServiceRegistrationImpl.getService(ServiceRegistrationImpl.java:247)

at org.apache.felix.framework.ServiceRegistry.getService(ServiceRegistry.java:344)

at org.apache.felix.framework.Felix.getService(Felix.java:3699)

at org.apache.felix.framework.BundleContextImpl.getService(BundleContextImpl.java:470)

at org.osgi.util.tracker.ServiceTracker.addingService(ServiceTracker.java:414)

at org.apache.sling.auth.core.impl.SlingAuthenticator$AuthenticationHandlerTracker.addingService(SlingAuthenticator.java:1634)

at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:941)

at org.osgi.util.tracker.ServiceTracker$Tracked.customizerAdding(ServiceTracker.java:870)

at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256)

at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:229)

at org.osgi.util.tracker.ServiceTracker$Tracked.serviceChanged(ServiceTracker.java:901)

at org.apache.felix.framework.util.EventDispatcher.invokeServiceListenerCallback(EventDispatcher.java:991)

at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:839)

at org.apache.felix.framework.util.EventDispatcher.fireServiceEvent(EventDispatcher.java:546)

at org.apache.felix.framework.Felix.fireServiceEvent(Felix.java:4557)

at org.apache.felix.framework.Felix.registerService(Felix.java:3549)

at org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:348)

at org.apache.felix.scr.impl.manager.AbstractComponentManager$3.register(AbstractComponentManager.java:869)

at org.apache.felix.scr.impl.manager.AbstractComponentManager$3.register(AbstractComponentManager.java:857)

at org.apache.felix.scr.impl.manager.RegistrationManager.changeRegistration(RegistrationManager.java:133)

at org.apache.felix.scr.impl.manager.AbstractComponentManager.registerService(AbstractComponentManager.java:915)

at org.apache.felix.scr.impl.manager.AbstractComponentManager.activateInternal(AbstractComponentManager.java:715)

at org.apache.felix.scr.impl.manager.DependencyManager$SingleStaticCustomizer.addedService(DependencyManager.java:954)

at org.apache.felix.scr.impl.manager.DependencyManager$SingleStaticCustomizer.addedService(DependencyManager.java:915)

at org.apache.felix.scr.impl.manager.ServiceTracker$Tracked.customizerAdded(ServiceTracker.java:1215)

at org.apache.felix.scr.impl.manager.ServiceTracker$Tracked.customizerAdded(ServiceTracker.java:1136)

at org.apache.felix.scr.impl.manager.ServiceTracker$AbstractTracked.trackAdding(ServiceTracker.java:945)

at org.apache.felix.scr.impl.manager.ServiceTracker$AbstractTracked.track(ServiceTracker.java:881)

at org.apache.felix.scr.impl.manager.ServiceTracker$Tracked.serviceChanged(ServiceTracker.java:1167)

at org.apache.felix.scr.impl.BundleComponentActivator$ListenerInfo.serviceChanged(BundleComponentActivator.java:120)

at org.apache.felix.framework.util.EventDispatcher.invokeServiceListenerCallback(EventDispatcher.java:991)

at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:839)

at org.apache.felix.framework.util.EventDispatcher.fireServiceEvent(EventDispatcher.java:546)

at org.apache.felix.framework.Felix.fireServiceEvent(Felix.java:4557)

at org.apache.felix.framework.Felix.registerService(Felix.java:3549)

at org.apache.felix.framework.BundleContextImpl.registerService(BundleContextImpl.java:348)

at org.apache.felix.scr.impl.manager.AbstractComponentManager$3.register(AbstractComponentManager.java:869)

at org.apache.felix.scr.impl.manager.AbstractComponentManager$3.register(AbstractComponentManager.java:857)

at org.apache.felix.scr.impl.manager.RegistrationManager.changeRegistration(RegistrationManager.java:133)

at org.apache.felix.scr.impl.manager.AbstractComponentManager.registerService(AbstractComponentManager.java:915)

at org.apache.felix.scr.impl.manager.AbstractComponentManager.activateInternal(AbstractComponentManager.java:715)

at org.apache.felix.scr.impl.manager.AbstractComponentManager.enable(AbstractComponentManager.java:399)

at org.apache.felix.scr.impl.config.ConfigurableComponentHolder.enableComponents(ConfigurableComponentHolder.java:676)

at org.apache.felix.scr.impl.BundleComponentActivator.initialEnable(BundleComponentActivator.java:339)

at org.apache.felix.scr.impl.Activator.loadComponents(Activator.java:360)

at org.apache.felix.scr.impl.Activator.access$000(Activator.java:53)

at org.apache.felix.scr.impl.Activator$ScrExtension.start(Activator.java:260)

at org.apache.felix.utils.extender.AbstractExtender.createExtension(AbstractExtender.java:259)

at org.apache.felix.utils.extender.AbstractExtender.modifiedBundle(AbstractExtender.java:232)

at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:482)

at org.osgi.util.tracker.BundleTracker$Tracked.customizerModified(BundleTracker.java:415)

at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:232)

at org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:444)

at org.apache.felix.framework.util.EventDispatcher.invokeBundleListenerCallback(EventDispatcher.java:916)

at org.apache.felix.framework.util.EventDispatcher.fireEventImmediately(EventDispatcher.java:835)

at org.apache.felix.framework.util.EventDispatcher.fireBundleEvent(EventDispatcher.java:517)

at org.apache.felix.framework.Felix.fireBundleEvent(Felix.java:4541)

at org.apache.felix.framework.Felix.startBundle(Felix.java:2172)

at org.apache.felix.framework.Felix$RefreshHelper.restart(Felix.java:5063)

at org.apache.felix.framework.Felix.refreshPackages(Felix.java:4253)

at org.apache.felix.framework.FrameworkWiringImpl.run(FrameworkWiringImpl.java:188)

at java.lang.Thread.run(Unknown Source)

Someone kindly guide me how i could resolve this.

Thanks in advance.

13 Replies

Avatar

Level 10

I am checking internally to see if we have an example for this. I am not aware of any community examples on this use case.

Avatar

Employee Advisor

Since you are using an external dependency (that is not available in AEM ootb) I believe you need to embed the dependency in your bundle: See Embedding Dependencies - Apache Felix - Apache Felix Maven Bundle Plugin (BND) on how to update your pom...

Avatar

Level 1

Even after embedding the dependency, the component is in "satisfied" state with the same error mentioned above.

Bundle info:

===========================================

Bundle-Version: 1.0.0.SNAPSHOT

Created-By: Apache Maven Bundle Plugin

Embed-Dependency: applicationinsights-web, geronimo-atinject_1.0_specm, oak-commons, org.apache.sling.event, jose4j, adal4j, json, oauth2-oidc-sdk, nimbus-jose-jwt, json-smart, asm, org.apache.oltu.oauth2.client

Embedded-Artifacts: applicationinsights-web-2.2.0.jar; g="com.microsoft.azure"; a="applicationinsights-web"; v="2.2.0", jose4j-0.4.4.jar; g="org.bitbucket.b_c"; a="jose4j"; v="0.4.4", adal4j-1.2.0.jar; g="com.microsoft.azure"; a="adal4j"; v="1.2.0", json-20090211.jar; g="org.json"; a="json"; v="20090211", oauth2-oidc-sdk-5.26.jar; g="com.nimbusds"; a="oauth2-oidc-sdk"; v="5.26", nimbus-jose-jwt-6.2.jar; g="com.nimbusds"; a="nimbus-jose-jwt"; v="2.10.1", json-smart-2.1.1.jar; g="net.minidev"; a="json-smart"; v="2.1.1", asm-1.0.2.jar; g="net.minidev"; a="asm"; v="1.0.2", org.apache.oltu.oauth2.client-1.0.2.jar; g="org.apache.oltu.oauth2"; a="org.apache.oltu.oauth2.client"

Avatar

Employee Advisor

We have done a small POC with App Insight successfully. But our use case was to track request and send analytics to App Insight.  Before I suggest something, I need more info to understand where problem might be-

1. Do you see the applicationinsights-web dependency resolved in your core project? You can check the bundle details in OSGi.

2. Are you embedding the jar in your bundle or creating an independent bundle for external dependency?

3. How are you checking AI logs? You will need logback dependency to get that- Explore Java trace logs in Azure Application Insights | Microsoft Docs.  In your code you can enable logs through this code-

Map<String, String> settings = new HashMap<>();

  settings.put("Level", "trace");

  settings.put("UniquePrefix", "app-insight");

   // Logs are cerated here- C:\Users\<usrid>\AppData\Local\Temp

  settings.put("BaseFolder", "app-insight-web-logs");

  InternalLogger.INSTANCE.initialize("file", settings);

4. Are you initializing necessary telemetry modules? There are two options

a. load ApplicationInsights.xml in bundle resources (You can find sample xml here - Java web app analytics with Azure Application Insights | Microsoft Docs )

b. Load through code (Just a sample, you can decide on what modules you really need):

TelemetryConfiguration.getActive().setInstrumentationKey("zzz2953-zzzzz-4e9a-azzzz-848fd147zzzz");

  List<TelemetryInitializer> initializers = new ArrayList<>();

  initializers.add(new WebOperationIdTelemetryInitializer());

  initializers.add(new WebOperationNameTelemetryInitializer());

  initializers.add(new WebSessionTelemetryInitializer());

  initializers.add(new WebUserTelemetryInitializer());

  initializers.add(new WebUserAgentTelemetryInitializer());

  TelemetryConfiguration.getActive().getTelemetryInitializers().addAll(initializers);

  TelemetryModule module = new WebRequestTrackingTelemetryModule();

  module.initialize(TelemetryConfiguration.getActive());

  TelemetryConfiguration.getActive().getTelemetryModules().add(module);

5. Last- Setting instrumentation key is most important. I hope you are setting up.

So I think few immediate next steps for you-

1. Dependency check

2. Enable AI logging to console/ file level

3. Add AI jar to your code source paths in eclipse and set up break points to debug(This helped us lot when we had no clue what was wrong).

If possible, can you share the sample maven project which was implemented from your side

Avatar

Level 1

Hi Jaspians,

     Could you please share the sample PoC project that worked well with AppInsights integration ? I'll be able to get instrumentation key from our Azure AppInsights portal. Please help with this request as the customer is looking for AEM (Hosted in Azure Cloud) & Azure AppInsights seamless integration with out-of-box logger/appender configs. Thanks.

Avatar

Employee Advisor

I have created a sample project - GitHub - vivekanand-mishra/app-insights  Checkout and deploy in your local and follow the testing instruction on Github page.

Avatar

Level 1

Hi Jaspians90,

     Thanks for sending the sample maven project. We can see request is being logged in AppInsights Dashboard but events data as per ApplicationInsights.xml is NOT being added to the request entry in AppInsights. While debugging more, we could see below error in SDK AppInsights temp log -

INFO 22-09-2019 20:45:44.612+0000, 27(FelixStartLevel): Configuration file 'ApplicationInsights.xml' was NOT  found by default class loader

WARN 22-09-2019 20:45:44.619+0000, 27(FelixStartLevel): Failed to find configuration file, exception while fetching from library location: Exception : 'java.lang.NullPointerException

at java.io.File.<init>(Unknown Source)

at com.microsoft.applicationinsights.internal.config.ConfigurationFileLocator.getConfigurationFromLibraryLocation(ConfigurationFileLocator.java:158)

at com.microsoft.applicationinsights.internal.config.ConfigurationFileLocator.getConfigurationFile(ConfigurationFileLocator.java:80)

at com.microsoft.applicationinsights.internal.config.TelemetryConfigurationFactory.initialize(TelemetryConfigurationFactory.java:119)

at com.microsoft.applicationinsights.TelemetryConfiguration.getActive(TelemetryConfiguration.java:70)

at com.insights.sample.core.filters.AppInsightsTestFilter.init(AppInsightsTestFilter.java:69)

Additionally, I could NOT see any log file is being generated under the location - ( // Logs are cerated here- C:\Users\<usrid>\AppData\Local\Temp).

We are using your sample maven project AS IS in 6.4.2.0 (adjusting just uber.jar version to make it compatible with 6.4.2.0) and added just our application instrumentation key.

Could you please see above error and advise why this error is coming & please help to resolve the same ? Thanks for your help.

Avatar

Level 1

Hi Jaspians,

    Could you please advise one additional thing  if you could make AppInsights Java Agent jar file work with AEM ? Could you please guide us on some pointers for AppInsights Java Agent working with AEM ? Thanks.

Regards,

Abhay

Avatar

Employee Advisor

Hi Abhay,

I hope you are following this article- Performance monitoring for Java web apps in Azure Application Insights | Microsoft Docs

I have not tried this. But I suggest modify your AEM start up script and change this line to include java-agent:

if not defined CQ_JVM_OPTS set CQ_JVM_OPTS=-Xmx1024m -XX:MaxPermSize=256M -Djava.awt.headless=true -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8080,suspend=n -javaagent:<full path to agent JAR file>

Are you facing any issue while setting up agent?

Thanks.

Hi Vivekanand,

       I am following the same above link for Java Agent set up in AEM. But somehow AppInsights Java Agent  is NOT capturing "Dependency calls" on it's own for AEM server side code. Ideally Java Agent must be able to track HTTP requests (server side tracking, not the client side), dependencies and exceptions.

Using sample example web app (not AEM app) deployed in Azure Tomcat - ApplicationInsights-Java/samples/app-services at master · microsoft/ApplicationInsights-Java · GitH... , I am able to capture HTTP requests (server side tracking, not the client side), dependencies and exceptions.

Now I need your help to make Java Agent working in AEM 6.4.2.0. Please be informed that as per documentation, I am putting java agent jar & AI-Agent.xml in the same folder but somehow dependency calls are NOT being tracked. Using your earlier provided Web Filter, I was able to track Server Side Web request and Unhandled exception. Request you to guide us on making "AppInsights Java Agent"  working with AEM. Thanks for the help.

Regards,

Abhay

Avatar

Employee Advisor

Hi Abhay,

There was some embed-dependency issue with the repo that I shared. I have updated the code at - GitHub - vivekanand-mishra/app-insights . Now, It should be running fine and request tracking should work properly with AEM just make sure to provide correct instrumentation key. Can you try out this in your AEM app and confirm back. I am looking for contributions in this repo to make this robust for any future AEM-App Insight Integration. If interested, please share your git user details, I will add you to it.

Regarding java agent - it has 3 simple steps to configure with AEM:

1. Deploy web tracking filter code to AEM (Reference code- GitHub - vivekanand-mishra/app-insights ) and ensure that the request tracking is working fine

2. Modify start.bat script to add javaagent parameter as below:

if not defined CQ_JVM_OPTS set CQ_JVM_OPTS=-Xmx1024m -XX:MaxPermSize=256M -Djava.awt.headless=true -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8080,suspend=n -javaagent:<full path to agent JAR file>

3. Check AEM error log and the app insight logs (Location: C:\Users\<usrid>\AppData\Local\Temp) for any issue.

In case if this does not work please contact Microsoft Azure team through their support center, they should be able to guide further on this topic.

Thanks!

There is a configurationPid = "com.insights.sample.core.filters.AppInsightsConfigFilter in the AppInsightsTestFilter but I am not able to find this above class. Can you help on that?