Skip to content

Commit

Permalink
Added jdbc library with ported version of OpenTracing JDBC Instrument…
Browse files Browse the repository at this point in the history
…ation.
  • Loading branch information
donbeave committed Jun 18, 2021
1 parent 0125a17 commit 5bf3376
Show file tree
Hide file tree
Showing 28 changed files with 3,882 additions and 0 deletions.
19 changes: 19 additions & 0 deletions instrumentation/jdbc/library/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
This product contains a modified part of OpenTracing JDBC Instrumentation:

* License:

Copyright 2017-2021 The OpenTracing Authors

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

https://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.

* Homepage: https://github.com/opentracing-contrib/java-jdbc
1 change: 1 addition & 0 deletions instrumentation/jdbc/library/jdbc-library.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apply plugin: "otel.library-instrumentation"
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

// Includes work from:
/*
* Copyright 2017-2021 The OpenTracing Authors
*
* 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.opentelemetry.instrumentation.jdbc;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;

public final class Classes {

/**
* Adds all interfaces extended by the specified {@code iface} interface {@link Class}.
*
* @param iface The interface {@link Class}.
* @param set The set into which all extended interfaces are to be added.
* @throws NullPointerException If {@code iface} or {@code set} is null.
*/
private static void recurse(final Class<?> iface, final HashSet<Class<?>> set) {
if (set.contains(iface)) {
return;
}

set.add(iface);
for (final Class<?> extended : iface.getInterfaces()) {
recurse(extended, set);
}
}

/**
* Returns all interfaces implemented by the class or interface represented by the specified
* class. This method differentiates itself from {@link Class#getInterfaces()} by returning
* <i>all</i> interfaces (full depth and breadth) instead of just the interfaces <i>directly</i>
* implemented by the class.
*
* @param cls The class.
* @return All interfaces implemented by the class or interface represented by the specified
* class.
* @throws NullPointerException If {@code cls} is null.
*/
public static Class<?>[] getAllInterfaces(final Class<?> cls) {
Class<?> parent = cls;
Class<?>[] ifaces = null;
HashSet<Class<?>> set = null;
do {
ifaces = parent.getInterfaces();
if (ifaces.length == 0) {
continue;
}

if (set == null) {
set = new HashSet<>(4);
}

for (final Class<?> iface : ifaces) {
recurse(iface, set);
}
}
while ((parent = parent.getSuperclass()) != null);
return set == null ? ifaces : set.toArray(new Class[set.size()]);
}

/**
* Returns a Method object that reflects the specified declared method of the class or interface
* represented by {@code cls} (excluding inherited methods), or {@code null} if the method is not
* found.
* <p>
* Declared methods include public, protected, default (package) access, and private visibility.
* <p>
* The {@code name} parameter is a String that specifies the simple name of the desired method,
* and the {@code parameterTypes} parameter is an array of Class objects that identify the
* method's formal parameter types, in declared order. If more than one method with the same
* parameter types is declared in a class, and one of these methods has a return type that is more
* specific than any of the others, that method is returned; otherwise one of the methods is
* chosen arbitrarily. If the name is {@code "<init>"} or {@code "<clinit>"} this method returns
* {@code null}. If this Class object represents an array type, then this method does not find the
* clone() method.
* <p>
* This method differentiates itself from {@link Class#getDeclaredMethod(String, Class...)} by
* returning {@code null} when a method is not found, instead of throwing {@link
* NoSuchMethodException}.
*
* @param cls The class in which to find the declared method.
* @param name The simple name of the method.
* @param parameterTypes The parameter array.
* @return A Method object that reflects the specified declared method of the class or interface
* represented by {@code cls} (excluding inherited methods), or {@code null} if the method is not
* found.
* @throws NullPointerException If {@code cls} or {@code name} is null.
*/
public static Method getDeclaredMethod(final Class<?> cls, final String name,
final Class<?>... parameterTypes) {
final Method[] methods = cls.getDeclaredMethods();
for (final Method method : methods) {
if (name.equals(method.getName()) && Arrays
.equals(method.getParameterTypes(), parameterTypes)) {
return method;
}
}

return null;
}

/**
* Returns a Method object that reflects the specified declared method of the class or interface
* represented by {@code cls} (including inherited methods), or {@code null} if the method is not
* found.
* <p>
* Declared methods include public, protected, default (package) access, and private visibility.
* <p>
* The {@code name} parameter is a String that specifies the simple name of the desired method,
* and the {@code parameterTypes} parameter is an array of Class objects that identify the
* method's formal parameter types, in declared order. If more than one method with the same
* parameter types is declared in a class, and one of these methods has a return type that is more
* specific than any of the others, that method is returned; otherwise one of the methods is
* chosen arbitrarily. If the name is {@code "<init>"} or {@code "<clinit>"} this method returns
* {@code null}. If this Class object represents an array type, then this method does not find the
* clone() method.
* <p>
* This method differentiates itself from {@link Class#getDeclaredMethod(String, Class...)} by
* returning {@code null} when a method is not found, instead of throwing {@link
* NoSuchMethodException}.
*
* @param cls The class in which to find the declared method.
* @param name The simple name of the method.
* @param parameterTypes The parameter array.
* @return A Method object that reflects the specified declared method of the class or interface
* represented by {@code cls} (including inherited methods), or {@code null} if the method is not
* found.
* @throws NullPointerException If {@code cls} or {@code name} is null.
*/
public static Method getDeclaredMethodDeep(Class<?> cls, final String name,
final Class<?>... parameterTypes) {
Method method;
do {
method = getDeclaredMethod(cls, name, parameterTypes);
}
while (method == null && (cls = cls.getSuperclass()) != null);
return method;
}

