Search in sources :

Example 1 with TransactionInsights

use of com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights in project newrelic-java-agent by newrelic.

the class InsightsServiceImplTest method testTransactionHarvest.

@Test
public void testTransactionHarvest() throws Exception {
    final Map<String, Object> config = createConfig(true, 2);
    InsightsServiceImpl insights = createService(config);
    insights.addHarvestableToService(appName);
    TransactionInsights txInsights = new TransactionInsights(AgentConfigImpl.createAgentConfig(config));
    txInsights.recordCustomEvent("everything", ImmutableMap.<String, Object>of("is", 5, "awesome", true));
    txInsights.recordCustomEvent("class", ImmutableMap.<String, Object>of("name", "Foo", "number", 666));
    txInsights.recordCustomEvent("method", ImmutableMap.of("className", "Foo", "methodName", "getSomething"));
    // Invalid:
    insights.recordCustomEvent("invalid-type", ImmutableMap.<String, Object>of("name", "Foo", "number", 666));
    Map<String, String> m;
    m = Collections.singletonMap("key", "value");
    insights.recordCustomEvent(null, m);
    m = Collections.singletonMap(null, null);
    insights.recordCustomEvent("bothNull", m);
    m = Collections.singletonMap("key", null);
    insights.recordCustomEvent("valueNull", m);
    m = Collections.singletonMap(null, "value");
    insights.recordCustomEvent("keyNull", m);
    m = new HashMap<>();
    m.put("goodKey1", "goodValue");
    m.put("goodKey2", null);
    m.put(null, null);
    insights.recordCustomEvent("AllKeyValueCombos", m);
    MockRPMService analyticsData = new MockRPMService();
    Mockito.when(ServiceFactory.getServiceManager().getRPMServiceManager().getOrCreateRPMService(appName)).thenReturn(analyticsData);
    TransactionData transactionData = Mockito.mock(TransactionData.class);
    Mockito.when(transactionData.getApplicationName()).thenReturn(appName);
    Mockito.when(transactionData.getInsightsData()).thenReturn(txInsights);
    insights.transactionListener.dispatcherTransactionFinished(transactionData, null);
    insights.harvestHarvestables();
    assertEquals(2, analyticsData.getEvents().size());
}
Also used : TransactionInsights(com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights) TransactionData(com.newrelic.agent.TransactionData) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 2 with TransactionInsights

use of com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights in project newrelic-java-agent by newrelic.

the class InsightsServiceImplTest method testWithTransaction.

