Azure Application Insights Integration with my component | Community
Skip to main content
November 1, 2018

Azure Application Insights Integration with my component

  • November 1, 2018
  • 3 replies
  • 16351 views
  • 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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

3 replies

smacdonald2008
Level 10
November 1, 2018

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.

Daniel_Gordon
Adobe Employee
Adobe Employee
November 1, 2018

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...

November 2, 2018

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"

Adobe Employee
November 2, 2018

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).

September 11, 2019

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.

October 15, 2019

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.


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