Skip to content

Add TextChunkingProcessor event and info stats #2

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

Draft
wants to merge 1 commit into
base: metrics-framework-clean
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.opensearch.index.mapper.IndexFieldMapper;
import org.opensearch.neuralsearch.processor.chunker.ChunkerFactory;
import org.opensearch.neuralsearch.processor.chunker.FixedTokenLengthChunker;
import org.opensearch.neuralsearch.stats.EventStatsManager;
import org.opensearch.neuralsearch.util.ProcessorDocumentUtils;

import static org.opensearch.neuralsearch.processor.chunker.Chunker.MAX_CHUNK_LIMIT_FIELD;
Expand Down Expand Up @@ -192,6 +193,7 @@ public IngestDocument execute(final IngestDocument ingestDocument) {
runtimeParameters.put(MAX_CHUNK_LIMIT_FIELD, maxChunkLimit);
runtimeParameters.put(CHUNK_STRING_COUNT_FIELD, chunkStringCount);
chunkMapType(sourceAndMetadataMap, fieldMap, runtimeParameters);
EventStatsManager.recordTextChunkingExecution(chunker.getAlgorithmName());
return ingestDocument;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package org.opensearch.neuralsearch.stats;

import org.opensearch.neuralsearch.processor.TextChunkingProcessor;
import org.opensearch.neuralsearch.stats.names.DerivedStatName;
import org.opensearch.neuralsearch.stats.names.StatType;
import org.opensearch.neuralsearch.util.NeuralSearchClusterUtil;
Expand All @@ -20,6 +21,15 @@ public class DerivedStatsManager {
public static final String PROCESSORS_KEY = "processors";
public static final String ALGORITHM_KEY = "algorithm";

// Text chunking processor keys
public static final String ALGORITHM_FIXED_TOKEN_LENGTH_KEY = "fixed_token_length";
public static final String ALGORITHM_DELIMITER_KEY = "delimiter";
public static final String TOKENIZER_KEY = "tokenizer";
public static final String TOKENIZER_STANDARD = "standard";
public static final String TOKENIZER_LETTER = "letter";
public static final String TOKENIZER_LOWERCASE = "lowercase";
public static final String TOKENIZER_WHITESPACE = "whitespace";

// Search Response
public static final String REQUEST_PROCESSORS_KEY = "request_processors";
public static final String RESPONSE_PROCESSORS_KEY = "response_processors";
Expand Down Expand Up @@ -91,14 +101,47 @@ private void addIngestProcessorStats(Map<String, Object> stats) {
String processorType = entry.getKey();
Map<String, Object> processorConfig = asMap(entry.getValue());
switch (processorType) {
// Custom processorConfig parsing to extract processor options, count processors, etc
// Goes here
case TextChunkingProcessor.TYPE:
addTextChunkingProcessorStats(stats, processorConfig);
break;
// Add additional ingest processor cases here
}
}
}
}
}

private void addTextChunkingProcessorStats(Map<String, Object> stats, Map<String, Object> processorConfig) {
increment(stats, DerivedStatName.INGEST_TEXT_CHUNKING_PROCESSOR_COUNT.getName());

Map<String, Object> algorithmField = asMap(asMap(processorConfig).get(ALGORITHM_KEY));
for (Map.Entry<String, Object> field : algorithmField.entrySet()) {
switch (field.getKey()) {
case ALGORITHM_DELIMITER_KEY:
increment(stats, DerivedStatName.INGEST_TEXT_CHUNKING_ALGORITHM_DELIMITER.getName());
break;
case ALGORITHM_FIXED_TOKEN_LENGTH_KEY:
increment(stats, DerivedStatName.INGEST_TEXT_CHUNKING_ALGORITHM_FIXED_LENGTH.getName());
String tokenizer = getValue(asMap(field.getValue()), TOKENIZER_KEY, String.class);
switch (tokenizer) {
case TOKENIZER_STANDARD:
increment(stats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_STANDARD.getName());
break;
case TOKENIZER_LETTER:
increment(stats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_LETTER.getName());
break;
case TOKENIZER_LOWERCASE:
increment(stats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_LOWERCASE.getName());
break;
case TOKENIZER_WHITESPACE:
increment(stats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_WHITESPACE.getName());
break;
}
break;
}
}
}

