microsoft / ApplicationInsights-Java

Application Insights for Java
http://aka.ms/application-insights
Other
296 stars 199 forks source link

Spring initialization not working? #273

Closed uded closed 8 years ago

uded commented 8 years ago

While initializing the RequestNameHandlerInterceptorAdapter I can't make it to work automatically as described. I am using annotation based config, but still the reaction is somewhat blank:

@EnableMetrics
@SpringBootApplication(scanBasePackages = {"my.package", "com.microsoft.applicationinsights.web.spring"})
public class FrontEndApplication {
    public static void main(String[] args) {
        SpringApplication.run(FrontEndApplication.class, args);
    }
}

If I will manually call TelemetryConfiguration.getActive() it will work like a charm...

@EnableMetrics
@SpringBootApplication(scanBasePackages = {"my.package", "com.microsoft.applicationinsights.web.spring"})
public class FrontEndApplication {
    public static void main(String[] args) {
        SpringApplication.run(FrontEndApplication.class, args);
        TelemetryConfiguration configuration = TelemetryConfiguration.getActive();
    }
}

Is this a correct way and I need to manually initialize ApplicationInisights? Or am I missing something else...

gupele commented 8 years ago

Thanks, We're looking into it. Can you please share more data, like the ApplicationInsights,xml file, initialization logs?

uded commented 8 years ago

Here you go:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationInsights xmlns="http://schemas.microsoft.com/ApplicationInsights/2013/Settings" schemaVersion="2014-05-30">
    <!-- The key from the portal: -->
    <InstrumentationKey>[[obfuscated]]</InstrumentationKey>

    <SDKLogger/>

    <!-- HTTP request component (not required for bare API) -->
    <TelemetryModules>
        <Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebRequestTrackingTelemetryModule"/>
        <Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebSessionTrackingTelemetryModule"/>
        <Add type="com.microsoft.applicationinsights.web.extensibility.modules.WebUserTrackingTelemetryModule"/>
    </TelemetryModules>

    <!-- Events correlation (not required for bare API) -->
    <!-- These initializers add context data to each event -->
    <TelemetryInitializers>
        <Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebOperationIdTelemetryInitializer"/>
        <Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebOperationNameTelemetryInitializer"/>
        <Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebSessionTelemetryInitializer"/>
        <Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebUserTelemetryInitializer"/>
        <Add type="com.microsoft.applicationinsights.web.extensibility.initializers.WebUserAgentTelemetryInitializer"/>
    </TelemetryInitializers>
</ApplicationInsights>

I am initalizing on the main class like this:

@SpringBootApplication(scanBasePackages = {"my.package", "com.microsoft.applicationinsights.web.spring"})

During the start up phase I only got this message:

AI: INFO 07-01-2016 00:41, 1: Configuration file has been successfully found as resource
AI: WARN 07-01-2016 00:41, 1: 'MaxTelemetryBufferCapacity': null value is replaced with '500'
AI: WARN 07-01-2016 00:41, 1: 'FlushIntervalInSeconds': null value is replaced with '5'
AI: TRACE 07-01-2016 00:41, 1: Using Http Client version 4.3+
AI: TRACE 07-01-2016 00:41, 1: No back-off container defined, using the default 'EXPONENTIAL'
AI: WARN 07-01-2016 00:41, 1: 'Channel.MaxTransmissionStorageCapacityInMB': null value is replaced with '10'
AI: TRACE 07-01-2016 00:41, 1: Windows performance counters are not relevant on this OS.
AI: ERROR 07-01-2016 00:41, 1: Performance Counter JSDK_ProcessIOPerformanceCounter: Error in file '/proc/18672/io': Can not read
AI: ERROR 07-01-2016 00:41, 1: Performance Counter JSDK_TotalCpuPerformanceCounter: Error in file '/proc/stat': Can not read
AI: ERROR 07-01-2016 00:41, 1: Performance Counter JSDK_TotalMemoryPerformanceCounter: Error in file '/proc/meminfo': Can not read
AI: TRACE 07-01-2016 00:41, 1: Registering PC 'JSDK_ProcessMemoryPerformanceCounter'
AI: TRACE 07-01-2016 00:41, 1: Registering PC 'JSDK_ProcessCpuPerformanceCounter'
AI: TRACE 07-01-2016 00:41, 1: Registering PC 'JSDK_ProcessIOPerformanceCounter'
AI: TRACE 07-01-2016 00:41, 1: Registering PC 'JSDK_TotalCpuPerformanceCounter'
AI: TRACE 07-01-2016 00:41, 1: Registering PC 'JSDK_TotalMemoryPerformanceCounter'

