From 05906d51c1c198936132cf208481418c8187883d Mon Sep 17 00:00:00 2001 From: Kirti Kumar Date: Thu, 3 Dec 2026 12:06:01 +0530 Subject: [PATCH 1/5] Empty commit From 8d50f5acabc46d6534d4b686eb337013cf9d762c Mon Sep 17 00:00:00 2001 From: Kirti Kumar Date: Thu, 3 Dec 2026 13:02:55 +0530 Subject: [PATCH 2/5] Support custom metrics on flat Postgres collections Co-authored-by: Cursor --- .../BaseDocStoreMetricProviderImpl.java | 12 +++- .../model/config/CustomMetricConfig.java | 2 + ...seDocumentStoreMetricProviderImplTest.java | 55 ++++++++++++++++++- 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/metric/BaseDocStoreMetricProviderImpl.java b/document-store/src/main/java/org/hypertrace/core/documentstore/metric/BaseDocStoreMetricProviderImpl.java index 758c3a8c4..97c7ef2ca 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/metric/BaseDocStoreMetricProviderImpl.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/metric/BaseDocStoreMetricProviderImpl.java @@ -26,6 +26,7 @@ import org.hypertrace.core.documentstore.Collection; import org.hypertrace.core.documentstore.Datastore; import org.hypertrace.core.documentstore.Document; +import org.hypertrace.core.documentstore.DocumentType; import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; import org.hypertrace.core.documentstore.model.options.QueryOptions; import org.hypertrace.core.documentstore.query.Query; @@ -42,7 +43,6 @@ public abstract class BaseDocStoreMetricProviderImpl implements DocStoreMetricPr @Override public List getCustomMetrics(final CustomMetricConfig customMetricConfig) { try { - final String collectionName = customMetricConfig.collectionName(); final Query query = customMetricConfig.query(); query .getSelections() @@ -57,7 +57,7 @@ public List getCustomMetrics(final CustomMetricConfig customMetr } }); - final Collection collection = dataStore.getCollection(collectionName); + final Collection collection = getCollection(customMetricConfig); final List metrics = new ArrayList<>(); try (final CloseableIterator iterator = @@ -132,6 +132,14 @@ public List getCustomMetrics(final CustomMetricConfig customMetr } } + private Collection getCollection(final CustomMetricConfig customMetricConfig) { + if (customMetricConfig.documentType() == DocumentType.NESTED) { + return dataStore.getCollection(customMetricConfig.collectionName()); + } + return dataStore.getCollectionForType( + customMetricConfig.collectionName(), customMetricConfig.documentType()); + } + private String getStringLabelValue(JsonNode jsonNode) { switch (jsonNode.getNodeType()) { case STRING: diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/model/config/CustomMetricConfig.java b/document-store/src/main/java/org/hypertrace/core/documentstore/model/config/CustomMetricConfig.java index 977772f6e..7d0826c4b 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/model/config/CustomMetricConfig.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/model/config/CustomMetricConfig.java @@ -4,6 +4,7 @@ import lombok.NonNull; import lombok.Value; import lombok.experimental.Accessors; +import org.hypertrace.core.documentstore.DocumentType; import org.hypertrace.core.documentstore.query.Query; @Value @@ -15,4 +16,5 @@ public class CustomMetricConfig { @NonNull String collectionName; @NonNull String metricName; @NonNull Query query; + @NonNull @Builder.Default DocumentType documentType = DocumentType.NESTED; } diff --git a/document-store/src/test/java/org/hypertrace/core/documentstore/metric/BaseDocumentStoreMetricProviderImplTest.java b/document-store/src/test/java/org/hypertrace/core/documentstore/metric/BaseDocumentStoreMetricProviderImplTest.java index deb2640e5..05042cd0e 100644 --- a/document-store/src/test/java/org/hypertrace/core/documentstore/metric/BaseDocumentStoreMetricProviderImplTest.java +++ b/document-store/src/test/java/org/hypertrace/core/documentstore/metric/BaseDocumentStoreMetricProviderImplTest.java @@ -3,6 +3,10 @@ import static java.util.Collections.emptyList; import static org.hypertrace.core.documentstore.model.options.DataFreshness.NEAR_REALTIME_FRESHNESS; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.IOException; @@ -13,6 +17,7 @@ import org.hypertrace.core.documentstore.Collection; import org.hypertrace.core.documentstore.Datastore; import org.hypertrace.core.documentstore.Document; +import org.hypertrace.core.documentstore.DocumentType; import org.hypertrace.core.documentstore.JSONDocument; import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; @@ -65,7 +70,55 @@ public DocStoreMetric getConnectionCountMetric() { } }; - when(mockDataStore.getCollection(collectionName)).thenReturn(mockCollection); + lenient().when(mockDataStore.getCollection(collectionName)).thenReturn(mockCollection); + } + + @Nested + class CollectionResolutionTest { + + @Test + void nestedDocumentType_usesGetCollection() { + when(mockCollection.query(query, queryOptions)).thenReturn(CloseableIterator.emptyIterator()); + baseDocStoreMetricProvider.getCustomMetrics(customMetricConfig); + verify(mockDataStore).getCollection(collectionName); + verify(mockDataStore, never()).getCollectionForType(any(), any()); + } + + @Test + void flatDocumentType_usesGetCollectionForType() throws IOException { + final CustomMetricConfig flatConfig = + CustomMetricConfig.builder() + .collectionName(collectionName) + .metricName(metricName) + .query(query) + .documentType(DocumentType.FLAT) + .build(); + when(mockDataStore.getCollectionForType(collectionName, DocumentType.FLAT)) + .thenReturn(mockCollection); + when(mockCollection.query(query, queryOptions)).thenReturn(mockIterator); + when(mockIterator.hasNext()).thenReturn(true, false); + when(mockIterator.next()) + .thenReturn( + new JSONDocument( + Map.of("label1", "l1value", "label2", "l2value", "metric_value", 5))); + + final List result = baseDocStoreMetricProvider.getCustomMetrics(flatConfig); + + assertEquals( + List.of( + DocStoreMetric.builder() + .name(metricName) + .value(5) + .labels(Map.of("label1", "l1value", "label2", "l2value")) + .build()), + result); + verify(mockDataStore, never()).getCollection(any()); + } + + @Test + void defaultDocumentType_isNested() { + assertEquals(DocumentType.NESTED, customMetricConfig.documentType()); + } } @Nested From eef9e0ed3b13219443d168384a766671abbc4532 Mon Sep 17 00:00:00 2001 From: Kirti Kumar Date: Thu, 3 Dec 2026 13:50:38 +0530 Subject: [PATCH 3/5] Add integration tests for custom metrics on flat collections Co-authored-by: Cursor --- .../FlatCollectionWriteTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/FlatCollectionWriteTest.java b/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/FlatCollectionWriteTest.java index d1721a837..11cd17c84 100644 --- a/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/FlatCollectionWriteTest.java +++ b/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/FlatCollectionWriteTest.java @@ -1,5 +1,7 @@ package org.hypertrace.core.documentstore; +import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.COUNT; +import static org.hypertrace.core.documentstore.model.config.CustomMetricConfig.VALUE_KEY; import static org.hypertrace.core.documentstore.utils.Utils.readFileFromResource; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -27,10 +29,15 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; +import org.hypertrace.core.documentstore.expression.impl.AggregateExpression; import org.hypertrace.core.documentstore.expression.impl.ConstantExpression; import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; import org.hypertrace.core.documentstore.expression.impl.RelationalExpression; +import org.hypertrace.core.documentstore.expression.impl.UnnestExpression; import org.hypertrace.core.documentstore.expression.operators.RelationalOperator; +import org.hypertrace.core.documentstore.metric.DocStoreMetric; +import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; import org.hypertrace.core.documentstore.model.exception.DuplicateDocumentException; import org.hypertrace.core.documentstore.model.exception.SchemaMismatchException; import org.hypertrace.core.documentstore.model.options.MissingColumnStrategy; @@ -4092,6 +4099,80 @@ void upsertHonorsQueryTimeoutOnRowLock() throws Exception { } } + @Nested + @DisplayName("Custom Metrics") + class CustomMetricTests { + + @Test + @DisplayName("Should report grouped counts over flat columns") + void testGroupedCountOnFlatColumns() { + final CustomMetricConfig config = + CustomMetricConfig.builder() + .collectionName(FLAT_COLLECTION_NAME) + .metricName("item.count") + .documentType(DocumentType.FLAT) + .query( + Query.builder() + .addSelection(IdentifierExpression.of("item")) + .addSelection( + AggregateExpression.of(COUNT, ConstantExpression.of(1)), VALUE_KEY) + .addAggregation(IdentifierExpression.of("item")) + .build()) + .build(); + + final List metrics = + postgresDatastore.getDocStoreMetricProvider().getCustomMetrics(config); + metrics.forEach(metric -> assertEquals("item.count", metric.name())); + + assertEquals( + Map.of( + "Soap", 3.0, "Shampoo", 2.0, "Comb", 2.0, "Mirror", 1.0, "Bottle", 1.0, "Cup", 1.0), + metrics.stream() + .collect( + Collectors.toMap(metric -> metric.labels().get("item"), DocStoreMetric::value))); + } + + @Test + @DisplayName("Should report counts over an unnested array column") + void testUnnestedArrayCount() { + final CustomMetricConfig config = + CustomMetricConfig.builder() + .collectionName(FLAT_COLLECTION_NAME) + .metricName("category.count") + .documentType(DocumentType.FLAT) + .query( + Query.builder() + .addFromClause( + UnnestExpression.of(IdentifierExpression.of("categoryTags"), false)) + .addSelection(IdentifierExpression.of("categoryTags"), "category") + .addSelection( + AggregateExpression.of(COUNT, ConstantExpression.of(1)), VALUE_KEY) + .addAggregation(IdentifierExpression.of("categoryTags")) + .build()) + .build(); + + final Map countsByCategory = + postgresDatastore.getDocStoreMetricProvider().getCustomMetrics(config).stream() + .collect( + Collectors.toMap( + metric -> metric.labels().get("category"), DocStoreMetric::value)); + + assertEquals( + Map.of( + "Hygiene", + 3.0, + "PersonalCare", + 2.0, + "HairCare", + 2.0, + "Grooming", + 2.0, + "HomeDecor", + 1.0), + countsByCategory); + } + } + private Collection getFlatCollectionWithQueryTimeout(String queryTimeout) { String postgresConnectionUrl = String.format("jdbc:postgresql://localhost:%s/", postgresContainer.getMappedPort(5432)); From 98515931aea155a51798aea328396a940c06e923 Mon Sep 17 00:00:00 2001 From: Kirti Kumar Date: Thu, 3 Dec 2026 14:16:15 +0530 Subject: [PATCH 4/5] Alias label selection in flat custom metric integration test Co-authored-by: Cursor --- .../hypertrace/core/documentstore/FlatCollectionWriteTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/FlatCollectionWriteTest.java b/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/FlatCollectionWriteTest.java index 11cd17c84..94b24c637 100644 --- a/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/FlatCollectionWriteTest.java +++ b/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/FlatCollectionWriteTest.java @@ -4113,7 +4113,7 @@ void testGroupedCountOnFlatColumns() { .documentType(DocumentType.FLAT) .query( Query.builder() - .addSelection(IdentifierExpression.of("item")) + .addSelection(IdentifierExpression.of("item"), "item") .addSelection( AggregateExpression.of(COUNT, ConstantExpression.of(1)), VALUE_KEY) .addAggregation(IdentifierExpression.of("item")) From e48acb9bd26e94af68951770996bec7176a30288 Mon Sep 17 00:00:00 2001 From: Kirti Kumar Date: Thu, 3 Dec 2026 20:05:39 +0530 Subject: [PATCH 5/5] Dummy commit Co-authored-by: Cursor