@StaticMetricsProxy

Generate a Dropwizard-timed wrapper for an interface at compile time.

@StaticMetricsProxy generates a wrapper class that records method timings through Dropwizard Metrics and delegates to the real implementation.

Usage

import com.svenruppert.proxybuilder.proxy.generated.annotations.StaticMetricsProxy;

@StaticMetricsProxy
public interface Service {
  String work(String input);
}

After compilation, use the generated class:

Service proxy = new ServiceStaticMetricsProxy()
    .withDelegator(new ServiceImpl());

Every call to a method on proxy is now timed.

Where the metrics live

Timings are recorded against RapidPMMetricsRegistry, the same registry used by the runtime .addMetrics() builder call. A JMX or console reporter attached to that registry sees both runtime-proxy and static-proxy metrics in the same place.

What is recorded

For each method invocation through the proxy:

  • A Dropwizard Timer is started.
  • The real implementation is called.
  • The timer stops when the call returns or throws.

The recorded duration is just the implementation. There is no per-proxy configuration to skip particular methods — every method on the interface is timed.

Generated class shape

public class ServiceStaticMetricsProxy implements Service {
  public ServiceStaticMetricsProxy() { /* … */ }
  public ServiceStaticMetricsProxy withDelegator(Service delegator) { /* … */ return this; }

  @Override
  public String work(String input) {
    // start timer
    String result = delegator.work(input);
    // stop timer
    return result;
  }
}

When to prefer this over the runtime version

  • You need metrics on a class loaded extremely early in startup, before any Dropwizard wiring has happened — generated code avoids that timing.
  • You want GraalVM native image without reflection configuration.
  • You want the proxy to be visible in stack traces (the generated class is named, JDK dynamic proxies show up as $Proxy42).

When the runtime version wins

  • You want to switch metrics on or off based on a feature flag at startup. With the static version, the proxy class always records.
  • You want different metric registries per proxy instance. The static generator uses one registry.

Edit this page on GitHub ↗