Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new metric type: error rate #580

Open
t1 opened this issue Nov 2, 2022 · 0 comments
Open

new metric type: error rate #580

t1 opened this issue Nov 2, 2022 · 0 comments

Comments

@t1
Copy link

t1 commented Nov 2, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant