|
1 | 1 | package org.hypertrace.entity.metric; |
2 | 2 |
|
| 3 | +import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.COUNT; |
| 4 | +import static org.hypertrace.core.documentstore.model.config.CustomMetricConfig.VALUE_KEY; |
| 5 | + |
| 6 | +import java.time.Duration; |
| 7 | +import java.util.List; |
3 | 8 | import org.hypertrace.core.documentstore.Datastore; |
| 9 | +import org.hypertrace.core.documentstore.expression.impl.AggregateExpression; |
| 10 | +import org.hypertrace.core.documentstore.expression.impl.ConstantExpression; |
| 11 | +import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; |
| 12 | +import org.hypertrace.core.documentstore.expression.impl.RelationalExpression; |
| 13 | +import org.hypertrace.core.documentstore.expression.operators.RelationalOperator; |
| 14 | +import org.hypertrace.core.documentstore.model.config.CustomMetricConfig; |
| 15 | +import org.hypertrace.core.documentstore.query.Filter; |
| 16 | +import org.hypertrace.core.documentstore.query.Query; |
| 17 | +import org.hypertrace.core.serviceframework.docstore.metrics.DocStoreCustomMetricReportingConfig; |
4 | 18 | import org.hypertrace.core.serviceframework.docstore.metrics.DocStoreMetricsRegistry; |
5 | 19 | import org.hypertrace.core.serviceframework.spi.PlatformServiceLifecycle; |
| 20 | +import org.hypertrace.entity.v1.entitytype.EntityType; |
6 | 21 |
|
7 | 22 | public class EntityMetricsReporter { |
| 23 | + |
| 24 | + private static final String API_COUNT_METRIC_NAME = "api.entities.count"; |
| 25 | + private static final String RAW_ENTITIES_COLLECTION = "raw_entities"; |
| 26 | + private static final String API_DISCOVERY_STATE_ENTITY_PATH = |
| 27 | + "attributes.api_discovery_state.value.string"; |
| 28 | + private static final String TENANT_ID_ENTITY_PATH = "tenantId"; |
| 29 | + private static final String ENTITY_TYPE_ENTITY_PATH = "entityType"; |
8 | 30 | private final DocStoreMetricsRegistry metricsRegistry; |
9 | 31 |
|
10 | 32 | public EntityMetricsReporter( |
11 | 33 | final Datastore datastore, final PlatformServiceLifecycle lifecycle) { |
12 | | - metricsRegistry = new DocStoreMetricsRegistry(datastore).withPlatformLifecycle(lifecycle); |
| 34 | + metricsRegistry = |
| 35 | + new DocStoreMetricsRegistry(datastore) |
| 36 | + .withPlatformLifecycle(lifecycle) |
| 37 | + .withCustomMetrics(apiCounterConfig); |
13 | 38 | } |
14 | 39 |
|
15 | 40 | public void monitor() { |
16 | 41 | metricsRegistry.monitor(); |
17 | 42 | } |
| 43 | + |
| 44 | + @SuppressWarnings("FieldCanBeLocal") |
| 45 | + private final List<DocStoreCustomMetricReportingConfig> apiCounterConfig = |
| 46 | + List.of( |
| 47 | + DocStoreCustomMetricReportingConfig.builder() |
| 48 | + .reportingInterval(Duration.ofHours(1)) |
| 49 | + .config( |
| 50 | + CustomMetricConfig.builder() |
| 51 | + .metricName(API_COUNT_METRIC_NAME) |
| 52 | + .collectionName(RAW_ENTITIES_COLLECTION) |
| 53 | + .query( |
| 54 | + Query.builder() |
| 55 | + .setFilter(getFilter()) |
| 56 | + .addSelection(IdentifierExpression.of(TENANT_ID_ENTITY_PATH)) |
| 57 | + .addSelection( |
| 58 | + AggregateExpression.of(COUNT, ConstantExpression.of(1)), |
| 59 | + VALUE_KEY) |
| 60 | + .addSelection( |
| 61 | + IdentifierExpression.of(API_DISCOVERY_STATE_ENTITY_PATH)) |
| 62 | + .addAggregation(IdentifierExpression.of(TENANT_ID_ENTITY_PATH)) |
| 63 | + .addAggregation( |
| 64 | + IdentifierExpression.of(API_DISCOVERY_STATE_ENTITY_PATH)) |
| 65 | + .build()) |
| 66 | + .build()) |
| 67 | + .build()); |
| 68 | + |
| 69 | + private Filter getFilter() { |
| 70 | + return Filter.builder() |
| 71 | + .expression( |
| 72 | + RelationalExpression.of( |
| 73 | + IdentifierExpression.of(ENTITY_TYPE_ENTITY_PATH), |
| 74 | + RelationalOperator.EQ, |
| 75 | + ConstantExpression.of(EntityType.API.name()))) |
| 76 | + .build(); |
| 77 | + } |
18 | 78 | } |
0 commit comments