Skip to content

Commit

Permalink
[#noissue] Refactor TransactionId
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jan 15, 2025
1 parent 1eea0e9 commit 9c4b180
Show file tree
Hide file tree
Showing 38 changed files with 344 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,14 @@ public interface TraceId {

long getSpanId();

String getTransactionId();

String getAgentId();

long getAgentStartTime();

long getTransactionSequence();

long getParentSpanId();

short getFlags();

boolean isRoot();


String getTransactionId();

String getTransactionUId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.navercorp.pinpoint.bootstrap.plugin.RequestRecorderFactory;
import com.navercorp.pinpoint.bootstrap.plugin.proxy.DisableRequestRecorder;
import com.navercorp.pinpoint.bootstrap.plugin.request.RequestAdaptor;
import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.plugin.jboss.interceptor.StandardHostValveInvokeInterceptor;
import com.navercorp.pinpoint.profiler.context.DefaultMethodDescriptor;
import com.navercorp.pinpoint.profiler.context.id.DefaultTraceId;
Expand Down Expand Up @@ -203,7 +204,7 @@ public void testValidHeaderExists() {
when(request.getRequestURI()).thenReturn("/hellotest.nhn");
when(request.getRemoteAddr()).thenReturn("10.0.0.1");

TraceId traceId = new DefaultTraceId("agentTest", System.currentTimeMillis(), 1);
TraceId traceId = new DefaultTraceId(TransactionId.of("agentTest", System.currentTimeMillis(), 1));
when(request.getHeader(Header.HTTP_TRACE_ID.toString())).thenReturn(traceId.getTransactionId());
when(request.getHeader(Header.HTTP_PARENT_SPAN_ID.toString())).thenReturn("PARENTSPANID");
when(request.getHeader(Header.HTTP_SPAN_ID.toString())).thenReturn("SPANID");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.profiler.test;

import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.profiler.context.DefaultAsyncSpanChunk;
import com.navercorp.pinpoint.profiler.context.DefaultLocalAsyncId;
import com.navercorp.pinpoint.profiler.context.DefaultSpanChunk;
Expand Down Expand Up @@ -67,7 +68,7 @@ public void testOrderingWithSameEventTime() {
final long startTime = 100;
final long spanId = 1L;

TraceId traceId = new DefaultTraceId(agentId, startTime, 0, -1L, spanId, (short) 0);
TraceId traceId = new DefaultTraceId(TransactionId.of(agentId, startTime, 0), null, -1L, spanId, (short) 0);
final TraceRoot traceRoot = TraceRoot.remote(traceId, agentId, startTime, 0);

Span span = createSpan(traceRoot, startTime);
Expand Down Expand Up @@ -111,13 +112,13 @@ public void testMultipleAsyncSpanEvents() {
// given
final long startTime1 = 100;
final long spanId = 1L;
TraceId traceId1 = new DefaultTraceId(agentId, startTime1, 0, -1L, spanId, (short) 0);
TraceId traceId1 = new DefaultTraceId(TransactionId.of(agentId, startTime1, 0), null, -1L, spanId, (short) 0);
final TraceRoot traceRoot1 = TraceRoot.remote(traceId1, agentId, startTime1, 0);


final long startTime2 = startTime1 + 10L;
final long spanId2 = 2L;
final TraceId traceId2 = new DefaultTraceId(agentId, startTime2, 0, -1L, spanId2, (short) 0);
final TraceId traceId2 = new DefaultTraceId(TransactionId.of(agentId, startTime2, 0), null, -1L, spanId2, (short) 0);
final TraceRoot traceRoot2 = TraceRoot.remote(traceId2, agentId, startTime2, 0);


Expand Down Expand Up @@ -164,12 +165,12 @@ public void testMultipleSpanOrdering() {
// given
final long startTime1 = 100;
final long spanId1 = 1L;
final TraceId traceId1 = new DefaultTraceId(agentId, startTime1, 0, -1L, spanId1, (short) 0);
final TraceId traceId1 = new DefaultTraceId(TransactionId.of(agentId, startTime1, 0), null, -1L, spanId1, (short) 0);
final TraceRoot traceRoot1 = TraceRoot.remote(traceId1, agentId, startTime1, 0);

final long startTime2 = startTime1 + 10L;
final long spanId2 = 2L;
final TraceId traceId2 = new DefaultTraceId(agentId, startTime2, 0, -1L, spanId2, (short) 0);
final TraceId traceId2 = new DefaultTraceId(TransactionId.of(agentId, startTime2, 0), null, -1L, spanId2, (short) 0);
final TraceRoot traceRoot2 = TraceRoot.remote(traceId2, agentId, startTime2, 0);

Span span1 = createSpan(traceRoot1, startTime1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package com.navercorp.pinpoint.profiler.context.grpc.mapper;

import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.grpc.trace.PTransactionId;
import com.navercorp.pinpoint.profiler.context.id.DefaultTraceId;
import org.mapstruct.Qualifier;

import java.lang.annotation.ElementType;
Expand All @@ -36,10 +38,15 @@ public class TraceIdMapStructUtils {

@ToTransactionId
public static PTransactionId newTransactionId(TraceId traceId) {
final PTransactionId.Builder builder = PTransactionId.newBuilder();
builder.setAgentId(traceId.getAgentId());
builder.setAgentStartTime(traceId.getAgentStartTime());
builder.setSequence(traceId.getTransactionSequence());
return builder.build();
if (traceId instanceof DefaultTraceId) {
DefaultTraceId defaultTraceId = (DefaultTraceId) traceId;
TransactionId txId = defaultTraceId.getInternalTransactionId();
final PTransactionId.Builder builder = PTransactionId.newBuilder();
builder.setAgentId(txId.getAgentId());
builder.setAgentStartTime(txId.getAgentStartTime());
builder.setSequence(txId.getTransactionSequence());
return builder.build();
}
throw new IllegalArgumentException("Unexpected TraceId type: " + traceId);

Check warning on line 50 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/mapper/TraceIdMapStructUtils.java

View check run for this annotation

Codecov / codecov/patch

agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/grpc/mapper/TraceIdMapStructUtils.java#L50

Added line #L50 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import com.navercorp.pinpoint.bootstrap.context.SpanId;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.common.profiler.util.TransactionIdUtils;
import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.common.profiler.util.TransactionUId;

import java.util.Objects;

Expand All @@ -27,49 +28,52 @@
*/
public class DefaultTraceId implements TraceId {

private final String agentId;
private final long agentStartTime;
private final long transactionSequence;
private final TransactionId transactionId;
private final TransactionUId transactionUId;

private final long parentSpanId;
private final long spanId;
private final short flags;

public DefaultTraceId(String agentId, long agentStartTime, long transactionId) {
this(agentId, agentStartTime, transactionId, SpanId.NULL, SpanId.newSpanId(), (short) 0);

public DefaultTraceId(TransactionId transactionId) {
this(transactionId, null, SpanId.NULL, SpanId.newSpanId(), (short) 0);
}

public TraceId getNextTraceId() {
return new DefaultTraceId(this.agentId, this.agentStartTime, transactionSequence, spanId, SpanId.nextSpanID(spanId, parentSpanId), flags);
public static TraceId v4(TransactionId transactionId, TransactionUId txId) {
return new DefaultTraceId(transactionId, txId, SpanId.NULL, SpanId.newSpanId(), (short) 0);

Check warning on line 44 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceId.java

View check run for this annotation

Codecov / codecov/patch

agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceId.java#L44

Added line #L44 was not covered by tests
}

public DefaultTraceId(String agentId, long agentStartTime, long transactionId, long parentSpanId, long spanId, short flags) {
this.agentId = Objects.requireNonNull(agentId, "agentId");
this.agentStartTime = agentStartTime;
this.transactionSequence = transactionId;
public DefaultTraceId(TransactionId transactionId,
TransactionUId transactionUId,
long parentSpanId, long spanId, short flags) {
this.transactionId = Objects.requireNonNull(transactionId, "transactionId");

// optional
this.transactionUId = transactionUId;

this.parentSpanId = parentSpanId;
this.spanId = spanId;
this.flags = flags;
}

public String getTransactionId() {
return TransactionIdUtils.formatString(agentId, agentStartTime, transactionSequence);
public TraceId getNextTraceId() {
return new DefaultTraceId(transactionId, transactionUId, spanId, SpanId.nextSpanID(spanId, parentSpanId), flags);

Check warning on line 61 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceId.java

View check run for this annotation

Codecov / codecov/patch

agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceId.java#L61

Added line #L61 was not covered by tests
}

public String getAgentId() {
return agentId;
public String getTransactionId() {
return transactionId.toString();
}

public long getAgentStartTime() {
return agentStartTime;
public TransactionId getInternalTransactionId() {
return transactionId;
}

public long getTransactionSequence() {
return transactionSequence;
@Override
public String getTransactionUId() {
return transactionUId.toString();

Check warning on line 74 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceId.java

View check run for this annotation

Codecov / codecov/patch

agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceId.java#L74

Added line #L74 was not covered by tests
}


public long getParentSpanId() {
return parentSpanId;
}
Expand All @@ -90,9 +94,8 @@ public boolean isRoot() {
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("DefaultTraceId{");
sb.append("agentId='").append(agentId).append('\'');
sb.append(", agentStartTime=").append(agentStartTime);
sb.append(", transactionSequence=").append(transactionSequence);
sb.append("transactionId='").append(transactionId).append('\'');
sb.append("transactionUId='").append(transactionUId).append('\'');
sb.append(", parentSpanId=").append(parentSpanId);
sb.append(", spanId=").append(spanId);
sb.append(", flags=").append(flags);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,23 @@ public DefaultTraceIdFactory(@AgentId String agentId, @AgentStartTime long agent

@Override
public TraceId newTraceId(long localTransactionId) {
return new DefaultTraceId(agentId, agentStartTime, localTransactionId);
TransactionId transactionId = TransactionId.of(agentId, agentStartTime, localTransactionId);
return newTraceId(transactionId);
}

public TraceId continueTraceId(String transactionId, long parentSpanId, long spanId, short flags) {
Objects.requireNonNull(transactionId, "transactionId");

final TransactionId parseId = TransactionIdUtils.parseTransactionId(transactionId);
return new DefaultTraceId(parseId.getAgentId(), parseId.getAgentStartTime(), parseId.getTransactionSequence(), parentSpanId, spanId, flags);
final TransactionId parseTxId = TransactionIdUtils.parseTransactionId(transactionId);
return continueTraceId(parseTxId, parentSpanId, spanId, flags);

Check warning on line 53 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceIdFactory.java

View check run for this annotation

Codecov / codecov/patch

agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceIdFactory.java#L52-L53

Added lines #L52 - L53 were not covered by tests
}


public static TraceId newTraceId(TransactionId transactionId) {
return new DefaultTraceId(transactionId);
}

public static TraceId continueTraceId(TransactionId transactionId, long parentSpanId, long spanId, short flags) {
return new DefaultTraceId(transactionId, null, parentSpanId, spanId, flags);

Check warning on line 62 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceIdFactory.java

View check run for this annotation

Codecov / codecov/patch

agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/id/DefaultTraceIdFactory.java#L62

Added line #L62 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package com.navercorp.pinpoint.profiler.name;

import com.navercorp.pinpoint.common.profiler.name.AgentUuidUtils;
import com.navercorp.pinpoint.common.profiler.name.Base64Utils;
import com.navercorp.pinpoint.common.util.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -63,7 +63,7 @@ public AgentIds resolve() {

private String newRandomAgentId() {
UUID agentUUID = UUID.randomUUID();
return AgentUuidUtils.encode(agentUUID);
return Base64Utils.encode(agentUUID);

Check warning on line 66 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdResolver.java

View check run for this annotation

Codecov / codecov/patch

agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/name/AgentIdResolver.java#L66

Added line #L66 was not covered by tests
}

private String getAgentId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void parseTest() {
String agent = "test";
long agentStartTime = System.currentTimeMillis();
long agentTransactionCount = 10;
TraceId traceId = new DefaultTraceId(agent, agentStartTime, agentTransactionCount);
TraceId traceId = new DefaultTraceId(TransactionId.of(agent, agentStartTime, agentTransactionCount));

String id = traceId.getTransactionId();

Expand Down Expand Up @@ -132,7 +132,7 @@ public void transactionCountTest() {

final long expectedSampledContinuationCount = 5L;
for (int i = 0; i < expectedSampledContinuationCount; i++) {
traceContext.continueTraceObject(new DefaultTraceId("agentId", 0L, i));
traceContext.continueTraceObject(new DefaultTraceId(TransactionId.of("agentId", 0L, i)));
traceContext.removeTraceObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
import com.navercorp.pinpoint.bootstrap.context.SpanRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.profiler.context.errorhandler.BypassErrorHandler;
import com.navercorp.pinpoint.profiler.context.errorhandler.IgnoreErrorHandler;
import com.navercorp.pinpoint.profiler.context.exception.ExceptionRecorder;
import com.navercorp.pinpoint.profiler.context.exception.disabled.DisabledExceptionContext;
import com.navercorp.pinpoint.profiler.context.exception.model.ExceptionContext;
import com.navercorp.pinpoint.profiler.context.id.DefaultTraceId;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import com.navercorp.pinpoint.profiler.context.recorder.DefaultSpanRecorder;
Expand Down Expand Up @@ -72,7 +71,7 @@ public class TraceTest {
@Test
public void trace() {

final TraceId traceId = new DefaultTraceId(agentId, agentStartTime, 1);
final TraceId traceId = new DefaultTraceId(TransactionId.of(agentId, agentStartTime, 1));
final TraceRoot traceRoot = TraceRoot.remote(traceId, agentId, traceStartTime, 0);

final CallStack<SpanEvent> callStack = newCallStack();
Expand Down Expand Up @@ -101,7 +100,7 @@ public void trace() {
@Test
public void popEventTest() {

final TraceId traceId = new DefaultTraceId(agentId, agentStartTime, 1);
final TraceId traceId = new DefaultTraceId(TransactionId.of(agentId, agentStartTime, 1));
final TraceRoot traceRoot = TraceRoot.remote(traceId, agentId, traceStartTime, 0);

final CallStack<SpanEvent> callStack = newCallStack();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceContext;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.profiler.context.MockTraceContextFactory;
import com.navercorp.pinpoint.profiler.context.id.DefaultTraceId;
import com.navercorp.pinpoint.profiler.context.id.DefaultTransactionCounter;
Expand Down Expand Up @@ -109,7 +110,7 @@ public void verifyActiveTraceCollectionAndTransactionCount() throws Exception {
List<ActiveTraceSnapshot> activeTraceInfos = this.activeTraceRepository.snapshot();
awaitLatch.countDown();
List<TraceThreadTuple> executedTraces = futures.get(5, TimeUnit.SECONDS);
Map<Long, TraceThreadTuple> executedTraceMap = new HashMap<Long, TraceThreadTuple>(executedTraces.size());
Map<Long, TraceThreadTuple> executedTraceMap = new HashMap<>(executedTraces.size());
for (TraceThreadTuple tuple : executedTraces) {
executedTraceMap.put(tuple.id, tuple);
}
Expand Down Expand Up @@ -140,7 +141,7 @@ private ListenableFuture<List<TraceThreadTuple>> executeTransactions(

int totalNewCount = sampledNewCount + unsampledNewCount;
final ListeningExecutorService executor = MoreExecutors.listeningDecorator(executorService);
final List<ListenableFuture<TraceThreadTuple>> futures = new ArrayList<ListenableFuture<TraceThreadTuple>>();
final List<ListenableFuture<TraceThreadTuple>> futures = new ArrayList<>();
for (int i = 0; i < totalNewCount; i++) {
futures.add(executeNewTrace(executor, awaitLatch, executeLatch));
}
Expand Down Expand Up @@ -175,7 +176,7 @@ private ListenableFuture<TraceThreadTuple> executeSampledContinuedTrace(Listenin
@Override
public TraceThreadTuple call() throws Exception {
try {
TraceId agentId1 = new DefaultTraceId("agentId", 0L, id);
TraceId agentId1 = new DefaultTraceId(TransactionId.of("agentId", 0L, id));
Trace agentId = traceContext.continueTraceObject(agentId1);
return new TraceThreadTuple(agentId, Thread.currentThread().getId());
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.profiler.context.compress;

import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.grpc.trace.PSpan;
import com.navercorp.pinpoint.grpc.trace.PSpanChunk;
import com.navercorp.pinpoint.grpc.trace.PSpanEvent;
Expand Down Expand Up @@ -66,7 +67,7 @@ public void preProcess() {
}

private Span newSpan() {
TraceId traceId = new DefaultTraceId("agent", 1, 0);
TraceId traceId = new DefaultTraceId(TransactionId.of("agent", 1, 0));
TraceRoot traceRoot = TraceRoot.remote(traceId, "agent", 0, 3);
return new Span(traceRoot);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.navercorp.pinpoint.profiler.context.grpc;

import com.navercorp.pinpoint.common.profiler.util.TransactionId;
import com.navercorp.pinpoint.grpc.trace.PException;
import com.navercorp.pinpoint.grpc.trace.PExceptionMetaData;
import com.navercorp.pinpoint.grpc.trace.PStackTraceElement;
Expand Down Expand Up @@ -38,7 +39,7 @@ private static TraceRoot newTraceRoot() {
TraceRoot traceRoot = mock(TraceRoot.class);
final String agentId = "agent";
final long agentStartTime = System.currentTimeMillis();
when(traceRoot.getTraceId()).thenReturn(new DefaultTraceId(agentId, agentStartTime, 0));
when(traceRoot.getTraceId()).thenReturn(new DefaultTraceId(TransactionId.of(agentId, agentStartTime, 0)));
when(traceRoot.getTraceStartTime()).thenReturn(agentStartTime + 100);
when(traceRoot.getLocalTransactionId()).thenReturn((long) 1);

Expand Down
Loading

0 comments on commit 9c4b180

Please sign in to comment.