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 6, 2025
1 parent 10c785e commit 18f5f84
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 260 deletions.
1 change: 1 addition & 0 deletions service/go.work
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use (
./exceptionmonitor
./launchpad
./pay
vlogs
)

replace (
Expand Down
8 changes: 8 additions & 0 deletions service/go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -746,17 +746,24 @@ github.com/astaxie/beego v1.12.3 h1:SAQkdD2ePye+v8Gn1r4X6IKZM1wd28EyUOVQ3PDSOOQ=
github.com/astaxie/beego v1.12.3/go.mod h1:p3qIm0Ryx7zeBHLljmd7omloyca1s4yu1a8kM1FkpIA=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.19 h1:woXadbf0c7enQ2UGCi8gW/WuKmE0xIzxBF/eD94jMKQ=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.19/go.mod h1:zminj5ucw7w0r65bP6nhyOd3xL6veAUMc3ElGMoLVb4=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.21 h1:AmoU1pziydclFT/xRV+xXE/Vb8fttJCLRPv8oAkprc0=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.21/go.mod h1:AjUdLYe4Tgs6kpH4Bv7uMZo7pottoyHMn4eTcIcneaY=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0/go.mod h1:0jp+ltwkf+SwG2fm/PKo8t4y8pJSgOCO4D8Lz3k0aHQ=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 h1:iXtILhvDxB6kPvEXgsDhGaZCSC6LQET5ZHSdJozeI0Y=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1/go.mod h1:9nu0fVANtYiAePIBh2/pFUSwtJ402hLnp854CNoDOeE=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.4/go.mod h1:4GQbF1vJzG60poZqWatZlhP31y8PGCCVTvIGPdaaYJ0=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6 h1:50+XsN70RS7dwJ2CkVNXzj7U2L1HKP8nqTd3XWEXBN4=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6/go.mod h1:WqgLmwY7so32kG01zD8CPTJWVWM+TzJoOVHwTg4aPug=
github.com/aws/aws-sdk-go-v2/service/sso v1.24.5/go.mod h1:wrMCEwjFPms+V86TCQQeOxQF/If4vT44FGIOFiMC2ck=
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 h1:rLnYAfXQ3YAccocshIH5mzNNwZBkBo+bP6EhIxak6Hw=
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7/go.mod h1:ZHtuQJ6t9A/+YDuxOLnbryAmITtr8UysSny3qcyvJTc=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.4/go.mod h1:Tp/ly1cTjRLGBBmNccFumbZ8oqpZlpdhFf80SrRh4is=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6 h1:JnhTZR3PiYDNKlXy50/pNeix9aGMo6lLpXwJ1mw8MD4=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6/go.mod h1:URronUEGfXZN1VpdktPSD1EkAL9mfrV+2F4sjH38qOY=
github.com/aws/aws-sdk-go-v2/service/sts v1.32.4/go.mod h1:9XEUty5v5UAsMiFOBJrNibZgwCeOma73jgGwwhgffa8=
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2 h1:s4074ZO1Hk8qv65GqNXqDjmkf4HSQqJukaLuuW0TpDA=
github.com/aws/aws-sdk-go-v2/service/sts v1.33.2/go.mod h1:mVggCnIWoM09jP71Wh+ea7+5gAp53q+49wDFs1SW5z8=
github.com/bazelbuild/rules_go v0.49.0 h1:5vCbuvy8Q11g41lseGJDc5vxhDjJtfxr6nM/IC4VmqM=
github.com/bazelbuild/rules_go v0.49.0/go.mod h1:Dhcz716Kqg1RHNWos+N6MlXNkjNP2EwZQ0LukRKJfMs=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down Expand Up @@ -1858,6 +1865,7 @@ google.golang.org/genproto/googleapis/bytestream v0.0.0-20231030173426-d783a09b4
google.golang.org/genproto/googleapis/bytestream v0.0.0-20231030173426-d783a09b4405/go.mod h1:GRUCuLdzVqZte8+Dl/D4N25yLzcGqqWaYkeVOwulFqw=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241021214115-324edc3d5d38 h1:42FIsHvG4GOaVHLDMcy/YMqC4clCbgAPojbcA2hXp5w=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241021214115-324edc3d5d38/go.mod h1:T8O3fECQbif8cez15vxAcjbwXxvL2xbnvbQ7ZfiMAMs=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241118233622-e639e219e697 h1:rY93Be8/KL+EtFM4im9lxMzjGn796GnwVUd75cyFCJg=
google.golang.org/genproto/googleapis/bytestream v0.0.0-20241118233622-e639e219e697/go.mod h1:qUsLYwbwz5ostUWtuFuXPlHmSJodC5NI/88ZlHj4M1o=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230526203410-71b5a4ffd15e/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc h1:XSJ8Vk1SWuNr8S18z1NZSziL0CPIXLCCMDOEFtHBOFc=
Expand Down
25 changes: 14 additions & 11 deletions service/pkg/api/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,22 @@ type Stats struct {
ExecutionTimeMsec int `json:"executionTimeMsec"`
}

type VlogsRequest struct {
Time string
NS string
App string
Pod string
Limit string
Json string
Keyword string
type JsonQuery struct {
key string
mode string
value string
}

type VlogsResult struct {
Time string `json:"_time"`
Msg string `json:"_msg"`
type VlogsRequest struct {
Time string
Namespace string
App string
Pod string
Container string
Limit string
JsonMode string
Keyword []string
JsonQuery []JsonQuery
}

var (
Expand Down
163 changes: 22 additions & 141 deletions service/vlogs/request/req.go
Original file line number Diff line number Diff line change
@@ -1,194 +1,75 @@
package request

import (
"bufio"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"github.com/labring/sealos/service/pkg/api"
"io"
"net/http"
"net/url"
"os"
"strings"
)

const (
defaultTime = "_time:1h"
defaultLimit = "| limit 10"
)

func GetQuery(query *api.VlogsRequest) (string, error) {
var builder strings.Builder

// 添加关键词
builder.WriteString(query.Keyword)
builder.WriteString(" ")

// 判断 namespace
if query.NS == "" {
return "", errors.New("namespace (NS) is required")
}
builder.WriteString(fmt.Sprintf("{namespace=%s}", query.NS))
builder.WriteString(" ")

// 添加 pod
if query.Pod != "" {
builder.WriteString(fmt.Sprintf("pod:%s", query.Pod))
builder.WriteString(" ")
}

// 添加时间
if query.Time == "" {
builder.WriteString(defaultTime)
} else {
builder.WriteString("_time:")
builder.WriteString(query.Time)
}
builder.WriteString(" ")

// JSON 模式
if query.Json == "true" {
builder.WriteString("| unpack_json")
builder.WriteString(" ")
}

// 添加 limit
if query.Limit == "" {
builder.WriteString(defaultLimit)
} else {
builder.WriteString("| limit ")
builder.WriteString(query.Limit)
}
builder.WriteString(" ")

//添加field

return builder.String(), nil
}

func QueryLogsByParams(query *api.VlogsRequest) ([]api.VlogsResult, error) {
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
func generateReq(query string) (*http.Request, error) {
//地址
vlogsHost := GetVLogsServerFromEnv()
vlogsHost = "https://vvvvvlogs.192.168.10.35.nip.io"
if vlogsHost == "" {
return nil, api.ErrNoVMHost
}
result, _ := GetQuery(query)
//if vlogsHost == "" {
// return nil, api.ErrNoVMHost
//}
baseURL, err := url.Parse(vlogsHost + "/select/logsql/query")
if err != nil {
return nil, fmt.Errorf("无法解析 API URL: %v", err)
}

//参数
params := url.Values{}
params.Add("query", result)
params.Add("query", query)
baseURL.RawQuery = params.Encode()

// 创建 HTTP 请求
//创建请求
req, err := http.NewRequest("GET", baseURL.String(), nil)
if err != nil {
return nil, fmt.Errorf("创建 HTTP 请求失败: %v", err)
}

// 添加 Basic Auth 认证
//认证
username, password := GetVLogsUsernameAndPasswordFromEnv()
username = "admin"
password = "sealos@123#@!"
req.SetBasicAuth(username, password)

// 发起请求
resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("HTTP 请求失败: %v", err)
}
defer resp.Body.Close()
// 检查 HTTP 状态码
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("请求失败,状态码: %d", resp)
}

// 使用 Scanner 逐行读取响应内容
var results []api.VlogsResult
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
var entry api.VlogsResult
err := json.Unmarshal([]byte(line), &entry)
if err != nil {
fmt.Printf("解析日志行失败: %v, 行内容: %s\n", err, line)
continue
}
results = append(results, entry)
}

return results, nil
return req, nil
}

func QueryLogsByLogsQl(query string) ([]api.VlogsResult, error) {
func QueryLogsByParams(query string, rw http.ResponseWriter) error {
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
vlogsHost := GetVLogsServerFromEnv()
vlogsHost = "https://vvvvvlogs.192.168.10.35.nip.io"
if vlogsHost == "" {
return nil, api.ErrNoVMHost
}
result := query
baseURL, err := url.Parse(vlogsHost + "/select/logsql/query")
if err != nil {
return nil, fmt.Errorf("无法解析 API URL: %v", err)
}
params := url.Values{}
params.Add("query", result)
baseURL.RawQuery = params.Encode()

// 创建 HTTP 请求
req, err := http.NewRequest("GET", baseURL.String(), nil)
req, err := generateReq(query)
if err != nil {
return nil, fmt.Errorf("创建 HTTP 请求失败: %v", err)
return err
}

// 添加 Basic Auth 认证
username, password := GetVLogsUsernameAndPasswordFromEnv()
username = "admin"
password = "sealos@123#@!"
req.SetBasicAuth(username, password)

// 发起请求
resp, err := httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("HTTP 请求失败: %v", err)
return fmt.Errorf("HTTP 请求失败: %v", err)
}
defer resp.Body.Close()

// 检查 HTTP 状态码
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("请求失败,状态码: %d", resp)
}

// 使用 Scanner 逐行读取响应内容
var results []api.VlogsResult
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
var entry api.VlogsResult
err := json.Unmarshal([]byte(line), &entry)
return fmt.Errorf("请求失败,状态码: %d", resp)
} else {
_, err := io.Copy(rw, resp.Body)
if err != nil {
fmt.Printf("解析日志行失败: %v, 行内容: %s\n", err, line)
continue
return err
}
results = append(results, entry)
}

return results, nil
return nil
}

func GetVLogsUsernameAndPasswordFromEnv() (string, string) {
Expand Down
Loading

0 comments on commit 18f5f84

Please sign in to comment.