This is an automated email from the ASF dual-hosted git repository.

zqr10159 pushed a commit to branch 2.0.0
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git


The following commit(s) were added to refs/heads/2.0.0 by this push:
     new 5948adf0df Scope trace queries by entity id
5948adf0df is described below

commit 5948adf0df8ce292fa4325b48bb05d4e9baa0c82
Author: Logic <[email protected]>
AuthorDate: Tue Jun 9 20:26:43 2026 +0800

    Scope trace queries by entity id
---
 .../traces/controller/TraceQueryController.java    | 29 ++++++++++++++--------
 .../controller/TraceQueryControllerTest.java       | 12 ++++-----
 2 files changed, 25 insertions(+), 16 deletions(-)

diff --git 
a/hertzbeat-observability/src/main/java/org/apache/hertzbeat/observability/traces/controller/TraceQueryController.java
 
b/hertzbeat-observability/src/main/java/org/apache/hertzbeat/observability/traces/controller/TraceQueryController.java
index 20a6a8e0d3..3a18a62e8f 100644
--- 
a/hertzbeat-observability/src/main/java/org/apache/hertzbeat/observability/traces/controller/TraceQueryController.java
+++ 
b/hertzbeat-observability/src/main/java/org/apache/hertzbeat/observability/traces/controller/TraceQueryController.java
@@ -69,7 +69,7 @@ public class TraceQueryController {
             @RequestParam(value = "hideInternal", required = false) Boolean 
hideInternal,
             @RequestParam(value = "pageIndex", defaultValue = "0") Integer 
pageIndex,
             @RequestParam(value = "pageSize", defaultValue = "20") Integer 
pageSize) {
-        String scopedResourceFilter = 
mergeEntityTypeResourceFilter(entityType, resourceFilter);
+        String scopedResourceFilter = 
mergeEntityContextResourceFilter(entityId, entityType, resourceFilter);
         Page<TraceListItemDto> page = entityTraceQueryService.queryTraceList(
                 entityId, start, end, traceId, errorOnly, serviceName, 
serviceNamespace, environment,
                 scopedResourceFilter, operationName, minDurationMs, 
maxDurationMs, pageIndex, pageSize, hideInternal,
@@ -95,7 +95,7 @@ public class TraceQueryController {
             @RequestParam(value = "maxDurationMs", required = false) Long 
maxDurationMs,
             @RequestParam(value = "spanScope", required = false) String 
spanScope,
             @RequestParam(value = "hideInternal", required = false) Boolean 
hideInternal) {
-        String scopedResourceFilter = 
mergeEntityTypeResourceFilter(entityType, resourceFilter);
+        String scopedResourceFilter = 
mergeEntityContextResourceFilter(entityId, entityType, resourceFilter);
         return 
ResponseEntity.ok(Message.success(entityTraceQueryService.getTraceOverview(
                 entityId, start, end, traceId, errorOnly, serviceName, 
serviceNamespace, environment,
                 scopedResourceFilter, operationName, minDurationMs, 
maxDurationMs, hideInternal, spanScope)));
@@ -123,7 +123,7 @@ public class TraceQueryController {
             @RequestParam(value = "minCount", required = false) Integer 
minCount,
             @RequestParam(value = "spanScope", required = false) String 
spanScope,
             @RequestParam(value = "hideInternal", required = false) Boolean 
hideInternal) {
-        String scopedResourceFilter = 
mergeEntityTypeResourceFilter(entityType, resourceFilter);
+        String scopedResourceFilter = 
mergeEntityContextResourceFilter(entityId, entityType, resourceFilter);
         return 
ResponseEntity.ok(Message.success(entityTraceQueryService.getTraceGroupByStats(
                 entityId, start, end, traceId, errorOnly, serviceName, 
serviceNamespace, environment,
                 scopedResourceFilter, operationName, minDurationMs, 
maxDurationMs, groupBy, limit, orderBy, minCount,
@@ -144,19 +144,28 @@ public class TraceQueryController {
         return 
ResponseEntity.ok(Message.success(entityTraceQueryService.getTraceSpans(entityId,
 traceId)));
     }
 
-    private String mergeEntityTypeResourceFilter(String entityType, String 
resourceFilter) {
+    private String mergeEntityContextResourceFilter(Long entityId, String 
entityType, String resourceFilter) {
         String normalizedResourceFilter = 
StringUtils.trimWhitespace(resourceFilter);
+        String scopedResourceFilter = normalizedResourceFilter;
+        if (entityId != null && entityId > 0 && 
(StringUtils.hasText(scopedResourceFilter)
+                ? 
!scopedResourceFilter.contains(OtlpResourceSemanticAttributes.HERTZBEAT_ENTITY_ID)
+                : true)) {
+            String entityIdFilter = 
OtlpResourceSemanticAttributes.HERTZBEAT_ENTITY_ID + "=\"" + entityId + "\"";
+            scopedResourceFilter = StringUtils.hasText(scopedResourceFilter)
+                    ? scopedResourceFilter + " and " + entityIdFilter
+                    : entityIdFilter;
+        }
         String normalizedEntityType = StringUtils.trimWhitespace(entityType);
         if (!StringUtils.hasText(normalizedEntityType) || 
!normalizedEntityType.matches("[A-Za-z0-9_.:-]+")) {
-            return normalizedResourceFilter;
+            return scopedResourceFilter;
         }
-        if (StringUtils.hasText(normalizedResourceFilter)
-                && 
normalizedResourceFilter.contains(OtlpResourceSemanticAttributes.HERTZBEAT_ENTITY_TYPE))
 {
-            return normalizedResourceFilter;
+        if (StringUtils.hasText(scopedResourceFilter)
+                && 
scopedResourceFilter.contains(OtlpResourceSemanticAttributes.HERTZBEAT_ENTITY_TYPE))
 {
+            return scopedResourceFilter;
         }
         String entityTypeFilter = 
OtlpResourceSemanticAttributes.HERTZBEAT_ENTITY_TYPE + "=\"" + 
normalizedEntityType + "\"";
-        return StringUtils.hasText(normalizedResourceFilter)
-                ? normalizedResourceFilter + " and " + entityTypeFilter
+        return StringUtils.hasText(scopedResourceFilter)
+                ? scopedResourceFilter + " and " + entityTypeFilter
                 : entityTypeFilter;
     }
 }
diff --git 
a/hertzbeat-observability/src/test/java/org/apache/hertzbeat/observability/traces/controller/TraceQueryControllerTest.java
 
b/hertzbeat-observability/src/test/java/org/apache/hertzbeat/observability/traces/controller/TraceQueryControllerTest.java
index 52af0ee314..f5ab96e167 100644
--- 
a/hertzbeat-observability/src/test/java/org/apache/hertzbeat/observability/traces/controller/TraceQueryControllerTest.java
+++ 
b/hertzbeat-observability/src/test/java/org/apache/hertzbeat/observability/traces/controller/TraceQueryControllerTest.java
@@ -68,7 +68,7 @@ class TraceQueryControllerTest {
         );
         when(entityTraceQueryService.queryTraceList(
                 1L, 100L, 200L, "trace-1", true, "checkout", "commerce", 
"prod",
-                "service.version=1.2.3 and hertzbeat.entity_type=\"service\"", 
"GET /checkout",
+                "service.version=1.2.3 and hertzbeat.entity_id=\"1\" and 
hertzbeat.entity_type=\"service\"", "GET /checkout",
                 100L, 500L, 2, 50, true, null))
                 .thenReturn(new PageImpl<>(List.of(item), PageRequest.of(2, 
50), 1));
 
@@ -96,7 +96,7 @@ class TraceQueryControllerTest {
 
         verify(entityTraceQueryService).queryTraceList(
                 1L, 100L, 200L, "trace-1", true, "checkout", "commerce", 
"prod",
-                "service.version=1.2.3 and hertzbeat.entity_type=\"service\"", 
"GET /checkout",
+                "service.version=1.2.3 and hertzbeat.entity_id=\"1\" and 
hertzbeat.entity_type=\"service\"", "GET /checkout",
                 100L, 500L, 2, 50, true, null);
     }
 
@@ -143,7 +143,7 @@ class TraceQueryControllerTest {
         TraceOverviewDto overview = new TraceOverviewDto(2, 1, 
1_710_000_000_000L, true);
         when(entityTraceQueryService.getTraceOverview(
                 9L, 100L, 200L, "trace-9", false, "payments", "core", "stage",
-                "service.version=2.0.0", "POST /pay", 200L, 900L, true, null))
+                "service.version=2.0.0 and hertzbeat.entity_id=\"9\"", "POST 
/pay", 200L, 900L, true, null))
                 .thenReturn(overview);
 
         mockMvc.perform(get("/api/traces/stats/overview")
@@ -168,7 +168,7 @@ class TraceQueryControllerTest {
 
         verify(entityTraceQueryService).getTraceOverview(
                 9L, 100L, 200L, "trace-9", false, "payments", "core", "stage",
-                "service.version=2.0.0", "POST /pay", 200L, 900L, true, null);
+                "service.version=2.0.0 and hertzbeat.entity_id=\"9\"", "POST 
/pay", 200L, 900L, true, null);
     }
 
     @Test
@@ -212,7 +212,7 @@ class TraceQueryControllerTest {
         );
         when(entityTraceQueryService.getTraceGroupByStats(
                 3L, 100L, 200L, "trace-3", true, "checkout", "commerce", 
"prod",
-                "host.name=checkout-1", "GET /checkout", 100L, 500L,
+                "host.name=checkout-1 and hertzbeat.entity_id=\"3\"", "GET 
/checkout", 100L, 500L,
                 "resource:service.version", 7, "latency-p95-desc", 5, true, 
"entrypoint"))
                 .thenReturn(result);
 
@@ -246,7 +246,7 @@ class TraceQueryControllerTest {
 
         verify(entityTraceQueryService).getTraceGroupByStats(
                 3L, 100L, 200L, "trace-3", true, "checkout", "commerce", 
"prod",
-                "host.name=checkout-1", "GET /checkout", 100L, 500L,
+                "host.name=checkout-1 and hertzbeat.entity_id=\"3\"", "GET 
/checkout", 100L, 500L,
                 "resource:service.version", 7, "latency-p95-desc", 5, true, 
"entrypoint");
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to