Skip to content

Commit

Permalink
Merge pull request #16 from GuanceCloud/jdbc-obfucation
Browse files Browse the repository at this point in the history
jdbc.sql.obfuscation
  • Loading branch information
songlonqi-java authored Jun 15, 2023
2 parents c064238 + fcc8b0a commit 30b3062
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.PeerServiceAttributesExtractor;
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
import io.opentelemetry.instrumentation.jdbc.internal.DbSetArgs;
import io.opentelemetry.instrumentation.jdbc.internal.JDBCAttributes;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcAttributesGetter;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcNetAttributesGetter;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import java.util.HashMap;
import javax.sql.DataSource;

public final class JdbcSingletons {
Expand All @@ -28,9 +31,23 @@ public final class JdbcSingletons {
public static final Instrumenter<DataSource, Void> DATASOURCE_INSTRUMENTER =
createDataSourceInstrumenter(GlobalOpenTelemetry.get());

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();
JdbcNetAttributesGetter netAttributesGetter = new JdbcNetAttributesGetter();
setArgs = new DbSetArgs(new HashMap<>());

STATEMENT_INSTRUMENTER =
Instrumenter.<DbRequest, Void>builder(
Expand All @@ -49,6 +66,7 @@ public final class JdbcSingletons {
.addAttributesExtractor(
PeerServiceAttributesExtractor.create(
netAttributesGetter, CommonConfig.get().getPeerServiceMapping()))
.addAttributesExtractor(JDBCAttributes.create(setArgs))
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
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.resetArgs;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.setArg;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.statementInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
Expand All @@ -18,6 +20,7 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.sql.PreparedStatement;
Expand All @@ -43,6 +46,10 @@ 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 All @@ -66,7 +73,6 @@ public static void onEnter(
if (callDepth.getAndIncrement() > 0) {
return;
}

Context parentContext = currentContext();
request = DbRequest.create(statement);

Expand All @@ -92,7 +98,34 @@ 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 (InstrumentationConfig.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
Expand Up @@ -4,7 +4,6 @@
*/

package io.opentelemetry.javaagent.instrumentation.jdbc;

import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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;

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

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

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,52 @@
/*
* 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;
}
for (Map.Entry<Integer,String> entry : args.getArgs().entrySet()) {
internalSet(attributes, AttributeKey.stringKey("origin_sql_"+entry.getKey()),entry.getValue());
}
}

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

0 comments on commit 30b3062

Please sign in to comment.