Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W3c trace context #188

Merged
merged 6 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@

package com.newrelic.agent.bridge;

import com.newrelic.api.agent.Headers;
import com.newrelic.api.agent.TransportType;

/**
* Payload used to connect two services in a distributed system.
*
* Use {@link com.newrelic.api.agent.Transaction#createDistributedTracePayload() createDistributedTracePayload()}
* to create a payload, and {@link com.newrelic.agent.bridge.Transaction#acceptDistributedTracePayload(com.newrelic.api.agent.DistributedTracePayload)} (DistributedTracePayload)} acceptDistributedTracePayload()} to accept the payload on the second service.
*
* @deprecated
*
* Note: this is an internal API. Consider using {@link com.newrelic.api.agent.DistributedTracePayload}.
* @deprecated Instead, use the Distributed Tracing API {@link Transaction#insertDistributedTraceHeaders(Headers)} to create a
* distributed tracing payload and {@link Transaction#acceptDistributedTraceHeaders(TransportType, Headers)} to link the services
* together.
*/
@Deprecated
public interface DistributedTracePayload extends com.newrelic.api.agent.DistributedTracePayload {

String text();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

import com.newrelic.api.agent.ApplicationNamePriority;
import com.newrelic.api.agent.DistributedTracePayload;
import com.newrelic.api.agent.ExtendedInboundHeaders;
import com.newrelic.api.agent.ExtendedRequest;
import com.newrelic.api.agent.Headers;
import com.newrelic.api.agent.InboundHeaders;
import com.newrelic.api.agent.OutboundHeaders;
import com.newrelic.api.agent.Request;
import com.newrelic.api.agent.Response;
import com.newrelic.api.agent.Segment;
import com.newrelic.api.agent.TransactionNamePriority;
import com.newrelic.api.agent.TransportType;

import java.net.URI;
import java.util.Collection;
Expand Down Expand Up @@ -305,6 +305,14 @@ public void acceptDistributedTracePayload(String payload) {
public void acceptDistributedTracePayload(DistributedTracePayload payload) {
}

@Override
public void insertDistributedTraceHeaders(Headers headers) {
}

@Override
public void acceptDistributedTraceHeaders(TransportType transportType, Headers headers) {
}

@Override
public void setTransportType(TransportType transportType) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.newrelic.api.agent.InboundHeaders;
import com.newrelic.api.agent.Request;
import com.newrelic.api.agent.Response;
import com.newrelic.api.agent.TransportType;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.io.IOException;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import java.io.IOException;

import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.nr.agent.instrumentation.rabbitamqp350.InboundWrapper;
import com.nr.agent.instrumentation.rabbitamqp350.RabbitAMQPMetricUtil;
import com.newrelic.agent.bridge.AgentBridge;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package com.rabbitmq.client;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.api.agent.Trace;
import com.newrelic.api.agent.weaver.MatchType;
import com.newrelic.api.agent.weaver.Weave;
Expand Down
13 changes: 5 additions & 8 deletions newrelic-agent/src/main/java/com/newrelic/agent/HeadersUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package com.newrelic.agent;

import com.google.common.collect.ImmutableSet;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.Headers;
import com.newrelic.agent.config.DistributedTracingConfig;
import com.newrelic.agent.tracers.DefaultTracer;
import com.newrelic.agent.tracing.DistributedTracePayloadImpl;
Expand All @@ -22,6 +22,7 @@
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.OutboundHeaders;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -179,6 +180,9 @@ private static List<String> getHeaders(InboundHeaders inboundHeaders, String key
if (inboundHeaders instanceof ExtendedInboundHeaders) {
return ((ExtendedInboundHeaders) inboundHeaders).getHeaders(key);
}
if (inboundHeaders instanceof Headers) {
return new ArrayList<>(((Headers) inboundHeaders).getHeaders(key));
}

String header = inboundHeaders.getHeader(key);
return header != null ? Collections.singletonList(header) : null;
Expand Down Expand Up @@ -231,13 +235,6 @@ public static void setTraceParentHeader(OutboundHeaders headers, String value) {
* @param inboundHeaders the request headers containing the distributed trace payload
*/
public static void parseAndAcceptDistributedTraceHeaders(Transaction tx, InboundHeaders inboundHeaders) {
//Don't override TransportType if it has already been set
if (TransportType.Unknown.equals(tx.getTransportType())) {
TransportType transportType = inboundHeaders.getHeaderType() == HeaderType.MESSAGE ?
TransportType.JMS : TransportType.HTTP;
tx.setTransportType(transportType);
}

List<String> traceParent = HeadersUtil.getTraceParentHeader(inboundHeaders);
if (traceParent != null && !traceParent.isEmpty()) {
List<String> traceState = HeadersUtil.getTraceStateHeader(inboundHeaders);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

package com.newrelic.agent;

import com.newrelic.agent.service.ServiceFactory;
import com.newrelic.agent.service.ServiceUtils;
import com.newrelic.api.agent.HeaderType;
import com.newrelic.api.agent.InboundHeaders;
import com.newrelic.api.agent.Request;
import com.newrelic.api.agent.TransportType;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;

Expand Down Expand Up @@ -155,6 +156,12 @@ private CatState parseCatHeaders() {
}

private void parseDistributedTraceHeaders() {
//Don't override TransportType if it has already been set
if (TransportType.Unknown.equals(tx.getTransportType())) {
TransportType transportType = inboundHeaders.getHeaderType() == HeaderType.MESSAGE ?
TransportType.JMS : TransportType.HTTP;
tx.setTransportType(transportType);
}
parseAndAcceptDistributedTraceHeaders(tx, inboundHeaders);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.newrelic.agent.bridge.NoOpToken;
import com.newrelic.agent.bridge.Token;
import com.newrelic.agent.bridge.TransactionNamePriority;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.agent.bridge.WebResponse;
import com.newrelic.agent.browser.BrowserTransactionState;
import com.newrelic.agent.browser.BrowserTransactionStateImpl;
Expand Down Expand Up @@ -290,14 +290,6 @@ public DistributedTracePayloadImpl createDistributedTracePayload(String spanId)
return payload;
}

void publicApiAcceptDistributedTracePayload(String payload) {
acceptDistributedTracePayload(payload);
}

void publicApiAcceptDistributedTracePayload(DistributedTracePayload payload) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What the.... did these methods actually serve any purpose? Or am I seeing correctly that all they did was wrap another method call? Weird...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're seeing it correctly. I didn't understand either. Went back to the PR where the code was added and didn't find any context that indicated intent.

acceptDistributedTracePayload(payload);
}

public boolean acceptDistributedTracePayload(String payload) {
if (getAgentConfig().getDistributedTracingConfig().isEnabled()) {
long elapsedMillis = TimeUnit.NANOSECONDS.toMillis(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@
import com.newrelic.agent.bridge.NoOpWebResponse;
import com.newrelic.agent.bridge.Token;
import com.newrelic.agent.bridge.TracedActivity;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.Headers;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.TransportType;
import com.newrelic.agent.bridge.WebResponse;
import com.newrelic.agent.service.ServiceFactory;
import com.newrelic.agent.tracers.Tracer;
import com.newrelic.api.agent.ApplicationNamePriority;
import com.newrelic.api.agent.DistributedTracePayload;
import com.newrelic.api.agent.ExtendedInboundHeaders;
import com.newrelic.api.agent.ExtendedRequest;
import com.newrelic.api.agent.InboundHeaders;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.OutboundHeaders;
import com.newrelic.api.agent.Request;
import com.newrelic.api.agent.Response;
import com.newrelic.api.agent.Segment;
Expand Down Expand Up @@ -476,16 +475,37 @@ public void expireAllTokens() {
public void acceptDistributedTracePayload(String payload) {
Transaction tx = getTransactionIfExists();
if (tx != null) {
tx.publicApiAcceptDistributedTracePayload(payload);
tx.acceptDistributedTracePayload(payload);
}
}

@Override
public void acceptDistributedTracePayload(DistributedTracePayload payload) {
Transaction tx = getTransactionIfExists();
if (tx != null) {
tx.publicApiAcceptDistributedTracePayload(payload);
tx.acceptDistributedTracePayload(payload);
}
}

@Override
public void insertDistributedTraceHeaders(Headers headers) {
Transaction tx = getTransactionIfExists();
if (tx == null) {
return;
}
HeadersUtil.createAndSetDistributedTraceHeaders(tx, NewRelic.getAgent().getTracedMethod(), headers);
}

@Override
public void acceptDistributedTraceHeaders(TransportType transportType, Headers headers) {
Transaction tx = getTransactionIfExists();
if (tx == null) {
return;
}
if (TransportType.Unknown.equals(tx.getTransportType())) {
tx.setTransportType(transportType);
}
HeadersUtil.parseAndAcceptDistributedTraceHeaders(tx, headers);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package com.newrelic.agent;

import com.newrelic.agent.attributes.AttributesService;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.agent.config.AgentConfig;
import com.newrelic.agent.config.AgentConfigImpl;
import com.newrelic.agent.config.TransactionTracerConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

package com.newrelic.agent.tracing;

import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.agent.interfaces.SamplingPriorityQueue;
import com.newrelic.agent.model.PriorityAware;
import com.newrelic.agent.tracers.Tracer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.newrelic.agent.Transaction;
import com.newrelic.agent.TransactionData;
import com.newrelic.agent.bridge.NoOpDistributedTracePayload;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.agent.config.AgentConfig;
import com.newrelic.agent.config.AgentConfigListener;
import com.newrelic.agent.config.DistributedTracingConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.newrelic.agent.attributes.CrossAgentInput;
import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.agent.bridge.Instrumentation;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.agent.config.AgentConfig;
import com.newrelic.agent.config.AgentConfigImpl;
import com.newrelic.agent.config.ConfigService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import com.newrelic.agent.tracing.DistributedTraceUtil;
import com.newrelic.agent.transaction.PriorityTransactionName;
import com.newrelic.agent.transaction.SegmentTest;
import com.newrelic.api.agent.TransportType;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import com.google.common.collect.ImmutableMap;
import com.newrelic.agent.TransactionData;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.agent.tracing.DistributedTracePayloadImpl;
import com.newrelic.agent.tracing.DistributedTraceService;
import com.newrelic.agent.tracing.W3CTraceParent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.newrelic.agent.MockServiceManager;
import com.newrelic.agent.Transaction;
import com.newrelic.agent.bridge.DistributedTracePayload;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.agent.config.AgentConfigImpl;
import com.newrelic.agent.config.ConfigService;
import com.newrelic.agent.config.ConfigServiceFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.newrelic.agent.Transaction;
import com.newrelic.agent.TransactionData;
import com.newrelic.agent.TransactionService;
import com.newrelic.agent.bridge.TransportType;
import com.newrelic.api.agent.TransportType;
import com.newrelic.agent.config.AgentConfig;
import com.newrelic.agent.config.AgentConfigImpl;
import com.newrelic.agent.config.ConfigService;
Expand Down
Loading