Skip to content

Commit

Permalink
[pinpoint-apm#9575] Polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Jan 6, 2023
1 parent dc60c05 commit 01e7af3
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.common.annotations.VisibleForTesting;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.exception.PinpointException;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder;
Expand Down Expand Up @@ -53,13 +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, boolean sampling,
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");
this.storage = Objects.requireNonNull(storage, "storage");
Assert.isTrue(sampling, "sampling must be true");

this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder");
this.wrappedSpanEventRecorder = Objects.requireNonNull(wrappedSpanEventRecorder, "wrappedSpanEventRecorder");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.navercorp.pinpoint.profiler.context;

import com.navercorp.pinpoint.bootstrap.context.AsyncState;
import com.navercorp.pinpoint.bootstrap.context.SpanRecorder;
import com.navercorp.pinpoint.profiler.context.recorder.WrappedSpanEventRecorder;
import com.navercorp.pinpoint.profiler.context.storage.Storage;

import java.util.Objects;

public class AsyncDefaultTrace extends DefaultTrace {
private final AsyncState asyncState;

public AsyncDefaultTrace(Span span,
CallStack<SpanEvent> callStack,
Storage storage,
SpanRecorder spanRecorder,
WrappedSpanEventRecorder wrappedSpanEventRecorder,
AsyncState asyncState) {
super(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, null, null);
this.asyncState = Objects.requireNonNull(asyncState, "asyncState");
}

@Override
public void close() {
if (asyncState.await()) {
// flush.
super.flush();
if (isDebug) {
logger.debug("Flush trace={}, asyncState={}", this, this.asyncState);
}
} else {
// close.
super.close();
if (isDebug) {
logger.debug("Close trace={}. asyncState={}", this, this.asyncState);
}
}
}

@Override
public String toString() {
return "AsyncDefaultTrace{" +
"asyncState=" + asyncState +
"} " + super.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.navercorp.pinpoint.bootstrap.context.Trace;
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.profiler.context.id.TraceRoot;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -32,32 +31,30 @@ public class AsyncTrace implements Trace {
private static final Logger logger = LogManager.getLogger(AsyncTrace.class.getName());
private static final boolean isDebug = logger.isDebugEnabled();

private final TraceRoot traceRoot;
private final DefaultTrace trace;

private final AsyncState asyncState;

public AsyncTrace(final TraceRoot traceRoot, final DefaultTrace trace, final AsyncState asyncState) {
this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");
public AsyncTrace(final DefaultTrace trace, final AsyncState asyncState) {
this.trace = Objects.requireNonNull(trace, "trace");
this.asyncState = Objects.requireNonNull(asyncState, "asyncState");
}


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

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


@Override
public TraceId getTraceId() {
return this.traceRoot.getTraceId();
return trace.getTraceId();
}

@Override
Expand All @@ -67,7 +64,7 @@ public boolean canSampled() {

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

@Override
Expand Down Expand Up @@ -152,7 +149,6 @@ public TraceScope addScope(String name) {
@Override
public String toString() {
return "AsyncTrace{" +
"traceRoot=" + traceRoot +
", trace=" + trace +
", asyncState=" + asyncState +
'}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,18 @@ 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();
final boolean sampling = state.isSampled();
if (sampling) {
if (state.isSampled()) {
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 SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), sampling);
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);
final ActiveTraceHandle handle = registerActiveTrace(traceRoot);

return new DefaultTrace(span, callStack, storage, sampling, spanRecorder,
return new DefaultTrace(span, callStack, storage, spanRecorder,
wrappedSpanEventRecorder, handle, uriStatStorage);
} else {
return newLocalTrace(state.nextId());
Expand Down Expand Up @@ -125,21 +124,19 @@ public Trace newTraceObject(String urlPath) {
}

Trace newTraceObject(TraceSampler.State state) {
final boolean sampling = state.isSampled();
if (sampling) {
if (state.isSampled()) {
final TraceRoot traceRoot = traceRootFactory.newTraceRoot(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 TraceId traceId = traceRoot.getTraceId();
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), sampling);
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);

final ActiveTraceHandle handle = registerActiveTrace(traceRoot);

return new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder,
return new DefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder,
handle, uriStatStorage);
} else {
return newLocalTrace(state.nextId());
Expand All @@ -155,11 +152,11 @@ public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId l

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

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

final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot);

return new AsyncChildTrace(traceRoot, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder, localAsyncId);
return new AsyncChildTrace(traceRoot, callStack, storage, spanRecorder, wrappedSpanEventRecorder, localAsyncId);
} else {
return new DisableAsyncChildTrace(traceRoot, localAsyncId);
}
Expand All @@ -171,8 +168,7 @@ public Trace continueAsyncContextTraceObject(TraceRoot traceRoot, LocalAsyncId l
@Override
public Trace continueAsyncTraceObject(final TraceId traceId) {
final TraceSampler.State state = traceSampler.isContinueSampled();
final boolean sampling = state.isSampled();
if (sampling) {
if (state.isSampled()) {
final TraceRoot traceRoot = traceRootFactory.continueTraceRoot(traceId, state.nextId());
final Span span = spanFactory.newSpan(traceRoot);

Expand All @@ -184,13 +180,10 @@ public Trace continueAsyncTraceObject(final TraceId traceId) {
final SpanAsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory);
final AsyncState asyncState = new ListenableAsyncState(traceRoot, asyncStateListener, handle, uriStatStorage);

final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), sampling);
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot, asyncState);


final DefaultTrace trace = new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder);

return new AsyncTrace(traceRoot, trace, asyncState);
return new AsyncDefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, asyncState);
} else {
return newLocalTrace(state.nextId());
}
Expand All @@ -211,8 +204,7 @@ public Trace newAsyncTraceObject(String urlPath) {
}

Trace newAsyncTraceObject(TraceSampler.State state) {
final boolean sampling = state.isSampled();
if (sampling) {
if (state.isSampled()) {
final TraceRoot traceRoot = traceRootFactory.newTraceRoot(state.nextId());
final Span span = spanFactory.newSpan(traceRoot);
final SpanChunkFactory spanChunkFactory = new DefaultSpanChunkFactory(traceRoot);
Expand All @@ -223,15 +215,10 @@ Trace newAsyncTraceObject(TraceSampler.State state) {
final SpanAsyncStateListener asyncStateListener = new SpanAsyncStateListener(span, storageFactory);
final AsyncState asyncState = new ListenableAsyncState(traceRoot, asyncStateListener, handle, uriStatStorage);


final TraceId traceId = traceRoot.getTraceId();
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span, traceId.isRoot(), sampling);
final SpanRecorder spanRecorder = recorderFactory.newSpanRecorder(span);
final WrappedSpanEventRecorder wrappedSpanEventRecorder = recorderFactory.newWrappedSpanEventRecorder(traceRoot, asyncState);


final DefaultTrace trace = new DefaultTrace(span, callStack, storage, sampling, spanRecorder, wrappedSpanEventRecorder);

return new AsyncTrace(traceRoot, trace, asyncState);
return new AsyncDefaultTrace(span, callStack, storage, spanRecorder, wrappedSpanEventRecorder, asyncState);
} else {
return newLocalTrace(state.nextId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.navercorp.pinpoint.bootstrap.context.TraceId;
import com.navercorp.pinpoint.bootstrap.context.scope.TraceScope;
import com.navercorp.pinpoint.common.annotations.VisibleForTesting;
import com.navercorp.pinpoint.common.util.Assert;
import com.navercorp.pinpoint.exception.PinpointException;
import com.navercorp.pinpoint.profiler.context.active.ActiveTraceHandle;
import com.navercorp.pinpoint.profiler.context.id.Shared;
Expand All @@ -42,10 +41,10 @@
* @author emeroad
* @author jaehong.kim
*/
public final class DefaultTrace implements Trace {
public class DefaultTrace implements Trace {

private static final Logger logger = LogManager.getLogger(DefaultTrace.class.getName());
private static final boolean isDebug = logger.isDebugEnabled();
static final Logger logger = LogManager.getLogger(DefaultTrace.class.getName());
static final boolean isDebug = logger.isDebugEnabled();

private final CallStack<SpanEvent> callStack;

Expand All @@ -64,20 +63,19 @@ public final class DefaultTrace implements Trace {
@Nullable
private final UriStatStorage uriStatStorage;

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

public DefaultTrace(Span span, CallStack<SpanEvent> callStack, Storage storage, boolean sampling,
public DefaultTrace(Span span, CallStack<SpanEvent> callStack, Storage storage,
SpanRecorder spanRecorder, WrappedSpanEventRecorder wrappedSpanEventRecorder,
ActiveTraceHandle activeTraceHandle,
UriStatStorage uriStatStorage) {

this.span = Objects.requireNonNull(span, "span");
this.callStack = Objects.requireNonNull(callStack, "callStack");
this.storage = Objects.requireNonNull(storage, "storage");
Assert.isTrue(sampling, "sampling must be true");

this.spanRecorder = Objects.requireNonNull(spanRecorder, "spanRecorder");
this.wrappedSpanEventRecorder = Objects.requireNonNull(wrappedSpanEventRecorder, "wrappedSpanEventRecorder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public DefaultRecorderFactory(Provider<AsyncContextFactory> asyncContextFactoryP
}

@Override
public SpanRecorder newSpanRecorder(Span span, boolean isRoot, boolean sampling) {
return new DefaultSpanRecorder(span, isRoot, sampling, stringMetaDataService, sqlMetaDataService, errorHandler);
public SpanRecorder newSpanRecorder(Span span) {
return new DefaultSpanRecorder(span, stringMetaDataService, sqlMetaDataService, errorHandler);
}

@Override
public SpanRecorder newTraceRootSpanRecorder(TraceRoot traceRoot, boolean sampling) {
return new TraceRootSpanRecorder(traceRoot, sampling);
public SpanRecorder newTraceRootSpanRecorder(TraceRoot traceRoot) {
return new TraceRootSpanRecorder(traceRoot);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,24 @@
import org.apache.logging.log4j.Logger;

/**
*
*
* @author jaehong.kim
*
*/
public class DefaultSpanRecorder extends AbstractRecorder implements SpanRecorder {
private static final Logger logger = LogManager.getLogger(DefaultTrace.class.getName());
private static final boolean isDebug = logger.isDebugEnabled();

private final Span span;
private final boolean isRoot;
private final boolean sampling;

public DefaultSpanRecorder(final Span span, final boolean isRoot, final boolean sampling,
final StringMetaDataService stringMetaDataService, SqlMetaDataService sqlMetaDataService,

public DefaultSpanRecorder(final Span span,
final StringMetaDataService stringMetaDataService,
final SqlMetaDataService sqlMetaDataService,
final IgnoreErrorHandler errorHandler) {
super(stringMetaDataService, sqlMetaDataService, errorHandler);
this.span = span;
this.isRoot = isRoot;
this.sampling = sampling;
}

public Span getSpan() {
return span;
}

@Override
public void recordStartTime(long startTime) {
Expand Down Expand Up @@ -124,12 +118,12 @@ public void recordAcceptorHost(String host) {

@Override
public boolean canSampled() {
return sampling;
return true;
}

@Override
public boolean isRoot() {
return isRoot;
return span.getTraceRoot().getTraceId().isRoot();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
*/
public interface RecorderFactory {

SpanRecorder newSpanRecorder(final Span span, final boolean isRoot, final boolean sampling);
SpanRecorder newSpanRecorder(final Span span);

SpanRecorder newTraceRootSpanRecorder(TraceRoot traceRoot, boolean sampling);
SpanRecorder newTraceRootSpanRecorder(TraceRoot traceRoot);

SpanRecorder newDisableSpanRecorder(LocalTraceRoot traceRoot);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@
public class TraceRootSpanRecorder implements SpanRecorder {

private final TraceRoot traceRoot;
private final boolean sampling;

public TraceRootSpanRecorder(TraceRoot traceRoot, boolean sampling) {
public TraceRootSpanRecorder(TraceRoot traceRoot) {
this.traceRoot = Objects.requireNonNull(traceRoot, "traceRoot");

this.sampling = sampling;
}

@Override
public boolean canSampled() {
return sampling;
return true;
}

@Override
Expand Down
Loading

0 comments on commit 01e7af3

Please sign in to comment.