private void addSearchProcessorStats(Map<String, Object> stats) {
List<Map<String, Object>> pipelineConfigs = PipelineInfoUtil.instance().getSearchPipelineConfigs();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.opensearch.neuralsearch.stats;

import org.opensearch.neuralsearch.processor.chunker.DelimiterChunker;
import org.opensearch.neuralsearch.processor.chunker.FixedTokenLengthChunker;
import org.opensearch.neuralsearch.stats.names.EventStatName;
import org.opensearch.neuralsearch.stats.names.StatType;
import org.opensearch.neuralsearch.stats.suppliers.CounterSupplier;
Expand All @@ -25,8 +27,6 @@ public static EventStatsManager instance() {
}

private static void increment(EventStatName eventStatName) {
// Should write helper methods to wrap this increment call
// Business logic should call wrapped methods, never increment directly
instance().getStats().computeIfAbsent(eventStatName.getName(), k -> new NeuralStat<>(new CounterSupplier())).increment();
}

Expand Down Expand Up @@ -59,4 +59,16 @@ public void resetStats() {
}
}
}

public static void recordTextChunkingExecution(String algorithm) {
increment(EventStatName.TEXT_CHUNKING_PROCESSOR_EXECUTIONS);
switch (algorithm) {
case DelimiterChunker.ALGORITHM_NAME:
increment(EventStatName.TEXT_CHUNKING_ALGORITHM_DELIMITER_EXECUTIONS);
break;
case FixedTokenLengthChunker.ALGORITHM_NAME:
increment(EventStatName.TEXT_CHUNKING_ALGORITHM_FIXED_LENGTH_EXECUTIONS);
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,30 @@
@Getter
public enum DerivedStatName {
// Cluster info
CLUSTER_VERSION("cluster_version", StatType.DERIVED_INFO_COUNTER);
CLUSTER_VERSION("cluster_version", StatType.DERIVED_INFO_COUNTER),

// Search processor info

// Ingest processor info
INGEST_TEXT_CHUNKING_PROCESSOR_COUNT("pipelines.ingest.processors.text_chunking.count", StatType.DERIVED_INFO_COUNTER),
INGEST_TEXT_CHUNKING_ALGORITHM_FIXED_LENGTH(
"pipelines.ingest.processors.text_chunking.algorithm.fixed_length",
StatType.DERIVED_INFO_COUNTER
),
INGEST_TEXT_CHUNKING_ALGORITHM_DELIMITER(
"pipelines.ingest.processors.text_chunking.algorithm.delimiter",
StatType.DERIVED_INFO_COUNTER
),
INGEST_TEXT_CHUNKING_TOKENIZER_STANDARD("pipelines.ingest.processors.text_chunking.tokenizer.standard", StatType.DERIVED_INFO_COUNTER),
INGEST_TEXT_CHUNKING_TOKENIZER_LETTER("pipelines.ingest.processors.text_chunking.tokenizer.letter", StatType.DERIVED_INFO_COUNTER),
INGEST_TEXT_CHUNKING_TOKENIZER_LOWERCASE(
"pipelines.ingest.processors.text_chunking.tokenizer.lowercase",
StatType.DERIVED_INFO_COUNTER
),
INGEST_TEXT_CHUNKING_TOKENIZER_WHITESPACE(
"pipelines.ingest.processors.text_chunking.tokenizer.whitespace",
StatType.DERIVED_INFO_COUNTER
);

private final String name;
private final StatType statType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
public enum EventStatName {
// Search processor events
// Ingest processor events
;
TEXT_CHUNKING_PROCESSOR_EXECUTIONS("ingest_processor.text_chunking.executions", StatType.EVENT_COUNTER),
TEXT_CHUNKING_ALGORITHM_FIXED_LENGTH_EXECUTIONS("ingest_processor.text_chunking.algorithm.fixed_length", StatType.EVENT_COUNTER),
TEXT_CHUNKING_ALGORITHM_DELIMITER_EXECUTIONS("ingest_processor.text_chunking.algorithm.delimiter", StatType.EVENT_COUNTER),;

private final String name;
private final StatType statType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.opensearch.neuralsearch.rest;

import lombok.extern.log4j.Log4j2;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.junit.After;
import org.junit.Before;
import org.opensearch.client.Response;
Expand All @@ -21,6 +22,7 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -102,6 +104,64 @@ public void tearDown() throws Exception {
executeClearNeuralStatRequest(Collections.emptyList());
}

public void test_text_chunking() throws Exception {
try {
createPipelineProcessor(FIXED_TOKEN_LENGTH_PIPELINE_WITH_STANDARD_TOKENIZER_NAME, INGEST_PIPELINE_NAME);
createTextChunkingIndex(INDEX_NAME, INGEST_PIPELINE_NAME);

String document = getDocumentFromFilePath(TEST_DOCUMENT);
ingestDocument(INDEX_NAME, document);

Response response = executeNeuralStatRequest(new ArrayList<>(), new ArrayList<>());
String responseBody = EntityUtils.toString(response.getEntity());
Map<String, Object> clusterStats = parseStatsResponse(responseBody);
Map<String, Object> nodeStats = parseNodeStatsResponse(responseBody).getFirst();
log.info(clusterStats);

assertEquals(1, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_PROCESSOR_COUNT));
assertEquals(0, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_ALGORITHM_DELIMITER));
assertEquals(1, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_ALGORITHM_FIXED_LENGTH));
assertEquals(1, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_STANDARD));
assertEquals(0, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_LETTER));
assertEquals(0, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_LOWERCASE));
assertEquals(0, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_WHITESPACE));

