Search in sources :

Example 16 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class DefaultTracerTest method testSpanEventHttp.

@Test
public void testSpanEventHttp() {
    DefaultTracer tracer = prepareTracer();
    tracer.reportAsExternal(HttpParameters.library("library").uri(URI.create("http://www.newrelic.com")).procedure("call").noInboundHeaders().build());
    tracer.finish(0, null);
    SpanEventsService spanEventService = ServiceFactory.getSpanEventService();
    ((SpanEventsServiceImpl) spanEventService).dispatcherTransactionFinished(new TransactionData(tracer.getTransaction(), 1024), new TransactionStats());
    SamplingPriorityQueue<SpanEvent> eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<SpanEvent> spanEvents = eventPool.asList();
    assertNotNull(spanEvents);
    assertEquals(1, spanEvents.size());
    SpanEvent spanEvent = Iterables.getFirst(spanEvents, null);
    assertNotNull(spanEvent);
    assertNull(spanEvent.getParentId());
    assertEquals("library", spanEvent.getIntrinsics().get("component"));
    assertEquals("client", spanEvent.getIntrinsics().get("span.kind"));
    assertEquals("http://www.newrelic.com", spanEvent.getAgentAttributes().get("http.url"));
    assertEquals("call", spanEvent.getAgentAttributes().get("http.method"));
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) SpanEventsServiceImpl(com.newrelic.agent.service.analytics.SpanEventsServiceImpl) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) SpanEvent(com.newrelic.agent.model.SpanEvent) TransactionData(com.newrelic.agent.TransactionData) Test(org.junit.Test)

Example 17 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class W3CTraceContextCrossAgentTest method testTraceContext.

