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

Raise gRPC version floor to 1.6 and use new context bridge API #2948

Merged
merged 3 commits into from
May 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ muzzle {
pass {
group = "io.grpc"
module = "grpc-core"
versions = "[1.5.0,)"
versions = "[1.6.0,)"
assertInverse = true
}
}

def grpcVersion = '1.5.0'
def grpcVersion = '1.6.0'

dependencies {
implementation project(':instrumentation:grpc-1.5:library')
implementation project(':instrumentation:grpc-1.6:library')

library group: 'io.grpc', name: 'grpc-core', version: grpcVersion

Expand All @@ -36,7 +37,7 @@ dependencies {
testLibrary group: 'io.grpc', name: 'grpc-services', version: grpcVersion
testLibrary group: 'io.grpc', name: 'grpc-stub', version: grpcVersion

testImplementation project(':instrumentation:grpc-1.5:testing')
testImplementation project(':instrumentation:grpc-1.6:testing')
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.grpc.v1_5;
package io.opentelemetry.javaagent.instrumentation.grpc.v1_6;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.extendsClass;
import static io.opentelemetry.javaagent.extension.matcher.ClassLoaderMatcher.hasClassesNamed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.grpc.v1_5;
package io.opentelemetry.javaagent.instrumentation.grpc.v1_6;

import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isStatic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.grpc.v1_5;
package io.opentelemetry.javaagent.instrumentation.grpc.v1_6;

import static java.util.Arrays.asList;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.grpc.v1_5;
package io.opentelemetry.javaagent.instrumentation.grpc.v1_6;

import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.safeHasSuperType;
import static io.opentelemetry.javaagent.extension.matcher.ClassLoaderMatcher.hasClassesNamed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.grpc.v1_5;
package io.opentelemetry.javaagent.instrumentation.grpc.v1_6;

import io.grpc.ClientInterceptor;
import io.grpc.Context;
import io.grpc.ServerInterceptor;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.grpc.v1_5.GrpcTracing;
import io.opentelemetry.instrumentation.grpc.v1_5.internal.ContextStorageBridge;
import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTracing;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.ContextStorageBridge;

// Holds singleton references.
public final class GrpcSingletons {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.grpc.v1_5
package io.opentelemetry.javaagent.instrumentation.grpc.v1_6

import io.grpc.ManagedChannelBuilder
import io.grpc.ServerBuilder
import io.opentelemetry.instrumentation.grpc.v1_5.AbstractGrpcStreamingTest
import io.opentelemetry.instrumentation.grpc.v1_6.AbstractGrpcStreamingTest
import io.opentelemetry.instrumentation.test.AgentTestTrait

class GrpcStreamingTest extends AbstractGrpcStreamingTest implements AgentTestTrait {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.grpc.v1_5
package io.opentelemetry.javaagent.instrumentation.grpc.v1_6

import io.grpc.ManagedChannelBuilder
import io.grpc.ServerBuilder
import io.opentelemetry.instrumentation.grpc.v1_5.AbstractGrpcTest
import io.opentelemetry.instrumentation.grpc.v1_6.AbstractGrpcTest
import io.opentelemetry.instrumentation.test.AgentTestTrait

class GrpcTest extends AbstractGrpcTest implements AgentTestTrait {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
apply from: "$rootDir/gradle/instrumentation-library.gradle"

def grpcVersion = '1.5.0'
def grpcVersion = '1.6.0'

dependencies {
library group: 'io.grpc', name: 'grpc-core', version: grpcVersion
Expand All @@ -11,5 +11,5 @@ dependencies {
testLibrary group: 'io.grpc', name: 'grpc-stub', version: grpcVersion

testImplementation deps.assertj
testImplementation project(':instrumentation:grpc-1.5:testing')
testImplementation project(':instrumentation:grpc-1.6:testing')
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package io.grpc.override;

import io.grpc.Context;
import io.opentelemetry.instrumentation.grpc.v1_5.internal.ContextStorageBridge;
import io.opentelemetry.instrumentation.grpc.v1_6.internal.ContextStorageBridge;

/**
* Override class for gRPC to pick up this class to replace the default {@link Context.Storage} with
Expand All @@ -17,8 +17,8 @@ public final class ContextStorageOverride extends Context.Storage {
private static final Context.Storage delegate = new ContextStorageBridge();

@Override
public void attach(Context toAttach) {
delegate.attach(toAttach);
public Context doAttach(Context toAttach) {
return delegate.doAttach(toAttach);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import static io.opentelemetry.api.trace.SpanKind.CLIENT;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import io.grpc.Metadata;
import io.opentelemetry.context.propagation.TextMapGetter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import io.grpc.Status.Code;
import io.opentelemetry.api.common.AttributeKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import io.grpc.Metadata;
import io.opentelemetry.context.propagation.TextMapSetter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import static io.opentelemetry.api.trace.SpanKind.SERVER;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import io.grpc.ClientInterceptor;
import io.grpc.ServerInterceptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import io.opentelemetry.api.OpenTelemetry;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import static io.opentelemetry.instrumentation.grpc.v1_5.GrpcInjectAdapter.SETTER;
import static io.opentelemetry.instrumentation.grpc.v1_6.GrpcInjectAdapter.SETTER;

import io.grpc.CallOptions;
import io.grpc.Channel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import io.grpc.Contexts;
import io.grpc.ForwardingServerCall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5.internal;
package io.opentelemetry.instrumentation.grpc.v1_6.internal;

import io.grpc.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -27,30 +23,19 @@ public final class ContextStorageBridge extends Context.Storage {
private static final ContextKey<Context> GRPC_CONTEXT = ContextKey.named("grpc-context");
private static final Context.Key<io.opentelemetry.context.Context> OTEL_CONTEXT =
Context.key("otel-context");

// Because the extension point is void, there is no way to return information about the backing
// OpenTelemetry context when attaching gRPC context. So the only option is to have this
// side-channel to keep track of scopes. Because the same context can be attached to multiple
// threads, we must use a ThreadLocal here - on the bright side it means the map doesn't have to
// be concurrent. This will add an additional threadlocal lookup when attaching / detaching gRPC
// context, but not when accessing the current. In many applications, this means a small
// difference
// since those operations are rare, but in highly reactive applications where the overhead of
// ThreadLocal was already a problem, this makes it worse.
private static final ThreadLocal<WeakHashMap<Context, Deque<Scope>>> contextScopes =
ThreadLocal.withInitial(WeakHashMap::new);
private static final Context.Key<Scope> OTEL_SCOPE = Context.key("otel-scope");

@Override
public void attach(Context toAttach) {
public Context doAttach(Context toAttach) {
io.opentelemetry.context.Context otelContext = io.opentelemetry.context.Context.current();
Context current = otelContext.get(GRPC_CONTEXT);

if (current == null) {
current = Context.ROOT;
}

if (current == toAttach) {
contextScopes
.get()
.computeIfAbsent(toAttach, unused -> new ArrayDeque<>())
.addLast(Scope.noop());
return;
return current.withValue(OTEL_SCOPE, Scope.noop());
}

io.opentelemetry.context.Context base = OTEL_CONTEXT.get(toAttach);
Expand All @@ -69,7 +54,7 @@ public void attach(Context toAttach) {
}

Scope scope = newOtelContext.makeCurrent();
contextScopes.get().computeIfAbsent(toAttach, unused -> new ArrayDeque<>()).addLast(scope);
return current.withValue(OTEL_SCOPE, scope);
}

@Override
Expand All @@ -85,9 +70,8 @@ public void detach(Context toDetach, Context toRestore) {
"Context was not attached when detaching",
new Throwable().fillInStackTrace());
}
Map<Context, Deque<Scope>> contextStacks = contextScopes.get();
Deque<Scope> stack = contextStacks.get(toDetach);
Scope scope = stack.pollLast();

Scope scope = OTEL_SCOPE.get(toRestore);
if (scope == null) {
logger.log(
Level.SEVERE,
Expand All @@ -96,9 +80,6 @@ public void detach(Context toDetach, Context toRestore) {
} else {
scope.close();
}
if (stack.isEmpty()) {
contextStacks.remove(toDetach);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5
package io.opentelemetry.instrumentation.grpc.v1_6

import io.grpc.Status
import io.opentelemetry.api.trace.StatusCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5
package io.opentelemetry.instrumentation.grpc.v1_6

import io.grpc.ManagedChannelBuilder
import io.grpc.ServerBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5
package io.opentelemetry.instrumentation.grpc.v1_6

import io.grpc.ManagedChannelBuilder
import io.grpc.ServerBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5;
package io.opentelemetry.instrumentation.grpc.v1_6;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

apply from: "$rootDir/gradle/java.gradle"

def grpcVersion = '1.5.0'
def grpcVersion = '1.6.0'

protobuf {
protoc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5
package io.opentelemetry.instrumentation.grpc.v1_6

import static io.opentelemetry.api.trace.SpanKind.CLIENT
import static io.opentelemetry.api.trace.SpanKind.SERVER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.grpc.v1_5
package io.opentelemetry.instrumentation.grpc.v1_6

import static io.opentelemetry.api.trace.SpanKind.CLIENT
import static io.opentelemetry.api.trace.SpanKind.SERVER
Expand Down
6 changes: 3 additions & 3 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ include ':instrumentation:geode-1.4:javaagent'
include ':instrumentation:google-http-client-1.19:javaagent'
include ':instrumentation:grails-3.0:javaagent'
include ':instrumentation:grizzly-2.0:javaagent'
include ':instrumentation:grpc-1.5:javaagent'
include ':instrumentation:grpc-1.5:library'
include ':instrumentation:grpc-1.5:testing'
include ':instrumentation:grpc-1.6:javaagent'
include ':instrumentation:grpc-1.6:library'
include ':instrumentation:grpc-1.6:testing'
include ':instrumentation:guava-10.0:javaagent'
include ':instrumentation:guava-10.0:library'
include ':instrumentation:gwt-2.0:javaagent'
Expand Down