-
Notifications
You must be signed in to change notification settings - Fork 205
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
POST /QuickPulseService.svc fails if instrumentation key is set programmatically #573
Comments
@julandw this is a very interesting situation and I haven't faced it personally. I would be happy to replicate this on my end but few questions to get an understanding : -
Now answering your questions : -
Now coming to why in the first place. The indication that QuickPulse is having null instrumentation key is because some how the beans initialization order in the Springboot application leaves the All this being said I realize that there is a weak spring boot documentation and hence is causing troubles on onboarding. I apologize but would suggest that you follow the following guide to onboard to spring boot with AI : https://github.com/AzureCAT-GSI/DevCamp/tree/master/HOL/java/06-appinsights There is one exception though in above :
Passing the application name in the WebRequestTrackingFilter and setting the order of filter to 1 is very important. I hope this helps and if problem persists please let us know. Also as a Spring Boot user will this help for you : #518 We are in process of creating an amazing starter for SpringBoot which we think could allow in friction free onboarding to spring boot apps. Do you think it would be valuable for ubiquitous usage of AI in your place? Let me know and hope this was helpful! |
Hello dhaval, thanks for the quick answer! To answer your questions:
Actually the basic idea is to set the instrumentation key as Spring application property and not as environment variable, quite similar as suggested in the pull request: #518. This allows a more Spring like configuration being able to easily define different instrumentation keys for different Spring profiles. So we do not set the instrumentation key as environment variable at, which leads to the described problem. In our current code, we read the instrumentation key from the Spring application properties file and configure the active TelemetryConfiguration object (if it not already set):
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.getActive(); We do this just before registering the WebRequestTrackingFilter (so that the filter always has the correct instrumentation key). For the Logback-Appender we explicitly set the same instrumentation key by first retrieving the instrumentation key as Spring property: and then initialize the Logback-Appender accordingly:
As an alternative you could also set the instrumentation key for the active TelemetryConfiguration instance at the earliest possible time in a ApplicationListener instance, which must be registered with the SpringApplication on startup. Doing this it is not necessary to explicitly configure the instrumentation key in the Logback-Appender, as the Logback-Appender automatically uses the active TelemetryConfigurtion instance. The problem with the "QuickPulse" implementation is that the initialization process takes place before the Spring environment (e.g. application properties) is properly set up and thus even before the "ApplicationEnvironmentPreparedEvent" is fired. At that time the instrumentation key is not (if not set via an environment variable or system property) leading to the original problem. So basically I see only two possibilities: b. or the "QuickPulse" service continuously tries to retrieve a valid instrumentation key from the active TelemetryConfiguration instance until it gets a non-null value. I would prefer the second option (and maybe there are other options too). Does this sound reasonable to you?
I tested it with version 1.0.9 and 2.0.0-BETA (version 1.0.10 seems not to send any traces) |
@julandw I totally agree that Spring Application developers would like to set iKey in the properties file and then retrive it using @value("$name") annotation. This is specifically because there are different properties file in the application to support different environments like Dev, Test and Prod where user requires different configuration. One example is that in dev time user would ideally configure tomcat properties to auto restart on code change in non static files however this is not a desirable for production. This ability is essentially the Spring Boots powerful capability. That being said - I think as I mentioned before that some how QuickPulse module is getting an non initialized TelemetryConfiguration instance and hence it complains that there is no instrumentation key. (most probably the reason you mentioned that the Quick pulse module is getting initialized much before than the PropertySourceBean which associates the properties file to application is loaded. Please correct me if I get this wrong here). As far as the solution to this problem is concerned I would say as follows for your options.
So how do we get 1. achieved - Look at https://github.com/gavlyukovskiy/ApplicationInsights-Java/blob/e8f688fa1a11e2aaadae8bf99f4fc273538e3cc7/azure-application-insights-spring-boot-starter/src/main/java/com/microsoft/applicationinsights/boot/ApplicationInsightsTelemetryAutoConfiguration.java#L153 As of today it will take some time to have the General Availability of AI SpringBoot starter (PR still needs some finishing and behavior testing and more importantly it would start with a Beta which should be fine though). I would therefore suggest the following work-around, though is not the best and would lead to some duplication and hardcoding. You could create a static constants of iKeys for different profiles inside your ApplicationInsights configuration class (where you have other AI beans) and then based on condition of profile activated you can pick the iKey. Also, I would be more than happy to try multiple alternatives on my end, if it is possible for you to upload a strip downed version of your application. Hope it helps! |
@dhaval24 OK, I totally agree with you that postponing the initialization of QuickPulse is the better option. I just thought that this option is not so easy to achieve. I also had a look at the ApplicationInsightsTelemetryAutoConfiguration java class and yes this all makes sense to easily use and configure AI for Spring-Boot. Maybe one more thought: you have to ensure that the QuickPulse Sopring-Bean is not created before the "TelemetryConfiguration" Spring-Bean (which sets the instrumentation key on the active configuration). You could do this by simply declare a dependency using the @dependsOn annotation. Thanks for your support and I am eagerly waiting for the Spring-Boot starter libray. |
@julandw thank you very much for your interest with Application Insights and the work we are doing here. Referencing this issue in the PR so we can keep a track of the suggestion. BTW we released a patch version 2.0.1 to fix invalid POM issue. You can try and see how the experience is. Hopefully this would be much better :) |
This is similar issue to what I had with logback. Logback was initialized much before than
You don't need such thread to reload instrumentation key, you can do it similar to what I did for logback - check that if instrumentation key is null at the moment of making request and then take new one from active Regarding spring boot auto-configuration you're right, I didn't think about it, though quick pulse is working for sample application, but it looks like coincidence and I will add explicit dependency on P.S. @julandw thanks for pointing
will change this one to |
@gavlyukovskiy Looks easier with the annotation :) Is there already a (rough) timeline, when PullRequest 518 will be released? |
@julandw Application Insights SpringBoot starter is now released. #646 is the PR and here is the readme: https://github.com/Microsoft/ApplicationInsights-Java/blob/master/azure-application-insights-spring-boot-starter/README.md Take a look. This should solve the problem in this issue too! |
In our Spring-Boot application we set the instrumentation key programmtically using the following code:
TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.getActive();
telemetryConfiguration.setInstrumentationKey(instrumentationKey);
This code is executed before the "WebRequestTrackingFilter" is registered, so that all HTTP request metrics are properly sent with the correct instrumentation key.
Additionally we explicitly set the instrumentation key on the logback "ApplicationInsightsAppender" so that traces (i.e. logs) are also sent to Application Insights with the correct instrumentation key.
However we noticed that the AI SDK tries to perform a POST request on the following URL every 5 seconds:
POST /QuickPulseService.svc/ping?ikey=null
HTTP/1.1 400 Bad Request
Here the instrumentation key is "null" and thus the request fails.
The question now is:
What is the purpose of this call?
Is it needed somehow or is it possible to simply disable it?
Or is it possible to somehow pass over the instrumentation key, so that this call can also use the programatically set instrumentation key?
And finally: If -for whatever reasons- the instrumentation key is "null", then no attempt should be made in the first place to contact the Application Insights server instance.
Any help/hints welcome.
Thanks.
The text was updated successfully, but these errors were encountered: