Search in sources :

Example 1 with W3CTraceStateHeader

use of com.newrelic.agent.tracing.W3CTraceStateHeader 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)

Aggregations

DistributedTracingConfig (com.newrelic.agent.config.DistributedTracingConfig)1 DistributedTracePayloadImpl (com.newrelic.agent.tracing.DistributedTracePayloadImpl)1 SpanProxy (com.newrelic.agent.tracing.SpanProxy)1 W3CTraceStateHeader (com.newrelic.agent.tracing.W3CTraceStateHeader)1