Skip to content

Commit

Permalink
Further listener simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
mcculls committed Nov 13, 2020
1 parent 03509a9 commit 4ee0984
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,16 @@ public ElementMatcher<TypeDescription> typeMatcher() {
@Override
public String[] helperClassNames() {
return new String[] {
packageName + ".AerospikeClientDecorator", packageName + ".TracingListener",
packageName + ".AerospikeClientDecorator",
packageName + ".TracingListener",
packageName + ".TracingListener$1",
packageName + ".TracingListener$2",
packageName + ".TracingListener$3",
packageName + ".TracingListener$4",
packageName + ".TracingListener$5",
packageName + ".TracingListener$6",
packageName + ".TracingListener$7",
packageName + ".TracingListener$8",
};
}

Expand All @@ -57,7 +66,7 @@ public Map<? extends ElementMatcher<? super MethodDescription>, String> transfor
public static final class TraceSyncRequestAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope beginRequest(@Advice.Origin("#m") final String methodName) {
AgentSpan clientSpan = DECORATE.startAerospikeSpan(methodName);
final AgentSpan clientSpan = DECORATE.startAerospikeSpan(methodName);
return activateSpan(clientSpan);
}

Expand All @@ -74,8 +83,8 @@ public static final class TraceAsyncRequestAdvice {
public static AgentScope beginRequest(
@Advice.Origin("#m") final String methodName,
@Advice.Argument(value = 1, readOnly = false, typing = DYNAMIC) Object listener) {
AgentSpan clientSpan = DECORATE.startAerospikeSpan(methodName);
AgentScope scope = activateSpan(clientSpan);
final AgentSpan clientSpan = DECORATE.startAerospikeSpan(methodName);
final AgentScope scope = activateSpan(clientSpan);
// always want to wrap even when there's no listener so we get the true async time
listener = new TracingListener(clientSpan, scope.capture(), listener);
return scope;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public final class TracingListener
ExecuteListener,
DeleteListener {

protected final AgentSpan clientSpan;
protected final Continuation continuation;
protected final Object listener;
private final AgentSpan clientSpan;
private final Continuation continuation;
private final Object listener;

public TracingListener(
final AgentSpan clientSpan, final Continuation continuation, final Object listener) {
Expand Down Expand Up @@ -69,120 +69,109 @@ public void onRecord(final BatchRead record) {

@Override
public void onSuccess(final Key key, final boolean exists) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish();

if (listener != null) {
try (final TraceScope scope = continuation.activate()) {
if (listener instanceof ExistsListener) {
((ExistsListener) listener).onSuccess(key, exists);
} else if (listener instanceof DeleteListener) {
((DeleteListener) listener).onSuccess(key, exists);
}
}
} else {
continuation.cancel();
}
onSuccess(
new Runnable() {
@Override
public void run() {
if (listener instanceof ExistsListener) {
((ExistsListener) listener).onSuccess(key, exists);
} else if (listener instanceof DeleteListener) {
((DeleteListener) listener).onSuccess(key, exists);
}
}
});
}

@Override
public void onSuccess(final Key[] keys, final boolean[] exists) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish();

if (listener != null) {
try (final TraceScope scope = continuation.activate()) {
((ExistsArrayListener) listener).onSuccess(keys, exists);
}
} else {
continuation.cancel();
}
onSuccess(
new Runnable() {
@Override
public void run() {
((ExistsArrayListener) listener).onSuccess(keys, exists);
}
});
}

@Override
public void onSuccess(final Key key, final Record record) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish();

if (listener != null) {
try (final TraceScope scope = continuation.activate()) {
((RecordListener) listener).onSuccess(key, record);
}
} else {
continuation.cancel();
}
onSuccess(
new Runnable() {
@Override
public void run() {
((RecordListener) listener).onSuccess(key, record);
}
});
}

@Override
public void onSuccess(final Key[] keys, final Record[] records) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish();

if (listener != null) {
try (final TraceScope scope = continuation.activate()) {
((RecordArrayListener) listener).onSuccess(keys, records);
}
} else {
continuation.cancel();
}
onSuccess(
new Runnable() {
@Override
public void run() {
((RecordArrayListener) listener).onSuccess(keys, records);
}
});
}

@Override
public void onSuccess(final List<BatchRead> records) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish();

if (listener != null) {
try (final TraceScope scope = continuation.activate()) {
((BatchListListener) listener).onSuccess(records);
}
} else {
continuation.cancel();
}
onSuccess(
new Runnable() {
@Override
public void run() {
((BatchListListener) listener).onSuccess(records);
}
});
}

@Override
public void onSuccess(final Key key) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish();

if (listener != null) {
try (final TraceScope scope = continuation.activate()) {
((WriteListener) listener).onSuccess(key);
}
} else {
continuation.cancel();
}
onSuccess(
new Runnable() {
@Override
public void run() {
((WriteListener) listener).onSuccess(key);
}
});
}

@Override
public void onSuccess(final Key key, final Object obj) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish();

if (listener != null) {
try (final TraceScope scope = continuation.activate()) {
((ExecuteListener) listener).onSuccess(key, obj);
}
} else {
continuation.cancel();
}
onSuccess(
new Runnable() {
@Override
public void run() {
((ExecuteListener) listener).onSuccess(key, obj);
}
});
}

@Override
public void onSuccess() {
onSuccess(
new Runnable() {
@Override
public void run() {
if (listener instanceof ExistsSequenceListener) {
((ExistsSequenceListener) listener).onSuccess();
} else if (listener instanceof RecordSequenceListener) {
((RecordSequenceListener) listener).onSuccess();
} else if (listener instanceof BatchSequenceListener) {
((BatchSequenceListener) listener).onSuccess();
}
}
});
}

private void onSuccess(final Runnable runnable) {
DECORATE.beforeFinish(clientSpan);
clientSpan.finish();

if (listener != null) {
try (final TraceScope scope = continuation.activate()) {
if (listener instanceof ExistsSequenceListener) {
((ExistsSequenceListener) listener).onSuccess();
} else if (listener instanceof RecordSequenceListener) {
((RecordSequenceListener) listener).onSuccess();
} else if (listener instanceof BatchSequenceListener) {
((BatchSequenceListener) listener).onSuccess();
}
runnable.run();
}
} else {
continuation.cancel();
Expand Down

0 comments on commit 4ee0984

Please sign in to comment.