Search in sources :

Example 1 with ServiceManagerImpl

use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.

the class MockCoreService method getMockAgentAndBootstrapTheServiceManager.

public static CoreService getMockAgentAndBootstrapTheServiceManager() throws Exception {
    AgentHelper.initializeConfig();
    MockCoreService mockCoreService = new MockCoreService();
    mockCoreService.setInstrumentation(Mockito.mock(InstrumentationProxy.class));
    Mockito.when(mockCoreService.getInstrumentation().getAllLoadedClasses()).thenReturn(new Class[] {});
    ConfigService configService = ConfigServiceFactory.createConfigService(mock(Logger.class), false);
    ServiceManager serviceManager = new ServiceManagerImpl(mockCoreService, configService);
    ServiceFactory.setServiceManager(serviceManager);
    serviceManager.start();
    return mockCoreService;
}
Also used : ConfigService(com.newrelic.agent.config.ConfigService) ServiceManager(com.newrelic.agent.service.ServiceManager) ServiceManagerImpl(com.newrelic.agent.service.ServiceManagerImpl) Logger(com.newrelic.api.agent.Logger)

Example 2 with ServiceManagerImpl

use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.

the class AgentLinkingMetadataTest method getLinkingMetadata.

@Test
public void getLinkingMetadata() {
    // Given
    final String expectedTraceId = "traceId1234";
    final String expectedSpanId = "spanId5678";
    final String expectedEntityGuid = "entityGuid91011";
    final String expectedEntityName = "entityName91011";
    final String expectedEntityType = AgentLinkingMetadata.ENTITY_TYPE_DEFAULT;
    TraceMetadataImpl traceMetadataMock = mock(TraceMetadataImpl.class);
    ServiceManagerImpl serviceManagerMock = mock(ServiceManagerImpl.class);
    RPMServiceManagerImpl rpmServiceManagerMock = mock(RPMServiceManagerImpl.class);
    RPMService rpmServiceMock = mock(RPMService.class);
    ConfigServiceImpl configServiceMock = mock(ConfigServiceImpl.class);
    AgentConfigImpl agentConfigMock = mock(AgentConfigImpl.class);
    ServiceFactory.setServiceManager(serviceManagerMock);
    // When
    when(traceMetadataMock.getTraceId()).thenReturn(expectedTraceId);
    when(traceMetadataMock.getSpanId()).thenReturn(expectedSpanId);
    when(serviceManagerMock.getRPMServiceManager()).thenReturn(rpmServiceManagerMock);
    when(serviceManagerMock.getConfigService()).thenReturn(configServiceMock);
    when(rpmServiceManagerMock.getRPMService()).thenReturn(rpmServiceMock);
    when(configServiceMock.getDefaultAgentConfig()).thenReturn(agentConfigMock);
    when(agentConfigMock.getApplicationName()).thenReturn(expectedEntityName);
    when(rpmServiceMock.getEntityGuid()).thenReturn(expectedEntityGuid);
    // Then
    Map<String, String> linkingMetadata = AgentLinkingMetadata.getLinkingMetadata(traceMetadataMock, ServiceFactory.getConfigService(), ServiceFactory.getRPMService());
    assertFalse("linkingMetadata map shouldn't be empty", linkingMetadata.isEmpty());
    // Can't assert on a specific hostname value as it will resolve to the actual hostname of the machine running the test
    assertFalse("hostname shouldn't be empty", linkingMetadata.get(AgentLinkingMetadata.HOSTNAME).isEmpty());
    assertEquals(expectedEntityGuid, linkingMetadata.get(AgentLinkingMetadata.ENTITY_GUID));
    assertEquals(expectedEntityName, linkingMetadata.get(AgentLinkingMetadata.ENTITY_NAME));
    assertEquals(expectedEntityType, linkingMetadata.get(AgentLinkingMetadata.ENTITY_TYPE));
    assertEquals(expectedTraceId, linkingMetadata.get(AgentLinkingMetadata.TRACE_ID));
    assertEquals(expectedSpanId, linkingMetadata.get(AgentLinkingMetadata.SPAN_ID));
}
Also used : AgentConfigImpl(com.newrelic.agent.config.AgentConfigImpl) ServiceManagerImpl(com.newrelic.agent.service.ServiceManagerImpl) ConfigServiceImpl(com.newrelic.agent.config.ConfigServiceImpl) Test(org.junit.Test)

Example 3 with ServiceManagerImpl

use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.

the class Agent method tryToInitializeServiceManager.

