use of com.newrelic.agent.database.SqlObfuscator in project newrelic-java-agent by newrelic.
the class DefaultSqlTracerTest method testInsertValuesTruncates.
@Test
public void testInsertValuesTruncates() throws Exception {
String sql = " Insert Into test \t\t VALUES ";
for (int i = 0; i < 100; i++) {
sql += "(333,444,555,666,777,888,999,111,222),";
}
DefaultSqlTracer tracer = newTracer(sql);
tracer.finish(Opcodes.RETURN, null);
assertEquals(getScopedMetric("test", "insert"), tracer.getMetricName());
SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
TransactionTracerConfig ttConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getTransactionTracerConfig();
TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);
JSONArray json = (JSONArray) AgentHelper.serializeJSON(segment);
JSONObject params = (JSONObject) json.get(3);
String truncatedSql = (String) params.get(SqlTracer.SQL_OBFUSCATED_PARAMETER_NAME);
assertNotNull(truncatedSql);
assertTrue(truncatedSql.length() < sql.length());
assertTrue(truncatedSql.endsWith("more chars)"));
}
use of com.newrelic.agent.database.SqlObfuscator in project newrelic-java-agent by newrelic.
the class DefaultSqlTracerTest method stackTrace.
@SuppressWarnings("unchecked")
@Test
public void stackTrace() throws Exception {
DefaultSqlTracer tracer = newTracer("select * from dude");
tracer.storeStackTrace();
SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
TransactionTracerConfig ttConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getTransactionTracerConfig();
TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);
JSONArray json = (JSONArray) AgentHelper.serializeJSON(segment);
Map params = (Map) json.get(3);
List backtrace = (List) params.get(DefaultSqlTracer.BACKTRACE_PARAMETER_NAME);
assertTrue(backtrace.size() > 2);
}
use of com.newrelic.agent.database.SqlObfuscator in project newrelic-java-agent by newrelic.
the class DefaultSqlTracerTest method testHighSecurityNormalFinish.
@SuppressWarnings("unchecked")
@Test
public void testHighSecurityNormalFinish() throws Exception {
TransactionTracerConfig ttConfig = mock(TransactionTracerConfig.class);
// These are set to -1 to allow this tracer to always go through slow sql logic. Otherwise if the tracer
// executes in less than 1 millisecond (e.g. 0 ms rounded) it won't be greater than the threshold (0) so
// we won't run this through the slow sql code.
when(ttConfig.getExplainThresholdInMillis()).thenReturn(-1d);
when(ttConfig.getExplainThresholdInNanos()).thenReturn(-1d);
when(ttConfig.getRecordSql()).thenReturn(RecordSql.obfuscated.name());
when(ttConfig.isExplainEnabled()).thenReturn(true);
when(ttConfig.isEnabled()).thenReturn(true);
AgentConfig agentConfig = AgentHelper.mockAgentConfig(ttConfig);
when(agentConfig.isHighSecurity()).thenReturn(true);
when(agentConfig.getTransactionTracerConfig()).thenReturn(ttConfig);
String inputSql = "select * from dudette where ssn = 123456789";
String obfuscatedInputSql = "select * from dudette where ssn = ?";
DefaultSqlTracer tracer = newTracer(inputSql);
assertTrue(tracer.getAgentAttributes().isEmpty());
tracer.finish(Opcodes.RETURN, null);
SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);
// exclusive_duration_millis, sql, sql_obfuscated, host, port_path_or_id
assertEquals(5, tracer.getAgentAttributes().size());
// shouldn't be obfuscated yet
assertEquals(inputSql, (String) tracer.getAgentAttributes().get("sql"));
JSONArray json = (JSONArray) AgentHelper.serializeJSON(segment);
Map params = (Map) json.get(3);
// the name changes when it is obfuscated
String sqlStatement = (String) params.get("sql_obfuscated");
assertEquals(obfuscatedInputSql, sqlStatement);
String rawSql = (String) params.get("sql");
// raw sql should be null since high security is on
assertNull(rawSql);
// Ensure that we do not send up unobfuscated sql in the intrinsics
Transaction transaction = Transaction.getTransaction(false);
assertNotNull(transaction);
Map<String, Object> intrinsics = transaction.getIntrinsicAttributes();
assertNotNull(intrinsics);
assertNull(intrinsics.get(SqlTracer.SQL_PARAMETER_NAME));
}
use of com.newrelic.agent.database.SqlObfuscator in project newrelic-java-agent by newrelic.
the class SqlTraceServiceTest method multipleHarvests.
@Test
public void multipleHarvests() throws Exception {
Map<String, Object> configMap = createStagingMap();
createServiceManager(configMap);
SqlTraceService sqlTraceService = ServiceFactory.getSqlTraceService();
Assert.assertTrue(sqlTraceService.isEnabled());
// run a transaction
Tracer requestDispatcherTracer = startDispatcherTracer();
long duration = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis() + 1, TimeUnit.MILLISECONDS);
startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
startSqlTracer("select * from dudette where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
requestDispatcherTracer.finish(Opcodes.RETURN, null);
// run a harvest
MockHarvestService mockharvestService = (MockHarvestService) ServiceFactory.getHarvestService();
mockharvestService.runHarvest(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName(), new StatsEngineImpl());
MockRPMService mockRPMService = (MockRPMService) ServiceFactory.getRPMService();
List<SqlTrace> sqlTraces = mockRPMService.getSqlTraces();
// verify results
Assert.assertEquals(2, sqlTraces.size());
// run a transaction
requestDispatcherTracer = startDispatcherTracer();
startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
startSqlTracer("select * from dude where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
startSqlTracer("select * from dudette where somevalue = 'cool'", duration).finish(Opcodes.RETURN, null);
requestDispatcherTracer.finish(Opcodes.RETURN, null);
// run a harvest
mockharvestService.runHarvest(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName(), new StatsEngineImpl());
sqlTraces = mockRPMService.getSqlTraces();
// verify results
Assert.assertEquals(2, sqlTraces.size());
long expectedDuration = TimeUnit.MILLISECONDS.convert(duration, TimeUnit.NANOSECONDS);
String expectedSql = "select * from dude where somevalue = ?";
SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
long expectedId = sqlObfuscator.obfuscateSql(expectedSql).hashCode();
SqlTrace sqlTrace = getSqlTrace(expectedId, sqlTraces);
Assert.assertNotNull(sqlTrace);
Assert.assertEquals(expectedId, sqlTrace.getId());
Assert.assertEquals(expectedSql, sqlTrace.getQuery());
Assert.assertEquals(2, sqlTrace.getCallCount());
Assert.assertEquals(expectedDuration * 2, sqlTrace.getTotal());
Assert.assertEquals(expectedDuration, sqlTrace.getMin());
Assert.assertEquals(expectedDuration, sqlTrace.getMax());
expectedSql = "select * from dudette where somevalue = ?";
expectedId = sqlObfuscator.obfuscateSql(expectedSql).hashCode();
sqlTrace = getSqlTrace(expectedId, sqlTraces);
Assert.assertNotNull(sqlTrace);
Assert.assertEquals(expectedId, sqlTrace.getId());
Assert.assertEquals(expectedSql, sqlTrace.getQuery());
Assert.assertEquals(1, sqlTrace.getCallCount());
Assert.assertEquals(expectedDuration, sqlTrace.getTotal());
Assert.assertEquals(expectedDuration, sqlTrace.getMin());
Assert.assertEquals(expectedDuration, sqlTrace.getMax());
}
use of com.newrelic.agent.database.SqlObfuscator in project newrelic-java-agent by newrelic.
the class SqlTraceServiceTest method insertSqlMaxLength.
@Test
public void insertSqlMaxLength() throws Exception {
Map<String, Object> configMap = createStagingMap();
createServiceManager(configMap);
// run a transaction
Tracer requestDispatcherTracer = startDispatcherTracer();
long duration = TimeUnit.NANOSECONDS.convert(getExplainThresholdInMillis() + 1, TimeUnit.MILLISECONDS);
char[] charData = new char[2100];
String sql = "select * from dude where where somevalue = 'cool'";
for (int i = 0; i < sql.length(); i++) {
charData[i] = sql.charAt(i);
}
for (int i = sql.length(); i < charData.length; i++) {
charData[i] = 'a';
}
sql = String.valueOf(charData);
startSqlTracer(sql, duration).finish(Opcodes.RETURN, null);
requestDispatcherTracer.finish(Opcodes.RETURN, null);
// run a harvest
MockHarvestService mockharvestService = (MockHarvestService) ServiceFactory.getHarvestService();
mockharvestService.runHarvest(ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName(), new StatsEngineImpl());
MockRPMService mockRPMService = (MockRPMService) ServiceFactory.getRPMService();
List<SqlTrace> sqlTraces = mockRPMService.getSqlTraces();
Assert.assertEquals(1, sqlTraces.size());
SqlTrace sqlTrace = sqlTraces.get(0);
int maxSqlLength = ServiceFactory.getConfigService().getDefaultAgentConfig().getTransactionTracerConfig().getInsertSqlMaxLength();
SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
sql = sqlObfuscator.obfuscateSql(sql);
Assert.assertEquals(TransactionSegment.truncateSql(sql, maxSqlLength).length(), sqlTrace.getQuery().length());
}
Aggregations