@Test
public void testTraceContext() throws Exception {
    String testName = (String) testData.get("test_name");
    String accountKey = (String) testData.get("trusted_account_key");
    String accountId = (String) testData.get("account_id");
    String transportType = (String) testData.get("transport_type");
    Boolean webTransaction = (Boolean) testData.get("web_transaction");
    Boolean raisesException = (Boolean) testData.get("raises_exception");
    Boolean forceSampledTrue = (Boolean) testData.get("force_sampled_true");
    Boolean spanEventsEnabled = (Boolean) testData.get("span_events_enabled");
    replaceConfig(spanEventsEnabled);
    System.out.println("Running test: " + testName);
    JSONArray outbound_payloads = (JSONArray) testData.get("outbound_payloads");
    JSONArray inbound_payloads = (JSONArray) testData.get("inbound_headers");
    List expectedMetrics = (List) testData.get("expected_metrics");
    Map<String, Object> intrinsics = (Map<String, Object>) testData.get("intrinsics");
    Map<String, Object> commonAssertions = intrinsics == null ? Collections.<String, Object>emptyMap() : (Map<String, Object>) intrinsics.get("common");
    List targetEvents = intrinsics == null ? Collections.emptyList() : (ArrayList) intrinsics.get("target_events");
    Map<String, Object> transactionAssertions = intrinsics == null ? Collections.<String, Object>emptyMap() : (Map<String, Object>) intrinsics.get("Transaction");
    Map<String, Object> spanAssertions = intrinsics == null ? Collections.<String, Object>emptyMap() : (Map<String, Object>) intrinsics.get("Span");
    Map<String, Object> connectInfo = Maps.newHashMap();
    connectInfo.put(DistributedTracingConfig.ACCOUNT_ID, accountId);
    connectInfo.put(DistributedTracingConfig.TRUSTED_ACCOUNT_KEY, accountKey);
    connectInfo.put(DistributedTracingConfig.PRIMARY_APPLICATION_ID, "2827902");
    AgentConfig agentConfig = AgentHelper.createAgentConfig(true, Collections.<String, Object>emptyMap(), connectInfo);
    distributedTraceService.connected(null, agentConfig);
    Transaction.clearTransaction();
    TransactionActivity.clear();
    spanEventService.clearReservoir();
    Transaction tx = Transaction.getTransaction();
    TransactionData transactionData = new TransactionData(tx, 0);
    TransactionStats transactionStats = transactionData.getTransaction().getTransactionActivity().getTransactionStats();
    eventPool = spanEventService.getOrCreateDistributedSamplingReservoir(APP_NAME);
    List<String> parents = Lists.newArrayList();
    List<String> states = Lists.newArrayList();
    List<String> newrelic = Lists.newArrayList();
    if (inbound_payloads != null) {
        for (Object payload : inbound_payloads) {
            JSONObject j = (JSONObject) payload;
            if (j.get("traceparent") != null) {
                parents.addAll(Lists.newArrayList(String.valueOf(j.get("traceparent"))));
            }
            if (j.get("tracestate") != null) {
                states.addAll(Lists.newArrayList(String.valueOf(j.get("tracestate"))));
            }
            if (j.get("newrelic") != null) {
                newrelic.addAll(Lists.newArrayList(String.valueOf(j.get("newrelic"))));
            }
        }
    }
    MockHttpRequest httpRequest = new MockHttpRequest();
    for (String parent : parents) {
        httpRequest.setHeader("traceparent", parent);
    }
    for (String state : states) {
        httpRequest.setHeader("tracestate", state);
    }
    for (String header : newrelic) {
        httpRequest.setHeader("newrelic", header);
    }
    Tracer rootTracer;
    if (webTransaction) {
        rootTracer = TransactionAsyncUtility.createAndStartDispatcherTracer(this, "WebTransaction", httpRequest);
    } else {
        rootTracer = TransactionAsyncUtility.createOtherTracer("OtherTransaction");
        tx.getTransactionActivity().tracerStarted(rootTracer);
        tx.provideRawHeaders(httpRequest);
    }
    if (raisesException) {
        tx.setThrowable(new Throwable(), TransactionErrorPriority.API, false);
    }
    setTransportType(tx, transportType);
    if (forceSampledTrue && tx.getPriority() < 1) {
        tx.setPriorityIfNotNull(new Random().nextFloat() + 1.0f);
    }
    if (outbound_payloads != null) {
        for (Object assertion : outbound_payloads) {
            MockHttpResponse mockHttpResponse = new MockHttpResponse();
            tx.getCrossProcessState().processOutboundRequestHeaders(mockHttpResponse);
            JSONObject payloadAssertions = (JSONObject) assertion;
            String traceparent = mockHttpResponse.getHeader("traceparent");
            String tracestate = mockHttpResponse.getHeader("tracestate");
            if (traceparent != null) {
                W3CTraceParent traceParent = W3CTraceParentParser.parseHeaders(Collections.singletonList(traceparent));
                assertOutboundTraceParentPayload(payloadAssertions, traceParent);
            } else {
                assertOutboundTraceParentPayload(payloadAssertions, null);
            }
            if (tracestate != null) {
                assertOutboundTraceStatePayload(payloadAssertions, W3CTraceStateSupport.parseHeaders(Collections.singletonList(mockHttpResponse.getHeader("tracestate"))));
            } else {
                assertOutboundTraceStatePayload(payloadAssertions, null);
            }
        }
    }
    rootTracer.finish(Opcodes.RETURN, 0);
    List<SpanEvent> spans = eventPool.asList();
    TransactionEvent transactionEvent = serviceManager.getTransactionEventsService().createEvent(transactionData, transactionStats, "wat");
    JSONObject txnEvents = serializeAndParseEvents(transactionEvent);
    StatsEngine statsEngine = statsService.getStatsEngineForHarvest(APP_NAME);
    assertExpectedMetrics(expectedMetrics, statsEngine);
    for (Object event : targetEvents) {
        if (event.toString().startsWith("Transaction") && transactionAssertions != null) {
            assertTransactionEvents(transactionAssertions, txnEvents);
            assertTransactionEvents(commonAssertions, txnEvents);
        } else if (event.toString().startsWith("Span") && spanAssertions != null) {
            assertSpanEvents(spanAssertions, spans);
            assertSpanEvents(commonAssertions, spans);
        }
    }
}
Also used : MockHttpRequest(com.newrelic.agent.tracers.servlet.MockHttpRequest) Tracer(com.newrelic.agent.tracers.Tracer) JSONArray(org.json.simple.JSONArray) TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) JSONObject(org.json.simple.JSONObject) SpanEvent(com.newrelic.agent.model.SpanEvent) JSONObject(org.json.simple.JSONObject) MockHttpResponse(com.newrelic.agent.tracers.servlet.MockHttpResponse) Test(org.junit.Test)

Example 18 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class XmlRpcPointCutTest method externalTest.

