|
| 1 | +package com.datadoghq.trace.agent; |
| 2 | + |
| 3 | +import com.datadoghq.trace.DDTraceAnnotationsInfo; |
| 4 | +import com.datadoghq.trace.DDTraceInfo; |
| 5 | +import com.datadoghq.trace.resolver.DDTracerFactory; |
| 6 | +import com.datadoghq.trace.resolver.FactoryUtils; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStream; |
| 9 | +import java.io.PrintWriter; |
| 10 | +import java.io.StringWriter; |
| 11 | +import java.net.URI; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.net.URL; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Enumeration; |
| 16 | +import java.util.List; |
| 17 | +import lombok.extern.slf4j.Slf4j; |
| 18 | +import org.jboss.byteman.agent.Retransformer; |
| 19 | + |
| 20 | +@Slf4j |
| 21 | +public class AgentRulesManager { |
| 22 | + |
| 23 | + // Initialize the info classes so they print out their version info: |
| 24 | + private static final String ddJavaAgentVersion = DDJavaAgentInfo.VERSION; |
| 25 | + private static final String ddTraceVersion = DDTraceInfo.VERSION; |
| 26 | + private static final String ddTraceAnnotationsVersion = DDTraceAnnotationsInfo.VERSION; |
| 27 | + |
| 28 | + private static final String SPRING_BOOT_RULE = "spring-boot-rule.btm"; |
| 29 | + |
| 30 | + protected static volatile AgentRulesManager INSTANCE; |
| 31 | + |
| 32 | + protected final Retransformer transformer; |
| 33 | + protected final TracingAgentConfig agentTracerConfig; |
| 34 | + protected final InstrumentationRulesManager instrumentationRulesManager; |
| 35 | + protected final TraceAnnotationsManager traceAnnotationsManager; |
| 36 | + |
| 37 | + public AgentRulesManager(Retransformer trans, TracingAgentConfig config) { |
| 38 | + transformer = trans; |
| 39 | + agentTracerConfig = config; |
| 40 | + traceAnnotationsManager = new TraceAnnotationsManager(trans, config); |
| 41 | + instrumentationRulesManager = new InstrumentationRulesManager(trans, config, this); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * This method initializes the manager. |
| 46 | + * |
| 47 | + * @param trans The ByteMan retransformer |
| 48 | + */ |
| 49 | + public static void initialize(final Retransformer trans) { |
| 50 | + log.debug("Initializing {}", AgentRulesManager.class.getSimpleName()); |
| 51 | + |
| 52 | + TracingAgentConfig config = |
| 53 | + FactoryUtils.loadConfigFromFilePropertyOrResource( |
| 54 | + DDTracerFactory.SYSTEM_PROPERTY_CONFIG_PATH, |
| 55 | + DDTracerFactory.CONFIG_PATH, |
| 56 | + TracingAgentConfig.class); |
| 57 | + |
| 58 | + log.debug("Configuration: {}", config.toString()); |
| 59 | + |
| 60 | + AgentRulesManager manager = new AgentRulesManager(trans, config); |
| 61 | + |
| 62 | + INSTANCE = manager; |
| 63 | + |
| 64 | + manager.loadRules(SPRING_BOOT_RULE, ClassLoader.getSystemClassLoader()); |
| 65 | + manager.traceAnnotationsManager.initialize(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * This method loads any OpenTracing Agent rules (integration-rules.btm) found as resources within |
| 70 | + * the supplied classloader. |
| 71 | + * |
| 72 | + * @param classLoader The classloader |
| 73 | + */ |
| 74 | + protected List<String> loadRules(String rulesFileName, final ClassLoader classLoader) { |
| 75 | + final List<String> scripts = new ArrayList<>(); |
| 76 | + if (transformer == null) { |
| 77 | + log.warn( |
| 78 | + "Attempt to load rules file {} on classloader {} before transformer initialized", |
| 79 | + rulesFileName, |
| 80 | + classLoader == null ? "bootstrap" : classLoader); |
| 81 | + return scripts; |
| 82 | + } |
| 83 | + |
| 84 | + log.debug("Loading rules with classloader {}", classLoader == null ? "bootstrap" : classLoader); |
| 85 | + |
| 86 | + final List<String> scriptNames = new ArrayList<>(); |
| 87 | + |
| 88 | + // Load default and custom rules |
| 89 | + try { |
| 90 | + final Enumeration<URL> iter = classLoader.getResources(rulesFileName); |
| 91 | + while (iter.hasMoreElements()) { |
| 92 | + loadRules(iter.nextElement().toURI(), scriptNames, scripts); |
| 93 | + } |
| 94 | + |
| 95 | + final StringWriter sw = new StringWriter(); |
| 96 | + try (PrintWriter writer = new PrintWriter(sw)) { |
| 97 | + try { |
| 98 | + transformer.installScript(scripts, scriptNames, writer); |
| 99 | + } catch (final Exception e) { |
| 100 | + log.warn("Failed to install scripts", e); |
| 101 | + } |
| 102 | + } |
| 103 | + log.debug(sw.toString()); |
| 104 | + } catch (IOException | URISyntaxException e) { |
| 105 | + log.warn("Failed to load rules", e); |
| 106 | + } |
| 107 | + |
| 108 | + log.debug("Rules loaded from {} on classloader {}", rulesFileName, classLoader); |
| 109 | + if (log.isTraceEnabled()) { |
| 110 | + for (final String rule : scripts) { |
| 111 | + log.trace("Loading rule: {}", rule); |
| 112 | + } |
| 113 | + } |
| 114 | + return scripts; |
| 115 | + } |
| 116 | + |
| 117 | + private static void loadRules( |
| 118 | + final URI uri, final List<String> scriptNames, final List<String> scripts) |
| 119 | + throws IOException { |
| 120 | + log.debug("Load rules from URI uri={} ", uri); |
| 121 | + |
| 122 | + final StringBuilder str = new StringBuilder(); |
| 123 | + try (InputStream is = uri.toURL().openStream()) { |
| 124 | + |
| 125 | + final byte[] b = new byte[10240]; |
| 126 | + int len; |
| 127 | + while ((len = is.read(b)) != -1) { |
| 128 | + str.append(new String(b, 0, len)); |
| 129 | + } |
| 130 | + } |
| 131 | + scripts.add(str.toString()); |
| 132 | + scriptNames.add(uri.toString()); |
| 133 | + } |
| 134 | +} |
0 commit comments