-
Notifications
You must be signed in to change notification settings - Fork 784
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt to add GRPC instrumentation (#1139)
* Adding preliminary GRPC instrumentation to Sleuth fixes #305
- Loading branch information
1 parent
a1832f6
commit 415a7a6
Showing
16 changed files
with
2,148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...org/springframework/cloud/sleuth/instrument/grpc/GrpcManagedChannelBuilderCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.sleuth.instrument.grpc; | ||
|
||
import io.grpc.ManagedChannelBuilder; | ||
|
||
/** | ||
* Callback interface that can be implemented by beans wishing to further customize the | ||
* {@link io.grpc.ManagedChannelBuilder} via the {@link SpringAwareManagedChannelBuilder}. | ||
* | ||
* @author tyler.vangorder | ||
*/ | ||
public interface GrpcManagedChannelBuilderCustomizer { | ||
|
||
void customize(ManagedChannelBuilder<?> managedChannelBuilder); | ||
|
||
} |
76 changes: 76 additions & 0 deletions
76
...va/org/springframework/cloud/sleuth/instrument/grpc/SpringAwareManagedChannelBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2013-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.sleuth.instrument.grpc; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import io.grpc.ManagedChannelBuilder; | ||
import io.grpc.inprocess.InProcessChannelBuilder; | ||
|
||
/** | ||
* This is a Spring-aware managed channel builder that wraps the static entry points of | ||
* gRPC's ManagedChannelBuilder to allow the configuration of the builder to be influenced | ||
* by the Spring Context. All GrpcManagedChannelBuilderCustomizer instances included in | ||
* the application context will have the opportunity to customize the builder. | ||
* | ||
* NOTE: There is nothing "Sleuth-specific" about this, however, there is currently not a | ||
* good spring abstraction for client-side gRPC. Ideally, this could be moved up into | ||
* grpc-spring-boot-starter or a new project could be created | ||
* "spring-grpc"/"spring-cloud-grpc"? | ||
This comment has been minimized.
Sorry, something went wrong. |
||
* | ||
* @author tyler.vangorder | ||
*/ | ||
public class SpringAwareManagedChannelBuilder { | ||
|
||
private List<GrpcManagedChannelBuilderCustomizer> customizers; | ||
|
||
public SpringAwareManagedChannelBuilder( | ||
Optional<List<GrpcManagedChannelBuilderCustomizer>> customizers) { | ||
this.customizers = customizers.orElse(null); | ||
} | ||
|
||
public ManagedChannelBuilder<?> forAddress(String name, int port) { | ||
|
||
ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(name, port); | ||
|
||
if (this.customizers != null) { | ||
this.customizers.stream() | ||
.forEach(customizer -> customizer.customize(builder)); | ||
} | ||
return builder; | ||
} | ||
|
||
public ManagedChannelBuilder<?> forTarget(String target) { | ||
ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forTarget(target); | ||
if (this.customizers != null) { | ||
this.customizers.stream() | ||
.forEach(customizer -> customizer.customize(builder)); | ||
} | ||
return builder; | ||
} | ||
|
||
public ManagedChannelBuilder<?> inProcessChannelBuilder(String serverName) { | ||
ManagedChannelBuilder<?> builder = InProcessChannelBuilder.forName(serverName); | ||
if (this.customizers != null) { | ||
this.customizers.stream() | ||
.forEach(customizer -> customizer.customize(builder)); | ||
} | ||
return builder; | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
...ain/java/org/springframework/cloud/sleuth/instrument/grpc/TraceGrpcAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2013-2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.sleuth.instrument.grpc; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import brave.Tracing; | ||
import brave.grpc.GrpcTracing; | ||
import io.grpc.ServerInterceptor; | ||
import org.lognet.springboot.grpc.GRpcGlobalInterceptor; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration | ||
* Auto-configuration} enables span information propagation when using GRPC. | ||
* | ||
* This configuration is only enabled when both grpc-spring-boot-starter and | ||
* brave-instrumentation-grpc are on the classpath. | ||
* | ||
* @author tyler.vangorder | ||
*/ | ||
@ConditionalOnClass({ GrpcTracing.class, GRpcGlobalInterceptor.class }) | ||
@ConditionalOnProperty(value = "spring.sleuth.grpc.enabled", matchIfMissing = true) | ||
@ConditionalOnBean(Tracing.class) | ||
public class TraceGrpcAutoConfiguration { | ||
|
||
@Bean | ||
public GrpcTracing grpcTracing(Tracing tracing) { | ||
return GrpcTracing.create(tracing); | ||
} | ||
|
||
// Register a global interceptor for both the server | ||
@Bean | ||
@GRpcGlobalInterceptor | ||
ServerInterceptor grpcServerBraveInterceptor(GrpcTracing grpcTracing) { | ||
return grpcTracing.newServerInterceptor(); | ||
} | ||
|
||
// This is wrapper around gRPC's managed channel builder that is spring-aware | ||
@Bean | ||
@ConditionalOnMissingBean(SpringAwareManagedChannelBuilder.class) | ||
public SpringAwareManagedChannelBuilder managedChannelBuilder( | ||
Optional<List<GrpcManagedChannelBuilderCustomizer>> customizers) { | ||
return new SpringAwareManagedChannelBuilder(customizers); | ||
} | ||
|
||
@Bean | ||
GrpcManagedChannelBuilderCustomizer tracingManagedChannelBuilderCustomizer( | ||
GrpcTracing grpcTracing) { | ||
return new TracingManagedChannelBuilderCustomizer(grpcTracing); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
.../springframework/cloud/sleuth/instrument/grpc/TracingManagedChannelBuilderCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2018 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.sleuth.instrument.grpc; | ||
|
||
import brave.grpc.GrpcTracing; | ||
import io.grpc.ManagedChannelBuilder; | ||
|
||
/** | ||
* @author tyler.vangorder | ||
*/ | ||
public class TracingManagedChannelBuilderCustomizer | ||
implements GrpcManagedChannelBuilderCustomizer { | ||
|
||
GrpcTracing grpcTracing; | ||
|
||
public TracingManagedChannelBuilderCustomizer(GrpcTracing grpcTracing) { | ||
this.grpcTracing = grpcTracing; | ||
} | ||
|
||
/** | ||
* Add brave's client interceptor to the builder. | ||
*/ | ||
@Override | ||
public void customize(ManagedChannelBuilder<?> managedChannelBuilder) { | ||
managedChannelBuilder.intercept(this.grpcTracing.newClientInterceptor()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
There is the yidongnan/grpc-spring-boot-starter project, which also supports the client-side of grpc. We have also considered joining the spring label, but don't know how to do so.
In that library there is already an
GrpcChannelConfigurer
for the same use case.We have support for sleuth build in natively, but we could move the few lines of code here as well.
GrpcCommonTraceAutoConfiguration
TraceClientAutoConfiguration
TraceServerAutoConfiguration
See also: #1209