Skip to content

elasticsearch-rest-7.0 instrumentation: missing throws exception causing NPE #11892

@kyy1996

Description

@kyy1996

Describe the bug

When using ElasticsearchRest7Telemetry from opentelemetry-elasticsearch-rest-7.0 to wrap the RestClient, the RestClientWrapper suppresses the real Exception if the underlying RestClient throws one. RestClientWrapper returns null in this case, which could cause java.lang.NullPointerException in RestHighLevelClient.internalPerformRequest since null is not expected for RestHighLevelClient.

Maybe the problem is due to missing throw at RestClientWrapper.java#L76` and RestClientWrapper.java#L104, the caught Exception should be rethrown.

Steps to reproduce

Just run the following demo program, no need to install the Elasticsearch service.

import java.io.IOException;

import org.apache.http.HttpHost;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.elasticsearch.rest.v7_0.ElasticsearchRest7Telemetry;

public class Application {
    public static void main(String[] args) throws IOException {
        RestClientBuilder restClientBuilder = RestClient.builder(HttpHost.create("http://127.0.0.1:9200")); // <-- suppose it's an unreachable host.
        try (RestClient lowLevelClient = restClientBuilder.build()) {
            RestClient enhancedRestClient = ElasticsearchRest7Telemetry.builder(GlobalOpenTelemetry.get()).build()
                    .wrap(lowLevelClient);
            Response rsp = enhancedRestClient.performRequest(new Request("GET", "test/")); // <--  java.net.ConnectException is expected to be thrown here but nothing happens
            System.out.println(rsp); // <-- `null` is printed
            Response realRsp = lowLevelClient.performRequest(new Request("GET", "test/")); // <-- java.net.ConnectException is thrown
            System.out.println(realRsp);

            RestHighLevelClient highLevelClient = new RestHighLevelClient(enhancedRestClient, RestClient::close,
                    Collections.emptyList()) {
            };
            System.out.println(highLevelClient.get(new GetRequest("test").id("1"), RequestOptions.DEFAULT)); // <-- java.lang.NullPointerException is thrown due to performRequest returning null
        }
    }
}

Expected behavior

When it goes to enhancedRestClient.performRequest, since http://127.0.0.1:9200 is not reachable, a java.net.ConnectException should be thrown as lowLevelClient.performRequest

Actual behavior

No exception is thrown, instead, it returns null.

Javaagent or library instrumentation version

1.33.4-alpha

Environment

JDK: OpenJDK 1.8.0_412
OS: Ubuntu 22.04.4 LTS

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds triageNew issue that requires triage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions