Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for AutoFinishScope #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jam01
Copy link
Contributor

@jam01 jam01 commented Sep 14, 2018

Adds support for AutoFinishScope by checking the type of ScopeManager and using a AutoFinishScope.Continuation when appropriate. Includes some tests.


public TracedCallable(Callable<V> delegate, Tracer tracer) {
this.delegate = delegate;
this.tracer = tracer;
this.span = tracer.activeSpan();

if (tracer.scopeManager() instanceof AutoFinishScopeManager && tracer.scopeManager().active() != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add braces in the if/else constructs here and following classes.


public TracedRunnable(Runnable delegate, Tracer tracer) {
this.delegate = delegate;
this.tracer = tracer;
this.span = tracer.activeSpan();

if (tracer.scopeManager() instanceof AutoFinishScopeManager && tracer.scopeManager().active() != null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation problem with this if/else construct.

}

@Override
public void run() {
Scope scope = span == null ? null : tracer.scopeManager().activate(span, false);
try {
Scope scope = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation problem with this code block.

import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand the imports.

/**
* @author Jose Montoya
*/
public class TracedAutoFinishTest {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should derive from the AbstractConcurrentTest and make use TestRunnable/Callable and latch mechanism, rather than use awaitility.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was going to, but I needed a Thread.sleep within the runnable/callable to make sure that I could check the current thread's span and scope were close before the runnable returned. I didn't want to modify the existing TestRunnable, even though the original tests should still pass. If you're OK with me making those changes then I'll go ahead and do that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could introduce a semaphore in the abstract test runnable/callable impls to allow the test to block their execution.

@sjoerdtalsma
Copy link
Collaborator

Since your AutoFinishScope behaviour is optional, could you please make sure not to introduce new runtime dependencies (such as opentracing-util from test to provided instead of runtime) ?

You will have to take care of some gotcha's to make this work:

  1. Don't import any optional classes but use their fully qualified name (otherwise the import will throw linkage error when your class is loaded instead of when the optional class is actually needed)
  2. Catch and anticipate a linkage error (usually NoClassDefFoundError)

<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the java version bump?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try with resources in a test. I suppose that does not merit the change. I'll revert these.

@@ -99,7 +106,6 @@
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
<scope>test</scope>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this provided and optional=true to avoid unnecessary transitive dependencies

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would result in java.lang.NoClassDefFoundError.

Copy link
Collaborator

@sjoerdtalsma sjoerdtalsma Sep 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would result in java.lang.NoClassDefFoundError.

Currently, yes.
However, as I explained in #12 (comment), the util usage is entirely optional (based on an instanceof check, which can only happen if the application already added the util lib to the classpath).
Therefore, catching the NoClassDefFoundError and interpreting it as "probably isn't an instance of AutoFinishScopeManager" is valid logic in my opinion.

Libraries should not declare force transitive dependencies if they're optional.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be quite expensive to have it in runnable/callable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (tracer.scopeManager() instanceof AutoFinishScopeManager && tracer.scopeManager().active() != null)
       cont = ((AutoFinishScope) tracer.scopeManager().active()).capture();
    else
      cont = null;

Could be replaced by:

if (hasAutoFinishScopeManager(tracer) && tracer.scopeManager().active() != null) {
    cont = ((io.opentracing.util.AutoFinishScope) tracer.scopeManager().active()).capture();
}
...
private static boolean hasAutoFinishScopeManager(Tracer tracer) {
    try {
        return tracer.scopeManager() instanceof io.opentracing.util.AutoFinishScopeManager;
    } catch (NoClassDefFoundError utilityJarNotLoaded) {
        LOGGER.finest("The opentracing-util library is not available, therefore the ScopeManager cannot be the AutoFinishScopeManager.");
        return false;
    }
}

Just make sure not to import io.opentrating.util classes..

I agree this borders on being too far-fetched, just pointing out the possibilities.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better way would be to wrap all auto-finish related coded in its own (package protected) class and only access that if the hasAutoFinish.. test returned true.

Copy link
Collaborator

@sjoerdtalsma sjoerdtalsma Sep 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be quite expensive to have it in runnable/callable.

Ok, then create a boolean constant and populate it in a static code block when the class is loaded..

For inspiration, you could look at PriorityComparator (optional use of javax.annotation.Priority) or TracerResolver (optional use of io.opentracing.util.GlobalTracer)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have consensus on the last proposal, static boolean. Then I don't mind adding that.

@jpkrohling
Copy link

What's the status of this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants