Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling Application Insights from Mule Application Options. #2984

Open
rampratapa opened this issue Mar 28, 2023 · 14 comments
Open

Enabling Application Insights from Mule Application Options. #2984

rampratapa opened this issue Mar 28, 2023 · 14 comments

Comments

@rampratapa
Copy link

rampratapa commented Mar 28, 2023

Hi, Good evening, I am trying to enable AppInsights into a Mulesoft API to do POC. I came across the following article on implementing the AppInsights using log4j appender.

https://help.mulesoft.com/s/article/Add-Microsoft-Application-Insights-Log-Appender-In-Mule-4-Application

The above article works and it is sending log data to App Insight workspace but it is not send dependencies and other information to the workspace to create Application MAP etc. Also the above article says "This document applies to Application Insights Java 2.x which is no longer recommended (although not excluded from use) by Microsoft. Documentation for the latest version can be found at Application Insights Java 3.x, however, this new version requires the addition of a jar file that can be called using -javaagent. We don't support the use of -javaagent on Cloudhub or Runtime Fabric at this time."

Based on the Microsoft website (https://github.com/microsoft/ApplicationInsights-Java/releases/tag/3.4.0) there is no need of Log4j2 as it automatically captured if we use agent. How can we invoke/start the agent within API using configuration ( like spring bean and DI) not using java argument to pass the JAR file. Is this feature is available with current version or can we use applicationinsights-runtime-attach or any other wrapper to invoke it. Can anyone provide some guidance so that we can implement AppInsights in Mulesoft API application.

Thank you for your help.

Thanks
Ram

@heyams
Copy link
Contributor

heyams commented Mar 28, 2023

Please check out how to enable Application Insights runtime attach programmatically and let us know if that works.

@rampratapa
Copy link
Author

rampratapa commented Mar 28, 2023

@heyams , Good evening, thank you for your quick response. I tried that in a different way as the mule API uses its own way to initialize it, I tried different way and not sure this will work.

I used org.springframework.beans.factory and used DI to call method.

I have defined the spring bean

<bean id="appinsightAttach"
          class="com.microsoft.applicationinsights.attach.ApplicationInsights"
          factory-method="attach">
     </bean>

and I was expected it will attach the agent,  as I have added the following dependcies to POM

<!-- Application Insights dependencies. -->
   <dependency>
     <groupId>com.microsoft.azure</groupId>
     <artifactId>applicationinsights-logging-log4j2</artifactId>
     <version>${applicationinsights.version}</version>
   </dependency>
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/applicationinsights-web -->
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>applicationinsights-web</artifactId>
    <version>3.4.10</version>
</dependency>

 <!-- https://mvnrepository.com/artifact/com.microsoft.azure/applicationinsights-web-auto -->
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>applicationinsights-web-auto</artifactId>
    <version>2.6.4</version>
</dependency>
 
 <!-- https://mvnrepository.com/artifact/com.microsoft.azure/applicationinsights-core -->
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>applicationinsights-core</artifactId>
    <version>3.4.10</version>
</dependency>

 <!-- https://mvnrepository.com/artifact/com.microsoft.azure/applicationinsights-agent -->
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>applicationinsights-agent</artifactId>
    <version>3.4.0</version>
</dependency>
 <dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>applicationinsights-runtime-attach</artifactId>
    <version>3.4.10</version>
</dependency>

However, I did not see any error in the log file at the same time I am not seeing any activity in AppInsights works space. I am still researching.

Please let me know if this approach is correct or something I need to attempt.

Thanks
Ram

@heyams
Copy link
Contributor

heyams commented Mar 28, 2023

this is the only dependency you need:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>applicationinsights-runtime-attach</artifactId>
    <version>3.4.10</version>
</dependency>

and then

In your code, you will need to invoke attach():

@SpringBootApplication
public class SpringBootApp {

  public static void main(String[] args) {
    ApplicationInsights.attach();
    SpringApplication.run(SpringBootApp.class, args);
  }
}

you can configure it using applicationinsights.json. or use APPLICATIONINSIGHTS_CONNECTION_STRING env var to send telemetry to the desired app insights resource. Hope this helps.

@rampratapa
Copy link
Author

rampratapa commented Mar 28, 2023

@heyams thank you for your quick response. I am trying to enable logging for SDK, is there anyway we can enable logging to seeing issues related to this attach. Earlier i was using the following ApplicationInsights.xml file but based on the article xml is no longer supported "Spring's application.properties or application.yaml files are not supported as as sources for Application Insights Java configuration."

     <SDKLogger type="FILE">
           <Level>TRACE</Level>
           <UniquePrefix>AI</UniquePrefix>
           <BaseFolderPath>C:/temp</BaseFolderPath>
       </SDKLogger>

I have used the following configuration to assign connection string and enable debugging. but did not work

{
   "connectionString": "InstrumentationKey=xxxxxxxxxx;IngestionEndpoint=https://centralus-2.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/",
   "instrumentation": {
    "logging": {
      "level": "DEBUG"
    }
  	}
 
}

Thank you
Ram

@heyams
Copy link
Contributor

heyams commented Mar 29, 2023

Here is the self-diagnosis log location by default:
https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-spring-boot#self-diagnostic-log-file-location

and this is how you set up self-diagnostics log:
https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-standalone-config#self-diagnostics

{
  "instrumentation": {
    "logging": {
      "level": "DEBUG"
    }
  }
}

this is used for setting up auto-collected logging level not the self-diagnostics logging level, here has more details.

If it still doesn't work, can you please share the error in the applicationinsights.log once you have the correct self-diagnostics log setup?

@rampratapa
Copy link
Author

@heyams, I am getting the following error "Agent is not attached because runtime attachment was not requested from main thread." as I am using MuleSoft, I have created a Bean using the following code

import com.microsoft.applicationinsights.attach.ApplicationInsights;

import java.util.logging.Logger;

import org.springframework.beans.factory.InitializingBean;

public class AppInsightsInitialize implements InitializingBean{
  String message;
  private static final Logger logger = Logger.getLogger(AppInsightsInitialize.class.getName());
  public void setMessage(String message) {
	  this.message = message;
	}
  public void afterPropertiesSet() throws Exception {
	  ApplicationInsights.attach();
	  System.out.println("Init method after properties are set" + message);
	  logger.info("Init method after properties are set" + message);
	}
}

Thinking, the initialization happens and it will be attach it. I am getting the above error. is there any workaround.

Thanks
Ram

@jeanbisutti
Copy link
Member

Hi @rampratapa,

ApplicationInsights.attach() has to be called at the beginning of the main method, an example here.

@rampratapa
Copy link
Author

rampratapa commented Mar 31, 2023

@jeanbisutti good morning, As I am using Mulesoft designer for API and main method NOT available to access. I think, the sequence I am seeing their launching sequence based on the logs

Launching a JVM...
Starting the Mule Container...
WrapperManager: Initializing...

When I capture the thread names and it says

setInitalize AppInsights Current Thread Name: WrapperListener_start_runner Parent Thread Name: main

I am trying to figure it out to see if there are any events available to invoke the AppInsights.

Are there any other options to use AppInsights as Mule won;t allow Javaarguments with an external file to start the application.

Thanks
Ram

@trask
Copy link
Member

trask commented Mar 31, 2023

Hi @rampratapa,

Since you aren't able to use the Java agent, you can still use the Azure Monitor OpenTelemetry Exporter directly:

https://learn.microsoft.com/en-us/java/api/overview/azure/monitor-opentelemetry-exporter-readme?view=azure-java-preview

  • AzureMonitorExporterBuilder.buildLogRecordExporter() to create the OpenTelemetry LogRecordExporter

and

https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/log4j/log4j-appender-2.17/library

to configure your log4j appender

let us know if and where you have trouble with this approach and we can help further

@rampratapa
Copy link
Author

Good afternoon @trask, Thank you for the response. It looks like we need to instrument the application before using the exporter "This package assumes your application is already instrumented with the OpenTelemetry SDK following the OpenTelemetry Specification." What I am trying to do is, if possible with minimal code with auto-instrumentation we wanted to enable AppInsights in Mulesoft API deployed to cloudHub. I was able to use Log4J Appender to send data to Appinsights workspace but it is not providing any dependencies or application map etc. Also, all the information we are logging is going as message so, no value other than helping to log all logs at central location and we can not convince team the need of AppInsights to just log. We wanted to use full potential if possible and demonstrate value so that we can request teams to implement it.

Thank you for your help.
Ram

@trask
Copy link
Member

trask commented Mar 31, 2023

What I am trying to do is, if possible with minimal code with auto-instrumentation we wanted to enable AppInsights in Mulesoft API deployed to cloudHub

the only option for minimal code / auto-instrumentation is using the Java agent

do you know what the issue is with Mule applications not allowing -javaagent arg?

@rampratapa
Copy link
Author

rampratapa commented Apr 3, 2023

@trask

What I am trying to do is, if possible with minimal code with auto-instrumentation we wanted to enable AppInsights in Mulesoft API deployed to cloudHub

the only option for minimal code / auto-instrumentation is using the Java agent

do you know what the issue is with Mule applications not allowing -javaagent arg?

Based on my understanding, CloudHub of Mule is deployed in AWS and they run API's in a Mule Container. They did not provide any mechanism for share file storage that it can recognize and we can not upload agent jar and refer and pass argument. One more thing, I ran the agent on my local and it only captures two methods which are related to Mule and not capturing the API that I have deployed.

image

if you look into the above attached screenshot, it only capture two methods, GET /mule/applications and GET mule/domains.
it looks like there another issue even if we use agent.

I am going to raise a ticket with Mule to see if they can help.

Please let me know if you need more information or if you wanted me try something different.

Thank you for your help.

Thanks
Ram

@king-alan-del
Copy link

I am going to raise a ticket with Mule to see if they can help.

Hi Ram, I'm trying to do something similar and have run into the same issues you have (only seems to log /mule/applications and /mule/domains requests but none of the regular logger compoonents that we'd see with log4j2 or the Log component). Did you end up getting a solution to this issue?

@eldevitto
Copy link

HI @rampratapa / @king-alan-del - Were either of you able to get any farther on this? I am running into the same challenge. Logs going in as traces is working great using log4j method that MuleSoft has documented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants