Skip to content

Commit

Permalink
fix: search traces by span attributes tags #290
Browse files Browse the repository at this point in the history
  • Loading branch information
sunface committed Nov 4, 2023
1 parent 8827337 commit ce10c52
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions query/internal/plugins/builtin/datav/api/traces.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"encoding/json"
"fmt"
"strconv"
"strings"
Expand All @@ -25,6 +26,7 @@ func GetTraces(c *gin.Context, ds *models.Datasource, conn ch.Conn, params map[s
limit, _ := strconv.ParseInt(c.Query("limit"), 10, 64)
min, _ := strconv.ParseInt(c.Query("min"), 10, 64)
max, _ := strconv.ParseInt(c.Query("max"), 10, 64)
rawTags := strings.TrimSpace(c.Query("tags"))

// @performace: 对 traceId 做 skip index
traceIds := strings.TrimSpace(c.Query("traceIds"))
Expand All @@ -49,10 +51,10 @@ func GetTraces(c *gin.Context, ds *models.Datasource, conn ch.Conn, params map[s
var operationNameQuery string
var operationNameQuery1 string
if operation == "" || operation == models.VarialbeAllOption {
if min > 0 || max > 0 {
query := fmt.Sprintf("select name from %s.%s where %s", config.Data.Observability.DefaultTraceDB, datavmodels.DefaultTopLevelOperationsTable, domainQuery)
operationNameQuery = fmt.Sprintf(" AND name in (%s)", query)
}
// if min > 0 || max > 0 || rawTags != "" {
query := fmt.Sprintf("select name from %s.%s where %s", config.Data.Observability.DefaultTraceDB, datavmodels.DefaultTopLevelOperationsTable, domainQuery)
operationNameQuery = fmt.Sprintf(" AND name in (%s)", query)
// }

} else {
operationNameQuery = fmt.Sprintf(" AND name='%s'", operation)
Expand All @@ -69,6 +71,33 @@ func GetTraces(c *gin.Context, ds *models.Datasource, conn ch.Conn, params map[s
durationQuery += fmt.Sprintf(" AND duration <= %d", max*1e6)
}

if rawTags != "" {
var tags map[string]interface{}
err := json.Unmarshal([]byte(rawTags), &tags)
if err != nil {
return models.GenPluginResult(models.PluginStatusError, fmt.Sprintf("decode tags error: %s", err.Error()), nil)
}

for k, v := range tags {
if strings.HasPrefix(k, "attributes.") {
realKey := k[11:]
if v == models.VarialbeAllOption {
domainQuery += fmt.Sprintf(" AND attributesMap['%s'] != ''", realKey)
} else {
domainQuery += fmt.Sprintf(" AND attributesMap['%s'] = '%s'", realKey, v)
}
} else if strings.HasPrefix(k, "resources.") {
realKey := k[10:]
if v == models.VarialbeAllOption {
domainQuery += fmt.Sprintf(" AND resourcesMap['%s'] != ''", realKey)
} else {
domainQuery += fmt.Sprintf(" AND resourcesMap['%s'] = '%s'", realKey, v)
}
}
}

}

traceIndexes := make([]*datavmodels.TraceIndex, 0)

if onlyChart != "true" {
Expand Down Expand Up @@ -297,7 +326,9 @@ type TagKey struct {
}

func GetTraceTagKeys(c *gin.Context, ds *models.Datasource, conn ch.Conn, params map[string]interface{}) models.PluginResult {
var domainQuery string
tenant := models.GetTenant(c)
domainQuery := datavutils.BuildBasicDomainQuery(tenant, params)

service := c.Query("service")
if service == "" {
serviceI := datavutils.GetValueListFromParams(params, "service")
Expand All @@ -308,15 +339,9 @@ func GetTraceTagKeys(c *gin.Context, ds *models.Datasource, conn ch.Conn, params
domainQuery += fmt.Sprintf(" AND serviceName='%s'", service)
}

if domainQuery != "" {
domainQuery = "WHERE " + domainQuery + " AND isColumn=true"
} else {
domainQuery = "WHERE isColumn=true"
}

tags := make([]*TagKey, 0)

query := fmt.Sprintf("SELECT DISTINCT tagKey,tagType,dataType FROM %s.%s %s ", config.Data.Observability.DefaultTraceDB, datavmodels.DefaultSpanAttributeKeysTable, domainQuery)
query := fmt.Sprintf("SELECT DISTINCT tagKey,tagType,dataType,isColumn FROM %s.%s WHERE (%s) OR isColumn=true", config.Data.Observability.DefaultTraceDB, datavmodels.DefaultSpanAttributeKeysTable, domainQuery)
// query traceIDs
rows, err := conn.Query(c.Request.Context(), query)
if err != nil {
Expand All @@ -327,12 +352,11 @@ func GetTraceTagKeys(c *gin.Context, ds *models.Datasource, conn ch.Conn, params

for rows.Next() {
tag := &TagKey{}
err := rows.Scan(&tag.Name, &tag.Type, &tag.DataType)
err := rows.Scan(&tag.Name, &tag.Type, &tag.DataType, &tag.IsColumn)
if err != nil {
logger.Warn("Error scan trace tag key", "error", err)
continue
}
tag.IsColumn = true
tags = append(tags, tag)
}

Expand Down

0 comments on commit ce10c52

Please sign in to comment.