Search in sources :

Example 6 with SlowQueryWithInputDatastoreParameters

use of com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters in project newrelic-java-agent by newrelic.

the class AgentPreprocessorsTest method testRewriteSlowQueryBuilderWithHighSecurity.

@Test
public void testRewriteSlowQueryBuilderWithHighSecurity() throws Exception {
    final ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    final String classname = "com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessorsTest$ExternalParametersFactoryTestClass6";
    Map<String, Object> confProps = new HashMap<>();
    confProps.put("high_security", true);
    AgentConfig agentConfig = AgentConfigImpl.createAgentConfig(confProps);
    byte[] bytes = getClassBytesFromClassLoaderResource(classname, classloader);
    Assert.assertNotNull(bytes);
    ClassNode source = WeaveUtils.convertToClassNode(bytes);
    ClassNode result = new ClassNode(WeaveUtils.ASM_API_LEVEL);
    ClassVisitor cv = new CheckClassAdapter(result);
    AgentPreprocessors preprocessors = new AgentPreprocessors(agentConfig, null);
    preprocessors.setInstrumentationTitle(INSTRUMENTATION_TITLE);
    cv = preprocessors.rewriteSlowQueryIfRequired(cv);
    source.accept(cv);
    Class<?> clazz = addToClassloader(result, classloader);
    Assert.assertNotNull(clazz);
    ExternalParametersFactoryTestClass6 testClass = (ExternalParametersFactoryTestClass6) clazz.newInstance();
    assertNotNull(testClass);
    ExternalParameters regularDatastore = testClass.createRegularDatastore();
    assertNotNull(regularDatastore);
    assertTrue(regularDatastore instanceof DatastoreParameters);
    ExternalParameters slowQueryDatastore = testClass.createSlowQueryDatastore();
    assertNotNull(slowQueryDatastore);
    // AgentPreprocessors should re-write the call to return a regular DatastoreParameters object
    assertTrue(!(slowQueryDatastore instanceof SlowQueryDatastoreParameters));
    ExternalParameters slowQueryWithInputDatastore = testClass.createSlowQueryWithInputDatastore();
    assertNotNull(slowQueryWithInputDatastore);
    // AgentPreprocessors should re-write the call to return a regular DatastoreParameters object
    assertTrue(!(slowQueryWithInputDatastore instanceof SlowQueryWithInputDatastoreParameters));
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DatastoreParameters(com.newrelic.api.agent.DatastoreParameters) SlowQueryDatastoreParameters(com.newrelic.api.agent.SlowQueryDatastoreParameters) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters) ClassVisitor(org.objectweb.asm.ClassVisitor) TokenNullCheckClassVisitor(com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessors.TokenNullCheckClassVisitor) SlowQueryDatastoreParameters(com.newrelic.api.agent.SlowQueryDatastoreParameters) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters) AgentConfig(com.newrelic.agent.config.AgentConfig) ExternalParameters(com.newrelic.api.agent.ExternalParameters) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) Test(org.junit.Test)

Example 7 with SlowQueryWithInputDatastoreParameters

use of com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters in project newrelic-java-agent by newrelic.

the class AgentPreprocessorsTest method testRewriteSlowQueryWithHighSecurity.

@Test
public void testRewriteSlowQueryWithHighSecurity() throws Exception {
    final ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    final String classname = "com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessorsTest$ExternalParametersFactoryTestClass2";
    Map<String, Object> confProps = new HashMap<>();
    confProps.put("high_security", true);
    AgentConfig agentConfig = AgentConfigImpl.createAgentConfig(confProps);
    byte[] bytes = getClassBytesFromClassLoaderResource(classname, classloader);
    Assert.assertNotNull(bytes);
    ClassNode source = WeaveUtils.convertToClassNode(bytes);
    ClassNode result = new ClassNode(WeaveUtils.ASM_API_LEVEL);
    ClassVisitor cv = new CheckClassAdapter(result);
    AgentPreprocessors preprocessors = new AgentPreprocessors(agentConfig, null);
    preprocessors.setInstrumentationTitle(INSTRUMENTATION_TITLE);
    cv = preprocessors.rewriteSlowQueryIfRequired(cv);
    source.accept(cv);
    Class<?> clazz = addToClassloader(result, classloader);
    Assert.assertNotNull(clazz);
    ExternalParametersFactoryTestClass2 testClass = (ExternalParametersFactoryTestClass2) clazz.newInstance();
    assertNotNull(testClass);
    ExternalParameters regularDatastore = testClass.createRegularDatastore();
    assertNotNull(regularDatastore);
    assertTrue(regularDatastore instanceof DatastoreParameters);
    ExternalParameters slowQueryDatastore = testClass.createSlowQueryDatastore();
    assertNotNull(slowQueryDatastore);
    // AgentPreprocessors should re-write the call to return a regular DatastoreParameters object
    assertTrue(!(slowQueryDatastore instanceof SlowQueryDatastoreParameters));
    ExternalParameters slowQueryWithInputDatastore = testClass.createSlowQueryWithInputDatastore();
    assertNotNull(slowQueryWithInputDatastore);
    // AgentPreprocessors should re-write the call to return a regular DatastoreParameters object
    assertTrue(!(slowQueryWithInputDatastore instanceof SlowQueryWithInputDatastoreParameters));
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DatastoreParameters(com.newrelic.api.agent.DatastoreParameters) SlowQueryDatastoreParameters(com.newrelic.api.agent.SlowQueryDatastoreParameters) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters) ClassVisitor(org.objectweb.asm.ClassVisitor) TokenNullCheckClassVisitor(com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessors.TokenNullCheckClassVisitor) SlowQueryDatastoreParameters(com.newrelic.api.agent.SlowQueryDatastoreParameters) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters) AgentConfig(com.newrelic.agent.config.AgentConfig) ExternalParameters(com.newrelic.api.agent.ExternalParameters) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) Test(org.junit.Test)

