A decorator wrapping Feign client method handlers in order to provide Dropwizard Metrics of calls to feign target interfaces.
Feign offers Metrics4Capability, which gives basic metrics, but in order to use metric annotations, add com.github.mwiede.metrics.feign.AnnotionMetricsCapability
like this:
@Timed
@Metered
@ExceptionMetered
@ResponseMetered
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
static class Contributor {
String login;
int contributions;
}
public static void main(final String... args) {
final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate("feign");
final ConsoleReporter reporter =
ConsoleReporter.forRegistry(metricRegistry).convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS).build();
final GitHub github =
Feign.builder().decoder(new GsonDecoder())
.addCapability(new AnnotionMetricsCapability(metricRegistry))
.target(GitHub.class, "https://api.github.com");
try {
// Fetch and print a list of the contributors to this library.
final List<Contributor> contributors = github.contributors("mwiede", "metrics-feign");
for (final Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
} finally {
reporter.report();
}
}
Basically you only have to replace Feign.builder()
with FeignWithMetrics.builder(metricRegistry)
.
@Timed
@Metered
@ExceptionMetered
@ResponseMetered
interface GitHub {
@RequestLine("GET /repos/{owner}/{repo}/contributors")
List<Contributor> contributors(@Param("owner") String owner, @Param("repo") String repo);
}
static class Contributor {
String login;
int contributions;
}
public static void main(final String... args) {
final MetricRegistry metricRegistry = new MetricRegistry();
final ConsoleReporter reporter =
ConsoleReporter.forRegistry(metricRegistry).convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS).build();
final GitHub github =
FeignWithMetrics.builder(metricRegistry).decoder(new GsonDecoder())
.target(GitHub.class, "https://api.github.com");
try {
// Fetch and print a list of the contributors to this library.
final List<Contributor> contributors = github.contributors("mwiede", "metrics-feign");
for (final Contributor contributor : contributors) {
System.out.println(contributor.login + " (" + contributor.contributions + ")");
}
} finally {
reporter.report();
}
}
Based of the example above, the following metrics were registered and reported:
- com.github.mwiede.metrics.example.Example$GitHub.contributors.1xx-responses
- com.github.mwiede.metrics.example.Example$GitHub.contributors.2xx-responses
- com.github.mwiede.metrics.example.Example$GitHub.contributors.3xx-responses
- com.github.mwiede.metrics.example.Example$GitHub.contributors.4xx-responses
- com.github.mwiede.metrics.example.Example$GitHub.contributors.5xx-responses
- com.github.mwiede.metrics.example.Example$GitHub.contributors.Metered
- com.github.mwiede.metrics.example.Example$GitHub.contributors.exceptions
- com.github.mwiede.metrics.example.Example$GitHub.contributors.reAttempts.Metered
- com.github.mwiede.metrics.example.Example$GitHub.contributors.retryExhausted.Metered
com.github.mwiede.metrics.example.Example$GitHub.contributors.Timed
You can use this library via maven:
<dependency>
<groupId>com.github.mwiede</groupId>
<artifactId>metrics-feign</artifactId>
<version>3.1</version>
</dependency>