Search in sources :

Example 16 with TransactionSegment

use of com.newrelic.agent.trace.TransactionSegment in project newrelic-java-agent by newrelic.

the class DefaultSqlTracerTest method testHighSecurityErrorFinish.

@SuppressWarnings("unchecked")
@Test
public void testHighSecurityErrorFinish() 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(new RuntimeException());
    SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
    TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);
    // exclusive_duration_millis, sql
    assertEquals(2, 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);
    assertEquals(obfuscatedInputSql, intrinsics.get(SqlTracer.SQL_PARAMETER_NAME));
}
Also used : AgentConfig(com.newrelic.agent.config.AgentConfig) TransactionSegment(com.newrelic.agent.trace.TransactionSegment) JSONArray(org.json.simple.JSONArray) SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) JSONObject(org.json.simple.JSONObject) TransactionTracerConfig(com.newrelic.agent.config.TransactionTracerConfig) Test(org.junit.Test)

Aggregations

TransactionSegment (com.newrelic.agent.trace.TransactionSegment)16 TransactionTracerConfig (com.newrelic.agent.config.TransactionTracerConfig)8 SqlObfuscator (com.newrelic.agent.database.SqlObfuscator)8 Test (org.junit.Test)8 JSONArray (org.json.simple.JSONArray)6 JSONObject (org.json.simple.JSONObject)5 TransactionTrace (com.newrelic.agent.trace.TransactionTrace)3 LinkedList (java.util.LinkedList)3 AgentConfig (com.newrelic.agent.config.AgentConfig)2 TransactionStats (com.newrelic.agent.stats.TransactionStats)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Transaction (com.newrelic.agent.Transaction)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1