Skip to content

Commit

Permalink
[#9504] Refactor DisableTrace to store metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Dec 14, 2022
1 parent cc2ce7e commit b4cc7d9
Show file tree
Hide file tree
Showing 23 changed files with 403 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ public void destroyed(REQ request, final Throwable throwable, final int statusCo
return;
}

final String rpcName = requestAdaptor.getRpcName(request);
// final String rpcName = requestAdaptor.getRpcName(request);

if (this.recordStatusCode) {
this.httpStatusCodeRecorder.record(trace.getSpanRecorder(), statusCode);
}

// TODO STATDISABLE this logic was added to disable statistics tracing
if (!trace.canSampled()) {
Expand All @@ -161,9 +165,6 @@ public void destroyed(REQ request, final Throwable throwable, final int statusCo
try {
final SpanEventRecorder recorder = trace.currentSpanEventRecorder();
recorder.recordException(throwable);
if (this.recordStatusCode) {
this.httpStatusCodeRecorder.record(trace.getSpanRecorder(), statusCode);
}
// Must be executed in destroyed()
this.parameterRecorder.record(recorder, request, throwable);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public void destroyed(RESP response, final Throwable throwable, final int status
return;
}

final SpanRecorder spanRecorder = trace.getSpanRecorder();
this.httpStatusCodeRecorder.record(spanRecorder, statusCode);
if (trace.canSampled()) {
final SpanRecorder spanRecorder = trace.getSpanRecorder();
this.httpStatusCodeRecorder.record(spanRecorder, statusCode);
this.serverResponseHeaderRecorder.recordHeader(spanRecorder, response);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.navercorp.pinpoint.profiler.context.SpanEvent;
import com.navercorp.pinpoint.profiler.context.SpanType;
import com.navercorp.pinpoint.profiler.context.id.DefaultTraceId;
import com.navercorp.pinpoint.profiler.context.id.DefaultTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -69,7 +68,7 @@ public void testOrderingWithSameEventTime() {
final long spanId = 1L;

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

Span span = createSpan(traceRoot, startTime);
SpanChunk event = wrapSpanChunk(traceRoot, createSpanEvent(traceRoot, 0, 0));
Expand Down Expand Up @@ -113,13 +112,13 @@ public void testMultipleAsyncSpanEvents() {
final long startTime1 = 100;
final long spanId = 1L;
TraceId traceId1 = new DefaultTraceId(agentId, startTime1, 0, -1L, spanId, (short) 0);
final TraceRoot traceRoot1 = new DefaultTraceRoot(traceId1, agentId, startTime1, 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 TraceRoot traceRoot2 = new DefaultTraceRoot(traceId2, agentId, startTime2, 0);
final TraceRoot traceRoot2 = TraceRoot.remote(traceId2, agentId, startTime2, 0);


Span span = createSpan(traceRoot1, startTime1);
Expand Down Expand Up @@ -166,12 +165,12 @@ public void testMultipleSpanOrdering() {
final long startTime1 = 100;
final long spanId1 = 1L;
final TraceId traceId1 = new DefaultTraceId(agentId, startTime1, 0, -1L, spanId1, (short) 0);
final TraceRoot traceRoot1 = new DefaultTraceRoot(traceId1, agentId, startTime1, 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 TraceRoot traceRoot2 = new DefaultTraceRoot(traceId2, agentId, startTime2, 0);
final TraceRoot traceRoot2 = TraceRoot.remote(traceId2, agentId, startTime2, 0);

Span span1 = createSpan(traceRoot1, startTime1);
SpanChunk event1_0 = wrapSpanChunk(traceRoot1, createSpanEvent(traceRoot1, 1, 0));
Expand Down Expand Up @@ -245,13 +244,11 @@ private Object unwrapSpanChunk(SpanType spanType) {
}

private SpanChunk wrapSpanChunk(TraceRoot traceRoot, SpanEvent event) {
SpanChunk spanChunk = new DefaultSpanChunk(traceRoot, Collections.singletonList(event));
return spanChunk;
return new DefaultSpanChunk(traceRoot, Collections.singletonList(event));
}

private SpanChunk wrapSpanChunk(TraceRoot traceRoot, SpanEvent event, LocalAsyncId localAsyncId) {
SpanChunk spanChunk = new DefaultAsyncSpanChunk(traceRoot, Collections.singletonList(event), localAsyncId);
return spanChunk;
return new DefaultAsyncSpanChunk(traceRoot, Collections.singletonList(event), localAsyncId);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.navercorp.pinpoint.profiler.context.id.TraceRootFactory;
import com.navercorp.pinpoint.profiler.context.recorder.RecorderFactory;
import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder;
import com.navercorp.pinpoint.profiler.context.storage.DisabledUriStatStorage;
import com.navercorp.pinpoint.profiler.context.storage.Storage;
import com.navercorp.pinpoint.profiler.context.storage.StorageFactory;
import com.navercorp.pinpoint.profiler.context.storage.UriStatStorage;
Expand Down Expand Up @@ -84,22 +83,22 @@ public Trace continueTraceObject(final TraceId traceId) {
// TODO need to consider as a target to sample in case Trace object has a sampling flag (true) marked on previous node.
// Check max throughput(permits per seconds)
final TraceSampler.State state = traceSampler.isContinueSampled();
if (state.isSampled()) {
final boolean sampling = state.isSampled();
if (sampling) {
final TraceRoot traceRoot = traceRootFactory.continueTraceRoot(traceId, state.nextId());
final Span span = spanFactory.newSpan(traceRoot);
final SpanChunkFactory spanChunkFactory = new DefaultSpanChunkFactory(traceRoot);
final Storage storage = storageFactory.createStorage(spanChunkFactory);
final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();

final boolean samplingEnable = true;
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), samplingEnable);
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), sampling);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);
final ActiveTraceHandle handle = registerActiveTrace(traceRoot);

return new DefaultTrace(span, callStack, storage, samplingEnable, spanRecorder,
return new DefaultTrace(span, callStack, storage, sampling, spanRecorder,
wrappedSpanEventRecorder, handle, uriStatStorage);
} else {
return newDisableTrace(state.nextId());
return newLocalTrace(state.nextId());
}
}

Expand Down Expand Up @@ -142,25 +141,24 @@ Trace newTraceObject(TraceSampler.State state) {
return new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder,
handle, uriStatStorage);
} else {
return newDisableTrace(state.nextId());
return newLocalTrace(state.nextId());
}
}

// internal async trace.
@Override
public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) {
if (canSampled) {
public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean sampling) {
if (sampling) {
final SpanChunkFactory spanChunkFactory = new AsyncSpanChunkFactory(traceRoot, localAsyncId);
final Storage storage = storageFactory.createStorage(spanChunkFactory);

final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();

final boolean samplingEnable = true;
final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot, samplingEnable);
final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot, sampling);

final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);

return new AsyncChildTrace(traceRoot, callStack, storage, samplingEnable, spanRecorder, wrappedSpanEventRecorder, localAsyncId);
return new AsyncChildTrace(traceRoot, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder, localAsyncId);
} else {
return new DisableAsyncChildTrace(traceRoot, localAsyncId);
}
Expand Down Expand Up @@ -189,12 +187,11 @@ public Trace continueAsyncTraceObject(final TraceId traceId) {
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot, asyncState);


final DefaultTrace trace = new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder,
ActiveTraceHandle.EMPTY_HANDLE, DisabledUriStatStorage.INSTANCE);
final DefaultTrace trace = new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder);

return new AsyncTrace(traceRoot, trace, asyncState);
} else {
return newDisableTrace(state.nextId());
return newLocalTrace(state.nextId());
}
}