@Test
public void testWithTransaction() throws Exception {
    Map<String, Object> config = createConfig(true, 2);
    config.put(AgentConfigImpl.ASYNC_TIMEOUT, 180);
    InsightsServiceImpl insights = createService(config);
    Transaction transaction = Mockito.mock(Transaction.class);
    Mockito.when(txService.getTransaction(false)).thenReturn(transaction);
    TransactionInsights txInsights = new TransactionInsights(AgentConfigImpl.createAgentConfig(Collections.<String, Object>emptyMap()));
    Mockito.when(transaction.getInsightsData()).thenReturn(txInsights);
    Mockito.when(transaction.getApplicationName()).thenReturn(appName);
    Mockito.when(transaction.isInProgress()).thenReturn(true);
    insights.recordCustomEvent("everything", ImmutableMap.<String, Object>of("is", 5, "awesome", true));
    insights.recordCustomEvent("class", ImmutableMap.<String, Object>of("name", "Foo", "number", 666));
    insights.recordCustomEvent("method", ImmutableMap.of("className", "Foo", "methodName", "getSomething"));
    // Invalid:
    insights.recordCustomEvent("invalid-type", ImmutableMap.<String, Object>of("name", "Foo", "number", 666));
    Map<String, String> m;
    m = Collections.singletonMap("key", "value");
    insights.recordCustomEvent(null, m);
    m = Collections.singletonMap(null, null);
    insights.recordCustomEvent("bothNull", m);
    m = Collections.singletonMap("key", null);
    insights.recordCustomEvent("valueNull", m);
    m = Collections.singletonMap(null, "value");
    insights.recordCustomEvent("keyNull", m);
    m = new HashMap<>();
    m.put("goodKey1", "goodValue");
    m.put("goodKey2", null);
    m.put(null, null);
    insights.recordCustomEvent("AllKeyValueCombos", m);
    MockRPMService analyticsData = new MockRPMService();
    Mockito.when(ServiceFactory.getServiceManager().getRPMServiceManager().getRPMService(appName)).thenReturn(analyticsData);
    insights.harvestHarvestables();
    assertEquals(0, analyticsData.getEvents().size());
    assertEquals(7, txInsights.events.size());
}
Also used : Transaction(com.newrelic.agent.Transaction) TransactionInsights(com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Example 3 with TransactionInsights

use of com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights in project newrelic-java-agent by newrelic.

the class InsightsServiceImplTest method testWithTransaction_JAVA2614Regression.

@Test
public void testWithTransaction_JAVA2614Regression() throws Exception {
    String appName = "Dude";
    Map<String, Object> config = new HashMap<>();
    config.put(AgentConfigImpl.APP_NAME, appName);
    config.put("custom_insights_events.max_samples_stored", 2);
    config.put("token_timeout", 180);
    HarvestService harvestService = Mockito.mock(HarvestService.class);
    ServiceFactory.setServiceManager(Mockito.mock(ServiceManager.class));
    Mockito.when(ServiceFactory.getServiceManager().getHarvestService()).thenReturn(harvestService);
    TransactionService txService = Mockito.mock(TransactionService.class);
    Mockito.when(ServiceFactory.getServiceManager().getTransactionService()).thenReturn(txService);
    Mockito.when(ServiceFactory.getServiceManager().getRPMServiceManager()).thenReturn(Mockito.mock(RPMServiceManager.class));
    Mockito.when(ServiceFactory.getServiceManager().getRPMServiceManager().getRPMService()).thenReturn(Mockito.mock(RPMService.class));
    Mockito.when(ServiceFactory.getServiceManager().getConfigService()).thenReturn(Mockito.mock(ConfigService.class));
    Mockito.when(ServiceFactory.getServiceManager().getConfigService().getDefaultAgentConfig()).thenReturn(AgentConfigImpl.createAgentConfig(config));
    Mockito.when(ServiceFactory.getServiceManager().getRPMServiceManager().getRPMService().getApplicationName()).thenReturn(appName);
    Transaction transaction = Mockito.mock(Transaction.class);
    Mockito.when(txService.getTransaction(false)).thenReturn(transaction);
    TransactionInsights txInsights = new TransactionInsights(AgentConfigImpl.createAgentConfig(Collections.<String, Object>emptyMap()));
    Mockito.when(transaction.getInsightsData()).thenReturn(txInsights);
    Mockito.when(transaction.getApplicationName()).thenReturn(appName);
    Mockito.when(transaction.isInProgress()).thenReturn(true);
    InsightsServiceImpl insights = new InsightsServiceImpl();
    insights.start();
    insights.recordCustomEvent("everything", ImmutableMap.<String, Object>of("is", 5, "awesome", true, "key1", Whatever.ONE_THING, "key2", Whatever.ANOTHER_THING));
    // JAVA-2614 - null values as recordCustomEvent arguments for eventType or the attribute Map's key/value result
    // in an NPE being thrown. The following five API calls will cause an NPE if we have reintroduced the problem.
    Map<String, String> m;
    m = Collections.singletonMap("key", "value");
    insights.recordCustomEvent(null, m);
    m = Collections.singletonMap(null, null);
    insights.recordCustomEvent("bothNull", m);
    m = Collections.singletonMap("key", null);
    insights.recordCustomEvent("valueNull", m);
    m = Collections.singletonMap(null, "value");
    insights.recordCustomEvent("keyNull", m);
    m = new HashMap<>();
    m.put("goodKey1", "goodValue");
    m.put("goodKey2", null);
    m.put(null, null);
    insights.recordCustomEvent("AllKeyValueCombos", m);
    MockRPMService analyticsData = new MockRPMService();
    Mockito.when(ServiceFactory.getServiceManager().getRPMServiceManager().getRPMService(appName)).thenReturn(analyticsData);
    assertEquals(0, analyticsData.getEvents().size());
    assertEquals(5, txInsights.events.size());
    CustomInsightsEvent customEvent = txInsights.events.poll();
    assertNotNull(customEvent);
    assertEquals(Whatever.ONE_THING.toString(), customEvent.getUserAttributesCopy().get("key1"));
    Writer writer = new StringWriter();
    customEvent.writeJSONString(writer);
    String json = writer.toString();
    assertNotNull(json);
    // JAVA-2614 - through 3.33, attribute values that were not JSON types would cause the Agent
    // to generate invalid JSON. We check for this by attempting to parse the JSON. If an exception
    // occurs in the line below, we have reintroduced the problem and are generating invalid JSON.
    // The assert may be useful, but is really just to ensure the parse actually occurs. N.B.: we
    // need to make the outer JSON look like a JSON object for the GSON parser to accept it.
    json = "{ 'x':" + json + "}";
    Reader reader = new InputStreamReader(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
    assertNotNull(new Gson().fromJson(reader, Object.class));
}
Also used : HarvestService(com.newrelic.agent.HarvestService) TransactionService(com.newrelic.agent.TransactionService) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) TransactionInsights(com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights) RPMServiceManager(com.newrelic.agent.RPMServiceManager) CustomInsightsEvent(com.newrelic.agent.model.CustomInsightsEvent) ConfigService(com.newrelic.agent.config.ConfigService) Transaction(com.newrelic.agent.Transaction) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) MockServiceManager(com.newrelic.agent.MockServiceManager) RPMServiceManager(com.newrelic.agent.RPMServiceManager) ServiceManager(com.newrelic.agent.service.ServiceManager) MockRPMService(com.newrelic.agent.MockRPMService) RPMService(com.newrelic.agent.RPMService) MockRPMService(com.newrelic.agent.MockRPMService) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Test(org.junit.Test)