Example 8 with SlowQueryWithInputDatastoreParameters

use of com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters in project newrelic-java-agent by newrelic.

the class AgentPreprocessorsTest method testRewriteSlowQueryBuilderWithHighSecurityAndNonPresentListToCollect.

@Test
public void testRewriteSlowQueryBuilderWithHighSecurityAndNonPresentListToCollect() throws Exception {
    final ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    final String classname = "com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessorsTest$ExternalParametersFactoryTestClass8";
    Map<String, Object> confProps = new HashMap<>();
    confProps.put("high_security", true);
    Map<String, Object> ttConfig = new HashMap<>();
    ttConfig.put("collect_slow_queries_from", "some-other-module");
    confProps.put("transaction_tracer", ttConfig);
    AgentConfig agentConfig = AgentConfigImpl.createAgentConfig(confProps);
    byte[] bytes = getClassBytesFromClassLoaderResource(classname, classloader);
    Assert.assertNotNull(bytes);
    ClassNode source = WeaveUtils.convertToClassNode(bytes);
    ClassNode result = new ClassNode(WeaveUtils.ASM_API_LEVEL);
    ClassVisitor cv = new CheckClassAdapter(result);
    AgentPreprocessors preprocessors = new AgentPreprocessors(agentConfig, null);
    preprocessors.setInstrumentationTitle(INSTRUMENTATION_TITLE);
    cv = preprocessors.rewriteSlowQueryIfRequired(cv);
    source.accept(cv);
    Class<?> clazz = addToClassloader(result, classloader);
    Assert.assertNotNull(clazz);
    ExternalParametersFactoryTestClass8 testClass = (ExternalParametersFactoryTestClass8) clazz.newInstance();
    assertNotNull(testClass);
    ExternalParameters regularDatastore = testClass.createRegularDatastore();
    assertNotNull(regularDatastore);
    assertTrue(regularDatastore instanceof DatastoreParameters);
    ExternalParameters slowQueryDatastore = testClass.createSlowQueryDatastore();
    assertNotNull(slowQueryDatastore);
    // AgentPreprocessors should re-write the call to return a regular DatastoreParameters object
    assertTrue(!(slowQueryDatastore instanceof SlowQueryDatastoreParameters));
    ExternalParameters slowQueryWithInputDatastore = testClass.createSlowQueryWithInputDatastore();
    assertNotNull(slowQueryWithInputDatastore);
    // AgentPreprocessors should re-write the call to return a regular DatastoreParameters object
    assertTrue(!(slowQueryWithInputDatastore instanceof SlowQueryWithInputDatastoreParameters));
}
Also used : ClassNode(org.objectweb.asm.tree.ClassNode) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DatastoreParameters(com.newrelic.api.agent.DatastoreParameters) SlowQueryDatastoreParameters(com.newrelic.api.agent.SlowQueryDatastoreParameters) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters) ClassVisitor(org.objectweb.asm.ClassVisitor) TokenNullCheckClassVisitor(com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessors.TokenNullCheckClassVisitor) SlowQueryDatastoreParameters(com.newrelic.api.agent.SlowQueryDatastoreParameters) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters) AgentConfig(com.newrelic.agent.config.AgentConfig) ExternalParameters(com.newrelic.api.agent.ExternalParameters) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) Test(org.junit.Test)

Example 9 with SlowQueryWithInputDatastoreParameters

use of com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters in project newrelic-java-agent by newrelic.

the class DefaultSlowQueryListener method noticeTracer.