@Test
public void externalTest() throws Exception {
    EnvironmentHolderSettingsGenerator envHolderSettings = new EnvironmentHolderSettingsGenerator(CONFIG_FILE, "all_enabled_test", CLASS_LOADER);
    EnvironmentHolder holder = new EnvironmentHolder(envHolderSettings);
    holder.setupEnvironment();
    try {
        doCall();
        SpanEventsService spanEventsService = ServiceFactory.getServiceManager().getSpanEventsService();
        String appName = ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName();
        SamplingPriorityQueue<SpanEvent> spanEventsPool = spanEventsService.getOrCreateDistributedSamplingReservoir(appName);
        assertNotNull(spanEventsPool);
        List<SpanEvent> spanEvents = spanEventsPool.asList();
        assertNotNull(spanEvents);
        assertEquals(2, spanEvents.size());
        boolean java = false;
        boolean external = false;
        for (SpanEvent span : spanEvents) {
            String name = (String) span.getIntrinsics().get("name");
            if (name.equals("External/newrelic.com/XmlRpc/invoke")) {
                external = true;
                assertEquals("XmlRpc", span.getIntrinsics().get("component"));
                assertEquals("http", span.getIntrinsics().get("category"));
                assertEquals("http://newrelic.com", span.getAgentAttributes().get("http.url"));
            } else if (name.equals("Java/com.newrelic.agent.instrumentation.pointcuts.javax.xml.rpc.XmlRpcPointCutTest/doCall")) {
                java = true;
                assertEquals("generic", span.getIntrinsics().get("category"));
            }
        }
        assertTrue("Unexpected span events found", java && external);
    } finally {
        holder.close();
    }
}
Also used : EnvironmentHolderSettingsGenerator(test.newrelic.EnvironmentHolderSettingsGenerator) EnvironmentHolder(test.newrelic.test.agent.EnvironmentHolder) SpanEventsService(com.newrelic.agent.service.analytics.SpanEventsService) SpanEvent(com.newrelic.agent.model.SpanEvent) Test(org.junit.Test)

Example 19 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class IntrospectorServiceManager method setup.

private void setup(Map<String, Object> config) {
    configService = ConfigServiceFactory.createConfigService(AgentConfigImpl.createAgentConfig(config), Collections.<String, Object>emptyMap());
    ServiceFactory.setServiceManager(this);
    coreService = new IntrospectorCoreService();
    threadService = new ThreadService();
    environmentService = new EnvironmentServiceImpl();
    transactionService = new TransactionService();
    rpmConnectionService = new IntrospectorRPMConnectService();
    rpmServiceManager = new IntrospectorRPMServiceManager();
    transactionTraceService = new IntrospectorTransactionTraceService();
    asyncTxService = new AsyncTransactionService();
    profilerService = new ProfilerService();
    statsService = new IntrospectorStatsService();
    harvestService = new IntrospectorHarvestService();
    sqlTraceService = new SqlTraceServiceImpl();
    insightsService = new IntrospectorInsightsService();
    logSenderService = new IntrospectorLogSenderService();
    expirationService = new ExpirationService();
    dbService = new DatabaseService();
    jarCollectorService = new IgnoringJarCollectorService();
    distributedTraceService = new DistributedTraceServiceImpl();
    TransactionDataToDistributedTraceIntrinsics transactionDataToDistributedTraceIntrinsics = new TransactionDataToDistributedTraceIntrinsics(distributedTraceService);
    transactionEventsService = new TransactionEventsService(transactionDataToDistributedTraceIntrinsics);
    normalizationService = new NormalizationServiceImpl();
    extensionService = new ExtensionService(configService, ExtensionsLoadedListener.NOOP);
    tracerService = new TracerService();
    commandParser = new CommandParser();
    remoteInstrumentationService = new RemoteInstrumentationServiceImpl();
    sourceLanguageService = new SourceLanguageService();
    classTransformerService = new NoOpClassTransformerService();
    jmxService = new JmxService(configService.getDefaultAgentConfig().getJmxConfig());
    attributesService = new AttributesService();
    circuitBreakerService = new CircuitBreakerService();
    AgentConfig agentConfig = createAgentConfig(config, (Map) config.get("distributed_tracing"));
    distributedTraceService.connected(null, agentConfig);
    ReservoirManager<SpanEvent> reservoirManager = new CollectorSpanEventReservoirManager(configService);
    ReservoirManager.EventSender<SpanEvent> collectorSender = new CollectorSpanEventSender(rpmServiceManager);
    Consumer<SpanEvent> infiniteTracing = new Consumer<SpanEvent>() {

        @Override
        public void accept(SpanEvent spanEvent) {
        }
    };
    SpanEventCreationDecider spanEventCreationDecider = new SpanEventCreationDecider(configService);
    spanEventsService = new IntrospectorSpanEventService(agentConfig, reservoirManager, collectorSender, infiniteTracing, spanEventCreationDecider, environmentService, transactionDataToDistributedTraceIntrinsics);
    configService.addIAgentConfigListener((IntrospectorSpanEventService) spanEventsService);
    transactionService.addTransactionListener((IntrospectorSpanEventService) spanEventsService);
    try {
        transactionTraceService.start();
        transactionEventsService.start();
        transactionService.start();
    } catch (Exception e) {
    // fall through
    }
}
Also used : ReservoirManager(com.newrelic.agent.interfaces.ReservoirManager) CommandParser(com.newrelic.agent.commands.CommandParser) Consumer(com.newrelic.agent.interfaces.backport.Consumer) ProfilerService(com.newrelic.agent.profile.ProfilerService) CircuitBreakerService(com.newrelic.agent.circuitbreaker.CircuitBreakerService) NormalizationServiceImpl(com.newrelic.agent.normalization.NormalizationServiceImpl) ExtensionService(com.newrelic.agent.extension.ExtensionService) RemoteInstrumentationServiceImpl(com.newrelic.agent.reinstrument.RemoteInstrumentationServiceImpl) SqlTraceServiceImpl(com.newrelic.agent.sql.SqlTraceServiceImpl) AsyncTransactionService(com.newrelic.agent.service.async.AsyncTransactionService) DistributedTraceServiceImpl(com.newrelic.agent.tracing.DistributedTraceServiceImpl) JmxService(com.newrelic.agent.jmx.JmxService) AttributesService(com.newrelic.agent.attributes.AttributesService) AsyncTransactionService(com.newrelic.agent.service.async.AsyncTransactionService) DatabaseService(com.newrelic.agent.database.DatabaseService) EnvironmentServiceImpl(com.newrelic.agent.environment.EnvironmentServiceImpl) SpanEvent(com.newrelic.agent.model.SpanEvent) SourceLanguageService(com.newrelic.agent.language.SourceLanguageService)

