Skip to content

chore: add tag type filter support in attribute keys #7522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 33 additions & 17 deletions pkg/query-service/app/clickhouseReader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2672,7 +2672,7 @@
var totalSpans uint64

queryStr := fmt.Sprintf("SELECT count() from %s.%s;", signozTraceDBName, r.traceTableName)
r.db.QueryRow(ctx, queryStr).Scan(&totalSpans)

Check failure on line 2675 in pkg/query-service/app/clickhouseReader/reader.go

View workflow job for this annotation

GitHub Actions / lint / go

Error return value of `(github.com/ClickHouse/clickhouse-go/v2/lib/driver.Row).Scan` is not checked (errcheck)

return totalSpans, nil
}
Expand All @@ -2685,7 +2685,7 @@
if r.useTraceNewSchema {
queryStr = fmt.Sprintf("SELECT count() from %s.%s where ts_bucket_start >= toUInt64(toUnixTimestamp(now() - toIntervalMinute(%d))) - 1800 and timestamp > toUnixTimestamp(now()-toIntervalMinute(%d));", signozTraceDBName, r.traceTableName, int(interval.Minutes()), int(interval.Minutes()))
}
r.db.QueryRow(ctx, queryStr).Scan(&spansInLastHeartBeatInterval)

Check failure on line 2688 in pkg/query-service/app/clickhouseReader/reader.go

View workflow job for this annotation

GitHub Actions / lint / go

Error return value of `(github.com/ClickHouse/clickhouse-go/v2/lib/driver.Row).Scan` is not checked (errcheck)

return spansInLastHeartBeatInterval, nil
}
Expand All @@ -2695,7 +2695,7 @@
var totalLogs uint64

queryStr := fmt.Sprintf("SELECT count() from %s.%s;", r.logsDB, r.logsTableName)
r.db.QueryRow(ctx, queryStr).Scan(&totalLogs)

Check failure on line 2698 in pkg/query-service/app/clickhouseReader/reader.go

View workflow job for this annotation

GitHub Actions / lint / go

Error return value of `(github.com/ClickHouse/clickhouse-go/v2/lib/driver.Row).Scan` is not checked (errcheck)

