Search in sources :

Example 1 with DistributedTracePayloadImpl

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());
    }
}
Also used : DistributedTraceService(com.newrelic.agent.tracing.DistributedTraceService) TransactionEvent(com.newrelic.agent.service.analytics.TransactionEvent) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl)

Example 2 with DistributedTracePayloadImpl

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;
}
Also used : SpanProxy(com.newrelic.agent.tracing.SpanProxy) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl)

Example 3 with DistributedTracePayloadImpl

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;
}
Also used : DistributedTracingConfig(com.newrelic.agent.config.DistributedTracingConfig) SpanProxy(com.newrelic.agent.tracing.SpanProxy) W3CTraceStateHeader(com.newrelic.agent.tracing.W3CTraceStateHeader) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl)

Example 4 with DistributedTracePayloadImpl

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));
}
Also used : BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl) Test(org.junit.Test) SegmentTest(com.newrelic.agent.transaction.SegmentTest)

Example 5 with DistributedTracePayloadImpl

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);
}
Also used : DistributedTraceService(com.newrelic.agent.tracing.DistributedTraceService) BasicRequestRootTracer(com.newrelic.agent.tracers.servlet.BasicRequestRootTracer) DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) OtherRootTracer(com.newrelic.agent.tracers.OtherRootTracer) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl) TransportType(com.newrelic.api.agent.TransportType) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test) SegmentTest(com.newrelic.agent.transaction.SegmentTest)

Aggregations

DistributedTracePayloadImpl (com.newrelic.agent.tracing.DistributedTracePayloadImpl)22 Test (org.junit.Test)8 Tracer (com.newrelic.agent.tracers.Tracer)6 SpanProxy (com.newrelic.agent.tracing.SpanProxy)5 Transaction (com.newrelic.agent.Transaction)4 SpanEvent (com.newrelic.agent.model.SpanEvent)3 TransactionStats (com.newrelic.agent.stats.TransactionStats)3 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)3 OtherRootTracer (com.newrelic.agent.tracers.OtherRootTracer)3 BasicRequestRootTracer (com.newrelic.agent.tracers.servlet.BasicRequestRootTracer)3 DistributedTraceService (com.newrelic.agent.tracing.DistributedTraceService)3 SegmentTest (com.newrelic.agent.transaction.SegmentTest)3 BoundTransactionApiImpl (com.newrelic.agent.BoundTransactionApiImpl)2 TransactionData (com.newrelic.agent.TransactionData)2 TransactionDataList (com.newrelic.agent.TransactionDataList)2 DistributedTracingConfig (com.newrelic.agent.config.DistributedTracingConfig)2 ErrorServiceImpl (com.newrelic.agent.errors.ErrorServiceImpl)2 SpanEventsService (com.newrelic.agent.service.analytics.SpanEventsService)2 SpanEventsServiceImpl (com.newrelic.agent.service.analytics.SpanEventsServiceImpl)2 TransactionEvent (com.newrelic.agent.service.analytics.TransactionEvent)2