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));
}
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));
}
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));
}
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);
}
}
}
Aggregations