use of com.newrelic.agent.tracing.DistributedTracePayloadImpl 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.tracing.DistributedTracePayloadImpl in project newrelic-java-agent by newrelic.
the class Transaction method createDistributedTracePayload.
// WARNING: Mutates this instance by mutating the span proxy
public DistributedTracePayloadImpl createDistributedTracePayload(String spanId) {
SpanProxy spanProxy = this.spanProxy.get();
long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - this.getTransactionTimer().getStartTimeInNanos());
long txnStartTimeSinceEpochInMillis = System.currentTimeMillis() - elapsedMillis;
spanProxy.setTimestamp(txnStartTimeSinceEpochInMillis);
DistributedTracePayloadImpl payload = (DistributedTracePayloadImpl) spanProxy.createDistributedTracePayload(priority.get(), spanId, getGuid());
if (payload != null) {
this.setPriorityIfNotNull(payload.priority);
}
return payload;
}
use of com.newrelic.agent.tracing.DistributedTracePayloadImpl in project newrelic-java-agent by newrelic.
the class HeadersUtil method createAndSetDistributedTraceHeaders.
/**
* creates new trace context distributed trace headers (and maybe new relic headers) and adds them to the headers object passed in
*
* @param tx current transaction
* @param tracedMethod the current traced method, used to grab the span id
* @param headers outbound headers where distributed trace headers will be added
* @return true if the headers were successfully added, false otherwise
*/
public static boolean createAndSetDistributedTraceHeaders(Transaction tx, com.newrelic.api.agent.TracedMethod tracedMethod, OutboundHeaders headers) {
final String spanId = getSpanId(tx, tracedMethod);
DistributedTracePayloadImpl payload = tx.createDistributedTracePayload(spanId);
if (payload == null) {
return false;
}
Agent.LOG.log(Level.FINER, "Sending distributed trace header in transaction {0}", tx);
DistributedTracingConfig distributedTracingConfig = tx.getAgentConfig().getDistributedTracingConfig();
boolean includeNewRelicHeader = distributedTracingConfig.isIncludeNewRelicHeader();
if (includeNewRelicHeader) {
HeadersUtil.setNewRelicTraceHeader(headers, payload.httpSafe());
}
try {
SpanProxy spanProxy = tx.getSpanProxy();
HeadersUtil.setTraceParentHeader(headers, W3CTraceParentHeader.create(spanProxy, payload.traceId, payload.guid, payload.sampled.booleanValue()));
W3CTraceStateHeader traceStateHeader = new W3CTraceStateHeader(spanEventsEnabled(tx), transactionEventsEnabled(tx));
String traceStateHeaderValue = traceStateHeader.create(spanProxy);
HeadersUtil.setTraceStateHeader(headers, traceStateHeaderValue);
tx.getMetricAggregator().incrementCounter(MetricNames.SUPPORTABILITY_TRACE_CONTEXT_CREATE_SUCCESS);
} catch (Exception e) {
tx.getMetricAggregator().incrementCounter(MetricNames.SUPPORTABILITY_TRACE_CONTEXT_CREATE_EXCEPTION);
}
return true;
}
use of com.newrelic.agent.tracing.DistributedTracePayloadImpl in project newrelic-java-agent by newrelic.
the class TransactionTest method testCreatePayload.
@Test
public void testCreatePayload() throws Exception {
Map<String, Object> configMap = createConfigMap();
configMap.put(AgentConfigImpl.DISTRIBUTED_TRACING, ImmutableMap.of("enabled", Boolean.TRUE));
createServiceManager(configMap);
Transaction.clearTransaction();
Tracer dispatcherTracer = createDispatcherTracer(true);
Transaction transaction = dispatcherTracer.getTransactionActivity().getTransaction();
transaction.getTransactionActivity().tracerStarted(dispatcherTracer);
String spanId = "honkhonkhonk";
DistributedTracePayloadImpl payload = transaction.createDistributedTracePayload(spanId);
assertNotNull(payload);
dispatcherTracer.finish(Opcodes.ARETURN, null);
assertEquals("App", payload.parentType);
assertEquals(spanId, payload.guid);
// first 10 transactions should be sampled
assertTrue(DistributedTraceUtil.isSampledPriority(payload.priority));
}
use of com.newrelic.agent.tracing.DistributedTracePayloadImpl in project newrelic-java-agent by newrelic.
the class TransactionTest method testWithNullPriorityDoesNotResetPriority.
@Test
public void testWithNullPriorityDoesNotResetPriority() throws Exception {
Map<String, Object> configMap = createConfigMap();
configMap.put(AgentConfigImpl.DISTRIBUTED_TRACING, ImmutableMap.of("enabled", Boolean.TRUE));
createServiceManager(configMap);
serviceManager.setDistributedTraceService(new DistributedTraceService() {
@Override
public boolean isEnabled() {
return true;
}
@Override
public int getMajorSupportedCatVersion() {
return 1;
}
@Override
public int getMinorSupportedCatVersion() {
return 0;
}
@Override
public String getAccountId() {
return "9123";
}
@Override
public String getApplicationId() {
return "1234";
}
@Override
public <T extends PriorityAware> float calculatePriority(Float priority, SamplingPriorityQueue<T> reservoir) {
return 0.678f;
}
@Override
public Map<String, Object> getIntrinsics(DistributedTracePayloadImpl inboundPayload, String guid, String traceId, TransportType transportType, long parentTransportDuration, long largestTransportDuration, String parentId, String parentSpanId, float priority) {
return null;
}
@Override
public String getTrustKey() {
return "67890";
}
@Override
public DistributedTracePayload createDistributedTracePayload(Tracer tracer) {
return null;
}
});
Transaction.clearTransaction();
Tracer dispatcherTracer = createDispatcherTracer(true);
Transaction transaction = dispatcherTracer.getTransactionActivity().getTransaction();
transaction.getTransactionActivity().tracerStarted(dispatcherTracer);
String inboundPayload = "{" + " \"v\": [0,2]," + " \"d\": {" + " \"ty\": \"Mobile\"," + " \"ac\": \"9123\"," + " \"tk\": \"67890\"," + " \"ap\": \"51424\"" + " \"id\": \"27856f70d3d314b7\"," + " \"tr\": \"3221bf09aa0bcf0d\"," + " \"pr\": null," + " \"ti\": 1482959525577" + " }" + "}";
transaction.acceptDistributedTracePayload(inboundPayload);
String spanId = "meatball101";
DistributedTracePayloadImpl payload = transaction.createDistributedTracePayload(spanId);
DistributedTracePayloadImpl secondPayload = transaction.createDistributedTracePayload(spanId);
dispatcherTracer.finish(Opcodes.ARETURN, null);
// Verify that when the inbound priority is null, that we retain our priority.
assertEquals("Mobile", transaction.getSpanProxy().getInboundDistributedTracePayload().parentType);
assertEquals(0.678f, payload.priority, 0.0f);
// Verify that the payload that gets passed on will preserve the priority we created.
assertEquals("App", secondPayload.parentType);
assertEquals("3221bf09aa0bcf0d", secondPayload.traceId);
assertEquals(spanId, secondPayload.guid);
assertEquals(0.678f, secondPayload.priority, 0.0f);
}
Aggregations