|
| 1 | +/* |
| 2 | + * Copyright 2013-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.sleuth.instrument.grpc; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +import io.grpc.ManagedChannelBuilder; |
| 23 | +import io.grpc.inprocess.InProcessChannelBuilder; |
| 24 | + |
| 25 | +/** |
| 26 | + * This is a Spring-aware managed channel builder that wraps the static entry points of |
| 27 | + * gRPC's ManagedChannelBuilder to allow the configuration of the builder to be influenced |
| 28 | + * by the Spring Context. All GrpcManagedChannelBuilderCustomizer instances included in |
| 29 | + * the application context will have the opportunity to customize the builder. |
| 30 | + * |
| 31 | + * NOTE: There is nothing "Sleuth-specific" about this, however, there is currently not a |
| 32 | + * good spring abstraction for client-side gRPC. Ideally, this could be moved up into |
| 33 | + * grpc-spring-boot-starter or a new project could be created |
| 34 | + * "spring-grpc"/"spring-cloud-grpc"? |
| 35 | + * |
| 36 | + * @author tyler.vangorder |
| 37 | + */ |
| 38 | +public class SpringAwareManagedChannelBuilder { |
| 39 | + |
| 40 | + private List<GrpcManagedChannelBuilderCustomizer> customizers; |
| 41 | + |
| 42 | + public SpringAwareManagedChannelBuilder( |
| 43 | + Optional<List<GrpcManagedChannelBuilderCustomizer>> customizers) { |
| 44 | + this.customizers = customizers.orElse(null); |
| 45 | + } |
| 46 | + |
| 47 | + public ManagedChannelBuilder<?> forAddress(String name, int port) { |
| 48 | + |
| 49 | + ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress(name, port); |
| 50 | + |
| 51 | + if (this.customizers != null) { |
| 52 | + this.customizers.stream() |
| 53 | + .forEach(customizer -> customizer.customize(builder)); |
| 54 | + } |
| 55 | + return builder; |
| 56 | + } |
| 57 | + |
| 58 | + public ManagedChannelBuilder<?> forTarget(String target) { |
| 59 | + ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forTarget(target); |
| 60 | + if (this.customizers != null) { |
| 61 | + this.customizers.stream() |
| 62 | + .forEach(customizer -> customizer.customize(builder)); |
| 63 | + } |
| 64 | + return builder; |
| 65 | + } |
| 66 | + |
| 67 | + public ManagedChannelBuilder<?> inProcessChannelBuilder(String serverName) { |
| 68 | + ManagedChannelBuilder<?> builder = InProcessChannelBuilder.forName(serverName); |
| 69 | + if (this.customizers != null) { |
| 70 | + this.customizers.stream() |
| 71 | + .forEach(customizer -> customizer.customize(builder)); |
| 72 | + } |
| 73 | + return builder; |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments