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

[Backport 2.x] Add IT for LogPatternTool #424

Merged
merged 2 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/main/java/org/opensearch/agent/tools/LogPatternTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,32 @@ public <T> void run(Map<String, String> parameters, ActionListener<T> listener)
SearchHit[] hits = r.getHits().getHits();

if (hits != null && hits.length > 0) {
Map<String, Object> firstLogSource = hits[0].getSourceAsMap();
String patternField = parameters.containsKey(PATTERN_FIELD)
? parameters.get(PATTERN_FIELD)
: findLongestField(hits[0].getSourceAsMap());
: findLongestField(firstLogSource);
if (patternField == null) {
listener.onResponse((T) "Pattern field is not set and this index doesn't contain any string field");
return;
throw new IllegalArgumentException("Pattern field is not set and this index doesn't contain any string field");
} else if (!firstLogSource.containsKey(patternField)) {
throw new IllegalArgumentException(
LoggerMessageFormat
.format(
null,
"Invalid parameter pattern_field: index {} does not have a field named {}",
parameters.getOrDefault(INDEX_FIELD, index),
patternField
)
);
} else if (!(firstLogSource.get(patternField) instanceof String)) {
throw new IllegalArgumentException(
LoggerMessageFormat
.format(
null,
"Invalid parameter pattern_field: pattern field {} in index {} is not type of String",
patternField,
parameters.getOrDefault(INDEX_FIELD, index)
)
);
}
Map<String, List<Map<String, Object>>> patternGroups = new HashMap<>();
for (SearchHit hit : hits) {
Expand Down Expand Up @@ -262,9 +282,9 @@ public static LogPatternTool.Factory getInstance() {

@Override
public LogPatternTool create(Map<String, Object> params) {
int docSize = params.containsKey(DOC_SIZE_FIELD) ? getInteger(params, DOC_SIZE_FIELD) : LOG_PATTERN_DEFAULT_DOC_SIZE;
int topNPattern = params.containsKey(TOP_N_PATTERN) ? getInteger(params, TOP_N_PATTERN) : DEFAULT_TOP_N_PATTERN;
int sampleLogSize = params.containsKey(SAMPLE_LOG_SIZE) ? getInteger(params, SAMPLE_LOG_SIZE) : DEFAULT_SAMPLE_LOG_SIZE;
int docSize = params.containsKey(DOC_SIZE_FIELD) ? getPositiveInteger(params, DOC_SIZE_FIELD) : LOG_PATTERN_DEFAULT_DOC_SIZE;
int topNPattern = params.containsKey(TOP_N_PATTERN) ? getPositiveInteger(params, TOP_N_PATTERN) : DEFAULT_TOP_N_PATTERN;
int sampleLogSize = params.containsKey(SAMPLE_LOG_SIZE) ? getPositiveInteger(params, SAMPLE_LOG_SIZE) : DEFAULT_SAMPLE_LOG_SIZE;
String patternStr = params.containsKey(PATTERN) ? (String) params.get(PATTERN) : null;
return LogPatternTool
.builder()
Expand Down
Loading
Loading