Skip to content

Commit

Permalink
merge guance
Browse files Browse the repository at this point in the history
  • Loading branch information
songlonqi-java committed May 23, 2024
2 parents 6666f39 + 31e364d commit 8ad0022
Show file tree
Hide file tree
Showing 176 changed files with 7,317 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "dd-java-agent/agent-jmxfetch/integrations-core"]
path = dd-java-agent/agent-jmxfetch/integrations-core
url = https://github.com/DataDog/integrations-core.git
url = https://github.com/GuanceCloud/integrations-core.git
5 changes: 3 additions & 2 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ tasks.compileKotlin {
}

tasks.test {
useJUnitPlatform()
enabled = project.hasProperty("runBuildSrcTests")
// useJUnitPlatform()
// dependsOn(":call-site-instrumentation-plugin:build")
// enabled = !project.hasProperty("skipBuildSrcTest")
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
package datadog.trace.bootstrap;

import static datadog.trace.api.ConfigDefaults.DEFAULT_STARTUP_LOGS_ENABLED;
import static datadog.trace.api.Platform.getRuntimeVendor;
import static datadog.trace.api.Platform.isJavaVersionAtLeast;
import static datadog.trace.api.Platform.isOracleJDK8;
import static datadog.trace.bootstrap.Library.WILDFLY;
import static datadog.trace.bootstrap.Library.detectLibraries;
import static datadog.trace.util.AgentThreadFactory.AgentThread.JMX_STARTUP;
import static datadog.trace.util.AgentThreadFactory.AgentThread.PROFILER_STARTUP;
import static datadog.trace.util.AgentThreadFactory.AgentThread.TRACE_STARTUP;
import static datadog.trace.util.AgentThreadFactory.newAgentThread;
import static datadog.trace.util.Strings.getResourceName;
import static datadog.trace.util.Strings.propertyNameToSystemPropertyName;
import static datadog.trace.util.Strings.toEnvVar;

import datadog.trace.api.Config;
import datadog.trace.api.Platform;
import datadog.trace.api.StatsDClientManager;
Expand Down Expand Up @@ -41,6 +27,9 @@
import datadog.trace.util.AgentTaskScheduler;
import datadog.trace.util.AgentThreadFactory.AgentThread;
import datadog.trace.util.throwable.FatalAgentMisconfigurationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.instrument.Instrumentation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -52,8 +41,20 @@
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static datadog.trace.api.ConfigDefaults.DEFAULT_STARTUP_LOGS_ENABLED;
import static datadog.trace.api.Platform.getRuntimeVendor;
import static datadog.trace.api.Platform.isJavaVersionAtLeast;
import static datadog.trace.api.Platform.isOracleJDK8;
import static datadog.trace.bootstrap.Library.WILDFLY;
import static datadog.trace.bootstrap.Library.detectLibraries;
import static datadog.trace.util.AgentThreadFactory.AgentThread.JMX_STARTUP;
import static datadog.trace.util.AgentThreadFactory.AgentThread.PROFILER_STARTUP;
import static datadog.trace.util.AgentThreadFactory.AgentThread.TRACE_STARTUP;
import static datadog.trace.util.AgentThreadFactory.newAgentThread;
import static datadog.trace.util.Strings.getResourceName;
import static datadog.trace.util.Strings.propertyNameToSystemPropertyName;
import static datadog.trace.util.Strings.toEnvVar;

/**
* Agent start up logic.
Expand Down Expand Up @@ -476,10 +477,10 @@ public void execute() {
AGENT_CLASSLOADER.loadClass("datadog.communication.ddagent.SharedCommunicationObjects");
sco = scoClass.getConstructor().newInstance();
} catch (ClassNotFoundException
| NoSuchMethodException
| InstantiationException
| IllegalAccessException
| InvocationTargetException e) {
| NoSuchMethodException
| InstantiationException
| IllegalAccessException
| InvocationTargetException e) {
throw new UndeclaredThrowableException(e);
}

Expand Down Expand Up @@ -661,15 +662,17 @@ private static synchronized void registerDeadlockDetectionEvent() {
final Method registerMethod = deadlockFactoryClass.getMethod("registerEvents");
registerMethod.invoke(null);
} catch (final NoClassDefFoundError
| ClassNotFoundException
| UnsupportedClassVersionError ignored) {
| ClassNotFoundException
| UnsupportedClassVersionError ignored) {
log.debug("JMX deadlock detection not supported");
} catch (final Throwable ex) {
log.error("Unable to initialize JMX thread deadlock detector", ex);
}
}

/** Enable JMX based system access provider once it is safe to touch JMX */
/**
* Enable JMX based system access provider once it is safe to touch JMX
*/
private static synchronized void initializeJmxSystemAccessProvider(
final ClassLoader classLoader) {
if (log.isDebugEnabled()) {
Expand Down Expand Up @@ -1058,7 +1061,9 @@ private static boolean isDebugMode() {
return false;
}

/** @return {@code true} if the agent feature is enabled */
/**
* @return {@code true} if the agent feature is enabled
*/
private static boolean isFeatureEnabled(AgentFeature feature) {
// must be kept in sync with logic from Config!
final String featureEnabledSysprop = feature.getSystemProp();
Expand Down Expand Up @@ -1200,7 +1205,9 @@ private static boolean isAppUsingCustomJMXBuilder(final EnumSet<Library> librari
return false;
}

/** Looks for the "dd." system property first then the "DD_" environment variable equivalent. */
/**
* Looks for the "dd." system property first then the "DD_" environment variable equivalent.
*/
private static String ddGetProperty(final String sysProp) {
String value = System.getProperty(sysProp);
if (null == value) {
Expand All @@ -1209,7 +1216,9 @@ private static String ddGetProperty(final String sysProp) {
return value;
}

/** Looks for the "DD_" environment variable equivalent of the given "dd." system property. */
/**
* Looks for the "DD_" environment variable equivalent of the given "dd." system property.
*/
private static String ddGetEnv(final String sysProp) {
return System.getenv(toEnvVar(sysProp));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package datadog.trace.bootstrap.instrumentation.decorator;

import static datadog.trace.api.cache.RadixTreeCache.PORTS;
import static datadog.trace.api.cache.RadixTreeCache.UNSET_PORT;

import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.api.Functions;
import datadog.trace.api.cache.QualifiedClassNameCache;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.Tags;

import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.Inet6Address;
Expand All @@ -18,6 +16,9 @@
import java.util.concurrent.ExecutionException;
import java.util.function.Function;

import static datadog.trace.api.cache.RadixTreeCache.PORTS;
import static datadog.trace.api.cache.RadixTreeCache.UNSET_PORT;

public abstract class BaseDecorator {

private static final QualifiedClassNameCache CLASS_NAMES =
Expand All @@ -38,15 +39,17 @@ public String apply(Class<?> clazz) {

protected final boolean traceAnalyticsEnabled;
protected final Double traceAnalyticsSampleRate;
protected final CharSequence version;

protected BaseDecorator() {
final Config config = Config.get();
final String[] instrumentationNames = instrumentationNames();
this.traceAnalyticsEnabled =
traceAnalyticsEnabled =
instrumentationNames.length > 0
&& config.isTraceAnalyticsIntegrationEnabled(
traceAnalyticsDefault(), instrumentationNames);
this.traceAnalyticsSampleRate =
version = config.getVersion();
traceAnalyticsSampleRate =
(double) config.getInstrumentationAnalyticsSampleRate(instrumentationNames);
}

Expand All @@ -65,6 +68,9 @@ public AgentSpan afterStart(final AgentSpan span) {
span.setSpanType(spanType());
}
span.setTag(Tags.COMPONENT, component());
if (version != ""){
span.setTag(Tags.DD_VERSION,version);
}
if (traceAnalyticsEnabled) {
span.setMetric(DDTags.ANALYTICS_SAMPLE_RATE, traceAnalyticsSampleRate);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package datadog.trace.bootstrap.instrumentation.jdbc;

import datadog.trace.api.Config;
import datadog.trace.api.cache.DDCache;
import datadog.trace.api.cache.DDCaches;
import datadog.trace.api.normalize.SQLNormalizer;
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
import java.util.function.Function;
import java.util.function.ToIntFunction;

import java.util.HashMap;
import java.util.Map;

import static java.nio.charset.StandardCharsets.UTF_8;

public final class DBQueryInfo {

private static final int COMBINED_SQL_LIMIT = 2 * 1024 * 1024; // characters
Expand All @@ -26,23 +32,46 @@ public static DBQueryInfo ofPreparedStatement(String sql) {

private final UTF8BytesString operation;
private final UTF8BytesString sql;
private Map<Integer, String> vals;
private UTF8BytesString originSql;

public boolean SqlObfuscation = Config.get().getJdbcSqlObfuscation();

public DBQueryInfo(String sql) {
this.sql = SQLNormalizer.normalize(sql);

if (SqlObfuscation) {
this.originSql = UTF8BytesString.create(sql.getBytes(UTF_8));
} else {
this.originSql = UTF8BytesString.EMPTY;
}
this.vals = new HashMap<>();
this.operation = UTF8BytesString.create(extractOperation(this.sql));
}

public UTF8BytesString getOperation() {
return operation;
}

public Map<Integer, String> getVals() {
return vals;
}

public void setVal(int index, String val) {
vals.put(index, val);
}

public UTF8BytesString getSql() {
return sql;
}


int weight() {
return sql.length();
}
public UTF8BytesString getOriginSql() {
return originSql;
}

public static CharSequence extractOperation(CharSequence sql) {
if (null == sql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static void run(final StatsDClientManager statsDClientManager, final Con
// Reduce noisiness of jmxfetch logging.
System.setProperty("org.slf4j.simpleLogger.log.org.datadog.jmxfetch", "warn");
}

System.setProperty("dd.jmxfetch.jmx_metric_fetch.enabled", "true");
final String jmxFetchConfigDir = config.getJmxFetchConfigDir();
final List<String> jmxFetchConfigs = config.getJmxFetchConfigs();
final List<String> internalMetricsConfigs = getInternalMetricFiles();
Expand Down
16 changes: 16 additions & 0 deletions dd-java-agent/instrumentation/axis-1/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
muzzle {
pass {
group = "org.apache.axis"
module = "axis"
versions = "[1.4,)"
}
}

apply from: "$rootDir/gradle/java.gradle"


dependencies {
compileOnly group: 'org.apache.axis', name: 'axis', version: '1.4'
compileOnly group: 'axis', name: 'axis-jaxrpc', version: '1.4'
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package datadog.trace.instrumentation.axis1;

import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface;
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.*;
import static datadog.trace.instrumentation.axis1.AxisMessageDecorator.AXIS2_MESSAGE;
import static datadog.trace.instrumentation.axis1.AxisMessageDecorator.DECORATE;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
import datadog.trace.agent.tooling.InstrumenterModule;
import datadog.trace.bootstrap.instrumentation.api.AgentScope;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.axis.MessageContext;

@AutoService(Instrumenter.class)
public final class AxisClientInstrumentation extends InstrumenterModule.Tracing
implements Instrumenter.ForTypeHierarchy {

public AxisClientInstrumentation() {super("axis1");}


@Override
public String hierarchyMarkerType() {
return "org.apache.axis.Handler";
}

@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".AxisMessageDecorator",
};
}

@Override
public ElementMatcher<TypeDescription> hierarchyMatcher() {
return implementsInterface(named(hierarchyMarkerType()));
}

@Override
public void methodAdvice(MethodTransformer transformation) {
transformation.applyAdvice(
isMethod()
.and(named("invoke"))
.and(takesArgument(0, named("org.apache.axis.MessageContext"))),
getClass().getName() + "$ClientInvokeMessageAdvice");
}

public static final class ClientInvokeMessageAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope beginInvoke(
@Advice.Argument(0) final MessageContext message) {
if (null == message ){
return null;
}

if (null == message.getOperation()){
return null;
}

AgentScope scope = activeScope();
if (null != scope) {
if (!DECORATE.sameTrace(scope.span(), message)) {
AgentSpan span = startSpan(AXIS2_MESSAGE);
DECORATE.afterStart(span);
DECORATE.onMessage(span, message);
return activateSpan(span);
}
}
return null;
}

@Advice.OnMethodExit(onThrowable = Throwable.class,suppress = Throwable.class)
public static void endInvoke(
@Advice.Enter final AgentScope scope,
@Advice.Argument(0) final MessageContext message,
@Advice.Thrown final Throwable error){
if (null == message){
return ;
}
if (null == scope) {
return;
}
System.out.println("getSoapAction name"+message.getOperation().getSoapAction());
AgentSpan span = scope.span();
if (null != error) {
DECORATE.onError(span, error);
}
DECORATE.beforeFinish(span, message);
scope.close();
span.finish();
}
}
}
Loading

0 comments on commit 8ad0022

Please sign in to comment.