Skip to content

Commit

Permalink
feat: add thread.id attribute to Spans (#1513)
Browse files Browse the repository at this point in the history
* feat: add thread.id attribute to Spans

---------

Co-authored-by: Andre Onuki <[email protected]>
  • Loading branch information
sdaubin and meiao authored Sep 25, 2023
1 parent 2c20813 commit b50676a
Show file tree
Hide file tree
Showing 7 changed files with 34 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 @@ -119,6 +119,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 @@ -7,10 +7,13 @@

package com.newrelic.agent.service.analytics;

import com.google.common.collect.ImmutableMap;
import com.newrelic.agent.attributes.AttributeNames;
import com.newrelic.agent.model.AttributeFilter;
import com.newrelic.agent.model.SpanCategory;
import com.newrelic.agent.model.SpanError;
import com.newrelic.agent.model.SpanEvent;
import com.newrelic.agent.tracers.DefaultTracer;
import com.newrelic.api.agent.DatastoreParameters;
import com.newrelic.api.agent.HttpParameters;
import org.junit.Test;
Expand Down Expand Up @@ -176,6 +179,21 @@ public void shouldSetDataStoreParameters() {
assertEquals("database name", target.getIntrinsics().get("db.instance"));
}

@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 b50676a

Please sign in to comment.