Skip to content
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

Do not shade opentelemetry in JDBC driver #23458

Merged
merged 7 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions client/trino-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@
<version>1.7</version>
</dependency>

<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-okhttp-3.0</artifactId>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-client</artifactId>
Expand All @@ -91,6 +86,18 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-context</artifactId>
<scope>provided</scope>
mosabua marked this conversation as resolved.
Show resolved Hide resolved
</dependency>

<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-okhttp-3.0</artifactId>
<scope>provided</scope>
</dependency>

wendigo marked this conversation as resolved.
Show resolved Hide resolved
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -187,6 +194,24 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-trace</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-blackhole</artifactId>
Expand Down Expand Up @@ -439,18 +464,6 @@
<pattern>okhttp3</pattern>
<shadedPattern>${shadeBase}.okhttp3</shadedPattern>
</relocation>
<relocation>
<pattern>io.opentelemetry.extension</pattern>
<shadedPattern>${shadeBase}.opentelemetry.extension</shadedPattern>
</relocation>
<relocation>
<pattern>io.opentelemetry.instrumentation</pattern>
<shadedPattern>${shadeBase}.opentelemetry.instrumentation</shadedPattern>
</relocation>
<relocation>
<pattern>io.opentelemetry.api.incubator</pattern>
<shadedPattern>${shadeBase}.opentelemetry.api.incubator</shadedPattern>
</relocation>
<relocation>
<pattern>okio</pattern>
<shadedPattern>${shadeBase}.okio</shadedPattern>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
*/
package io.trino.jdbc;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.okhttp.v3_0.OkHttpTelemetry;
import io.trino.client.uri.HttpClientFactory;
import okhttp3.Call;
import okhttp3.ConnectionPool;
Expand Down Expand Up @@ -79,24 +76,17 @@ public Connection connect(String url, Properties info)

return new TrinoConnection(
uri,
instrumentClient(httpClientBuilder.build()),
instrumentClient(segmentHttpClientBuilder.build()));
wrapClient(httpClientBuilder.build()),
wrapClient(segmentHttpClientBuilder.build()));
}
catch (RuntimeException e) {
throw new SQLException(e.getMessage(), e);
}
}

private Call.Factory instrumentClient(OkHttpClient client)
protected Call.Factory wrapClient(OkHttpClient client)
{
try {
OpenTelemetry openTelemetry = GlobalOpenTelemetry.get();
return OkHttpTelemetry.builder(openTelemetry).build().newCallFactory(client);
}
catch (NoClassDefFoundError | NoSuchMethodError ignored) {
// assume OTEL is not (fully) available and return the original client
return (Call.Factory) client;
}
return client;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.jdbc;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.okhttp.v3_0.OkHttpTelemetry;
import okhttp3.Call;
import okhttp3.OkHttpClient;

import java.util.Optional;

import static java.util.Objects.requireNonNull;

public class TracingTrinoDriver
extends NonRegisteringTrinoDriver
{
private final OpenTelemetry openTelemetry;

public TracingTrinoDriver(OpenTelemetry openTelemetry)
{
this.openTelemetry = requireNonNull(openTelemetry, "openTelemetry is null");
mosabua marked this conversation as resolved.
Show resolved Hide resolved
}

public TracingTrinoDriver()
{
this(GlobalOpenTelemetry.get());
}

@Override
protected Call.Factory wrapClient(OkHttpClient client)
{
return OkHttpTelemetry
.builder(Optional.ofNullable(openTelemetry).orElse(GlobalOpenTelemetry.get()))
.build()
.newCallFactory(client);
}
}
20 changes: 20 additions & 0 deletions client/trino-jdbc/src/test/java/io/trino/jdbc/JdbcDriverIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ public void testDependenciesRelocated()
}
}

@Test
public void testOpenTelemetryIsNotShaded()
{
String file = System.getProperty("jdbc-jar");
try (JarFile jarFile = new JarFile(file)) {
List<String> openTelemetryFiles = jarFile.stream()
.filter(value -> !value.isDirectory())
.map(ZipEntry::getName)
.filter(name -> name.contains("io/opentelemetry"))
.collect(toImmutableList());

assertThat(openTelemetryFiles)
.describedAs("OpenTelemetry files in the shaded jar")
.isEmpty();
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static boolean isExpectedFile(String filename)
{
return MANIFEST_FILES.contains(filename) || filename.startsWith("io/trino/jdbc");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.jdbc;

import io.airlift.log.Logging;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor;
import io.trino.plugin.memory.MemoryPlugin;
import io.trino.server.testing.TestingTrinoServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.parallel.Execution;

import java.io.IOException;
import java.net.URI;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;

@TestInstance(PER_CLASS)
@Execution(CONCURRENT)
public class TestTracingTrinoDriver
{
private TestingTrinoServer server;

@BeforeAll
public void setupServer()
{
Logging.initialize();
server = TestingTrinoServer.create();

server.installPlugin(new MemoryPlugin());
server.createCatalog("memory", "memory");
}

@AfterAll
public void tearDown()
throws Exception
{
server.close();
server = null;
}

@Test
public void testInitialize()
throws IOException, SQLException
{
InMemorySpanExporter exporter = InMemorySpanExporter.create();
SdkTracerProvider tracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(SimpleSpanProcessor.create(exporter))
.build();

OpenTelemetry telemetry = OpenTelemetrySdk.builder()
.setTracerProvider(tracerProvider)
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.build();

try (TracingTrinoDriver tracingDriver = new TracingTrinoDriver(telemetry)) {
try (Connection connection = createConnection(tracingDriver, "memory")) {
try (Statement statement = connection.createStatement()) {
statement.execute("SHOW SCHEMAS FROM memory");
}
}
}

List<String> uris = exporter.getFinishedSpanItems().stream()
.filter(span -> span.getAttributes().get(longKey("http.response.status_code")) != null)
.map(span -> span.getAttributes().get(stringKey("url.full")))
.map(value -> URI.create(value).getPath())
.collect(toImmutableList());

assertThat(uris)
.hasSizeBetween(3, 10) // POST, queued, executing
.contains("/v1/statement");
}

private Connection createConnection(Driver driver, String catalog)
throws SQLException
{
String url = format("jdbc:trino://%s/%s?explicitPrepare=true&user=test", server.getAddress(), catalog);
return driver.connect(url, new Properties());
}
}