17
17
18
18
import static javax .lang .model .element .Modifier .FINAL ;
19
19
import static javax .lang .model .element .Modifier .PRIVATE ;
20
+ import static javax .lang .model .element .Modifier .STATIC ;
20
21
import static software .amazon .awssdk .codegen .poet .client .ClientClassUtils .applyPaginatorUserAgentMethod ;
21
22
import static software .amazon .awssdk .codegen .poet .client .ClientClassUtils .applySignerOverrideMethod ;
22
23
23
24
import com .squareup .javapoet .ClassName ;
24
25
import com .squareup .javapoet .FieldSpec ;
25
26
import com .squareup .javapoet .MethodSpec ;
27
+ import com .squareup .javapoet .ParameterizedTypeName ;
26
28
import com .squareup .javapoet .TypeSpec ;
27
29
import com .squareup .javapoet .TypeSpec .Builder ;
28
30
import java .net .URI ;
29
31
import java .util .ArrayList ;
32
+ import java .util .Collections ;
30
33
import java .util .List ;
31
- import java .util .Optional ;
32
34
import java .util .stream .Collectors ;
33
35
import javax .lang .model .element .Modifier ;
34
36
import software .amazon .awssdk .annotations .SdkInternalApi ;
48
50
import software .amazon .awssdk .codegen .poet .client .specs .QueryProtocolSpec ;
49
51
import software .amazon .awssdk .codegen .poet .client .specs .XmlProtocolSpec ;
50
52
import software .amazon .awssdk .codegen .utils .PaginatorUtils ;
53
+ import software .amazon .awssdk .core .RequestOverrideConfiguration ;
51
54
import software .amazon .awssdk .core .client .config .SdkClientConfiguration ;
52
55
import software .amazon .awssdk .core .client .config .SdkClientOption ;
53
56
import software .amazon .awssdk .core .client .handler .SyncClientHandler ;
54
57
import software .amazon .awssdk .core .endpointdiscovery .EndpointDiscoveryRefreshCache ;
55
58
import software .amazon .awssdk .core .endpointdiscovery .EndpointDiscoveryRequest ;
56
- import software .amazon .awssdk .core .internal .util .MetricUtils ;
57
59
import software .amazon .awssdk .core .metrics .CoreMetric ;
58
60
import software .amazon .awssdk .metrics .MetricCollector ;
59
61
import software .amazon .awssdk .metrics .MetricPublisher ;
@@ -89,7 +91,8 @@ public TypeSpec poetSpec() {
89
91
.addMethod (constructor ())
90
92
.addMethod (nameMethod ())
91
93
.addMethods (protocolSpec .additionalMethods ())
92
- .addMethods (operations ());
94
+ .addMethods (operations ())
95
+ .addMethod (resolveMetricPublishersMethod ());
93
96
94
97
protocolSpec .createErrorResponseHandler ().ifPresent (classBuilder ::addMethod );
95
98
@@ -196,7 +199,7 @@ private List<MethodSpec> operationMethodSpecs(OperationModel opModel) {
196
199
method .addStatement ("$1T $2N = $1T.create($3S)" ,
197
200
MetricCollector .class , metricCollectorName , "ApiCall" );
198
201
199
- String publisherName = "metricPublisher " ;
202
+ String publishersName = "metricPublishers " ;
200
203
201
204
method .beginControlFlow ("try" )
202
205
.addStatement ("$N.reportMetric($T.$L, $S)" , metricCollectorName , CoreMetric .class , "SERVICE_ID" ,
@@ -206,13 +209,12 @@ private List<MethodSpec> operationMethodSpecs(OperationModel opModel) {
206
209
.addCode (protocolSpec .executionHandler (opModel ))
207
210
.endControlFlow ()
208
211
.beginControlFlow ("finally" )
209
- .addStatement ("$T<$T> $N = $T.resolvePublisher (clientConfiguration, $N)" ,
210
- Optional .class ,
212
+ .addStatement ("$T<$T> $N = resolveMetricPublishers (clientConfiguration, $N.overrideConfiguration().orElse(null) )" ,
213
+ List .class ,
211
214
MetricPublisher .class ,
212
- publisherName ,
213
- MetricUtils .class ,
215
+ publishersName ,
214
216
opModel .getInput ().getVariableName ())
215
- .addStatement ("$N.ifPresent (p -> p.publish($N.collect()))" , publisherName , metricCollectorName )
217
+ .addStatement ("$N.forEach (p -> p.publish($N.collect()))" , publishersName , metricCollectorName )
216
218
.endControlFlow ();
217
219
218
220
methods .add (method .build ());
@@ -286,4 +288,39 @@ static ProtocolSpec getProtocolSpecs(PoetExtensions poetExtensions, Intermediate
286
288
throw new RuntimeException ("Unknown protocol: " + protocol .name ());
287
289
}
288
290
}
291
+
292
+ private MethodSpec resolveMetricPublishersMethod () {
293
+ String clientConfigName = "clientConfiguration" ;
294
+ String requestOverrideConfigName = "requestOverrideConfiguration" ;
295
+
296
+ MethodSpec .Builder methodBuilder = MethodSpec .methodBuilder ("resolveMetricPublishers" )
297
+ .addModifiers (PRIVATE , STATIC )
298
+ .returns (ParameterizedTypeName .get (List .class , MetricPublisher .class ))
299
+ .addParameter (SdkClientConfiguration .class , clientConfigName )
300
+ .addParameter (RequestOverrideConfiguration .class , requestOverrideConfigName );
301
+
302
+ String publishersName = "publishers" ;
303
+
304
+ methodBuilder .addStatement ("$T $N = null" , ParameterizedTypeName .get (List .class , MetricPublisher .class ), publishersName );
305
+
306
+ methodBuilder .beginControlFlow ("if ($N != null)" , requestOverrideConfigName )
307
+ .addStatement ("$N = $N.metricPublishers()" , publishersName , requestOverrideConfigName )
308
+ .endControlFlow ();
309
+
310
+ methodBuilder .beginControlFlow ("if ($1N == null || $1N.isEmpty())" , publishersName )
311
+ .addStatement ("$N = $N.option($T.$N)" ,
312
+ publishersName ,
313
+ clientConfigName ,
314
+ SdkClientOption .class ,
315
+ "METRIC_PUBLISHERS" )
316
+ .endControlFlow ();
317
+
318
+ methodBuilder .beginControlFlow ("if ($1N == null)" , publishersName )
319
+ .addStatement ("$N = $T.emptyList()" , publishersName , Collections .class )
320
+ .endControlFlow ();
321
+
322
+ methodBuilder .addStatement ("return $N" , publishersName );
323
+
324
+ return methodBuilder .build ();
325
+ }
289
326
}
0 commit comments