Search in sources :

Example 11 with SqlObfuscator

use of com.newrelic.agent.database.SqlObfuscator in project newrelic-java-agent by newrelic.

the class DefaultSqlTracerTest method testSqlInParameterMap.

@SuppressWarnings("unchecked")
@Test
public void testSqlInParameterMap() throws Exception {
    String inputSql = "select * from dudette";
    DefaultSqlTracer tracer = newTracer(inputSql);
    assertTrue(tracer.getAgentAttributes().isEmpty());
    SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
    TransactionTracerConfig ttConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getTransactionTracerConfig();
    TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);
    // exclusive_duration_millis and sql
    assertEquals(2, tracer.getAgentAttributes().size());
    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(inputSql, sqlStatement);
}
Also used : TransactionSegment(com.newrelic.agent.trace.TransactionSegment) JSONArray(org.json.simple.JSONArray) SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) TransactionTracerConfig(com.newrelic.agent.config.TransactionTracerConfig) Test(org.junit.Test)

Example 12 with SqlObfuscator

use of com.newrelic.agent.database.SqlObfuscator 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)

Example 13 with SqlObfuscator

use of com.newrelic.agent.database.SqlObfuscator in project newrelic-java-agent by newrelic.

the class SqlObfuscationCrossAgentTest method runTest.

@Test
public void runTest() {
    String rawSql = input.getRawSql();
    Set<String> expectedObfuscatedSql = input.getObfuscatedSql();
    SqlObfuscator sqlObfuscator = SqlObfuscator.getDefaultSqlObfuscator();
    String actualObfuscatedSql = sqlObfuscator.obfuscateSql(rawSql, input.getDialect());
    Assert.assertTrue("Expected: " + expectedObfuscatedSql + ", Actual: " + actualObfuscatedSql, expectedObfuscatedSql.contains(actualObfuscatedSql));
}
Also used : SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) Test(org.junit.Test)

Example 14 with SqlObfuscator

use of com.newrelic.agent.database.SqlObfuscator in project newrelic-java-agent by newrelic.

the class SqlTraceServiceTest method overExplainThreshold.

@Test
public void overExplainThreshold() throws Exception {
    Map<String, Object> configMap = createStagingMap();
    createServiceManager(configMap);
    // 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());
    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());
}
Also used : StatsEngineImpl(com.newrelic.agent.stats.StatsEngineImpl) SqlTracer(com.newrelic.agent.tracers.SqlTracer) OtherRootSqlTracer(com.newrelic.agent.tracers.OtherRootSqlTracer) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Tracer(com.newrelic.agent.tracers.Tracer) MockHarvestService(com.newrelic.agent.MockHarvestService) SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 15 with SqlObfuscator

use of com.newrelic.agent.database.SqlObfuscator in project newrelic-java-agent by newrelic.

the class TransactionSegmentTest method createSegment.

private TransactionSegment createSegment(MockServiceManager manager, Map<String, Object> tracerAttributes) {
    TransactionTracerConfig ttConfig = manager.getConfigService().getDefaultAgentConfig().getTransactionTracerConfig();
    Tracer tracer = Mockito.mock(Tracer.class);
    Mockito.when(tracer.getClassMethodSignature()).thenReturn(new ClassMethodSignature("class", "method", "methodDesc"));
    Mockito.when(tracer.getTransactionSegmentName()).thenReturn("segmentName");
    Mockito.when(tracer.getAgentAttributes()).thenReturn(tracerAttributes);
    Mockito.when(tracer.getTransactionSegmentUri()).thenReturn("http://host:1234/path/to/chocolate");
    SqlObfuscator obfuscator = SqlObfuscator.getDefaultSqlObfuscator();
    String appName = manager.getConfigService().getDefaultAgentConfig().getApplicationName();
    return new TransactionSegment(ttConfig, appName, obfuscator, 0, tracer, null);
}
Also used : ClassMethodSignature(com.newrelic.agent.tracers.ClassMethodSignature) Tracer(com.newrelic.agent.tracers.Tracer) SqlObfuscator(com.newrelic.agent.database.SqlObfuscator) TransactionTracerConfig(com.newrelic.agent.config.TransactionTracerConfig)

Aggregations

SqlObfuscator (com.newrelic.agent.database.SqlObfuscator)15 Test (org.junit.Test)14 TransactionTracerConfig (com.newrelic.agent.config.TransactionTracerConfig)9 TransactionSegment (com.newrelic.agent.trace.TransactionSegment)8 Tracer (com.newrelic.agent.tracers.Tracer)6 JSONArray (org.json.simple.JSONArray)6 MockHarvestService (com.newrelic.agent.MockHarvestService)5 MockRPMService (com.newrelic.agent.MockRPMService)5 StatsEngineImpl (com.newrelic.agent.stats.StatsEngineImpl)5 OtherRootSqlTracer (com.newrelic.agent.tracers.OtherRootSqlTracer)5 SqlTracer (com.newrelic.agent.tracers.SqlTracer)5 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)5 JSONObject (org.json.simple.JSONObject)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 ClassMethodSignature (com.newrelic.agent.tracers.ClassMethodSignature)1 List (java.util.List)1 Map (java.util.Map)1