Example 4 with TransactionInsights

use of com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights in project newrelic-java-agent by newrelic.

the class InsightsServiceImplTest method testHighSecurity.

@Test
public void testHighSecurity() throws Exception {
    Map<String, Object> config = createConfig(true, 2);
    config.put(AgentConfigImpl.HIGH_SECURITY, true);
    config.put(AgentConfigImpl.ASYNC_TIMEOUT, 180);
    InsightsServiceImpl insights = createService(config);
    Transaction transaction = Mockito.mock(Transaction.class);
    Mockito.when(ServiceFactory.getTransactionService().getTransaction(false)).thenReturn(transaction);
    TransactionInsights txInsights = new TransactionInsights(AgentConfigImpl.createAgentConfig(Collections.<String, Object>emptyMap()));
    Mockito.when(transaction.getInsightsData()).thenReturn(txInsights);
    Mockito.when(transaction.getApplicationName()).thenReturn(appName);
    Mockito.when(transaction.isInProgress()).thenReturn(true);
    insights.recordCustomEvent("everything", ImmutableMap.<String, Object>of("is", 5, "awesome", true));
    insights.recordCustomEvent("class", ImmutableMap.<String, Object>of("name", "Foo", "number", 666));
    insights.recordCustomEvent("method", ImmutableMap.of("className", "Foo", "methodName", "getSomething"));
    MockRPMService analyticsData = new MockRPMService();
    Mockito.when(ServiceFactory.getServiceManager().getRPMServiceManager().getRPMService(appName)).thenReturn(analyticsData);
    TransactionData transactionData = Mockito.mock(TransactionData.class);
    Mockito.when(transactionData.getApplicationName()).thenReturn(appName);
    Mockito.when(transactionData.getInsightsData()).thenReturn(txInsights);
    insights.transactionListener.dispatcherTransactionFinished(transactionData, null);
    insights.harvestHarvestables();
    assertEquals(0, analyticsData.getEvents().size());
    assertEquals(0, txInsights.events.size());
}
Also used : Transaction(com.newrelic.agent.Transaction) TransactionInsights(com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights) TransactionData(com.newrelic.agent.TransactionData) MockRPMService(com.newrelic.agent.MockRPMService) Test(org.junit.Test)

Aggregations

MockRPMService (com.newrelic.agent.MockRPMService)4 TransactionInsights (com.newrelic.agent.service.analytics.InsightsServiceImpl.TransactionInsights)4 Test (org.junit.Test)4 Transaction (com.newrelic.agent.Transaction)3 TransactionData (com.newrelic.agent.TransactionData)2 Gson (com.google.gson.Gson)1 HarvestService (com.newrelic.agent.HarvestService)1 MockServiceManager (com.newrelic.agent.MockServiceManager)1 RPMService (com.newrelic.agent.RPMService)1 RPMServiceManager (com.newrelic.agent.RPMServiceManager)1 TransactionService (com.newrelic.agent.TransactionService)1 ConfigService (com.newrelic.agent.config.ConfigService)1 CustomInsightsEvent (com.newrelic.agent.model.CustomInsightsEvent)1 ServiceManager (com.newrelic.agent.service.ServiceManager)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1