Skip to content

Commit

Permalink
add jdbc obfuscation
Browse files Browse the repository at this point in the history
  • Loading branch information
songlonqi-java committed Jan 2, 2025
1 parent c3bfa1c commit 50d181f
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Comparing source compatibility of opentelemetry-instrumentation-annotations-2.11.0.jar against opentelemetry-instrumentation-annotations-2.10.0.jar
Comparing source compatibility of opentelemetry-instrumentation-annotations-2.11.0.jar against opentelemetry-instrumentation-annotations-2.11.0.jar
No changes.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Comparing source compatibility of opentelemetry-instrumentation-api-2.11.0.jar against opentelemetry-instrumentation-api-2.10.0.jar
Comparing source compatibility of opentelemetry-instrumentation-api-2.11.0.jar against opentelemetry-instrumentation-api-2.11.0.jar
No changes.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Comparing source compatibility of opentelemetry-spring-boot-autoconfigure-2.11.0.jar against opentelemetry-spring-boot-autoconfigure-2.10.0.jar
Comparing source compatibility of opentelemetry-spring-boot-autoconfigure-2.11.0.jar against opentelemetry-spring-boot-autoconfigure-2.11.0.jar
No changes.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Comparing source compatibility of opentelemetry-spring-boot-starter-2.11.0.jar against opentelemetry-spring-boot-starter-2.10.0.jar
Comparing source compatibility of opentelemetry-spring-boot-starter-2.11.0.jar against opentelemetry-spring-boot-starter-2.11.0.jar
No changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,39 @@
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcAttributesGetter;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcNetworkAttributesGetter;
import io.opentelemetry.instrumentation.jdbc.internal.DbSetArgs;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcAttributes;
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import io.opentelemetry.javaagent.bootstrap.jdbc.DbInfo;
import javax.sql.DataSource;

import java.util.HashMap;

public final class JdbcSingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.jdbc";

private static final Instrumenter<DbRequest, Void> STATEMENT_INSTRUMENTER;
public static final Instrumenter<DataSource, DbInfo> DATASOURCE_INSTRUMENTER =
createDataSourceInstrumenter(GlobalOpenTelemetry.get(), true);

public static final DbSetArgs setArgs;

public static void setArg(Integer index,String arg){
if (setArgs == null){
return;
}
setArgs.setArg(index,arg);
}

public static void resetArgs(){
setArgs.resetArgs();
}

static {
JdbcAttributesGetter dbAttributesGetter = new JdbcAttributesGetter();
JdbcNetworkAttributesGetter netAttributesGetter = new JdbcNetworkAttributesGetter();
setArgs = new DbSetArgs(new HashMap<>());

STATEMENT_INSTRUMENTER =
Instrumenter.<DbRequest, Void>builder(
Expand All @@ -52,6 +70,7 @@ public final class JdbcSingletons {
PeerServiceAttributesExtractor.create(
netAttributesGetter, AgentCommonConfig.get().getPeerServiceResolver()))
.addOperationMetrics(DbClientMetrics.get())
.addAttributesExtractor(JdbcAttributes.create(setArgs))
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.statementInstrumenter;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.resetArgs;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.setArg;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand All @@ -19,6 +21,7 @@
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcData;
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.sql.PreparedStatement;
Expand All @@ -44,6 +47,9 @@ public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
nameStartsWith("execute").and(takesArguments(0)).and(isPublic()),
PreparedStatementInstrumentation.class.getName() + "$PreparedStatementAdvice");
transformer.applyAdviceToMethod(
nameStartsWith("set").and(isPublic()).and(takesArguments(2)),
PreparedStatementInstrumentation.class.getName() + "$SetStringAdvice");
}

@SuppressWarnings("unused")
Expand Down Expand Up @@ -99,7 +105,33 @@ public static void stopSpan(
if (scope != null) {
scope.close();
statementInstrumenter().end(context, request, null, throwable);
resetArgs();
}
}
}
@SuppressWarnings("unused")
public static class SetStringAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.AllArguments Object[] args, @Advice.This Statement statement) {

int index = 0;
String arg = "";
if (args.length != 2) {
return;
}

if (args[0] instanceof Integer) {
index = (Integer) args[0];
}
arg = args[1].toString();

if (AgentInstrumentationConfig.get().getBoolean("otel.jdbc.sql.obfuscation", false)) {
setArg(index, arg);
}
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(@Advice.Thrown Throwable throwable) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.jdbc.internal;

import java.util.HashMap;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
public final class DbSetArgs {
private HashMap<Integer, String> args;

@SuppressWarnings("NonApiType")
public DbSetArgs(HashMap<Integer, String> map) {
this.args = map;
}

@SuppressWarnings("NonApiType")
public HashMap<Integer, String> getArgs() {
return this.args;
}

@SuppressWarnings("NonApiType")
public void setArgs(HashMap<Integer, String> args) {
this.args = args;
}

public void setArg(Integer index, String arg) {
this.args.put(index, arg);
}

public void resetArgs() {
this.args = new HashMap<Integer, String>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.jdbc.internal;

import static io.opentelemetry.instrumentation.api.internal.AttributesExtractorUtil.internalSet;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import java.util.Map;
import javax.annotation.Nullable;

/**
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
* any time.
*/
@SuppressWarnings("UnusedTypeParameter")
public final class JdbcAttributes<REQUEST, RESPONSE> implements AttributesExtractor<REQUEST, RESPONSE> {

private DbSetArgs args;

private JdbcAttributes(){}

public static JdbcAttributes<DbRequest, Void> create(DbSetArgs args) {
JdbcAttributes<DbRequest, Void> j = new JdbcAttributes<>();
j.setArgs(args);
return j;
}

private void setArgs(DbSetArgs args) {
this.args = args;
}

@Override
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
if (this.args == null || this.args.getArgs()==null){
return;
}
StringBuilder sb = new StringBuilder();
for (Map.Entry<Integer,String> entry : args.getArgs().entrySet()) {
// internalSet(attributes, AttributeKey.stringKey("origin_sql_"+entry.getKey()),entry.getValue());
sb.append("index_").
append(entry.getKey()).
append(":").
append(entry.getValue()).
append(", ");
}
internalSet(attributes, AttributeKey.stringKey("db_args"),sb.toString());
}

@Override
public void onEnd(AttributesBuilder attributes, Context context, REQUEST request,
@Nullable RESPONSE response, @Nullable Throwable error) {
}
}

0 comments on commit 50d181f

Please sign in to comment.