I am uncertain whether I can get more, I was not looking into this at all. If there is a way to get more, just tell me how.

It seems like there is no data being send, no metrics for any of my HTTP methods.

When I tried to debug what I've found is that in the com.microsoft.applicationinsights.web.spring.RequestNameHandlerInterceptorAdapter#preHandle method ThreadContext.getRequestTelemetryContext() is returning null. But then on my local machine it seems like it is always null.

I was able to send my own, custom telemetry though...

gupele commented 8 years ago

Thanks a lot! Are you using a filter? Please check the 'Add an HTTP filter' in https://www.visualstudio.com/en-us/get-started/insights/app-insights-java and, from the log you've sent, seems there is a problem reading some system files, can you please point out the OS version you're using?

uded commented 8 years ago

No, I am not using a filter. Spring configuration, if I understood that correctly, requires only to register a configuration bean, specifically:

@EnableWebMvc
@Configuration
public class InterceptorRegistry extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(org.springframework.web.servlet.config.annotation.InterceptorRegistry registry) {
        registry.addInterceptor(new RequestNameHandlerInterceptorAdapter());
    }
}

This is far from perfect IMO. It requires me to use a trick by providing a wider scanning classpath like this:

@SpringBootApplication(scanBasePackages = {"my.package", "com.microsoft.applicationinsights.web.spring"})

It would be much more efficient if you could provide a simple annotation, as pretty much any Spring related library, in a form of:

@EnableApplicationInsights

That should be way more efficient and is much closer to what all Spring related libraries are doing for a long time now.

Happy to do that for you. Although I still do not know why ThreadContext.getRequestTelemetryContext() is returning null. I had only little time to debug the lib itself. But just from the look of it the logic within Spring interceptor and servlet filter is different, just by comparison to com.microsoft.applicationinsights.web.internal.WebRequestTrackingFilter#init.

When it comes to the system files - I am using MacOSX 10.11.2 as my development OS and I do not have `/proc/ folder on my machine. It seems like your library have no support for this operating system, which is fine by me - I won't run my app on MacOSX anyway, just developing on this machine...

gupele commented 8 years ago

Yes, using the filter is currently the the way to go. And yes, we know that it should be configurable in an easier way and definitely annotation is a good choice. This is on our backlog and I hope we can get to it soon enough

uded commented 8 years ago

OK, hold on - the interceptor should have pretty much the same functionality as filter. I would prefer to avoid using technology which is not Spring oriented. So the only way to go is with filter? Then one should update official docs, since they suggest using the interceptor alone...

hannayurkevich commented 7 years ago

Hi guys.

I have the same issue. Can you suggest how to configure this via interceptors?

UPDATE: The documentation https://docs.microsoft.com/en-us/azure/application-insights/app-insights-java-get-started states that it is possible to configure it using interceptors, but in fact it seems not...

sudhanshu20 commented 6 years ago

Hi guys Is this issue with interceptors fixed? I tried with the filter configuration , it works but tomcat 8.5 does not seem to gracefully stop after adding this filter. This creates issues with jenkins auto build and deploy.

dhaval24 commented 6 years ago

@sudhanshu20 can you please create a seperate issue with the details of the behavior your application is facing. The InterceptorRegistry is still present which has @EnableWebMvc still there. w It is concerning for the springboot-starter too but the current build is locked for release so most likely the change will come in the next version.

It would be great if you can create a seperate issue for follow up on this with more details of exact behavior in the system.

sudhanshu20 commented 6 years ago

Created new issue https://github.com/Microsoft/ApplicationInsights-Java/issues/712