1 /* 2 * Copyright 2022 Google LLC 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 * https://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 com.google.cloud.trace.v2.stub; 18 19 import com.google.api.gax.core.BackgroundResource; 20 import com.google.api.gax.core.BackgroundResourceAggregation; 21 import com.google.api.gax.grpc.GrpcCallSettings; 22 import com.google.api.gax.grpc.GrpcStubCallableFactory; 23 import com.google.api.gax.rpc.ClientContext; 24 import com.google.api.gax.rpc.UnaryCallable; 25 import com.google.common.collect.ImmutableMap; 26 import com.google.devtools.cloudtrace.v2.BatchWriteSpansRequest; 27 import com.google.devtools.cloudtrace.v2.Span; 28 import com.google.longrunning.stub.GrpcOperationsStub; 29 import com.google.protobuf.Empty; 30 import io.grpc.MethodDescriptor; 31 import io.grpc.protobuf.ProtoUtils; 32 import java.io.IOException; 33 import java.util.concurrent.TimeUnit; 34 import javax.annotation.Generated; 35 36 // AUTO-GENERATED DOCUMENTATION AND CLASS. 37 /** 38 * gRPC stub implementation for the TraceService service API. 39 * 40 * <p>This class is for advanced usage and reflects the underlying API directly. 41 */ 42 @Generated("by gapic-generator-java") 43 public class GrpcTraceServiceStub extends TraceServiceStub { 44 private static final MethodDescriptor<BatchWriteSpansRequest, Empty> 45 batchWriteSpansMethodDescriptor = 46 MethodDescriptor.<BatchWriteSpansRequest, Empty>newBuilder() 47 .setType(MethodDescriptor.MethodType.UNARY) 48 .setFullMethodName("google.devtools.cloudtrace.v2.TraceService/BatchWriteSpans") 49 .setRequestMarshaller( 50 ProtoUtils.marshaller(BatchWriteSpansRequest.getDefaultInstance())) 51 .setResponseMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance())) 52 .build(); 53 54 private static final MethodDescriptor<Span, Span> createSpanMethodDescriptor = 55 MethodDescriptor.<Span, Span>newBuilder() 56 .setType(MethodDescriptor.MethodType.UNARY) 57 .setFullMethodName("google.devtools.cloudtrace.v2.TraceService/CreateSpan") 58 .setRequestMarshaller(ProtoUtils.marshaller(Span.getDefaultInstance())) 59 .setResponseMarshaller(ProtoUtils.marshaller(Span.getDefaultInstance())) 60 .build(); 61 62 private final UnaryCallable<BatchWriteSpansRequest, Empty> batchWriteSpansCallable; 63 private final UnaryCallable<Span, Span> createSpanCallable; 64 65 private final BackgroundResource backgroundResources; 66 private final GrpcOperationsStub operationsStub; 67 private final GrpcStubCallableFactory callableFactory; 68 create(TraceServiceStubSettings settings)69 public static final GrpcTraceServiceStub create(TraceServiceStubSettings settings) 70 throws IOException { 71 return new GrpcTraceServiceStub(settings, ClientContext.create(settings)); 72 } 73 create(ClientContext clientContext)74 public static final GrpcTraceServiceStub create(ClientContext clientContext) throws IOException { 75 return new GrpcTraceServiceStub(TraceServiceStubSettings.newBuilder().build(), clientContext); 76 } 77 create( ClientContext clientContext, GrpcStubCallableFactory callableFactory)78 public static final GrpcTraceServiceStub create( 79 ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException { 80 return new GrpcTraceServiceStub( 81 TraceServiceStubSettings.newBuilder().build(), clientContext, callableFactory); 82 } 83 84 /** 85 * Constructs an instance of GrpcTraceServiceStub, using the given settings. This is protected so 86 * that it is easy to make a subclass, but otherwise, the static factory methods should be 87 * preferred. 88 */ GrpcTraceServiceStub(TraceServiceStubSettings settings, ClientContext clientContext)89 protected GrpcTraceServiceStub(TraceServiceStubSettings settings, ClientContext clientContext) 90 throws IOException { 91 this(settings, clientContext, new GrpcTraceServiceCallableFactory()); 92 } 93 94 /** 95 * Constructs an instance of GrpcTraceServiceStub, using the given settings. This is protected so 96 * that it is easy to make a subclass, but otherwise, the static factory methods should be 97 * preferred. 98 */ GrpcTraceServiceStub( TraceServiceStubSettings settings, ClientContext clientContext, GrpcStubCallableFactory callableFactory)99 protected GrpcTraceServiceStub( 100 TraceServiceStubSettings settings, 101 ClientContext clientContext, 102 GrpcStubCallableFactory callableFactory) 103 throws IOException { 104 this.callableFactory = callableFactory; 105 this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory); 106 107 GrpcCallSettings<BatchWriteSpansRequest, Empty> batchWriteSpansTransportSettings = 108 GrpcCallSettings.<BatchWriteSpansRequest, Empty>newBuilder() 109 .setMethodDescriptor(batchWriteSpansMethodDescriptor) 110 .setParamsExtractor( 111 request -> { 112 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 113 params.put("name", String.valueOf(request.getName())); 114 return params.build(); 115 }) 116 .build(); 117 GrpcCallSettings<Span, Span> createSpanTransportSettings = 118 GrpcCallSettings.<Span, Span>newBuilder() 119 .setMethodDescriptor(createSpanMethodDescriptor) 120 .setParamsExtractor( 121 request -> { 122 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 123 params.put("name", String.valueOf(request.getName())); 124 return params.build(); 125 }) 126 .build(); 127 128 this.batchWriteSpansCallable = 129 callableFactory.createUnaryCallable( 130 batchWriteSpansTransportSettings, settings.batchWriteSpansSettings(), clientContext); 131 this.createSpanCallable = 132 callableFactory.createUnaryCallable( 133 createSpanTransportSettings, settings.createSpanSettings(), clientContext); 134 135 this.backgroundResources = 136 new BackgroundResourceAggregation(clientContext.getBackgroundResources()); 137 } 138 getOperationsStub()139 public GrpcOperationsStub getOperationsStub() { 140 return operationsStub; 141 } 142 143 @Override batchWriteSpansCallable()144 public UnaryCallable<BatchWriteSpansRequest, Empty> batchWriteSpansCallable() { 145 return batchWriteSpansCallable; 146 } 147 148 @Override createSpanCallable()149 public UnaryCallable<Span, Span> createSpanCallable() { 150 return createSpanCallable; 151 } 152 153 @Override close()154 public final void close() { 155 try { 156 backgroundResources.close(); 157 } catch (RuntimeException e) { 158 throw e; 159 } catch (Exception e) { 160 throw new IllegalStateException("Failed to close resource", e); 161 } 162 } 163 164 @Override shutdown()165 public void shutdown() { 166 backgroundResources.shutdown(); 167 } 168 169 @Override isShutdown()170 public boolean isShutdown() { 171 return backgroundResources.isShutdown(); 172 } 173 174 @Override isTerminated()175 public boolean isTerminated() { 176 return backgroundResources.isTerminated(); 177 } 178 179 @Override shutdownNow()180 public void shutdownNow() { 181 backgroundResources.shutdownNow(); 182 } 183 184 @Override awaitTermination(long duration, TimeUnit unit)185 public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { 186 return backgroundResources.awaitTermination(duration, unit); 187 } 188 } 189