Skip to content

Commit

Permalink
Spymemcached: add shouldStart checks (#4358)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Oct 12, 2021
1 parent 3a90673 commit 57146b3
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@

package io.opentelemetry.javaagent.instrumentation.spymemcached;

import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import java.util.concurrent.ExecutionException;
import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.internal.BulkGetFuture;
import org.checkerframework.checker.nullness.qual.Nullable;

public class BulkGetCompletionListener extends CompletionListener<BulkGetFuture<?>>
implements net.spy.memcached.internal.BulkGetCompletionListener {

public BulkGetCompletionListener(
private BulkGetCompletionListener(Context parentContext, SpymemcachedRequest request) {
super(parentContext, request);
}

@Nullable
public static BulkGetCompletionListener create(
Context parentContext, MemcachedConnection connection, String methodName) {
super(parentContext, connection, methodName);
SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName);
if (!instrumenter().shouldStart(parentContext, request)) {
return null;
}
return new BulkGetCompletionListener(parentContext, request);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import io.opentelemetry.instrumentation.api.config.Config;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import net.spy.memcached.MemcachedConnection;

public abstract class CompletionListener<T> {

Expand All @@ -28,9 +27,8 @@ public abstract class CompletionListener<T> {
private final Context context;
private final SpymemcachedRequest request;

protected CompletionListener(
Context parentContext, MemcachedConnection connection, String methodName) {
request = SpymemcachedRequest.create(connection, methodName);
protected CompletionListener(Context parentContext, SpymemcachedRequest request) {
this.request = request;
context = instrumenter().start(parentContext, request);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@

package io.opentelemetry.javaagent.instrumentation.spymemcached;

import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import java.util.concurrent.ExecutionException;
import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.internal.GetFuture;
import org.checkerframework.checker.nullness.qual.Nullable;

public class GetCompletionListener extends CompletionListener<GetFuture<?>>
implements net.spy.memcached.internal.GetCompletionListener {
public GetCompletionListener(

private GetCompletionListener(Context parentContext, SpymemcachedRequest request) {
super(parentContext, request);
}

@Nullable
public static GetCompletionListener create(
Context parentContext, MemcachedConnection connection, String methodName) {
super(parentContext, connection, methodName);
SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName);
if (!instrumenter().shouldStart(parentContext, request)) {
return null;
}
return new GetCompletionListener(parentContext, request);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ public static void methodExit(

if (future != null) {
OperationCompletionListener listener =
new OperationCompletionListener(currentContext(), client.getConnection(), methodName);
future.addListener(listener);
OperationCompletionListener.create(
currentContext(), client.getConnection(), methodName);
if (listener != null) {
future.addListener(listener);
}
}
}
}
Expand All @@ -99,8 +102,10 @@ public static void methodExit(

if (future != null) {
GetCompletionListener listener =
new GetCompletionListener(currentContext(), client.getConnection(), methodName);
future.addListener(listener);
GetCompletionListener.create(currentContext(), client.getConnection(), methodName);
if (listener != null) {
future.addListener(listener);
}
}
}
}
Expand All @@ -126,8 +131,10 @@ public static void methodExit(

if (future != null) {
BulkGetCompletionListener listener =
new BulkGetCompletionListener(currentContext(), client.getConnection(), methodName);
future.addListener(listener);
BulkGetCompletionListener.create(currentContext(), client.getConnection(), methodName);
if (listener != null) {
future.addListener(listener);
}
}
}
}
Expand All @@ -145,15 +152,15 @@ public static SyncCompletionListener methodEnter(
return null;
}

return new SyncCompletionListener(currentContext(), client.getConnection(), methodName);
return SyncCompletionListener.create(currentContext(), client.getConnection(), methodName);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter SyncCompletionListener listener,
@Advice.Thrown Throwable thrown,
@Advice.Local("otelCallDepth") CallDepth callDepth) {
if (callDepth.decrementAndGet() > 0) {
if (callDepth.decrementAndGet() > 0 || listener == null) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,30 @@

package io.opentelemetry.javaagent.instrumentation.spymemcached;

import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import java.util.concurrent.ExecutionException;
import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.internal.OperationFuture;
import org.checkerframework.checker.nullness.qual.Nullable;

public class OperationCompletionListener extends CompletionListener<OperationFuture<?>>
implements net.spy.memcached.internal.OperationCompletionListener {
public OperationCompletionListener(

private OperationCompletionListener(Context parentContext, SpymemcachedRequest request) {
super(parentContext, request);
}

@Nullable
public static OperationCompletionListener create(
Context parentContext, MemcachedConnection connection, String methodName) {
super(parentContext, connection, methodName);
SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName);
if (!instrumenter().shouldStart(parentContext, request)) {
return null;
}
return new OperationCompletionListener(parentContext, request);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@

package io.opentelemetry.javaagent.instrumentation.spymemcached;

import static io.opentelemetry.javaagent.instrumentation.spymemcached.SpymemcachedSingletons.instrumenter;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import net.spy.memcached.MemcachedConnection;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SyncCompletionListener extends CompletionListener<Void> {

private static final Logger logger = LoggerFactory.getLogger(SyncCompletionListener.class);

public SyncCompletionListener(
private SyncCompletionListener(Context parentContext, SpymemcachedRequest request) {
super(parentContext, request);
}

@Nullable
public static SyncCompletionListener create(
Context parentContext, MemcachedConnection connection, String methodName) {
super(parentContext, connection, methodName);
SpymemcachedRequest request = SpymemcachedRequest.create(connection, methodName);
if (!instrumenter().shouldStart(parentContext, request)) {
return null;
}
return new SyncCompletionListener(parentContext, request);
}

@Override
Expand Down

0 comments on commit 57146b3

Please sign in to comment.