private Classes() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

// Includes work from:
/*
* Copyright 2017-2021 The OpenTracing Authors
*
* 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.opentelemetry.instrumentation.jdbc;

public class ConnectionInfo {

public static ConnectionInfo UNKNOWN_CONNECTION_INFO = new Builder("unknown_peer")
.dbType("unknown_type").dbInstance("unknown_instance").build();

private final String dbType;
private final String dbUser;
private final String dbInstance;
private final String dbPeer;
private final String dbPeerService;

private ConnectionInfo(String dbType, String dbUser, String dbInstance, String dbHost,
Integer dbPort) {
this.dbType = dbType;
this.dbUser = dbUser;
this.dbInstance = dbInstance;
if (dbHost != null && dbPort != null) {
this.dbPeer = dbHost + ":" + dbPort;
} else {
this.dbPeer = "";
}

this.dbPeerService = makePeerService();
}

private ConnectionInfo(String dbType, String dbUser, String dbInstance, String dbPeer) {
this.dbType = dbType;
this.dbUser = dbUser;
this.dbInstance = dbInstance;
this.dbPeer = dbPeer;

this.dbPeerService = makePeerService();
}

/**
* Make a unique serviceName that could be used in dependency diagram.
*/
private String makePeerService() {
if (null != dbInstance && !dbInstance.isEmpty()) {
return dbInstance + "[" + dbType + "(" + dbPeer + ")]";
} else {
return dbType + "(" + dbPeer + ")";
}
}

public String getDbType() {
return dbType;
}

public String getDbUser() {
return dbUser;
}

public String getDbInstance() {
return dbInstance;
}

public String getDbPeer() {
return dbPeer;
}

public String getPeerService() {
return dbPeerService;
}

public static class Builder {

private String dbType;
private String dbUser;
private String dbInstance;
private String dbHost;
private Integer dbPort;
private String dbPeer;

public Builder(String dbPeer) {
this.dbPeer = dbPeer;
}

public Builder(String dbHost, Integer dbPort) {
this.dbHost = dbHost;
this.dbPort = dbPort;
}

public Builder dbType(String dbType) {
this.dbType = dbType;
return this;
}

public Builder dbUser(String dbUser) {
this.dbUser = dbUser;
return this;
}

public Builder dbInstance(String dbInstance) {
this.dbInstance = dbInstance;
return this;
}

public ConnectionInfo build() {
if (this.dbPeer != null && !dbPeer.isEmpty()) {
return new ConnectionInfo(this.dbType, this.dbUser, this.dbInstance, this.dbPeer);
}
return new ConnectionInfo(this.dbType, this.dbUser, this.dbInstance, this.dbHost,
this.dbPort);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

// Includes work from:
/*
* Copyright 2017-2021 The OpenTracing Authors
*
* 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.opentelemetry.instrumentation.jdbc;

public class JdbcTracing {

private static boolean traceEnabled = true;

/**
* Sets the {@code traceEnabled} property to enable or disable traces.
*
* @param traceEnabled The {@code traceEnabled} value.
*/
public static void setTraceEnabled(boolean traceEnabled) {
JdbcTracing.traceEnabled = traceEnabled;
}

public static boolean isTraceEnabled() {
return JdbcTracing.traceEnabled;
}

/**
* can be modified by application code
*/
private static int slowQueryThresholdMs = Integer
.getInteger("io.opentracing.contrib.jdbc.slowQueryThresholdMs", 0);

public static int getSlowQueryThresholdMs() {
return slowQueryThresholdMs;
}

public static void setSlowQueryThresholdMs(final int slowQueryThresholdMs) {
JdbcTracing.slowQueryThresholdMs = slowQueryThresholdMs;
}

private static int excludeFastQueryThresholdMs = Integer
.getInteger("io.opentracing.contrib.jdbc.excludeFastQueryThresholdMs", 0);

public static int getExcludeFastQueryThresholdMs() {
return excludeFastQueryThresholdMs;
}

public static void setExcludeFastQueryThresholdMs(final int excludeFastQueryThresholdMs) {
JdbcTracing.excludeFastQueryThresholdMs = excludeFastQueryThresholdMs;
}

}
Loading

0 comments on commit 5bf3376

Please sign in to comment.