My team generates some Java code using RestTemplate
s from an OpenAPI spec using the maven plugins for this. We have used both the swagger-codegen-maven-plugin
and openapi-generator-maven-plugin
which both seem to generate approximately the same code.
When it comes to collecting metrics from these RestTemplate
s using Micrometer, I find that the URIs aren't templated in the tags. For example, with a REST endpoint like /customers/{cid}
, I'd want the Micrometer metric to be tagged with uri=/customers/{cid}
. However, instead we find that the metric is tagged with uri=/customers/customer1
, uri=/customers/customer2
, etc. This effectively makes this tag useless for us.
Note from this question that if the RestTemplate
is allowed to do the templating itself, then the metric will be tagged in the way I want.
However, the generated code always looks like this:
String path = UriComponentsBuilder.fromPath("/customers/{cid}").buildAndExpand(uriVariables).toUriString();
ie, the templating is done before the RestTemplate
, and thus the metrics are tagged with the fully fleshed out URI.
Our configuration for the plugins looks like this. When using the openapi-generator version it also looks similar. I've tried playing around with a few of the configuration parameters to no avail.
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/my-spec.yml</inputSpec>
<language>java</language>
<generateApiTests>false</generateApiTests>
<generateModelTests>false</generateModelTests>
<configOptions>
<useOptional>true</useOptional>
<modelPackage>${my.groupId}.model</modelPackage>
<apiPackage>${my.groupId}.api</apiPackage>
<invokerPackage>${my.groupId}</invokerPackage>
<groupId>${my.groupId}</groupId>
<artifactId>api</artifactId>
<fullJavaUtil>false</fullJavaUtil>
<java8>true</java8>
<dateLibrary>java8-instant</dateLibrary>
</configOptions>
<library>resttemplate</library>
</configuration>
</execution>
</executions>
</plugin>
I've looked into the configuration properties for both the swagger-codegen plugin and the openapi-generator plugin and haven't found anything that seems to help with this. Does anyone know of a way to fix/improve this behaviour?
EDIT: After looking into it a bit more, the openapi-generator-maven-plugin
version of the code does seem to do something that I'd expect to use the RestTemplate
for url templating. However in the RestTemplateExchangeTagsProvider
the urlTemplate
is still coming through as null
.