javaspring-bootazureazure-application-insightsappinsights

In Java, Azure applicationinsights starter dependency not compatible with spring boot 3.x.x


I wanted to integrate application insights into my spring boot application**(v3.x.x)** and for that I have used below dependency in my build.gradle.

implementation 'com.microsoft.azure:applicationinsights-spring-boot-starter:2.6.4'

In my controller, I have injected TelemetryClient bean like:

@RestController
@RequestMapping("/")
public class UserController {
    @Autowired
    TelemetryClient telemetryClient;

    @GetMapping("/greetings")
    public String greetings() {
        // send event
        telemetryClient.trackEvent("URI /greeting is triggered");

        return "Hello World!";
    }
}

And, To connect to my azure app insights I used :

APPLICATIONINSIGHTS_CONNECTION_STRING=somevalue

as Environment variable while running application in IntellijIdea.

Now, Application runs and complains about:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field telemetryClient in com.example.demo.app.insights.UserController required a bean of type 'com.microsoft.applicationinsights.TelemetryClient' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.microsoft.applicationinsights.TelemetryClient' in your configuration.

But, as soon as I downgrade my spring boot version from 3.x.x to 2.7.11, it works fine with the same piece of code and starts the application.

Has anyone else also has experienced similar issue of appinsights with spring boot on 3.x.x ? or Any idea of its fix in latest spring boot version ?


Solution

  • This issue has been resolved in https://github.com/microsoft/ApplicationInsights-Java/issues/3016

    Below dependency is compatible with spring boot 3.x:

    api('com.microsoft.azure:applicationinsights-core:3.4.11');
    // api 'com.microsoft.azure:applicationinsights-spring-boot-starter:2.6.4' // not compatible with spring 3.x
    

    After that TelemetryClient Bean can be injected like:

    @RestController
    @RequestMapping("/")
    public class UserController {
    
        private final TelemetryClient telemetryClient = new TelemetryClient();
    
        @GetMapping("/greetings")
        public String greetings() {
            // send event
            telemetryClient.trackEvent("URI /greeting is triggered");
    
            return "Hello World!";
        }
    }