private static boolean tryToInitializeServiceManager(Instrumentation inst) {
    try {
        CoreService coreService = new CoreServiceImpl(inst);
        ConfigService configService = ConfigServiceFactory.createConfigService(Agent.LOG, System.getProperty("newrelic.checkconfig") != null);
        ServiceManager serviceManager = new ServiceManagerImpl(coreService, configService);
        ServiceFactory.setServiceManager(serviceManager);
        if (isLicenseKeyEmpty(serviceManager.getConfigService().getDefaultAgentConfig().getLicenseKey())) {
            LOG.error("license_key is empty in the config. Not starting New Relic Agent.");
            return false;
        }
        if (!serviceManager.getConfigService().getDefaultAgentConfig().isAgentEnabled()) {
            LOG.warning("agent_enabled is false in the config. Not starting New Relic Agent.");
            return false;
        }
        // Now that we know the agent is enabled, add the ApiClassTransformer
        BootstrapLoader.forceCorrectNewRelicApi(inst);
        // init problem classes before class transformer service is active
        InitProblemClasses.loadInitialClasses();
    } catch (ForceDisconnectException e) {
        /* Note: Our use of ForceDisconnectException is a bit misleading here as we haven't even tried to connect
             * to RPM at this point (that happens a few lines down when we call serviceManager.start()). This exception
             * comes from ConfigServiceFactory when it attempts to validate the local yml and finds that both HSM and
             * LASP are enabled. The LASP spec says in this scenario that "Shutdown will follow the behavior of the
             * ForceDisconnectException response from "New Relic." Not specifically that we should throw ForceDisconnectException.
             * Perhaps we should throw a different, more accurately named exception, that simply has the same behavior
             * as ForceDisconnectException as it will be replaced by a 410 response code in Protocol 17.
             */
        LOG.log(Level.SEVERE, e.getMessage());
        return false;
    } catch (Throwable t) {
        // this is the last point where we can stop the agent gracefully if something has gone wrong.
        LOG.log(Level.SEVERE, t, "Unable to start the New Relic Agent. Your application will continue to run but it will not be monitored.");
        return false;
    }
    return true;
}
Also used : ServiceManager(com.newrelic.agent.service.ServiceManager) ServiceManagerImpl(com.newrelic.agent.service.ServiceManagerImpl) CoreService(com.newrelic.agent.core.CoreService) CoreServiceImpl(com.newrelic.agent.core.CoreServiceImpl)

Example 4 with ServiceManagerImpl

use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.

the class AgentLinkingMetadataTest method getLogEventLinkingMetadataWithEmptyTraceAttributes.

@Test
public void getLogEventLinkingMetadataWithEmptyTraceAttributes() {
    // Given
    final String expectedTraceId = "";
    final String expectedSpanId = "";
    final String expectedEntityGuid = "entityGuid91011";
    final String expectedEntityName = "entityName91011";
    TraceMetadataImpl traceMetadataMock = mock(TraceMetadataImpl.class);
    ServiceManagerImpl serviceManagerMock = mock(ServiceManagerImpl.class);
    RPMServiceManagerImpl rpmServiceManagerMock = mock(RPMServiceManagerImpl.class);
    RPMService rpmServiceMock = mock(RPMService.class);
    ConfigServiceImpl configServiceMock = mock(ConfigServiceImpl.class);
    AgentConfigImpl agentConfigMock = mock(AgentConfigImpl.class);
    ServiceFactory.setServiceManager(serviceManagerMock);
    // When
    when(traceMetadataMock.getTraceId()).thenReturn(expectedTraceId);
    when(traceMetadataMock.getSpanId()).thenReturn(expectedSpanId);
    when(serviceManagerMock.getRPMServiceManager()).thenReturn(rpmServiceManagerMock);
    when(serviceManagerMock.getConfigService()).thenReturn(configServiceMock);
    when(rpmServiceManagerMock.getRPMService()).thenReturn(rpmServiceMock);
    when(configServiceMock.getDefaultAgentConfig()).thenReturn(agentConfigMock);
    when(agentConfigMock.getApplicationName()).thenReturn(expectedEntityName);
    when(rpmServiceMock.getEntityGuid()).thenReturn(expectedEntityGuid);
    // Then
    Map<String, String> linkingMetadata = AgentLinkingMetadata.getLogEventLinkingMetadata(traceMetadataMock, ServiceFactory.getConfigService(), ServiceFactory.getRPMService());
    assertFalse("linkingMetadata map shouldn't be empty", linkingMetadata.isEmpty());
    // Can't assert on a specific hostname value as it will resolve to the actual hostname of the machine running the test
    assertFalse("hostname shouldn't be empty", linkingMetadata.get(AgentLinkingMetadata.HOSTNAME).isEmpty());
    assertFalse("entity.type should not be included in LogEvent linking metadata", linkingMetadata.containsKey(AgentLinkingMetadata.ENTITY_TYPE));
    assertEquals(expectedEntityName, linkingMetadata.get(AgentLinkingMetadata.ENTITY_NAME));
    assertEquals(expectedEntityGuid, linkingMetadata.get(AgentLinkingMetadata.ENTITY_GUID));
    // trace.id and span.id would be empty values if getLogEventLinkingMetadata was called outside of a transaction, in which case they are omitted
    assertFalse("empty trace.id value should not be included in LogEvent linking metadata", linkingMetadata.containsKey(AgentLinkingMetadata.TRACE_ID));
    assertFalse("empty span.id value should not be included in LogEvent linking metadata", linkingMetadata.containsKey(AgentLinkingMetadata.SPAN_ID));
}
Also used : AgentConfigImpl(com.newrelic.agent.config.AgentConfigImpl) ServiceManagerImpl(com.newrelic.agent.service.ServiceManagerImpl) ConfigServiceImpl(com.newrelic.agent.config.ConfigServiceImpl) Test(org.junit.Test)

