Skip to content

Commit

Permalink
[#9575] ...
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jan 6, 2023
1 parent 74275da commit bbfd377
Show file tree
Hide file tree
Showing 24 changed files with 431 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public class AsyncChildTrace implements Trace {
private final TraceRoot traceRoot;
private final LocalAsyncId localAsyncId;

public AsyncChildTrace(final TraceRoot traceRoot, CallStack<SpanEvent> callStack, Storage storage,
SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder, final LocalAsyncId localAsyncId) {
public AsyncChildTrace(final TraceRoot traceRoot,
CallStack<SpanEvent> callStack,
Storage storage,
SpanRecorder spanRecorder,
WrappedSpanEventRecorder wrappedSpanEventRecorder,
final LocalAsyncId localAsyncId) {

this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");
this.callStack = Objects.requireNonNull(callStack, "callStack");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
import com.navercorp.pinpoint.bootstrap.context.AsyncState;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;

/**
Expand All @@ -31,4 +32,7 @@ public interface AsyncContextFactory {

AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolean canSampled, AsyncState asyncState);

AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot);

AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot, AsyncState asyncState);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.profiler.context;

import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;


Expand All @@ -31,6 +32,8 @@ public interface AsyncTraceContext {

Trace newAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);

Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot);

Reference<Trace> currentRawTraceObject();

Reference<Trace> currentTraceObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.common.annotations.InterfaceAudience;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;

/**
Expand All @@ -34,7 +35,9 @@ public interface BaseTraceFactory {
@InterfaceAudience.LimitedPrivate("vert.x")
Trace continueAsyncTraceObject(TraceId traceId);

Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled);
Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId);

Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot);

Trace newTraceObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.bootstrap.context.AsyncState;
import com.navercorp.pinpoint.bootstrap.context.MethodDescriptor;
import com.navercorp.pinpoint.profiler.context.id.AsyncIdGenerator;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import com.navercorp.pinpoint.profiler.context.method.PredefinedMethodDescriptorRegistry;

Expand Down Expand Up @@ -71,5 +72,14 @@ public AsyncContext newAsyncContext(TraceRoot traceRoot, AsyncId asyncId, boolea
return new StatefulAsyncContext(asyncTraceContext, traceRoot, asyncId, asyncMethodApiId, asyncState, canSampled);
}

@Override
public AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot) {
return new DisableAsyncContext(traceRoot, asyncTraceContext);
}

@Override
public AsyncContext newDisableAsyncContext(LocalTraceRoot traceRoot, AsyncState asyncState) {
return new DisableAsyncContext(traceRoot, asyncTraceContext, asyncState);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import com.google.inject.Provider;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import java.util.Objects;
import com.navercorp.pinpoint.exception.PinpointException;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Objects;


/**
Expand Down Expand Up @@ -54,8 +56,22 @@ public Reference<Trace> continueAsyncContextTraceObject(TraceRoot traceRoot, Loc

@Override
public Trace newAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId, boolean canSampled) {
if (canSampled) {
return continueAsyncContextTraceObject(traceRoot, localAsyncId);
} else {
return continueDisableAsyncContextTraceObject(traceRoot);
}
}

public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId localAsyncId) {
final BaseTraceFactory baseTraceFactory = baseTraceFactoryProvider.get();
return baseTraceFactory.continueAsyncContextTraceObject(traceRoot, localAsyncId);
}

@Override
public Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot) {
final BaseTraceFactory baseTraceFactory = baseTraceFactoryProvider.get();
return baseTraceFactory.continueAsyncContextTraceObject(traceRoot, localAsyncId, canSampled);
return baseTraceFactory.continueDisableAsyncContextTraceObject(traceRoot);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.navercorp.pinpoint.profiler.context;

import com.navercorp.pinpoint.bootstrap.context.AsyncState;
import com.navercorp.pinpoint.bootstrap.context.SpanEventRecorder;
import com.navercorp.pinpoint.bootstrap.context.SpanRecorder;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
Expand Down Expand Up @@ -146,21 +147,25 @@ Trace newTraceObject(TraceSampler.State state) {

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

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

final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot);
final SpanRecorder spanRecorder = recorderFactory.newTraceRootSpanRecorder(traceRoot);

final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);

return new AsyncChildTrace(traceRoot, callStack, storage, spanRecorder, wrappedSpanEventRecorder, localAsyncId);
} else {
return new DisableAsyncChildTrace(traceRoot, localAsyncId);
}
return new AsyncChildTrace(traceRoot, callStack, storage, spanRecorder, wrappedSpanEventRecorder, localAsyncId);
}

@Override
public Trace continueDisableAsyncContextTraceObject(LocalTraceRoot traceRoot) {
final AsyncState asyncState = new DisableAsyncState();
SpanRecorder spanRecorder = recorderFactory.newDisableSpanRecorder(traceRoot);
SpanEventRecorder spanEventRecorder = recorderFactory.newDisableSpanEventRecorder(traceRoot, asyncState);
return new DisableAsyncChildTrace(traceRoot, spanRecorder, spanEventRecorder);
}


Expand All @@ -178,7 +183,7 @@ public Trace continueAsyncTraceObject(final TraceId traceId) {
final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();

final ActiveTraceHandle handle = registerActiveTrace(traceRoot);
final SpanAsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory);
final ListenableAsyncState.AsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory);
final AsyncState asyncState = new ListenableAsyncState(traceRoot, asyncStateListener, handle, uriStatStorage);

final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot());
Expand Down Expand Up @@ -217,7 +222,7 @@ Trace newAsyncTraceObject(TraceSampler.State state) {
final CallStack<SpanEvent> callStack = callStackFactory.newCallStack();

final ActiveTraceHandle handle = registerActiveTrace(traceRoot);
final SpanAsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory);
final ListenableAsyncState.AsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory);
final AsyncState asyncState = new ListenableAsyncState(traceRoot, asyncStateListener, handle, uriStatStorage);


Expand All @@ -244,9 +249,10 @@ public Trace disableSampling() {
private Trace newLocalTrace(long nextDisabledId) {
final LocalTraceRoot traceRoot = traceRootFactory.newDisableTraceRoot(nextDisabledId);
final SpanRecorder spanRecorder = recorderFactory.newDisableSpanRecorder(traceRoot);
final SpanEventRecorder spanEventRecorder = recorderFactory.newDisableSpanEventRecorder(traceRoot, new DisableAsyncState());
final long traceStartTime = traceRoot.getTraceStartTime();
final long threadId = Thread.currentThread().getId();
final ActiveTraceHandle activeTraceHandle = registerActiveTrace(nextDisabledId, traceStartTime, threadId);
return new DisableTrace(traceRoot, spanRecorder, activeTraceHandle, uriStatStorage);
return new DisableTrace(traceRoot, spanRecorder, spanEventRecorder, activeTraceHandle, uriStatStorage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,59 @@ public class DisableAsyncChildTrace implements Trace {
private DefaultTraceScopePool scopePool;

private final LocalTraceRoot traceRoot;
private final LocalAsyncId localAsyncId;
private int depth;

public DisableAsyncChildTrace(final LocalTraceRoot traceRoot, final LocalAsyncId localAsyncId) {
private final SpanRecorder spanRecorder;
private final SpanEventRecorder spanEventRecorder;

public DisableAsyncChildTrace(final LocalTraceRoot traceRoot, SpanRecorder spanRecorder, SpanEventRecorder spanEventRecorder) {
this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");
this.localAsyncId = Objects.requireNonNull(localAsyncId, "localAsyncId");
this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder");
this.spanEventRecorder = Objects.requireNonNull(spanEventRecorder, "spanEventRecorder");
}

@Override
public SpanEventRecorder traceBlockBegin() {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
return traceBlockBegin(DEFAULT_STACKID);
}

@Override
public SpanEventRecorder traceBlockBegin(int stackId) {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
push();
return getSpanEventRecorder();
}

@Override
public void traceBlockEnd() {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
traceBlockBegin(DEFAULT_STACKID);
}


@Override
public void traceBlockEnd(int stackId) {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
pop();
}

private SpanEventRecorder getSpanEventRecorder() {
return spanEventRecorder;
}

private int push() {
return this.depth++;
}

private void pop() {
this.depth--;
}

@Override
public boolean isRootStack() {
throw new UnsupportedOperationException(UNSUPPORTED_OPERATION);
return depth == 0;
}

@Override
public int getCallStackFrameId() {
return 0;
return DEFAULT_STACKID;
}

private LocalTraceRoot getTraceRoot() {
Expand All @@ -82,7 +100,7 @@ public long getId() {

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

@Override
Expand All @@ -97,7 +115,7 @@ public boolean canSampled() {

@Override
public boolean isRoot() {
return this.getTraceId().isRoot();
return false;
}

@Override
Expand All @@ -107,12 +125,12 @@ public boolean isAsync() {

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

@Override
public SpanEventRecorder currentSpanEventRecorder() {
return null;
return spanEventRecorder;
}

@Override
Expand Down Expand Up @@ -148,8 +166,7 @@ public TraceScope addScope(String name) {
@Override
public String toString() {
return "DisableAsyncChildTrace{" +
"traceRoot=" + getTraceRoot() +
", localAsyncId=" + localAsyncId +
'}';
"traceRoot=" + traceRoot +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.navercorp.pinpoint.profiler.context;

import com.navercorp.pinpoint.bootstrap.context.AsyncContext;
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.profiler.context.id.LocalTraceRoot;

import java.util.Objects;

/**
* @author Woonduk Kang(emeroad)
*/
public class DisableAsyncContext implements AsyncContext {
private final LocalTraceRoot traceRoot;
private final AsyncTraceContext asyncTraceContext;

public DisableAsyncContext(LocalTraceRoot traceRoot, AsyncTraceContext asyncTraceContext) {
this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");
this.asyncTraceContext = Objects.requireNonNull(asyncTraceContext, "asyncTraceContext");
}

@Override
public Trace continueAsyncTraceObject() {
final Reference<Trace> reference = asyncTraceContext.currentRawTraceObject();
final Trace nestedTrace = reference.get();
if (nestedTrace != null) {
return nestedTrace;
}
return asyncTraceContext.continueDisableAsyncContextTraceObject(traceRoot);
}

@Override
public Trace currentAsyncTraceObject() {
final Reference<Trace> reference = asyncTraceContext.currentRawTraceObject();
return reference.get();
}

@Override
public void close() {
asyncTraceContext.removeTraceObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.navercorp.pinpoint.profiler.context;

import com.navercorp.pinpoint.bootstrap.context.AsyncState;

/**
* @author Woonduk Kang(emeroad)
*/
public class DisableAsyncState implements AsyncState {
@Override
public void setup() {

}

@Override
public boolean await() {
return false;
}

@Override
public void finish() {

}
}
Loading

0 comments on commit bbfd377

Please # to comment.