Search in sources :

Example 36 with TransactionService

use of com.newrelic.agent.TransactionService in project newrelic-java-agent by newrelic.

the class HighSecurityAttributesTest method testHighSecurityOffWithOtherDefaultsThroughNoticeErrorAPIMessage.

@Test
public void testHighSecurityOffWithOtherDefaultsThroughNoticeErrorAPIMessage() {
    try {
        Map<String, Object> settings = new HashMap<>();
        settings.put("app_name", APP_NAME);
        settings.put("high_security", Boolean.FALSE);
        enableBrowser(settings);
        manager.setConfigService(ConfigServiceFactory.createConfigServiceUsingSettings(settings));
        manager.setTransactionService(new TransactionService());
        manager.setTransactionTraceService(new TransactionTraceService());
        AttributesService service = new AttributesService();
        manager.setAttributesService(service);
        RPMServiceManager mockRPMServiceManager = manager.getRPMServiceManager();
        RPMService mockRPMService = mock(RPMService.class);
        ErrorService errorService = new ErrorServiceImpl(APP_NAME);
        when(mockRPMServiceManager.getRPMService()).thenReturn(mockRPMService);
        when(mockRPMService.getErrorService()).thenReturn(errorService);
        Transaction t = Transaction.getTransaction();
        BasicRequestRootTracer tracer = createDispatcherTracer();
        t.getTransactionActivity().tracerStarted(tracer);
        NewRelicApiImplementation impl = new NewRelicApiImplementation();
        Map<String, String> atts = new HashMap<>();
        atts.put("abc.thread", "1");
        atts.put("request.many", "1");
        atts.put("message.many", "1");
        atts.put("request.parameters.foo", "1");
        atts.put("request.parameters.bar", "1");
        atts.put("message.parameters.foo", "1");
        atts.put("message.parameters.bar", "1");
        impl.noticeError("hello", atts);
        // user attributes should be off
        Set<String> expected = Sets.newHashSet("abc.thread", "request.many", "message.many");
        verifyOutput(service.filterErrorEventAttributes(APP_NAME, t.getErrorAttributes()), expected);
        verifyOutput(service.filterTransactionEventAttributes(APP_NAME, t.getErrorAttributes()), expected);
        verifyOutput(service.filterTransactionTraceAttributes(APP_NAME, t.getErrorAttributes()), expected);
        verifyOutput(service.filterBrowserAttributes(APP_NAME, t.getErrorAttributes()), expected);
        verifyOutput(service.filterSpanEventAttributes(APP_NAME, t.getErrorAttributes()), expected);
        verifyOutput(service.filterTransactionSegmentAttributes(APP_NAME, t.getErrorAttributes()), expected);
    } finally {
        Transaction.clearTransaction();
    }
}
Also used : TransactionService(com.newrelic.agent.TransactionService) ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) HashMap(java.util.HashMap) RPMServiceManager(com.newrelic.agent.RPMServiceManager) NewRelicApiImplementation(com.newrelic.api.agent.NewRelicApiImplementation) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) ErrorService(com.newrelic.agent.errors.ErrorService) Transaction(com.newrelic.agent.Transaction) RPMService(com.newrelic.agent.RPMService) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Test(org.junit.Test)

Example 37 with TransactionService

use of com.newrelic.agent.TransactionService in project newrelic-java-agent by newrelic.

the class HighSecurityAttributesTest method testHighSecurityOnWithOtherDefaultsThroughAPI.