Example 5 with ServiceManagerImpl

use of com.newrelic.agent.service.ServiceManagerImpl in project newrelic-java-agent by newrelic.

the class AgentLinkingMetadataTest method getLinkingMetadataWithEmptyTraceAttributes.

@Test
public void getLinkingMetadataWithEmptyTraceAttributes() {
    // Given
    final String expectedTraceId = "";
    final String expectedSpanId = "";
    final String expectedEntityGuid = "entityGuid91011";
    final String expectedEntityName = "entityName91011";
    final String expectedEntityType = AgentLinkingMetadata.ENTITY_TYPE_DEFAULT;
    TraceMetadataImpl traceMetadataMock = mock(TraceMetadataImpl.class);
    ServiceManagerImpl serviceManagerMock = mock(ServiceManagerImpl.class);
    RPMServiceManagerImpl rpmServiceManagerMock = mock(RPMServiceManagerImpl.class);
    RPMService rpmServiceMock = mock(RPMService.class);
    ConfigServiceImpl configServiceMock = mock(ConfigServiceImpl.class);
    AgentConfigImpl agentConfigMock = mock(AgentConfigImpl.class);
    ServiceFactory.setServiceManager(serviceManagerMock);
    // When
    when(traceMetadataMock.getTraceId()).thenReturn(expectedTraceId);
    when(traceMetadataMock.getSpanId()).thenReturn(expectedSpanId);
    when(serviceManagerMock.getRPMServiceManager()).thenReturn(rpmServiceManagerMock);
    when(serviceManagerMock.getConfigService()).thenReturn(configServiceMock);
    when(rpmServiceManagerMock.getRPMService()).thenReturn(rpmServiceMock);
    when(configServiceMock.getDefaultAgentConfig()).thenReturn(agentConfigMock);
    when(agentConfigMock.getApplicationName()).thenReturn(expectedEntityName);
    when(rpmServiceMock.getEntityGuid()).thenReturn(expectedEntityGuid);
    // Then
    Map<String, String> linkingMetadata = AgentLinkingMetadata.getLinkingMetadata(traceMetadataMock, ServiceFactory.getConfigService(), ServiceFactory.getRPMService());
    assertFalse("linkingMetadata map shouldn't be empty", linkingMetadata.isEmpty());
    // Can't assert on a specific hostname value as it will resolve to the actual hostname of the machine running the test
    assertFalse("hostname shouldn't be empty", linkingMetadata.get(AgentLinkingMetadata.HOSTNAME).isEmpty());
    assertEquals(expectedEntityGuid, linkingMetadata.get(AgentLinkingMetadata.ENTITY_GUID));
    assertEquals(expectedEntityName, linkingMetadata.get(AgentLinkingMetadata.ENTITY_NAME));
    assertEquals(expectedEntityType, linkingMetadata.get(AgentLinkingMetadata.ENTITY_TYPE));
    // trace.id and span.id would be empty values if getLinkingMetadata was called outside of a transaction.
    // With the getLinkingMetadata API the returned map includes keys with empty values
    assertEquals(expectedTraceId, linkingMetadata.get(AgentLinkingMetadata.TRACE_ID));
    assertEquals(expectedSpanId, linkingMetadata.get(AgentLinkingMetadata.SPAN_ID));
}
Also used : AgentConfigImpl(com.newrelic.agent.config.AgentConfigImpl) ServiceManagerImpl(com.newrelic.agent.service.ServiceManagerImpl) ConfigServiceImpl(com.newrelic.agent.config.ConfigServiceImpl) Test(org.junit.Test)

Aggregations

ServiceManagerImpl (com.newrelic.agent.service.ServiceManagerImpl)6 AgentConfigImpl (com.newrelic.agent.config.AgentConfigImpl)4 ConfigServiceImpl (com.newrelic.agent.config.ConfigServiceImpl)4 Test (org.junit.Test)4 ServiceManager (com.newrelic.agent.service.ServiceManager)2 ConfigService (com.newrelic.agent.config.ConfigService)1 CoreService (com.newrelic.agent.core.CoreService)1 CoreServiceImpl (com.newrelic.agent.core.CoreServiceImpl)1 Logger (com.newrelic.api.agent.Logger)1