Example 20 with SpanEvent

use of com.newrelic.agent.model.SpanEvent in project newrelic-java-agent by newrelic.

the class SpanErrorsTest method checkSpans.

private void checkSpans(Map<String, List<String>> spanChecks, List<String> defaultChecks) {
    SamplingPriorityQueue<SpanEvent> reservoir = ServiceFactory.getSpanEventService().getOrCreateDistributedSamplingReservoir(APP_NAME);
    assertTrue(reservoir.asList().size() > 0);
    for (SpanEvent event : reservoir.asList()) {
        if (spanChecks.containsKey(event.getName())) {
            assertEquals("wrong error.message for " + event.getName(), spanChecks.get(event.getName()).get(0), event.getAgentAttributes().get("error.message"));
            assertEquals("wrong error.class for " + event.getName(), spanChecks.get(event.getName()).get(1), event.getAgentAttributes().get("error.class"));
        } else {
            assertEquals("wrong error.message for " + event.getName(), defaultChecks.get(0), event.getAgentAttributes().get("error.message"));
            assertEquals("wrong error.class for " + event.getName(), defaultChecks.get(1), event.getAgentAttributes().get("error.class"));
        }
        assertNull("error.status should never be on " + event.getName(), event.getAgentAttributes().get("error.status"));
    }
}
Also used : SpanEvent(com.newrelic.agent.model.SpanEvent)

Aggregations

SpanEvent (com.newrelic.agent.model.SpanEvent)81 Test (org.junit.Test)64 AttributeFilter (com.newrelic.agent.model.AttributeFilter)23 SpanEventsService (com.newrelic.agent.service.analytics.SpanEventsService)19 TransactionStats (com.newrelic.agent.stats.TransactionStats)14 TransactionData (com.newrelic.agent.TransactionData)12 SpanEventsServiceImpl (com.newrelic.agent.service.analytics.SpanEventsServiceImpl)12 Tracer (com.newrelic.agent.tracers.Tracer)9 HashMap (java.util.HashMap)8 Map (java.util.Map)5 TransactionEvent (com.newrelic.agent.service.analytics.TransactionEvent)4 TransactionDataList (com.newrelic.agent.TransactionDataList)3 DistributedTracePayloadImpl (com.newrelic.agent.tracing.DistributedTracePayloadImpl)3 Test (org.junit.jupiter.api.Test)3 SpanConverterTest.buildSpanEvent (com.newrelic.SpanConverterTest.buildSpanEvent)2 BoundTransactionApiImpl (com.newrelic.agent.BoundTransactionApiImpl)2 AttributesService (com.newrelic.agent.attributes.AttributesService)2 AgentConfig (com.newrelic.agent.config.AgentConfig)2 ReservoirManager (com.newrelic.agent.interfaces.ReservoirManager)2 Consumer (com.newrelic.agent.interfaces.backport.Consumer)2