@Test
public void testHighSecurityOnWithOtherDefaultsThroughAPI() {
    try {
        Map<String, Object> settings = new HashMap<>();
        settings.put("app_name", APP_NAME);
        settings.put("high_security", Boolean.TRUE);
        enableBrowser(settings);
        manager.setConfigService(ConfigServiceFactory.createConfigServiceUsingSettings(settings));
        manager.setTransactionService(new TransactionService());
        manager.setTransactionTraceService(new TransactionTraceService());
        Transaction t = Transaction.getTransaction();
        NewRelicApiImplementation impl = new NewRelicApiImplementation();
        impl.addCustomParameter("abc.thread", "1");
        impl.addCustomParameter("request.many", "1");
        impl.addCustomParameter("message.many", "1");
        impl.addCustomParameter("request.parameters.foo", "1");
        impl.addCustomParameter("request.parameters.bar", "1");
        impl.addCustomParameter("message.parameters.foo", "1");
        impl.addCustomParameter("message.parameters.bar", "1");
        // user attributes should be off
        Set<String> expected = new HashSet<>();
        AttributesService service = new AttributesService();
        verifyOutput(service.filterErrorEventAttributes(APP_NAME, t.getUserAttributes()), expected);
        verifyOutput(service.filterTransactionEventAttributes(APP_NAME, t.getUserAttributes()), expected);
        verifyOutput(service.filterTransactionTraceAttributes(APP_NAME, t.getUserAttributes()), expected);
        verifyOutput(service.filterBrowserAttributes(APP_NAME, t.getUserAttributes()), expected);
        verifyOutput(service.filterSpanEventAttributes(APP_NAME, t.getUserAttributes()), expected);
        verifyOutput(service.filterTransactionSegmentAttributes(APP_NAME, t.getUserAttributes()), expected);
    } finally {
        Transaction.clearTransaction();
    }
}
Also used : TransactionService(com.newrelic.agent.TransactionService) Transaction(com.newrelic.agent.Transaction) HashMap(java.util.HashMap) NewRelicApiImplementation(com.newrelic.api.agent.NewRelicApiImplementation) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 38 with TransactionService

use of com.newrelic.agent.TransactionService in project newrelic-java-agent by newrelic.

the class NoticeErrorAttributesTest method testStringifiesAndTruncates.

@Test
public void testStringifiesAndTruncates() {
    try {
        Map<String, Object> settings = new HashMap<>();
        settings.put("app_name", APP_NAME);
        manager.setConfigService(ConfigServiceFactory.createConfigServiceUsingSettings(settings));
        manager.setTransactionService(new TransactionService());
        manager.setTransactionTraceService(new TransactionTraceService());
        AttributesService service = new AttributesService();
        manager.setAttributesService(service);
        RPMServiceManager mockRPMServiceManager = manager.getRPMServiceManager();
        RPMService mockRPMService = mock(RPMService.class);
        ErrorService errorService = new ErrorServiceImpl(APP_NAME);
        when(mockRPMServiceManager.getRPMService()).thenReturn(mockRPMService);
        when(mockRPMService.getErrorService()).thenReturn(errorService);
        Transaction t = Transaction.getTransaction();
        BasicRequestRootTracer tracer = createDispatcherTracer();
        t.getTransactionActivity().tracerStarted(tracer);
        NewRelicApiImplementation impl = new NewRelicApiImplementation();
        Map<String, Object> atts = new HashMap<>();
        atts.put("test.foo", new Object() {

            @Override
            public String toString() {
                String base = "a";
                for (int i = 0; i < 10; i++) {
                    base += base;
                }
                return base;
            }
        });
        impl.noticeError("hello", atts);
        Assert.assertEquals(t.getErrorAttributes().keySet(), Sets.newHashSet("test.foo"));
        Assert.assertEquals(255, ((String) t.getErrorAttributes().get("test.foo")).length());
        Assert.assertNotEquals(255, atts.get("test.foo").toString().length());
    } finally {
        Transaction.clearTransaction();
    }
}
Also used : TransactionService(com.newrelic.agent.TransactionService) ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) HashMap(java.util.HashMap) RPMServiceManager(com.newrelic.agent.RPMServiceManager) NewRelicApiImplementation(com.newrelic.api.agent.NewRelicApiImplementation) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) ErrorService(com.newrelic.agent.errors.ErrorService) Transaction(com.newrelic.agent.Transaction) RPMService(com.newrelic.agent.RPMService) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) Test(org.junit.Test)

