microsoft / ApplicationInsights-Java

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

Application Insights Java Agent does not track Request in a Server Server Event Rest API #2945

Open apescione opened 1 year ago

apescione commented 1 year ago

Context

I'm using a Spring Boot Application (Java) and WebFlux as the web layer (Reactor), and Reactor Netty is used as an Embedded web server. In my application, I've Server-Sent-Event API like this:

  @RequestMapping(value = "/messages",
        produces = {"text/event-stream", "application/stream+json"},
        method = RequestMethod.GET)
    public Mono<ResponseEntity<Flux<ServerSentEvent<String>>>> getSseMessages() {

This API sends events to clients using a HOT Stream. I'm using the Application Insights java agent to instrument my application deployed into a docker container. I cannot see in Application insights, Log section the requests through this API. I can see other API's requests. This behavior is only with netty (default embedded web server for spring webflux), but not with undertow and tomcat.

Expected behavior I am seeing the invocation request in Application insight for Server Sent Event API.

Actual behavior Java Agent cannot send Request log to application insight ... To Reproduce It's very simple, create with web flux a server sent event api that produces an event every few seconds, connect the client (browser is enough).

Sample Application
@RestController
@RequestMapping("/api")
public class ServerSentEventApi {
...

    @RequestMapping(value = "/messages",
        produces = {"text/event-stream", "application/stream+json"},
        method = RequestMethod.GET)
    public Mono<ResponseEntity<Flux<ServerSentEvent<String>>>> getSseMessages() {
        Flux<ServerSentEvent<String>> sseFlux = Flux.interval(Duration.ofSeconds(5))
            .map(i -> ServerSentEvent.<String>builder().event("ping")
                .data(i.toString()).id(RandomStringUtils.randomAlphanumeric(8)).build());
        return Mono.just(ResponseEntity.ok().body(sseFlux));
    }
...

}

System information Please provide the following information:

SDK Version: Java 8 Zulu, Spring Boot 2.6.8, Webflux 5.3.20, netty 4.1.77 OS type and version: Container Linux alphine Application Insight Java Agent version 3.4.9

Many thanks for considering my request.

trask commented 1 year ago

hi @apescione! is this a duplicate of #2941?

apescione commented 1 year ago

No, they are two different issues. This is on reactor netty web server, and the java agent is not able to trace requests for Server Sent Event API. The other issue is that the java agent is not able to suppress "IOException: Broken pipe" on Tomcat embedded web server.

heyams commented 1 year ago

@apescione can i use the same app to repro? https://github.com/apescione/applicationinsightTestApps

apescione commented 1 year ago

Yes, but you need to remove tomcat and netty exclusion so that you can change the embedded server and reproduce this issue. change this

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-reactor-netty</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>

with

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

This is because netty is the default server for web-flux.