Search in sources :

Example 1 with SpanProxy

use of com.newrelic.agent.tracing.SpanProxy 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 2 with SpanProxy

use of com.newrelic.agent.tracing.SpanProxy 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 3 with SpanProxy

use of com.newrelic.agent.tracing.SpanProxy in project newrelic-java-agent by newrelic.

the class TransactionDataTestBuilder method build.

public TransactionData build() {
    when(tx.getRootTracer()).thenReturn(tracer);
    if (synJobId == null || synMonitorId == null || synResourceId == null) {
        when(tx.isSynthetic()).thenReturn(false);
    } else {
        when(tx.isSynthetic()).thenReturn(true);
    }
    when(tx.getGuid()).thenReturn("guid");
    Dispatcher mockDispatcher = mock(Dispatcher.class);
    when(mockDispatcher.getUri()).thenReturn(requestUri);
    when(mockDispatcher.isWebTransaction()).thenReturn(dispatcher == null ? true : dispatcher.isWebTransaction());
    when(tx.getDispatcher()).thenReturn(mockDispatcher);
    if (throwable != null) {
        when(tx.getThrowable()).thenReturn(new TransactionThrowable(throwable, expectedError, null));
    }
    PriorityTransactionName priorityTransactionName = mock(PriorityTransactionName.class);
    when(priorityTransactionName.getName()).thenReturn(frontendMetricName);
    when(tx.getPriorityTransactionName()).thenReturn(priorityTransactionName);
    Map<String, Map<String, String>> prefixed = new HashMap<>();
    prefixed.put("request.parameters.", requestParams);
    when(tx.getPrefixedAgentAttributes()).thenReturn(prefixed);
    when(tx.getUserAttributes()).thenReturn(userParams);
    when(tx.getAgentAttributes()).thenReturn(agentParams);
    when(tx.getErrorAttributes()).thenReturn(errorParams);
    when(tx.getIntrinsicAttributes()).thenReturn(intrinsics);
    when(tx.isIgnore()).thenReturn(false);
    when(tx.getStatus()).thenReturn(responseStatus);
    when(tx.getStatusMessage()).thenReturn(statusMessage);
    when(tx.isErrorReportableAndNotIgnored()).thenReturn(true);
    when(tx.getSpanProxy()).thenReturn(new SpanProxy());
    ErrorServiceImpl errorService = mock(ErrorServiceImpl.class);
    IRPMService rpmService = mock(IRPMService.class);
    when(rpmService.getApplicationName()).thenReturn(appName);
    when(rpmService.getErrorService()).thenReturn(errorService);
    when(tx.getRPMService()).thenReturn(rpmService);
    when(tx.getAgentConfig()).thenReturn(agentConfig);
    when(tx.getWallClockStartTimeMs()).thenReturn(startTime);
    if (slowQueryListener != null) {
        when(tx.getSlowQueryListener(anyBoolean())).thenReturn(slowQueryListener);
    }
    when(tx.getTracers()).thenReturn(tracers);
    CrossProcessTransactionState crossProcessTransactionState = mock(CrossProcessTransactionState.class);
    when(tx.getCrossProcessTransactionState()).thenReturn(crossProcessTransactionState);
    when(crossProcessTransactionState.getTripId()).thenReturn("tripId");
    InboundHeaderState ihs = mock(InboundHeaderState.class);
    when(ihs.getSyntheticsJobId()).thenReturn(synJobId);
    when(ihs.getSyntheticsMonitorId()).thenReturn(synMonitorId);
    when(ihs.getSyntheticsResourceId()).thenReturn(synResourceId);
    when(ihs.getSyntheticsVersion()).thenReturn(HeadersUtil.SYNTHETICS_MIN_VERSION);
    when(tx.getInboundHeaderState()).thenReturn(ihs);
    TransactionTimer timer = new TransactionTimer(tracer.getStartTime());
    timer.markTransactionActivityAsDone(tracer.getEndTime(), tracer.getDuration());
    timer.markTransactionAsDone();
    when(tx.getTransactionTimer()).thenReturn(timer);
    // when(tx.getApplicationName()).thenReturn(appName);
    Set<TransactionActivity> activities = new HashSet<>();
    for (Map.Entry<Long, Collection<Duration>> entry : threadIdToDuration.asMap().entrySet()) {
        for (Duration duration : entry.getValue()) {
            TransactionActivity activity = mock(TransactionActivity.class);
            when(activity.getThreadId()).thenReturn(entry.getKey());
            Tracer rootTracer = mock(Tracer.class);
            when(rootTracer.getStartTime()).thenReturn(duration.startTime);
            when(rootTracer.getEndTime()).thenReturn(duration.endTime);
            when(activity.getRootTracer()).thenReturn(rootTracer);
            activities.add(activity);
        }
    }
    when(tx.getFinishedChildren()).thenReturn(activities);
    if (includeDistributedTracePayload) {
        SpanProxy spanProxy = mock(SpanProxy.class);
        DistributedTracePayloadImpl payload = DistributedTracePayloadImpl.createDistributedTracePayload("abc", "def", "def", new Random().nextFloat());
        when(spanProxy.getInboundDistributedTracePayload()).thenReturn(payload);
        when(tx.getSpanProxy()).thenReturn(spanProxy);
    }
    return new TransactionData(tx, 0);
}
Also used : ErrorServiceImpl(com.newrelic.agent.errors.ErrorServiceImpl) HashMap(java.util.HashMap) SpanProxy(com.newrelic.agent.tracing.SpanProxy) Tracer(com.newrelic.agent.tracers.Tracer) PriorityTransactionName(com.newrelic.agent.transaction.PriorityTransactionName) Dispatcher(com.newrelic.agent.dispatchers.Dispatcher) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl) Random(java.util.Random) TransactionThrowable(com.newrelic.agent.transaction.TransactionThrowable) Collection(java.util.Collection) TransactionTimer(com.newrelic.agent.transaction.TransactionTimer) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with SpanProxy