assertEquals(1, getNestedValue(nodeStats, EventStatName.TEXT_CHUNKING_PROCESSOR_EXECUTIONS));
assertEquals(0, getNestedValue(nodeStats, EventStatName.TEXT_CHUNKING_ALGORITHM_DELIMITER_EXECUTIONS));
assertEquals(1, getNestedValue(nodeStats, EventStatName.TEXT_CHUNKING_ALGORITHM_FIXED_LENGTH_EXECUTIONS));

createPipelineProcessor(DELIMITER_PIPELINE_NAME, INGEST_PIPELINE_NAME_2);
createTextChunkingIndex(INDEX_NAME_2, INGEST_PIPELINE_NAME_2);

ingestDocument(INDEX_NAME_2, document);
ingestDocument(INDEX_NAME_2, document);

createPipelineProcessor(FIXED_TOKEN_LENGTH_PIPELINE_WITH_LOWERCASE_TOKENIZER_NAME, INGEST_PIPELINE_NAME_3);

response = executeNeuralStatRequest(new ArrayList<>(), new ArrayList<>());
responseBody = EntityUtils.toString(response.getEntity());
clusterStats = parseStatsResponse(responseBody);
nodeStats = parseNodeStatsResponse(responseBody).getFirst();

assertEquals(3, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_PROCESSOR_COUNT));
assertEquals(1, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_ALGORITHM_DELIMITER));
assertEquals(2, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_ALGORITHM_FIXED_LENGTH));
assertEquals(1, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_STANDARD));
assertEquals(0, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_LETTER));
assertEquals(1, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_LOWERCASE));
assertEquals(0, getNestedValue(clusterStats, DerivedStatName.INGEST_TEXT_CHUNKING_TOKENIZER_WHITESPACE));

assertEquals(3, getNestedValue(nodeStats, EventStatName.TEXT_CHUNKING_PROCESSOR_EXECUTIONS));
assertEquals(2, getNestedValue(nodeStats, EventStatName.TEXT_CHUNKING_ALGORITHM_DELIMITER_EXECUTIONS));
assertEquals(1, getNestedValue(nodeStats, EventStatName.TEXT_CHUNKING_ALGORITHM_FIXED_LENGTH_EXECUTIONS));

} finally {
wipeOfTestResources(INDEX_NAME, INGEST_PIPELINE_NAME, null, null);
wipeOfTestResources(INDEX_NAME_2, INGEST_PIPELINE_NAME_2, null, null);
wipeOfTestResources(null, INGEST_PIPELINE_NAME_3, null, null);
}
}

protected String uploadTextEmbeddingModel() throws Exception {
String requestBody = Files.readString(Path.of(classLoader.getResource("processor/UploadModelRequestBody.json").toURI()));
return registerModelGroupAndUploadModel(requestBody);
Expand Down
Loading