smallrye-metrics icon indicating copy to clipboard operation
smallrye-metrics copied to clipboard

new metric type: error rate

Open t1 opened this issue 3 years ago • 0 comments

As it has been suggested in MP Metrics, it would be cool to have a new type of metric for an error rate. The API could look something like this:

public interface ErrorRate extends Metric {
	void addError();

	void addSuccess();

	/** The values since the application started */
	ErrorRateSnapshot getTotal();

	/** The values since the last snapshot */
	ErrorRateSnapshot getSnapshot();

	static ErrorRate create() {
		return new ErrorRateImpl();
	}
}


public interface ErrorRateSnapshot {
	long getErrors();

	long getSuccesses();

	default double getErrorRate() {
		return ((double) getErrors()) / getCount();
	}

	default long getCount() {
		return getErrors() + getSuccesses();
	}
}

@Inherited
@Documented
@InterceptorBinding
@Retention(RUNTIME)
@Target({TYPE, CONSTRUCTOR, METHOD, ANNOTATION_TYPE})
public @interface ErrorRated {

	/**
	 * Create a default class so the value is not required to be set all the time.
	 */
	class DEFAULT implements ErrorRateHandler<Object> {
		@Override public boolean isError(Object value) {
			return false;
		}

		@Override public boolean isError(Throwable throwable) {
			return true;
		}
	}

	/**
	 * Specify the error rate handler class to be used. The type parameter of the fallback class must be assignable to the
	 * return type of the annotated method.
	 *
	 * @see #applyOn()
	 * @see #skipOn()
	 */
	@Nonbinding
	Class<? extends ErrorRateHandler<?>> value() default DEFAULT.class;

	/**
	 * The name of the meter.
	 */
	@Nonbinding
	String name() default "";

	/**
	 * The tags of the meter. Each {@code String} tag must be in the form of 'key=value'. If the input is empty or does
	 * not contain a '=' sign, the entry is ignored.
	 *
	 * @see org.eclipse.microprofile.metrics.Metadata
	 */
	@Nonbinding
	String[] tags() default {};

	/**
	 * If {@code true}, use the given name as an absolute name. If {@code false} (default), use the given name
	 * relative to the annotated class. When annotating a class, this must be {@code false}.
	 */
	@Nonbinding
	boolean absolute() default false;

	/**
	 * The display name of the meter.
	 *
	 * @see org.eclipse.microprofile.metrics.Metadata
	 */
	@Nonbinding
	String displayName() default "";

	/**
	 * The description of the meter.
	 *
	 * @see org.eclipse.microprofile.metrics.Metadata
	 */
	@Nonbinding
	String description() default "";

	/**
	 * The list of exception types which should be considered errors, including subclasses.
	 * <p>
	 * Only if an exception is <em>not</em> in this list, the {@link ErrorRateHandler} is considered.
	 *
	 * @see #value()
	 */
	@Nonbinding
	Class<? extends Throwable>[] applyOn() default {};

	/**
	 * The list of exception types which should <em>not</em> be considered errors, including subclasses.
	 * <p>
	 * Only if an exception is <em>not</em> in this list, the {@link ErrorRateHandler} is considered.
	 *
	 * @see #value()
	 */
	@Nonbinding
	Class<? extends Throwable>[] skipOn() default {};
}

WDYT? I could do a PR.

t1 avatar Nov 02 '22 07:11 t1