Skip to content

Commit

Permalink
feat:add vlogs server
Browse files Browse the repository at this point in the history
  • Loading branch information
bearslyricattack committed Jan 10, 2025
1 parent d5a8af9 commit 0b977a9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
22 changes: 12 additions & 10 deletions service/pkg/api/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ type JsonQuery struct {
}

type VlogsRequest struct {
Time string
Namespace string
App string
Limit string
JsonMode string
StderrMode string
Pod []string
Container []string
Keyword []string
JsonQuery []JsonQuery
Time string `json:"time"`
Namespace string `json:"namespace"`
App string `json:"app"`
Limit string `json:"limit"`
JsonMode string `json:"jsonMode"`
StderrMode string `json:"stderrMode"`
NumberMode string `json:"numberMode"`
NumberLevel string `json:"numberLevel"`
Pod []string `json:"pod"`
Container []string `json:"container"`
Keyword string `json:"keyword"`
JsonQuery []JsonQuery `json:"jsonQuery"`
}

var (
Expand Down
2 changes: 1 addition & 1 deletion service/vlogs/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
server:
addr: ":8428"
path: "https://vvvvvlogs.192.168.10.35.nip.io"
path: "https://vvvvvlogs.usw.sailos.io"
username: "admin"
password: "sealos@123#@!"
39 changes: 28 additions & 11 deletions service/vlogs/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (vl *VLogsServer) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
}

func (vl *VLogsServer) queryLogsByParams(rw http.ResponseWriter, req *http.Request) {
//kubeConfig, namespace, query, err := vl.ParseParamsRequest(req)
//kubeConfig, namespace, query, err := vl.generateParamsRequest(req)
_, _, query, err := vl.generateParamsRequest(req)
if err != nil {
http.Error(rw, fmt.Sprintf("Bad request (%s)", err), http.StatusBadRequest)
Expand Down Expand Up @@ -103,22 +103,24 @@ func (vl *VLogsServer) generateParamsRequest(req *http.Request) (string, string,

func generateKeywordQuery(req *api.VlogsRequest) (string, error) {
var builder strings.Builder
for _, key := range req.Keyword {
builder.WriteString(key)
builder.WriteString(" ")
}
builder.WriteString(req.Keyword)
builder.WriteString(" ")
stream, err := generateStreamQuery(req)
if err != nil {
return "", err
}
return builder.String() + stream + generateCommonQuery(req) + generateDropQuery(), nil
return builder.String() + stream + generateCommonQuery(req) + generateDropQuery() + generateNumberQuery(req), nil
}

func generateJsonQuery(req *api.VlogsRequest) (string, error) {
stream, err := generateStreamQuery(req)
if err != nil {
return "", err
}
var builder strings.Builder
builder.WriteString(" | unpack_json")
if len(req.JsonQuery) == 0 {
return builder.String(), nil
return stream + generateCommonQuery(req) + builder.String() + generateDropQuery() + generateNumberQuery(req), nil
}
for _, jsonQuery := range req.JsonQuery {
var item string
Expand All @@ -134,7 +136,7 @@ func generateJsonQuery(req *api.VlogsRequest) (string, error) {
}
builder.WriteString(item)
}
return "", nil
return stream + generateCommonQuery(req) + builder.String() + generateDropQuery() + generateNumberQuery(req), nil
}

func generateStreamQuery(req *api.VlogsRequest) (string, error) {
Expand Down Expand Up @@ -178,11 +180,15 @@ func generateStreamQuery(req *api.VlogsRequest) (string, error) {

func generateCommonQuery(req *api.VlogsRequest) string {
var builder strings.Builder
item := fmt.Sprintf(`_time:%s app:="%s" `, req.Time, req.App)
builder.WriteString(item)
if req.StderrMode == "true" {
item := fmt.Sprintf(` _time:%s app:="%s" stream:="stderr" | limit %s `, req.Time, req.App, req.Limit)
item := fmt.Sprintf(` stream:="stderr" `)
builder.WriteString(item)
} else {
item := fmt.Sprintf(` _time:%s app:="%s" | limit %s `, req.Time, req.App, req.Limit)
}
// if query number,dont use limit param
if req.NumberMode == "false" {
item := fmt.Sprintf(` | limit %s `, req.Limit)
builder.WriteString(item)
}
return builder.String()
Expand All @@ -193,3 +199,14 @@ func generateDropQuery() string {
builder.WriteString("| Drop _stream_id,_stream,app,container,job,namespace,node,pod ")
return builder.String()
}

func generateNumberQuery(req *api.VlogsRequest) string {
var builder strings.Builder
if req.NumberMode == "true" {
item := fmt.Sprintf(" | stats by (_time:1%s) count() logs_total ", req.NumberLevel)
builder.WriteString(item)
return builder.String()
} else {
return ""
}
}

0 comments on commit 0b977a9

Please sign in to comment.