return totalLogs, nil
}
Expand Down Expand Up @@ -2756,7 +2756,7 @@
for rows.Next() {

var value uint64
rows.Scan(&value)

Check failure on line 2759 in pkg/query-service/app/clickhouseReader/reader.go

View workflow job for this annotation

GitHub Actions / lint / go

Error return value of `rows.Scan` is not checked (errcheck)
totalTS += value
if count == 0 {
maxTS = value
Expand Down Expand Up @@ -2797,7 +2797,7 @@
clusterInfo := []model.ClusterInfo{}

queryStr := `SELECT shard_num, shard_weight, replica_num, errors_count, slowdowns_count, estimated_recovery_time FROM system.clusters where cluster='cluster';`
r.db.Select(ctx, &clusterInfo, queryStr)

Check failure on line 2800 in pkg/query-service/app/clickhouseReader/reader.go

View workflow job for this annotation

GitHub Actions / lint / go

Error return value of `r.db.Select` is not checked (errcheck)
if len(clusterInfo) == 1 {
return clusterInfo[0].GetMapFromStruct(), nil
}
Expand Down Expand Up @@ -3928,11 +3928,16 @@
var rows driver.Rows
var response v3.FilterAttributeKeyResponse

tagTypeFilter := `tag_type != 'logfield'`
if req.TagType != "" {
tagTypeFilter = fmt.Sprintf(`tag_type != 'logfield' and tag_type = '%s'`, req.TagType)
}

if len(req.SearchText) != 0 {
query = fmt.Sprintf("select distinct tag_key, tag_type, tag_data_type from %s.%s where tag_type != 'logfield' and tag_key ILIKE $1 limit $2", r.logsDB, r.logsTagAttributeTableV2)
query = fmt.Sprintf("select distinct tag_key, tag_type, tag_data_type from %s.%s where %s and tag_key ILIKE $1 limit $2", r.logsDB, r.logsTagAttributeTableV2, tagTypeFilter)
rows, err = r.db.Query(ctx, query, fmt.Sprintf("%%%s%%", req.SearchText), req.Limit)
} else {
query = fmt.Sprintf("select distinct tag_key, tag_type, tag_data_type from %s.%s where tag_type != 'logfield' limit $1", r.logsDB, r.logsTagAttributeTableV2)
query = fmt.Sprintf("select distinct tag_key, tag_type, tag_data_type from %s.%s where %s limit $1", r.logsDB, r.logsTagAttributeTableV2, tagTypeFilter)
rows, err = r.db.Query(ctx, query, req.Limit)
}

Expand Down Expand Up @@ -3967,13 +3972,16 @@
response.AttributeKeys = append(response.AttributeKeys, key)
}

// add other attributes
for _, f := range constants.StaticFieldsLogsV3 {
if (v3.AttributeKey{} == f) {
continue
}
if len(req.SearchText) == 0 || strings.Contains(f.Key, req.SearchText) {
response.AttributeKeys = append(response.AttributeKeys, f)
// add other attributes only when the tagType is not specified
// i.e retrieve all attributes
if req.TagType == "" {
for _, f := range constants.StaticFieldsLogsV3 {
if (v3.AttributeKey{} == f) {
continue
}
if len(req.SearchText) == 0 || strings.Contains(f.Key, req.SearchText) {
response.AttributeKeys = append(response.AttributeKeys, f)
}
}
}

Expand Down Expand Up @@ -4521,7 +4529,7 @@
}
for _, event := range *eventsFromDB {
var eventMap map[string]interface{}
json.Unmarshal([]byte(event), &eventMap)

Check failure on line 4532 in pkg/query-service/app/clickhouseReader/reader.go

View workflow job for this annotation

GitHub Actions / lint / go

Error return value of `json.Unmarshal` is not checked (errcheck)
events = append(events, eventMap)
}
row[columnNames[idx]] = events
Expand Down Expand Up @@ -4715,7 +4723,12 @@
var rows driver.Rows
var response v3.FilterAttributeKeyResponse

query = fmt.Sprintf("SELECT DISTINCT(tag_key), tag_type, tag_data_type FROM %s.%s WHERE tag_key ILIKE $1 and tag_type != 'spanfield' LIMIT $2", r.TraceDB, r.spanAttributeTableV2)
tagTypeFilter := `tag_type != 'spanfield'`
if req.TagType != "" {
tagTypeFilter = fmt.Sprintf(`tag_type != 'spanfield' and tag_type = '%s'`, req.TagType)
}

query = fmt.Sprintf("SELECT DISTINCT(tag_key), tag_type, tag_data_type FROM %s.%s WHERE tag_key ILIKE $1 and %s LIMIT $2", r.TraceDB, r.spanAttributeTableV2, tagTypeFilter)

rows, err = r.db.Query(ctx, query, fmt.Sprintf("%%%s%%", req.SearchText), req.Limit)

Expand Down Expand Up @@ -4760,13 +4773,16 @@
fields = constants.DeprecatedStaticFieldsTraces
}

// add the new static fields
for _, f := range fields {
if (v3.AttributeKey{} == f) {
continue
}
if len(req.SearchText) == 0 || strings.Contains(f.Key, req.SearchText) {
response.AttributeKeys = append(response.AttributeKeys, f)
// add the new static fields only when the tagType is not specified
// i.e retrieve all attributes
if req.TagType == "" {
for _, f := range fields {
if (v3.AttributeKey{} == f) {
continue
}
if len(req.SearchText) == 0 || strings.Contains(f.Key, req.SearchText) {
response.AttributeKeys = append(response.AttributeKeys, f)
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions pkg/query-service/app/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,21 @@ func parseFilterAttributeKeyRequest(r *http.Request) (*v3.FilterAttributeKeyRequ
aggregateOperator := v3.AggregateOperator(r.URL.Query().Get("aggregateOperator"))
aggregateAttribute := r.URL.Query().Get("aggregateAttribute")
limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
tagType := v3.TagType(r.URL.Query().Get("tagType"))

// empty string is a valid tagType
// i.e retrieve all attributes
if tagType != "" {
// what is happening here?
// if tagType is undefined(uh oh javascript) or any invalid value, set it to empty string
// instead of failing the request. Ideally, we should fail the request.
// but we are not doing that to maintain backward compatibility.
if err := tagType.Validate(); err != nil {
// if the tagType is invalid, set it to empty string
tagType = ""
}
}

if err != nil {
limit = 50
}
Expand All @@ -739,6 +754,7 @@ func parseFilterAttributeKeyRequest(r *http.Request) (*v3.FilterAttributeKeyRequ
AggregateAttribute: aggregateAttribute,
Limit: limit,
SearchText: r.URL.Query().Get("searchText"),
TagType: tagType,
}
return &req, nil
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/query-service/app/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestParseFilterAttributeKeyRequest(t *testing.T) {
expectedSearchText string
expectErr bool
errMsg string
expectedTagType v3.TagType
}{
{
desc: "valid operator and data source",
Expand Down Expand Up @@ -168,6 +169,38 @@ func TestParseFilterAttributeKeyRequest(t *testing.T) {
expectedDataSource: v3.DataSourceTraces,
expectedLimit: 50,
},
{
desc: "invalid tag type",
queryString: "aggregateOperator=avg&dataSource=traces&tagType=invalid",
expectedOperator: v3.AggregateOperatorAvg,
expectedDataSource: v3.DataSourceTraces,
expectedTagType: "",
expectedLimit: 50,
},
{
desc: "valid tag type",
queryString: "aggregateOperator=avg&dataSource=traces&tagType=resource",
expectedOperator: v3.AggregateOperatorAvg,
expectedDataSource: v3.DataSourceTraces,
expectedTagType: v3.TagTypeResource,
expectedLimit: 50,
},
{
desc: "valid tag type",
queryString: "aggregateOperator=avg&dataSource=traces&tagType=scope",
expectedOperator: v3.AggregateOperatorAvg,
expectedDataSource: v3.DataSourceTraces,
expectedTagType: v3.TagTypeInstrumentationScope,
expectedLimit: 50,
},
{
desc: "valid tag type",
queryString: "aggregateOperator=avg&dataSource=traces&tagType=tag",
expectedOperator: v3.AggregateOperatorAvg,
expectedDataSource: v3.DataSourceTraces,
expectedTagType: v3.TagTypeTag,
expectedLimit: 50,
},
}

for _, reqCase := range reqCases {
Expand Down
1 change: 1 addition & 0 deletions pkg/query-service/model/v3/v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ func (q TagType) Validate() error {
type FilterAttributeKeyRequest struct {
DataSource DataSource `json:"dataSource"`
AggregateOperator AggregateOperator `json:"aggregateOperator"`
TagType TagType `json:"tagType"`
AggregateAttribute string `json:"aggregateAttribute"`
SearchText string `json:"searchText"`
Limit int `json:"limit"`
Expand Down
Loading