Expand Down Expand Up @@ -231,26 +228,27 @@ Trace newAsyncTraceObject(TraceSampler.State state) {
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot, asyncState);


final DefaultTrace trace = new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder,
ActiveTraceHandle.EMPTY_HANDLE, DisabledUriStatStorage.INSTANCE);
final DefaultTrace trace = new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder);

return new AsyncTrace(traceRoot, trace, asyncState);
} else {
return newDisableTrace(state.nextId());
return newLocalTrace(state.nextId());
}
}

@Override
public Trace disableSampling() {
final TraceSampler.State state = traceSampler.getContinueDisableState();
final long nextContinuedDisabledId = state.nextId();
return newDisableTrace(nextContinuedDisabledId);
return newLocalTrace(nextContinuedDisabledId);
}

private Trace newDisableTrace(long nextDisabledId) {
final long traceStartTime = System.currentTimeMillis();
private Trace newLocalTrace(long nextDisabledId) {
final TraceRoot traceRoot = traceRootFactory.newDisableTraceRoot(nextDisabledId);
final SpanRecorder spanRecorder = recorderFactory.newDisableSpanRecorder(traceRoot);
final long traceStartTime = traceRoot.getTraceStartTime();
final long threadId = Thread.currentThread().getId();
final ActiveTraceHandle activeTraceHandle = registerActiveTrace(nextDisabledId, traceStartTime, threadId);
return new DisableTrace(nextDisabledId, traceStartTime, activeTraceHandle, uriStatStorage);
return new DisableTrace(traceRoot, spanRecorder, activeTraceHandle, uriStatStorage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.annotation.Nullable;
import java.util.Objects;

/**
Expand All @@ -58,9 +59,15 @@ public final class DefaultTrace implements Trace {
private DefaultTraceScopePool scopePool;

private final Span span;
@Nullable
private final ActiveTraceHandle activeTraceHandle;
@Nullable
private final UriStatStorage uriStatStorage;

public DefaultTrace(Span span, CallStack<SpanEvent> callStack, Storage storage, boolean sampling,
SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder) {
this(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder, null, null);
}

public DefaultTrace(Span span, CallStack<SpanEvent> callStack, Storage storage, boolean sampling,
SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder,
Expand All @@ -75,8 +82,8 @@ public DefaultTrace(Span span, CallStack<SpanEvent> callStack, Storage storage,
this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder");
this.wrappedSpanEventRecorder = Objects.requireNonNull(wrappedSpanEventRecorder, "wrappedSpanEventRecorder");

this.activeTraceHandle = Objects.requireNonNull(activeTraceHandle, "activeTraceHandle");
this.uriStatStorage = Objects.requireNonNull(uriStatStorage, "uriStatStorage");
this.activeTraceHandle = activeTraceHandle;
this.uriStatStorage = uriStatStorage;

setCurrentThread();
}
Expand Down Expand Up @@ -204,6 +211,10 @@ public void close() {
}

private void recordUriTemplate(long afterTime) {
if (uriStatStorage == null) {
return;
}

TraceRoot traceRoot = this.getTraceRoot();
Shared shared = traceRoot.getShared();
String uriTemplate = shared.getUriTemplate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.profiler.context.active.ActiveTraceHandle;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import com.navercorp.pinpoint.profiler.context.scope.DefaultTraceScopePool;
import com.navercorp.pinpoint.profiler.context.storage.UriStatStorage;

Expand All @@ -37,29 +38,30 @@ public class DisableTrace implements Trace {
public static final String UNSUPPORTED_OPERATION = "disable trace";
public static final long DISABLE_TRACE_OBJECT_ID = -1;

private final long id;
private final long startTime;
private final TraceRoot traceRoot;
private final SpanRecorder spanRecorder;
private DefaultTraceScopePool scopePool;
private final ActiveTraceHandle handle;
private final UriStatStorage uriStatStorage;

private boolean closed = false;
private String uriTemplate = null;

public DisableTrace(long id, long startTime, ActiveTraceHandle handle, UriStatStorage uriStatStorage) {
this.id = id;
this.startTime = startTime;
public DisableTrace(TraceRoot traceRoot, SpanRecorder spanRecorder, ActiveTraceHandle handle, UriStatStorage uriStatStorage) {
this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");
this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder");

this.handle = Objects.requireNonNull(handle, "handle");
this.uriStatStorage = Objects.requireNonNull(uriStatStorage, "uriStatStorage");
}

@Override
public long getId() {
return id;
return traceRoot.getLocalTransactionId();
}

@Override
public long getStartTime() {
return startTime;
return traceRoot.getTraceStartTime();
}


Expand Down Expand Up @@ -125,7 +127,13 @@ public void close() {

final long purgeTime = System.currentTimeMillis();
handle.purge(purgeTime);
uriStatStorage.store(this.uriTemplate, true, startTime, purgeTime);
boolean status = getStatus();
String uriTemplate = traceRoot.getShared().getUriTemplate();
uriStatStorage.store(uriTemplate, status, traceRoot.getTraceStartTime(), purgeTime);
}

private boolean getStatus() {
return traceRoot.getShared().getErrorCode() == 0;
}


Expand All @@ -136,7 +144,7 @@ public int getCallStackFrameId() {

@Override
public SpanRecorder getSpanRecorder() {
return null;
return spanRecorder;
}

@Override
Expand All @@ -162,15 +170,11 @@ public TraceScope addScope(String name) {

@Override
public boolean recordUriTemplate(String uriTemplate) {
if (this.uriTemplate == null) {
this.uriTemplate = uriTemplate;
return true;
}
return false;
return this.traceRoot.getShared().setUriTemplate(uriTemplate);
}

@Override
public String getUriTemplate() {
return this.uriTemplate;
return this.traceRoot.getShared().getUriTemplate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ public DefaultTraceRootFactory(@AgentId String agentId, TraceIdFactory traceIdFa
public TraceRoot newTraceRoot(long transactionId) {
final TraceId traceId = traceIdFactory.newTraceId(transactionId);
final long startTime = traceStartTime();
return new DefaultTraceRoot(traceId, this.agentId, startTime, transactionId);
return TraceRoot.remote(traceId, this.agentId, startTime, transactionId);
}


@Override
public TraceRoot newDisableTraceRoot(long transactionId) {
final long startTime = traceStartTime();
return TraceRoot.local(agentId, startTime, transactionId);
}

private long traceStartTime() {
Expand All @@ -53,6 +60,6 @@ public TraceRoot continueTraceRoot(TraceId traceId, long transactionId) {
Objects.requireNonNull(traceId, "traceId");

final long startTime = traceStartTime();
return new DefaultTraceRoot(traceId, this.agentId, startTime, transactionId);
return TraceRoot.remote(traceId, this.agentId, startTime, transactionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.navercorp.pinpoint.bootstrap.context.AsyncState;
import com.navercorp.pinpoint.common.annotations.InterfaceAudience;
import com.navercorp.pinpoint.profiler.context.active.ActiveTraceHandle;
import com.navercorp.pinpoint.profiler.context.storage.DisabledUriStatStorage;
import com.navercorp.pinpoint.profiler.context.storage.UriStatStorage;

import java.util.Objects;
Expand Down Expand Up @@ -66,6 +67,9 @@ public void finish() {
}

private void recordUriTemplate(long purgeTime) {
if (uriStatStorage == DisabledUriStatStorage.INSTANCE) {
return;
}
Shared shared = this.traceRoot.getShared();
long traceStartTime = this.traceRoot.getTraceStartTime();
boolean status = getStatus(shared.getErrorCode());
Expand Down
Loading

0 comments on commit b4cc7d9

Please sign in to comment.