Can't load AppInsights under Spring Boot

I followed https://docs.microsoft.com/en-us/azure/application-insights/app-insights-java-get-started but still unsuccessfully.

  • I have applicationinsights web dependency in place via maven
  • I added ApplicationInsights.xml

    to main / resources with a hardcoded tool key and even with <SDKLogger />

    inside
  • I added the scan path: @ComponentScan({...., "com.microsoft.applicationinsights.web.spring"})

Results:

  • I don't see any logs about looking up the config file even if I make a syntax error or delete it entirely.
  • In debug I see what RequestNameHandlerInterceptorAdapter

    is created through com.microsoft.applicationinsights.web.spring.internal.InterceptorRegistry

    , and during calls the method is called preHandle

    , but calls ThreadContext.getRequestTelemetryContext()

    always return to zero and nothing else happens

It looks like it's something obvious, but I don't know what. Which part / classes are responsible for loading the config file?

+3


source to share


4 answers


I was a little confused with the documentation. As yonisha mentioned, the filter does all the magic. The following configuration class takes care of creating and adding a filter to your Spring Boot application.

import com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

@Configuration
@ComponentScan("com.microsoft.applicationinsights.web.spring")
public class ApplicationInsightsConfiguration {

@Bean
public FilterRegistrationBean someFilterRegistration() {

    FilterRegistrationBean registration = new FilterRegistrationBean();
    registration.setFilter(appInsightsWebRequestTrackingFilter());
    registration.addUrlPatterns("/*");
    registration.setName("webRequestTrackingFilter");
    registration.setOrder(1);
    return registration;
}


@Bean(name = "appInsightsWebRequestTrackingFilter")
public Filter appInsightsWebRequestTrackingFilter() {
    return new WebRequestTrackingFilter();
}

      

Important: This will work well if you set the property to a server.context-path

value. If not, AI initialization will fail.



AI: ERROR 03-04-2017 14:11, 20: WebApp name is not found, unable to register WebApp

      

To keep the servlet context empty I had to implement wrappers for the filter and 2 other classes to override it, but that was a very messy fix ... It would be great if the name could be passed as a parameter to the filter, but that's not yet possibly ( https://github.com/Microsoft/ApplicationInsights-Java/issues/359 )

+4


source


In spring boot we need to customize WebRequestTrackingFilter

by extending WebSecurityConfigurerAdapter

and overridingconfigure(HttpSecurity httpSecurity)

    @Bean
    public WebRequestTrackingFilter applicationInsightsFilterBean() throws Exception {
        WebRequestTrackingFilter webRequestTrackingFilter = new WebRequestTrackingFilter();
        return webRequestTrackingFilter;
    }

    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        //other stuff...
        httpSecurity.addFilterBefore(applicationInsightsFilterBean(), UsernamePasswordAuthenticationFilter.class);
    }

      



you also need to have below config.

  • applicationinsights-web dependency in place via maven
  • added ApplicationInsights.xml to main / resources
+1


source


Below is the new Spring Boot Application Insights integration guide that now works well for me:

https://github.com/AzureCAT-GSI/DevCamp/tree/master/HOL/java/06-appinsights

The idea is basically that Tomas is taller with some minor differences.

package devCamp.WebApp.configurations;

import javax.servlet.Filter;

import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter;


@Configuration
public class AppInsightsConfig {

    @Bean
    public String telemetryConfig() {
        String telemetryKey = System.getenv("APPLICATION_INSIGHTS_IKEY");
        if (telemetryKey != null) {
            TelemetryConfiguration.getActive().setInstrumentationKey(telemetryKey);
        }
        return telemetryKey;
    }

    @Bean
    public FilterRegistrationBean aiFilterRegistration() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new WebRequestTrackingFilter());
        registration.addUrlPatterns("/**");
        registration.setOrder(1);
        return registration;
    } 

    @Bean(name = "WebRequestTrackingFilter")
    public Filter WebRequestTrackingFilter() {
        return new WebRequestTrackingFilter();
    }   
}

      

The tutorial from the link above has a complete set of instructions and includes client-side js and sample java log appender. Hope this helps.

+1


source


The above method works! However, you can try out a whole new seamless experience using the Application Insights SpringBoot Starter.

https://github.com/Microsoft/ApplicationInsights-Java/blob/master/azure-application-insights-spring-boot-starter/README.md

Currently at BETA

0


source







All Articles