Refactored spymemcached (#67)

* Refactored spymemcached

* Fixed formatting issues

* Fixed formatting in an accidentally changed file

Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
This commit is contained in:
Pontus Rydin 2020-01-16 22:36:32 -05:00 committed by Trask Stalnaker
parent 1d474353a5
commit 736c70ffe0
6 changed files with 26 additions and 23 deletions

View File

@ -1,6 +1,6 @@
package io.opentelemetry.auto.instrumentation.spymemcached; package io.opentelemetry.auto.instrumentation.spymemcached;
import io.opentelemetry.auto.instrumentation.api.AgentSpan; import io.opentelemetry.trace.Span;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import net.spy.memcached.MemcachedConnection; import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.internal.BulkGetFuture; import net.spy.memcached.internal.BulkGetFuture;
@ -18,7 +18,7 @@ public class BulkGetCompletionListener extends CompletionListener<BulkGetFuture<
} }
@Override @Override
protected void processResult(final AgentSpan span, final BulkGetFuture<?> future) protected void processResult(final Span span, final BulkGetFuture<?> future)
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
/* /*
Note: for now we do not have an affective way of representing results of bulk operations, Note: for now we do not have an affective way of representing results of bulk operations,

View File

@ -1,11 +1,10 @@
package io.opentelemetry.auto.instrumentation.spymemcached; package io.opentelemetry.auto.instrumentation.spymemcached;
import static io.opentelemetry.auto.instrumentation.api.AgentTracer.activateSpan;
import static io.opentelemetry.auto.instrumentation.api.AgentTracer.startSpan;
import static io.opentelemetry.auto.instrumentation.spymemcached.MemcacheClientDecorator.DECORATE; import static io.opentelemetry.auto.instrumentation.spymemcached.MemcacheClientDecorator.DECORATE;
import static io.opentelemetry.auto.instrumentation.spymemcached.MemcacheClientDecorator.TRACER;
import io.opentelemetry.auto.instrumentation.api.AgentScope; import io.opentelemetry.context.Scope;
import io.opentelemetry.auto.instrumentation.api.AgentSpan; import io.opentelemetry.trace.Span;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -26,12 +25,12 @@ public abstract class CompletionListener<T> {
static final String MISS = "miss"; static final String MISS = "miss";
private final MemcachedConnection connection; private final MemcachedConnection connection;
private final AgentSpan span; private final Span span;
public CompletionListener(final MemcachedConnection connection, final String methodName) { public CompletionListener(final MemcachedConnection connection, final String methodName) {
this.connection = connection; this.connection = connection;
span = startSpan(OPERATION_NAME); span = TRACER.spanBuilder(OPERATION_NAME).startSpan();
try (final AgentScope scope = activateSpan(span, false)) { try (final Scope scope = TRACER.withSpan(span)) {
DECORATE.afterStart(span); DECORATE.afterStart(span);
DECORATE.onConnection(span, connection); DECORATE.onConnection(span, connection);
DECORATE.onOperation(span, methodName); DECORATE.onOperation(span, methodName);
@ -39,7 +38,7 @@ public abstract class CompletionListener<T> {
} }
protected void closeAsyncSpan(final T future) { protected void closeAsyncSpan(final T future) {
try (final AgentScope scope = activateSpan(span, false)) { try (final Scope scope = TRACER.withSpan(span)) {
try { try {
processResult(span, future); processResult(span, future);
} catch (final CancellationException e) { } catch (final CancellationException e) {
@ -61,23 +60,23 @@ public abstract class CompletionListener<T> {
DECORATE.onError(span, e); DECORATE.onError(span, e);
} finally { } finally {
DECORATE.beforeFinish(span); DECORATE.beforeFinish(span);
span.finish(); span.end();
} }
} }
} }
protected void closeSyncSpan(final Throwable thrown) { protected void closeSyncSpan(final Throwable thrown) {
try (final AgentScope scope = activateSpan(span, false)) { try (final Scope scope = TRACER.withSpan(span)) {
DECORATE.onError(span, thrown); DECORATE.onError(span, thrown);
DECORATE.beforeFinish(span); DECORATE.beforeFinish(span);
span.finish(); span.end();
} }
} }
protected abstract void processResult(AgentSpan span, T future) protected abstract void processResult(Span span, T future)
throws ExecutionException, InterruptedException; throws ExecutionException, InterruptedException;
protected void setResultTag(final AgentSpan span, final boolean hit) { protected void setResultTag(final Span span, final boolean hit) {
span.setAttribute(MEMCACHED_RESULT, hit ? HIT : MISS); span.setAttribute(MEMCACHED_RESULT, hit ? HIT : MISS);
} }
} }

View File

@ -1,6 +1,6 @@
package io.opentelemetry.auto.instrumentation.spymemcached; package io.opentelemetry.auto.instrumentation.spymemcached;
import io.opentelemetry.auto.instrumentation.api.AgentSpan; import io.opentelemetry.trace.Span;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import net.spy.memcached.MemcachedConnection; import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.internal.GetFuture; import net.spy.memcached.internal.GetFuture;
@ -17,7 +17,7 @@ public class GetCompletionListener extends CompletionListener<GetFuture<?>>
} }
@Override @Override
protected void processResult(final AgentSpan span, final GetFuture<?> future) protected void processResult(final Span span, final GetFuture<?> future)
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
final Object result = future.get(); final Object result = future.get();
setResultTag(span, result != null); setResultTag(span, result != null);

View File

@ -1,14 +1,18 @@
package io.opentelemetry.auto.instrumentation.spymemcached; package io.opentelemetry.auto.instrumentation.spymemcached;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.api.MoreTags; import io.opentelemetry.auto.api.MoreTags;
import io.opentelemetry.auto.api.SpanTypes; import io.opentelemetry.auto.api.SpanTypes;
import io.opentelemetry.auto.decorator.DatabaseClientDecorator; import io.opentelemetry.auto.decorator.DatabaseClientDecorator;
import io.opentelemetry.auto.instrumentation.api.AgentSpan; import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Tracer;
import net.spy.memcached.MemcachedConnection; import net.spy.memcached.MemcachedConnection;
public class MemcacheClientDecorator extends DatabaseClientDecorator<MemcachedConnection> { public class MemcacheClientDecorator extends DatabaseClientDecorator<MemcachedConnection> {
public static final MemcacheClientDecorator DECORATE = new MemcacheClientDecorator(); public static final MemcacheClientDecorator DECORATE = new MemcacheClientDecorator();
public static final Tracer TRACER = OpenTelemetry.getTracerFactory().get("io.opentelemetry.auto");
@Override @Override
protected String[] instrumentationNames() { protected String[] instrumentationNames() {
return new String[] {"spymemcached"}; return new String[] {"spymemcached"};
@ -44,7 +48,7 @@ public class MemcacheClientDecorator extends DatabaseClientDecorator<MemcachedCo
return null; return null;
} }
public AgentSpan onOperation(final AgentSpan span, final String methodName) { public Span onOperation(final Span span, final String methodName) {
final char[] chars = final char[] chars =
methodName methodName

View File

@ -1,6 +1,6 @@
package io.opentelemetry.auto.instrumentation.spymemcached; package io.opentelemetry.auto.instrumentation.spymemcached;
import io.opentelemetry.auto.instrumentation.api.AgentSpan; import io.opentelemetry.trace.Span;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import net.spy.memcached.MemcachedConnection; import net.spy.memcached.MemcachedConnection;
import net.spy.memcached.internal.OperationFuture; import net.spy.memcached.internal.OperationFuture;
@ -19,7 +19,7 @@ public class OperationCompletionListener
} }
@Override @Override
protected void processResult(final AgentSpan span, final OperationFuture<? extends Object> future) protected void processResult(final Span span, final OperationFuture<? extends Object> future)
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
future.get(); future.get();
} }

View File

@ -1,6 +1,6 @@
package io.opentelemetry.auto.instrumentation.spymemcached; package io.opentelemetry.auto.instrumentation.spymemcached;
import io.opentelemetry.auto.instrumentation.api.AgentSpan; import io.opentelemetry.trace.Span;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.spy.memcached.MemcachedConnection; import net.spy.memcached.MemcachedConnection;
@ -12,7 +12,7 @@ public class SyncCompletionListener extends CompletionListener<Void> {
} }
@Override @Override
protected void processResult(final AgentSpan span, final Void future) protected void processResult(final Span span, final Void future)
throws ExecutionException, InterruptedException { throws ExecutionException, InterruptedException {
log.error("processResult was called on SyncCompletionListener. This should never happen. "); log.error("processResult was called on SyncCompletionListener. This should never happen. ");
} }