@Override
public <T> void noticeTracer(Tracer tracer, SlowQueryDatastoreParameters<T> slowQueryDatastoreParameters) {
    if (tracer.getDurationInMilliseconds() > thresholdInMillis) {
        T rawQuery = slowQueryDatastoreParameters.getRawQuery();
        QueryConverter<T> queryConverter = slowQueryDatastoreParameters.getQueryConverter();
        if (rawQuery == null || queryConverter == null) {
            // Ignore tracer
            return;
        }
        String rawQueryString = queryConverter.toRawQueryString(rawQuery);
        if (rawQueryString == null || rawQueryString.trim().isEmpty()) {
            // Ignore tracer
            return;
        }
        String obfuscatedQueryString = queryConverter.toObfuscatedQueryString(rawQuery);
        if (obfuscatedQueryString == null) {
            // Ignore tracer if no obfuscated query is provided
            return;
        }
        // Handle an "input query" from an ORM or a framework that automatically generates queries
        if (slowQueryDatastoreParameters instanceof SlowQueryWithInputDatastoreParameters) {
            handleInputQuery(tracer, (SlowQueryWithInputDatastoreParameters) slowQueryDatastoreParameters);
        }
        // This allows transaction traces to show slow queries directly in the trace details
        tracer.setAgentAttribute(SqlTracer.SQL_PARAMETER_NAME, rawQueryString);
        tracer.setAgentAttribute(SqlTracer.SQL_OBFUSCATED_PARAMETER_NAME, obfuscatedQueryString);
        DatastoreConfig datastoreConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getDatastoreConfig();
        boolean allUnknown = slowQueryDatastoreParameters.getHost() == null && slowQueryDatastoreParameters.getPort() == null && slowQueryDatastoreParameters.getPathOrId() == null;
        if (datastoreConfig.isInstanceReportingEnabled() && !allUnknown) {
            tracer.setAgentAttribute(DatastoreMetrics.DATASTORE_HOST, DatastoreMetrics.replaceLocalhost(slowQueryDatastoreParameters.getHost()));
            tracer.setAgentAttribute(DatastoreMetrics.DATASTORE_PORT_PATH_OR_ID, DatastoreMetrics.getIdentifierOrPort(slowQueryDatastoreParameters.getPort(), slowQueryDatastoreParameters.getPathOrId()));
        }
        if (datastoreConfig.isDatabaseNameReportingEnabled() && slowQueryDatastoreParameters.getDatabaseName() != null) {
            tracer.setAgentAttribute(DatastoreMetrics.DB_INSTANCE, slowQueryDatastoreParameters.getDatabaseName());
        }
        if (slowQueryInfoCache == null) {
            slowQueryInfoCache = new BoundedConcurrentCache<>(MAX_SQL_TRACERS);
        }
        SlowQueryInfo existingInfo = slowQueryInfoCache.get(obfuscatedQueryString);
        if (existingInfo != null) {
            // Aggregate tracers by SQL.
            existingInfo.aggregate(tracer);
            slowQueryInfoCache.putReplace(obfuscatedQueryString, existingInfo);
        } else {
            SlowQueryInfo sqlInfo = new SlowQueryInfo(null, tracer, rawQueryString, obfuscatedQueryString, tracer.getTransactionActivity().getTransaction().getAgentConfig().getSqlTraceConfig());
            sqlInfo.aggregate(tracer);
            slowQueryInfoCache.putIfAbsent(obfuscatedQueryString, sqlInfo);
        }
    }
}
Also used : DatastoreConfig(com.newrelic.agent.config.DatastoreConfig) SlowQueryWithInputDatastoreParameters(com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters)

Aggregations

SlowQueryWithInputDatastoreParameters (com.newrelic.api.agent.SlowQueryWithInputDatastoreParameters)9 AgentConfig (com.newrelic.agent.config.AgentConfig)8 TokenNullCheckClassVisitor (com.newrelic.agent.instrumentation.weaver.preprocessors.AgentPreprocessors.TokenNullCheckClassVisitor)8 DatastoreParameters (com.newrelic.api.agent.DatastoreParameters)8 ExternalParameters (com.newrelic.api.agent.ExternalParameters)8 SlowQueryDatastoreParameters (com.newrelic.api.agent.SlowQueryDatastoreParameters)8 HashMap (java.util.HashMap)8 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)8 Test (org.junit.Test)8 ClassVisitor (org.objectweb.asm.ClassVisitor)8 ClassNode (org.objectweb.asm.tree.ClassNode)8 CheckClassAdapter (org.objectweb.asm.util.CheckClassAdapter)8 DatastoreConfig (com.newrelic.agent.config.DatastoreConfig)1