Skip to content

Commit

Permalink
78 collect stats for usage and health (#79)
Browse files Browse the repository at this point in the history
Signed-off-by: Johannes Peter <[email protected]>
  • Loading branch information
JohannesDaniel authored Dec 5, 2024
1 parent 54ab628 commit c19e3a3
Show file tree
Hide file tree
Showing 30 changed files with 947 additions and 448 deletions.
178 changes: 0 additions & 178 deletions src/javaRestTest/java/com/o19s/es/ltr/action/LTRStatsActionIT.java

This file was deleted.

25 changes: 19 additions & 6 deletions src/main/java/com/o19s/es/explore/ExplorerQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package com.o19s.es.explore;

import org.apache.lucene.search.QueryVisitor;
import org.opensearch.ltr.settings.LTRSettings;
import org.opensearch.ltr.stats.LTRStats;
import org.opensearch.ltr.stats.StatName;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermStates;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryVisitor;
import org.apache.lucene.search.ScoreMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Weight;
Expand All @@ -35,7 +38,6 @@

import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.similarities.ClassicSimilarity;
import org.opensearch.ltr.settings.LTRSettings;

import java.io.IOException;
import java.util.HashSet;
Expand All @@ -45,10 +47,12 @@
public class ExplorerQuery extends Query {
private final Query query;
private final String type;
private final LTRStats ltrStats;

public ExplorerQuery(Query query, String type) {
public ExplorerQuery(Query query, String type, LTRStats ltrStats) {
this.query = query;
this.type = type;
this.ltrStats = ltrStats;
}

private boolean isCollectionScoped() {
Expand All @@ -62,6 +66,7 @@ private boolean isCollectionScoped() {

public String getType() { return this.type; }

@SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
@Override
public boolean equals(Object other) {
return sameClassAs(other) &&
Expand All @@ -78,7 +83,7 @@ public Query rewrite(IndexReader reader) throws IOException {
Query rewritten = query.rewrite(reader);

if (rewritten != query) {
return new ExplorerQuery(rewritten, type);
return new ExplorerQuery(rewritten, type, ltrStats);
}

return this;
Expand All @@ -90,12 +95,20 @@ public int hashCode() {
}

@Override
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost)
throws IOException {
public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
if (!LTRSettings.isLTRPluginEnabled()) {
throw new IllegalStateException("LTR plugin is disabled. To enable, update ltr.plugin.enabled to true");
}

try {
return createWeightInternal(searcher, scoreMode, boost);
} catch (Exception e) {
ltrStats.getStats().get(StatName.LTR_REQUEST_ERROR_COUNT.getName()).increment();
throw e;
}
}

private Weight createWeightInternal(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException {
if (!scoreMode.needsScores()) {
return searcher.createWeight(query, scoreMode, boost);
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/o19s/es/explore/ExplorerQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

package com.o19s.es.explore;

import org.opensearch.ltr.stats.LTRStats;
import org.opensearch.ltr.stats.StatName;
import org.apache.lucene.search.Query;
import org.opensearch.core.ParseField;
import org.opensearch.core.common.ParsingException;
Expand Down Expand Up @@ -53,18 +55,20 @@ public class ExplorerQueryBuilder extends AbstractQueryBuilder<ExplorerQueryBuil

private QueryBuilder query;
private String type;
private LTRStats ltrStats;

public ExplorerQueryBuilder() {
}


public ExplorerQueryBuilder(StreamInput in) throws IOException {
public ExplorerQueryBuilder(StreamInput in, LTRStats ltrStats) throws IOException {
super(in);
query = in.readNamedWriteable(QueryBuilder.class);
type = in.readString();
this.ltrStats = ltrStats;
}

public static ExplorerQueryBuilder fromXContent(XContentParser parser) throws IOException {
public static ExplorerQueryBuilder fromXContent(XContentParser parser, LTRStats ltrStats) throws IOException {
final ExplorerQueryBuilder builder;

try {
Expand All @@ -79,6 +83,7 @@ public static ExplorerQueryBuilder fromXContent(XContentParser parser) throws IO
if (builder.statsType() == null) {
throw new ParsingException(parser.getTokenLocation(), "Field [" + TYPE_NAME + "] is mandatory.");
}
builder.ltrStats(ltrStats);
return builder;
}

Expand All @@ -99,7 +104,8 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
return new ExplorerQuery(query.toQuery(context), type);
ltrStats.getStat(StatName.LTR_REQUEST_TOTAL_COUNT.getName()).increment();
return new ExplorerQuery(query.toQuery(context), type, ltrStats);
}

@Override
Expand All @@ -109,6 +115,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
ExplorerQueryBuilder rewritten = new ExplorerQueryBuilder();
rewritten.type = this.type;
rewritten.query = Rewriteable.rewrite(query, queryRewriteContext);
rewritten.ltrStats = this.ltrStats;
rewritten.boost(boost());
rewritten.queryName(queryName());

Expand Down Expand Up @@ -144,6 +151,11 @@ public ExplorerQueryBuilder query(QueryBuilder query) {
return this;
}

public ExplorerQueryBuilder ltrStats(LTRStats ltrStats) {
this.ltrStats = ltrStats;
return this;
}

public String statsType() {
return type;
}
Expand Down
Loading

0 comments on commit c19e3a3

Please sign in to comment.