Skip to content

Commit

Permalink
Merge branch 'main' into sdaubin/include-stacktrace-in-spans
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonjkeller authored Sep 25, 2023
2 parents 60fa9b2 + b50676a commit a3819b9
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ public final class AttributeNames {
public static final String TIMEOUT_CAUSE = "nr.timeoutCause";
public static final String ERROR_EXPECTED = "error.expected";

public static final String COMPONENT = "component";
public static final String HTTP_METHOD = "http.method";
public static final String HTTP_STATUS_CODE = "http.statusCode";
public static final String HTTP_STATUS_TEXT = "http.statusText";

public static final String LOCK_THREAD_NAME = "jvm.lock_thread_name";
public static final String THREAD_NAME = "jvm.thread_name";
public static final String THREAD_ID = "thread.id";

public static final String MESSAGE_REQUEST_PREFIX = "message.parameters.";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ public SpanEventFactory setClmAttributes(Map<String, Object> agentAttributes) {
if (agentAttributes == null || agentAttributes.isEmpty()) {
return this;
}
final Object threadId = agentAttributes.get(AttributeNames.THREAD_ID);
if (threadId != null) {
builder.putAgentAttribute(AttributeNames.THREAD_ID, threadId);
}
if (agentAttributes.containsKey(AttributeNames.CLM_NAMESPACE) && agentAttributes.containsKey(AttributeNames.CLM_FUNCTION)) {
builder.putAgentAttribute(AttributeNames.CLM_NAMESPACE, agentAttributes.get(AttributeNames.CLM_NAMESPACE));
builder.putAgentAttribute(AttributeNames.CLM_FUNCTION, agentAttributes.get(AttributeNames.CLM_FUNCTION));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ public class DefaultTracer extends AbstractTracer {
public static final int DEFAULT_TRACER_FLAGS = TracerFlags.TRANSACTION_TRACER_SEGMENT
| TracerFlags.GENERATE_SCOPED_METRIC;

private static final String COMPONENT_PARAMETER_NAME = "component";
private static final String HTTP_METHOD_PARAMETER_NAME = "http.method";

private final long startTime;
private final long timestamp;
private long duration;
Expand Down Expand Up @@ -290,6 +287,7 @@ public void performFinishWork(long finishTime, int opcode, Object returnValue) {
}

try {
setAgentAttribute(AttributeNames.THREAD_ID, getTransactionActivity().getThreadId());
if (classMethodSignature != null && getTransaction() != null &&
ServiceFactory.getConfigService().getDefaultAgentConfig().getCodeLevelMetricsConfig().isEnabled()) {
String className = classMethodSignature.getClassName();
Expand Down Expand Up @@ -712,9 +710,9 @@ private void recordExternalMetricsHttp(HttpParameters externalParameters) {
String uriStr = uri == null ? ExternalMetrics.UNKNOWN_HOST : uri.toString();

String library = externalParameters.getLibrary();
setAgentAttribute(COMPONENT_PARAMETER_NAME, library);
setAgentAttribute(AttributeNames.COMPONENT, library);
String procedure = externalParameters.getProcedure();
setAgentAttribute(HTTP_METHOD_PARAMETER_NAME, procedure);
setAgentAttribute(AttributeNames.HTTP_METHOD, procedure);

ExternalMetrics.makeExternalComponentTrace(transaction.isWebTransaction(), this, host, library, true,
uriStr, procedure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ public static JsonTraceSegment createTraceSegment(JSONArray segmentArray, String
seg.className = (String) segmentArray.get(5);
seg.methodName = (String) segmentArray.get(6);

// attributes added to all traces
seg.requestParams.put("code.namespace", seg.className);
seg.requestParams.put("code.function", seg.methodName);
seg.requestParams.put("thread.id", "*");
return seg;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.newrelic.agent.MockConfigService;
import com.newrelic.agent.MockServiceManager;
import com.newrelic.agent.config.AgentConfig;
import com.newrelic.agent.attributes.AttributeNames;
import com.newrelic.agent.model.AttributeFilter;
import com.newrelic.agent.model.SpanCategory;
import com.newrelic.agent.model.SpanError;
Expand Down Expand Up @@ -197,6 +198,21 @@ public void shouldStoreStackTrace() {
assertNotNull(stackTrace);
}

@Test
public void shouldSetCLMParameters() {
Map<String, Object> agentAttributes = ImmutableMap.of(
AttributeNames.CLM_NAMESPACE, "nr",
AttributeNames.CLM_FUNCTION, "process",
AttributeNames.THREAD_ID, 666
);

SpanEvent target = spanEventFactory.setClmAttributes(agentAttributes).build();

assertEquals("nr", target.getAgentAttributes().get(AttributeNames.CLM_NAMESPACE));
assertEquals("process", target.getAgentAttributes().get(AttributeNames.CLM_FUNCTION));
assertEquals(666, target.getAgentAttributes().get(AttributeNames.THREAD_ID));
}

@Test
public void shouldFilterUserAttributes() {
SpanEventFactory target = new SpanEventFactory("blerb", new AttributeFilter.PassEverythingAttributeFilter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void testHighSecurityNormalFinish() throws Exception {
SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);

assertEquals(7, tracer.getAgentAttributes().size()); // exclusive_duration_millis, sql, sql_obfuscated, host, port_path_or_id, code.namespace, code.function
assertEquals(8, tracer.getAgentAttributes().size()); // exclusive_duration_millis, sql, sql_obfuscated, host, port_path_or_id, code.namespace, code.function, thread.id
assertEquals(inputSql, tracer.getAgentAttributes().get("sql")); // shouldn't be obfuscated yet
assertClm(tracer);

Expand Down Expand Up @@ -220,7 +220,7 @@ public void testHighSecurityErrorFinish() throws Exception {
SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);

assertEquals(4, tracer.getAgentAttributes().size()); // exclusive_duration_millis, sql, code.namespace, code.function
assertEquals(5, tracer.getAgentAttributes().size()); // exclusive_duration_millis, sql, code.namespace, code.function, thread.id
assertEquals(inputSql, (String) tracer.getAgentAttributes().get("sql")); // shouldn't be obfuscated yet
assertClm(tracer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void testHighSecurityNormalFinish() throws Exception {
SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);

assertEquals(5, tracer.getAgentAttributes().size()); // exclusive_duration_millis, sql, sql_obfuscated, host, port_path_or_id
assertEquals(6, tracer.getAgentAttributes().size()); // exclusive_duration_millis, sql, sql_obfuscated, host, port_path_or_id, thread.id
assertEquals(inputSql, (String) tracer.getAgentAttributes().get("sql")); // shouldn't be obfuscated yet

JSONArray json = (JSONArray) AgentHelper.serializeJSON(segment);
Expand Down Expand Up @@ -198,7 +198,7 @@ public void testHighSecurityErrorFinish() throws Exception {
SqlObfuscator sqlObfuscator = ServiceFactory.getDatabaseService().getDefaultSqlObfuscator();
TransactionSegment segment = new TransactionSegment(ttConfig, sqlObfuscator, 0, tracer);

assertEquals(2, tracer.getAgentAttributes().size()); // exclusive_duration_millis, sql
assertEquals(3, tracer.getAgentAttributes().size()); // exclusive_duration_millis, sql, thread.id
assertEquals(inputSql, (String) tracer.getAgentAttributes().get("sql")); // shouldn't be obfuscated yet

JSONArray json = (JSONArray) AgentHelper.serializeJSON(segment);
Expand Down

0 comments on commit a3819b9

Please sign in to comment.