use of com.newrelic.agent.tracing.SpanProxy in project newrelic-java-agent by newrelic.

the class TracerToSpanEventTest method setup.

@Before
public void setup() {
    transactionAgentAttributes = new HashMap<>();
    transactionUserAttributes = new HashMap<>();
    tracerAgentAttributes = new HashMap<>();
    tracerUserAttributes = new HashMap<>();
    expectedAgentAttributes = new HashMap<>();
    expectedUserAttributes = new HashMap<>();
    expectedAgentAttributes.put("error.class", "0");
    expectedAgentAttributes.put("port", 9191);
    expectedIntrinsicAttributes = new HashMap<>();
    expectedIntrinsicAttributes.put("traceId", traceId);
    expectedIntrinsicAttributes.put("duration", (float) duration / TimeConversion.NANOSECONDS_PER_SECOND);
    expectedIntrinsicAttributes.put("type", "Span");
    expectedIntrinsicAttributes.put("category", "generic");
    expectedIntrinsicAttributes.put("sampled", sampled);
    expectedIntrinsicAttributes.put("nr.entryPoint", true);
    expectedIntrinsicAttributes.put("timestamp", timestamp);
    expectedIntrinsicAttributes.put("priority", priority);
    expectedIntrinsicAttributes.put("transaction.name", txnName.getName());
    tracer = mock(Tracer.class);
    txnData = mock(TransactionData.class);
    spanErrorBuilder = mock(SpanErrorBuilder.class);
    spanError = mock(SpanError.class);
    spanProxy = mock(SpanProxy.class);
    throwable = mock(TransactionThrowable.class);
    environmentService = mock(EnvironmentService.class);
    environment = mock(Environment.class);
    transactionDataToDistributedTraceIntrinsics = mock(TransactionDataToDistributedTraceIntrinsics.class);
    txnStats = mock(TransactionStats.class, RETURNS_DEEP_STUBS);
    errorBuilderMap = new HashMap<>();
    errorBuilderMap.put(appName, spanErrorBuilder);
    when(tracer.getDuration()).thenReturn(duration);
    when(tracer.getStartTimeInMillis()).thenReturn(timestamp);
    when(tracer.getAgentAttributes()).thenReturn(tracerAgentAttributes);
    when(tracer.getCustomAttributes()).thenReturn(tracerUserAttributes);
    when(spanErrorBuilder.buildSpanError(tracer, isRoot, responseStatus, statusMessage, throwable)).thenReturn(spanError);
    when(spanErrorBuilder.areErrorsEnabled()).thenReturn(true);
    when(txnData.getApplicationName()).thenReturn(appName);
    when(txnData.getResponseStatus()).thenReturn(responseStatus);
    when(txnData.getStatusMessage()).thenReturn(statusMessage);
    when(txnData.getThrowable()).thenReturn(throwable);
    when(txnData.getPriority()).thenReturn(priority);
    when(txnData.sampled()).thenReturn(sampled);
    when(txnData.getSpanProxy()).thenReturn(spanProxy);
    when(txnData.getPriorityTransactionName()).thenReturn(txnName);
    when(txnData.getUserAttributes()).thenReturn(transactionUserAttributes);
    when(spanProxy.getOrCreateTraceId()).thenReturn(traceId);
    when(environmentService.getEnvironment()).thenReturn(environment);
    when(environment.getAgentIdentity()).thenReturn(new AgentIdentity("dispatcher", "1.2.3", 9191, "myInstance"));
}
Also used : TransactionStats(com.newrelic.agent.stats.TransactionStats) Tracer(com.newrelic.agent.tracers.Tracer) SpanProxy(com.newrelic.agent.tracing.SpanProxy) TransactionThrowable(com.newrelic.agent.transaction.TransactionThrowable) SpanError(com.newrelic.agent.model.SpanError) Environment(com.newrelic.agent.environment.Environment) EnvironmentService(com.newrelic.agent.environment.EnvironmentService) TransactionData(com.newrelic.agent.TransactionData) AgentIdentity(com.newrelic.agent.environment.AgentIdentity) Before(org.junit.Before)

