use of com.newrelic.agent.service.analytics.TransactionEvent in project newrelic-java-agent by newrelic.
the class Transaction method checkAndSetPriority.
private void checkAndSetPriority() {
if (getAgentConfig().getDistributedTracingConfig().isEnabled()) {
DistributedTraceService distributedTraceService = ServiceFactory.getDistributedTraceService();
DistributedTracePayloadImpl inboundPayload = spanProxy.get().getInboundDistributedTracePayload();
Float inboundPriority = inboundPayload != null ? inboundPayload.priority : null;
DistributedSamplingPriorityQueue<TransactionEvent> reservoir = ServiceFactory.getTransactionEventsService().getOrCreateDistributedSamplingReservoir(getApplicationName());
priority.compareAndSet(null, distributedTraceService.calculatePriority(inboundPriority, reservoir));
} else {
priority.compareAndSet(null, DistributedTraceServiceImpl.nextTruncatedFloat());
}
}
use of com.newrelic.agent.service.analytics.TransactionEvent 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);
}
}
}
use of com.newrelic.agent.service.analytics.TransactionEvent in project newrelic-java-agent by newrelic.
the class DistributedTraceServiceImplTest method testExponentialBackoff.
@Test
public void testExponentialBackoff() {
rpmServiceManager.getOrCreateRPMService("Test");
// Create reservoir
ServiceFactory.getTransactionEventsService().harvestEvents("Test");
DistributedSamplingPriorityQueue<TransactionEvent> reservoir = ServiceFactory.getTransactionEventsService().getOrCreateDistributedSamplingReservoir("Test");
// First 10 traces
for (int i = 0; i < 10; i++) {
assertTrue(DistributedTraceUtil.isSampledPriority(distributedTraceService.calculatePriority(null, reservoir)));
TransactionEvent transactionEvent = Mockito.mock(TransactionEvent.class);
when(transactionEvent.getPriority()).thenReturn(1.0f);
when(transactionEvent.decider()).thenReturn(true);
reservoir.add(transactionEvent);
}
distributedTraceService.beforeHarvest("Test", new StatsEngineImpl());
ServiceFactory.getTransactionEventsService().harvestEvents("Test");
reservoir = ServiceFactory.getTransactionEventsService().getOrCreateDistributedSamplingReservoir("Test");
assertEquals(0, reservoir.getSampled());
// Test that we hit target
for (int i = 0; i < reservoir.getTarget(); i++) {
if (DistributedTraceUtil.isSampledPriority(distributedTraceService.calculatePriority(null, reservoir))) {
TransactionEvent transactionEvent = Mockito.mock(TransactionEvent.class);
when(transactionEvent.getPriority()).thenReturn(1.0f);
when(transactionEvent.decider()).thenReturn(true);
reservoir.add(transactionEvent);
}
}
assertTrue("Sampled fewer than target transactions", reservoir.getSampled() >= reservoir.getTarget());
ServiceFactory.getTransactionEventsService().harvestEvents("Test");
distributedTraceService.beforeHarvest("Test", new StatsEngineImpl());
reservoir = ServiceFactory.getTransactionEventsService().getOrCreateDistributedSamplingReservoir("Test");
// Test that we do not go above 2x target
for (int i = 0; i < 1000 * reservoir.getTarget(); i++) {
if (DistributedTraceUtil.isSampledPriority(distributedTraceService.calculatePriority(null, reservoir))) {
TransactionEvent transactionEvent = Mockito.mock(TransactionEvent.class);
when(transactionEvent.getPriority()).thenReturn(1.0f);
when(transactionEvent.decider()).thenReturn(true);
reservoir.add(transactionEvent);
}
}
assertTrue("Sampled fewer than target transactions", reservoir.getSampled() >= reservoir.getTarget());
assertTrue("Sampled more than 2x target ", reservoir.getSampled() <= (2 * reservoir.getTarget()));
}
use of com.newrelic.agent.service.analytics.TransactionEvent in project newrelic-java-agent by newrelic.
the class DistributedTraceServiceImplTest method testEventsByPriority.
@Test
public void testEventsByPriority() {
rpmServiceManager.getOrCreateRPMService("Test");
// Create reservoir
ServiceFactory.getTransactionEventsService().harvestEvents("Test");
DistributedSamplingPriorityQueue<TransactionEvent> reservoir = ServiceFactory.getTransactionEventsService().getOrCreateDistributedSamplingReservoir("Test");
float minPriority = 100.0f;
float maxPriority = 0.0f;
for (int i = 0; i < 3000; i++) {
TransactionEvent transactionEvent = Mockito.mock(TransactionEvent.class);
Float priority = DistributedTraceServiceImplTest.distributedTraceService.calculatePriority(null, reservoir);
// Store the smallest priority we've seen
minPriority = Math.min(priority, minPriority);
// Store the largest priority we've seen
maxPriority = Math.max(priority, maxPriority);
when(transactionEvent.getPriority()).thenReturn(priority);
when(transactionEvent.decider()).thenReturn(true);
reservoir.add(transactionEvent);
}
for (int i = 0; i < 1000; i++) {
TransactionEvent transactionEvent = Mockito.mock(TransactionEvent.class);
Float priority = DistributedTraceServiceImplTest.distributedTraceService.calculatePriority(null, reservoir);
when(transactionEvent.getPriority()).thenReturn(priority);
when(transactionEvent.decider()).thenReturn(false);
reservoir.add(transactionEvent);
}
assertTrue(reservoir.peek().getPriority() > minPriority);
assertEquals(maxPriority, reservoir.peek().getPriority(), 0.0f);
assertEquals(4000, reservoir.getNumberOfTries());
assertTrue(reservoir.getSampled() >= 11);
List<TransactionEvent> events = reservoir.asList();
int sampled = reservoir.getSampled();
// verify that the number of "sampled" events equals the number of events with priority >= 1.0 where decider = true
for (int i = 0; i < sampled + 1; i++) {
if (i < sampled) {
assertTrue(DistributedTraceUtil.isSampledPriority(events.get(i).getPriority()));
} else {
assertTrue(events.get(i).getPriority() < 1.0f);
}
}
// harvest in order to examine seen vs sent metrics
ServiceFactory.getTransactionEventsService().harvestEvents("Test");
StatsEngine statsEngineForHarvest = ServiceFactory.getStatsService().getStatsEngineForHarvest("Test");
assertTrue(statsEngineForHarvest.getStats(MetricName.create(MetricNames.SUPPORTABILITY_TRANSACTION_EVENT_SERVICE_TRANSACTION_EVENT_SEEN)).hasData());
assertTrue(statsEngineForHarvest.getStats(MetricName.create(MetricNames.SUPPORTABILITY_TRANSACTION_EVENT_SERVICE_TRANSACTION_EVENT_SENT)).hasData());
assertEquals(reservoir.getNumberOfTries(), statsEngineForHarvest.getStats(MetricName.create(MetricNames.SUPPORTABILITY_TRANSACTION_EVENT_SERVICE_TRANSACTION_EVENT_SEEN)).getCallCount());
assertEquals(2000, statsEngineForHarvest.getStats(MetricName.create(MetricNames.SUPPORTABILITY_TRANSACTION_EVENT_SERVICE_TRANSACTION_EVENT_SENT)).getCallCount());
}
use of com.newrelic.agent.service.analytics.TransactionEvent in project newrelic-java-agent by newrelic.
the class SpanParentTest method testSpanAndTransactionParenting.
@Test
public void testSpanAndTransactionParenting() throws Exception {
EnvironmentHolder holder = setupEnvironemntHolder("all_enabled_test");
executeSpanAndTransactionParentingTest();
try {
TransactionDataList transactionList = holder.getTransactionList();
assertEquals(2, transactionList.size());
TransactionData tx1 = transactionList.get(0);
TransactionData tx2 = transactionList.get(1);
Collection<Tracer> tracers1 = tx1.getTracers();
// Only a "rootTracer" on this transaction (root tracer is not in this list)
assertEquals(0, tracers1.size());
Collection<Tracer> tracers2 = tx2.getTracers();
// 1 "rootTracer" (not in this list) + 2 non-external/datastore tracers + 2 external datastore tracers
assertEquals(4, tracers2.size());
SpanEventsService spanEventsService = ServiceFactory.getServiceManager().getSpanEventsService();
String appName = ServiceFactory.getConfigService().getDefaultAgentConfig().getApplicationName();
SamplingPriorityQueue<SpanEvent> spanEventsPool = spanEventsService.getOrCreateDistributedSamplingReservoir(appName);
assertNotNull(spanEventsPool);
List<SpanEvent> spanEvents = spanEventsPool.asList();
spanEventsPool.clear();
assertNotNull(spanEvents);
assertEquals(6, spanEvents.size());
SpanEvent rootSpanEvent = null;
Set<String> spanEventGuids = new HashSet<>();
for (SpanEvent spanEvent : spanEvents) {
if (spanEvent.getParentId() == null) {
rootSpanEvent = spanEvent;
}
spanEventGuids.add(spanEvent.getGuid());
}
assertNotNull(rootSpanEvent);
assertEquals(6, spanEventGuids.size());
// Ensure that spans are only parented to other spans
for (SpanEvent spanEvent : spanEvents) {
if (spanEvent.getParentId() == null) {
continue;
}
assertTrue(spanEventGuids.contains(spanEvent.getParentId()));
}
TransactionEventsService transactionEventsService = ServiceFactory.getServiceManager().getTransactionEventsService();
DistributedSamplingPriorityQueue<TransactionEvent> txEventPool = transactionEventsService.getOrCreateDistributedSamplingReservoir(appName);
assertNotNull(txEventPool);
List<TransactionEvent> txEvents = txEventPool.asList();
txEventPool.clear();
assertNotNull(txEvents);
assertEquals(2, txEvents.size());
TransactionEvent parent = null;
TransactionEvent child = null;
for (TransactionEvent txEvent : txEvents) {
if (txEvent.getParentId() == null) {
parent = txEvent;
} else {
child = txEvent;
}
}
assertNotNull(parent);
assertNotNull(child);
assertNull(parent.getParentId());
assertEquals(parent.getGuid(), child.getParentId());
} finally {
holder.close();
}
}
Aggregations