Example 39 with TransactionService

use of com.newrelic.agent.TransactionService in project newrelic-java-agent by newrelic.

the class BrowserTransactionStateTest method createServiceManager.

public static void createServiceManager(Set<String> include, Set<String> exclude) throws Exception {
    MockServiceManager serviceManager = new MockServiceManager();
    ServiceFactory.setServiceManager(serviceManager);
    // Needed by TransactionService
    ThreadService threadService = new ThreadService();
    serviceManager.setThreadService(threadService);
    // Needed by TransactionTraceService
    Map<String, Object> map = createConfigMap(include, exclude);
    ConfigService configService = ConfigServiceFactory.createConfigService(AgentConfigImpl.createAgentConfig(map), map);
    serviceManager.setConfigService(configService);
    // Needed by Transaction
    TransactionService transactionService = new TransactionService();
    serviceManager.setTransactionService(transactionService);
    MockCoreService agent = new MockCoreService();
    serviceManager.setCoreService(agent);
    // Null pointers if not set
    serviceManager.setStatsService(Mockito.mock(StatsService.class));
    // Needed by Transaction
    TransactionTraceService transactionTraceService = new TransactionTraceService();
    serviceManager.setTransactionTraceService(transactionTraceService);
    // Needed by Transaction
    MockRPMServiceManager rpmServiceManager = new MockRPMServiceManager();
    serviceManager.setRPMServiceManager(rpmServiceManager);
    MockRPMService rpmService = new MockRPMService();
    rpmService.setApplicationName("name");
    rpmService.setErrorService(new ErrorServiceImpl("name"));
    rpmServiceManager.setRPMService(rpmService);
    AttributesService attService = new AttributesService();
    serviceManager.setAttributesService(attService);
    ServiceFactory.setServiceManager(serviceManager);
}
Also used : TransactionService(com.newrelic.agent.TransactionService) ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) StatsService(com.newrelic.agent.stats.StatsService) AttributesService(com.newrelic.agent.attributes.AttributesService) MockRPMServiceManager(com.newrelic.agent.MockRPMServiceManager) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) ThreadService(com.newrelic.agent.ThreadService) MockConfigService(com.newrelic.agent.MockConfigService) ConfigService(com.newrelic.agent.config.ConfigService) MockServiceManager(com.newrelic.agent.MockServiceManager) MockCoreService(com.newrelic.agent.MockCoreService) MockRPMService(com.newrelic.agent.MockRPMService)

Example 40 with TransactionService

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

Aggregations

TransactionService (com.newrelic.agent.TransactionService)66 MockServiceManager (com.newrelic.agent.MockServiceManager)42 TransactionTraceService (com.newrelic.agent.trace.TransactionTraceService)39 ConfigService (com.newrelic.agent.config.ConfigService)38 MockRPMServiceManager (com.newrelic.agent.MockRPMServiceManager)30 ThreadService (com.newrelic.agent.ThreadService)30 HashMap (java.util.HashMap)29 HarvestService (com.newrelic.agent.HarvestService)26 MockRPMService (com.newrelic.agent.MockRPMService)26 Test (org.junit.Test)24 AttributesService (com.newrelic.agent.attributes.AttributesService)23 MockHarvestService (com.newrelic.agent.MockHarvestService)22 MockCoreService (com.newrelic.agent.MockCoreService)19 ErrorServiceImpl (com.newrelic.agent.errors.ErrorServiceImpl)19 AgentConfig (com.newrelic.agent.config.AgentConfig)17 StatsServiceImpl (com.newrelic.agent.stats.StatsServiceImpl)17 SqlTraceService (com.newrelic.agent.sql.SqlTraceService)16 SqlTraceServiceImpl (com.newrelic.agent.sql.SqlTraceServiceImpl)16 StatsService (com.newrelic.agent.stats.StatsService)15 Transaction (com.newrelic.agent.Transaction)13