Skip to content

Commit

Permalink
feat: add service filter to getServicesInfo api #290
Browse files Browse the repository at this point in the history
  • Loading branch information
sunface committed Oct 18, 2023
1 parent 14c3f43 commit ad4f636
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
20 changes: 17 additions & 3 deletions query/internal/plugins/builtin/observability/api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"strconv"
"strings"

ch "github.com/ClickHouse/clickhouse-go/v2"
obmodels "github.com/DataObserve/datav/query/internal/plugins/builtin/observability/models"
Expand Down Expand Up @@ -79,6 +80,15 @@ func GetServiceInfoList(c *gin.Context, ds *models.Datasource, conn ch.Conn, par
return models.GenPluginResult(models.PluginStatusError, "start and end is required", nil)
}

serviceFilter := ""
serviceI := params["service"]
serviceNames := make([]string, 0)
if serviceI != nil {
service := serviceI.(string)
serviceFilter = " serviceName IN @serviceNames AND"
serviceNames = strings.Split(service, "|")
}

serviceMap := make(map[string]*ServiceInfo)
query := fmt.Sprintf(
`SELECT
Expand All @@ -88,11 +98,15 @@ func GetServiceInfoList(c *gin.Context, ds *models.Datasource, conn ch.Conn, par
count(DISTINCT traceID) as numCalls,
count(*) as numOperations
FROM %s.%s
WHERE timestamp>= %d AND timestamp<= %d
WHERE %s timestamp>= %d AND timestamp<= %d
GROUP BY serviceName`,
config.Data.Observability.DefaultTraceDB, obmodels.DefaultIndexTable, start, end)
config.Data.Observability.DefaultTraceDB, obmodels.DefaultIndexTable, serviceFilter, start, end)
args := []interface{}{}
args = append(args,
ch.Named("serviceNames", serviceNames),
)

rows, err := conn.Query(c.Request.Context(), query)
rows, err := conn.Query(c.Request.Context(), query, args...)
if err != nil {
logger.Warn("Error Query service operations", "query", query, "error", err)
return models.GenPluginResult(models.PluginStatusError, err.Error(), nil)
Expand Down
2 changes: 2 additions & 0 deletions query/pkg/models/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ type Variable struct {
SortWeight int `json:"sortWeight"`
TeamId int64 `json:"teamId"`
}

const VarialbeAllOption = "__all__"
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ const apiList = [{
params: `{
"env": "prod"
}`,
paramsDesc: [["env", "environment name, such as dev, test, prod etc"]]
paramsDesc: [
["env", "environment name, such as dev, test, prod etc"],
["service", "filter by service names, e.g datav|driver"]
]
},
{
name: "getServiceNames",
Expand Down

0 comments on commit ad4f636

Please sign in to comment.