Example 5 with SpanProxy

use of com.newrelic.agent.tracing.SpanProxy in project newrelic-java-agent by newrelic.

the class CrossProcessTransactionStateImplTest method setupTestForDistributedTracing.

private CrossProcessTransactionStateImpl setupTestForDistributedTracing(boolean includeNewRelicHeader) {
    String encodingKey = "test";
    String txGuid = "ee8e5ef1a374c0ec";
    DistributedTracePayloadImpl payload = DistributedTracePayloadImpl.createDistributedTracePayload("14", "15", txGuid, 1.678f);
    when(crossProcessConfig.isCrossApplicationTracing()).thenReturn(true);
    when(crossProcessConfig.getEncodingKey()).thenReturn(encodingKey);
    when(distributedTracingConfig.isEnabled()).thenReturn(true);
    when(distributedTracingConfig.isIncludeNewRelicHeader()).thenReturn(includeNewRelicHeader);
    when(spanEventsConfig.isEnabled()).thenReturn(true);
    when(transactionEventsConfig.isEnabled()).thenReturn(true);
    when(tx.getPriorityTransactionName()).thenReturn(PriorityTransactionName.create("Test", "TEST", TransactionNamePriority.NONE));
    when(tx.getApplicationName()).thenReturn("TestApp");
    when(tx.getGuid()).thenReturn(txGuid);
    when(tx.createDistributedTracePayload(anyString())).thenReturn(payload);
    mockConfigFromTransaction();
    InboundHeaderState ihs = mock(InboundHeaderState.class);
    SpanProxy spanProxy = mock(SpanProxy.class);
    when(tx.getInboundHeaderState()).thenReturn(ihs);
    when(tx.sampled()).thenReturn(true);
    when(tx.getMetricAggregator()).thenReturn(mock(MetricAggregator.class));
    when(tx.getSpanProxy()).thenReturn(spanProxy);
    W3CTraceParent traceParent = new W3CTraceParent("1.0", "traceId123abc", "parentId987", 11);
    when(spanProxy.getInitiatingW3CTraceParent()).thenReturn(traceParent);
    when(spanProxy.getOutboundDistributedTracePayload()).thenReturn(payload);
    TransactionTraceService transactionTraceService = mock(TransactionTraceService.class);
    serviceManager.setTransactionTraceService(transactionTraceService);
    return CrossProcessTransactionStateImpl.create(tx);
}
Also used : SpanProxy(com.newrelic.agent.tracing.SpanProxy) W3CTraceParent(com.newrelic.agent.tracing.W3CTraceParent) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TransactionTraceService(com.newrelic.agent.trace.TransactionTraceService) MetricAggregator(com.newrelic.api.agent.MetricAggregator) DistributedTracePayloadImpl(com.newrelic.agent.tracing.DistributedTracePayloadImpl)

Aggregations

SpanProxy (com.newrelic.agent.tracing.SpanProxy)8 DistributedTracePayloadImpl (com.newrelic.agent.tracing.DistributedTracePayloadImpl)5 TransactionData (com.newrelic.agent.TransactionData)2 TransactionStats (com.newrelic.agent.stats.TransactionStats)2 Tracer (com.newrelic.agent.tracers.Tracer)2 TransactionThrowable (com.newrelic.agent.transaction.TransactionThrowable)2 DistributedTracingConfig (com.newrelic.agent.config.DistributedTracingConfig)1 Dispatcher (com.newrelic.agent.dispatchers.Dispatcher)1 AgentIdentity (com.newrelic.agent.environment.AgentIdentity)1 Environment (com.newrelic.agent.environment.Environment)1 EnvironmentService (com.newrelic.agent.environment.EnvironmentService)1 ErrorServiceImpl (com.newrelic.agent.errors.ErrorServiceImpl)1 SpanError (com.newrelic.agent.model.SpanError)1 SpanEvent (com.newrelic.agent.model.SpanEvent)1 SpanEventsService (com.newrelic.agent.service.analytics.SpanEventsService)1 SpanEventsServiceImpl (com.newrelic.agent.service.analytics.SpanEventsServiceImpl)1 TransactionTraceService (com.newrelic.agent.trace.TransactionTraceService)1 W3CTraceParent (com.newrelic.agent.tracing.W3CTraceParent)1 W3CTraceState (com.newrelic.agent.tracing.W3CTraceState)1 W3CTraceStateHeader (com.newrelic.agent.tracing.W3CTraceStateHeader)1