Skip to content

Commit 75107d4

Browse files
ENG-44604: Add metrics to count no. of API entities per category per tenant (#285)
* add metric * minor refactor * corrected attribute paths
1 parent 8cc5fd8 commit 75107d4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

entity-service-impl/src/main/java/org/hypertrace/entity/metric/EntityMetricsReporter.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.hypertrace.core.documentstore.expression.impl.ConstantExpression;
1111
import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression;
1212
import org.hypertrace.core.documentstore.expression.impl.RelationalExpression;
13+
import org.hypertrace.core.documentstore.expression.impl.UnnestExpression;
1314
import org.hypertrace.core.documentstore.expression.operators.RelationalOperator;
1415
import org.hypertrace.core.documentstore.model.config.CustomMetricConfig;
1516
import org.hypertrace.core.documentstore.query.Filter;
@@ -22,10 +23,15 @@
2223
public class EntityMetricsReporter {
2324

2425
private static final String API_COUNT_METRIC_NAME = "api.entities.count";
26+
private static final String API_CATEGORY_METRIC_NAME = "api.categories.count";
2527
private static final String RAW_ENTITIES_COLLECTION = "raw_entities";
2628
private static final String API_DISCOVERY_STATE_ENTITY_PATH =
2729
"attributes.api_discovery_state.value.string";
30+
private static final String API_CATEGORIES_ENTITY_PATH = "attributes.categories.valueList.values";
31+
private static final String API_CATEGORY_ENTITY_PATH =
32+
"attributes.categories.valueList.values.value.string";
2833
private static final String API_DISCOVERY_STATE = "apiDiscoveryState";
34+
private static final String API_CATEGORY = "category";
2935
private static final String TENANT_ID_ENTITY_PATH = "tenantId";
3036
private static final String ENTITY_TYPE_ENTITY_PATH = "entityType";
3137
private final DocStoreMetricsRegistry metricsRegistry;
@@ -66,6 +72,31 @@ public void monitor() {
6672
IdentifierExpression.of(API_DISCOVERY_STATE_ENTITY_PATH))
6773
.build())
6874
.build())
75+
.build(),
76+
DocStoreCustomMetricReportingConfig.builder()
77+
.reportingInterval(Duration.ofHours(6))
78+
.config(
79+
CustomMetricConfig.builder()
80+
.metricName(API_CATEGORY_METRIC_NAME)
81+
.collectionName(RAW_ENTITIES_COLLECTION)
82+
.query(
83+
Query.builder()
84+
.setFilter(getFilter())
85+
.addFromClause(
86+
UnnestExpression.builder()
87+
.identifierExpression(
88+
IdentifierExpression.of(API_CATEGORIES_ENTITY_PATH))
89+
.build())
90+
.addSelection(IdentifierExpression.of(TENANT_ID_ENTITY_PATH))
91+
.addSelection(
92+
IdentifierExpression.of(API_CATEGORY_ENTITY_PATH), API_CATEGORY)
93+
.addSelection(
94+
AggregateExpression.of(COUNT, ConstantExpression.of(1)),
95+
VALUE_KEY)
96+
.addAggregation(IdentifierExpression.of(TENANT_ID_ENTITY_PATH))
97+
.addAggregation(IdentifierExpression.of(API_CATEGORY_ENTITY_PATH))
98+
.build())
99+
.build())
69100
.build());
70101

71102
private Filter getFilter() {

0 commit comments

Comments
 (0)