Skip to content

Commit

Permalink
Add rule based error classification
Browse files Browse the repository at this point in the history
  • Loading branch information
itsankit-google committed Feb 5, 2025
1 parent f3955ad commit 06338bd
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ public final class ErrorClassificationResponse {
private final String errorCode;
private final String supportedDocumentationUrl;
private transient final String throwableClassName;
private transient final Integer rulePriority;

private ErrorClassificationResponse(String stageName, String errorCategory, String errorReason,
String errorMessage, String errorType, String dependency, String errorCodeType,
String errorCode, String supportedDocumentationUrl, String throwableClassName) {
String errorCode, String supportedDocumentationUrl, String throwableClassName,
Integer rulePriority) {
this.stageName = stageName;
this.errorCategory = errorCategory;
this.errorReason = errorReason;
Expand All @@ -46,6 +48,7 @@ private ErrorClassificationResponse(String stageName, String errorCategory, Stri
this.errorCode = errorCode;
this.supportedDocumentationUrl = supportedDocumentationUrl;
this.throwableClassName = throwableClassName;
this.rulePriority = rulePriority;
}

/**
Expand Down Expand Up @@ -118,6 +121,14 @@ public String getThrowableClassName() {
return throwableClassName;
}

/**
*
* Gets the rule priority for ErrorClassificationResponse.
*/
public Integer getRulePriority() {
return rulePriority;
}

@Override
public boolean equals(Object o) {
if (!(o instanceof ErrorClassificationResponse)) {
Expand All @@ -132,13 +143,16 @@ public boolean equals(Object o) {
&& Objects.equals(this.dependency, that.dependency)
&& Objects.equals(this.errorCodeType, that.errorCodeType)
&& Objects.equals(this.errorCode, that.errorCode)
&& Objects.equals(this.supportedDocumentationUrl, that.supportedDocumentationUrl);
&& Objects.equals(this.supportedDocumentationUrl, that.supportedDocumentationUrl)
&& Objects.equals(this.throwableClassName, that.throwableClassName)
&& Objects.equals(this.rulePriority, that.rulePriority);
}

@Override
public int hashCode() {
return Objects.hash(stageName, errorCategory, errorReason, errorMessage, errorType,
dependency, errorCodeType, errorCode, supportedDocumentationUrl);
dependency, errorCodeType, errorCode, supportedDocumentationUrl, throwableClassName,
rulePriority);
}

/**
Expand All @@ -155,6 +169,7 @@ public static class Builder {
private String errorCode;
private String supportedDocumentationUrl;
private String throwableClassName;
private Integer rulePriority;

/**
* Sets the stage name for ErrorClassificationResponse.
Expand Down Expand Up @@ -236,13 +251,22 @@ public Builder setThrowableClassName(String throwableClassName) {
return this;
}

/**
*
* Sets the rule priority for ErrorClassificationResponse.
*/
public Builder setRulePriority(Integer rulePriority) {
this.rulePriority = rulePriority;
return this;
}

/**
* Builds and returns a new instance of ErrorClassificationResponse.
*/
public ErrorClassificationResponse build() {
return new ErrorClassificationResponse(stageName, errorCategory, errorReason, errorMessage,
errorType, dependency, errorCodeType, errorCode, supportedDocumentationUrl,
throwableClassName);
throwableClassName, rulePriority);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/*
* Copyright © 2025 Cask Data, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/

package io.cdap.cdap.logging;

import com.google.common.base.Strings;
import io.cdap.cdap.api.exception.ErrorType;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

/**
* Represents the rule for classifying error logs.
*/
public final class ErrorClassificationRule implements Comparable<ErrorClassificationRule> {
public static final Pattern DEFAULT_PATTERN = Pattern.compile(".*");
private final String id;
private final String description;
private final int priority;
private final Pattern exceptionClassRegex;
private final Pattern codeMethodRegex;
private final ErrorType errorType;
private final boolean dependency;

private ErrorClassificationRule(String id, String description, int priority,
@Nullable String exceptionClassRegex,@Nullable String codeMethodRegex,
ErrorType errorType, boolean dependency) {
this.id = id;
this.description = description;
this.priority = priority;
this.exceptionClassRegex = Strings.isNullOrEmpty(exceptionClassRegex) ? null
: Pattern.compile(exceptionClassRegex);
this.codeMethodRegex = Strings.isNullOrEmpty(codeMethodRegex) ? null
: Pattern.compile(codeMethodRegex);
this.errorType = errorType;
this.dependency = dependency;
}

/**
* Returns the id for ErrorClassificationRule.
*/
public String getId() {
return id;
}

/**
* Returns the description for ErrorClassificationRule.
*/
public String getDescription() {
return description;
}

/**
* Returns the priority for ErrorClassificationRule.
*/
public int getPriority() {
return priority;
}

/**
* Returns the exception class regex for ErrorClassificationRule.
*/
public Pattern getExceptionClassRegex() {
return exceptionClassRegex == null ? DEFAULT_PATTERN : exceptionClassRegex;
}

/**
* Returns the code method regex for ErrorClassificationRule.
*/
public Pattern getCodeMethodRegex() {
return codeMethodRegex == null ? DEFAULT_PATTERN : codeMethodRegex;
}

/**
* Returns the {@link ErrorType} for ErrorClassificationRule.
*/
public ErrorType getErrorType() {
return errorType == null ? ErrorType.UNKNOWN : errorType;
}

/**
* Returns the dependency flag for ErrorClassificationRule.
*/
public boolean isDependency() {
return dependency;
}

@Override
public int compareTo(ErrorClassificationRule that) {
return Integer.compare(this.priority, that.priority);
}

@Override
public String toString() {
return String.format("Rule Id: '%s', Description: '%s', Exception Class Name: '%s',"
+ "Code Method Regex: '%s', Error Type: '%s', Dependency: '%s'", id, description,
getExceptionClassRegex().pattern(), getCodeMethodRegex().pattern(), errorType, dependency);
}

public static class Builder {
private String id;
private String description;
private int priority;
private String exceptionClassRegex;
private String codeMethodRegex;
private ErrorType errorType;
private boolean dependency;

/**
* Sets the id for ErrorClassificationRule.
*/
public Builder setId(String id) {
this.id = id;
return this;
}

/**
* Sets the description for ErrorClassificationRule.
*/
public Builder setDescription(String description) {
this.description = description;
return this;
}

/**
* Sets the priority for ErrorClassificationRule.
*/
public Builder setPriority(int priority) {
this.priority = priority;
return this;
}

/**
* Sets the exception class name regex for ErrorClassificationRule.
*/
public Builder setExceptionClassName(String exceptionClassRegex) {
this.exceptionClassRegex = exceptionClassRegex;
return this;
}

/**
* Sets the code method regex for ErrorClassificationRule.
*/
public Builder setCodeMethodRegex(String codeMethodRegex) {
this.codeMethodRegex = codeMethodRegex;
return this;
}

/**
* Sets the {@link ErrorType} for ErrorClassificationRule.
*/
public Builder setErrorType(ErrorType errorType) {
this.errorType = errorType;
return this;
}

/**
* Sets the dependency flag for ErrorClassificationRule.
*/
public Builder setDependency(boolean dependency) {
this.dependency = dependency;
return this;
}

public ErrorClassificationRule build() {
return new ErrorClassificationRule(id, description, priority, exceptionClassRegex,
codeMethodRegex, errorType, dependency);
}
}
}
Loading

0 comments on commit 06338bd

Please sign in to comment.