Skip to content

Commit

Permalink
ddc support logs detail
Browse files Browse the repository at this point in the history
  • Loading branch information
yangxin16 committed Aug 18, 2021
1 parent 38d738b commit 4d48c76
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bce/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// Constants and default values for the package bce
const (
SDK_VERSION = "0.9.85"
SDK_VERSION = "0.9.86"
URI_PREFIX = "/" // now support uri without prefix "v1" so just set root path
DEFAULT_DOMAIN = "baidubce.com"
DEFAULT_PROTOCOL = "http"
Expand Down
95 changes: 95 additions & 0 deletions doc/DDCv2.md
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,101 @@ fmt.Println("bbc access logs link: ", downloadInfo.Downloadurl.Bbc)
fmt.Println("bos access logs link: ", downloadInfo.Downloadurl.Bos)
```

## 错误日志
通过此接口可以获取错误日志(仅支持DDC)。
```go
// import ddcrds "github.com/baidubce/bce-sdk-go/services/ddc/v2"

args := &ddcrds.GetErrorLogsArgs{
// 实例ID,必填参数
InstanceId: "ddc-mp8lme9w",
// 开始时间,UTC 格式,必填参数
StartTime: "2021-08-16T02:28:51Z",
// 结束时间,UTC 格式,必填参数
EndTime: "2021-08-17T02:28:51Z",
// 分页页码,必填参数
PageNo: 1,
// 分页大小,取值范围:[1-2000],必填参数
PageSize: 10,
// 实例角色,取值:master(主实例-主节点)、backup(主实例-备节点)、slave(只读实例),必填参数
Role: "master",
// 搜索关键词,选填参数
KeyWord: "Aborted",
}

errorLogsResponse, err := client.GetErrorLogs(args)
if err != nil {
fmt.Printf("get error logs error: %+v\n", err)
return
}
fmt.Println("get error logs success.")
fmt.Println("error logs count: ", errorLogsResponse.Count)
for _, errorLog := range errorLogsResponse.ErrorLogs {
fmt.Println("=================================================")
fmt.Println("error log instanceId: ", errorLog.InstanceId)
// 采集时间,本地时间
fmt.Println("error log executeTime: ", errorLog.ExecuteTime)
fmt.Println("error log logLevel: ", errorLog.LogLevel)
// 日志内容
fmt.Println("error log logText: ", errorLog.LogText)
}
```

## 慢日志
通过此接口可以获取慢日志(仅支持DDC)。
```go
// import ddcrds "github.com/baidubce/bce-sdk-go/services/ddc/v2"

args := &ddcrds.GetSlowLogsArgs{
// 实例ID,必填参数
InstanceId: "ddc-mp8lme9w",
// 开始时间,UTC 格式,必填参数
StartTime: "2021-08-16T02:28:51Z",
// 结束时间,UTC 格式,必填参数
EndTime: "2021-08-17T02:28:51Z",
// 分页页码,必填参数
PageNo: 1,
// 分页大小,取值范围:[1-2000],必填参数
PageSize: 10,
// 实例角色,取值:master(主实例-主节点)、backup(主实例-备节点)、slave(只读实例),必填参数
Role: "master",
// 数据库名列表,选填参数
DbName: []string{"baidu_dba"},
// 用户名列表,选填参数
UserName: []string{"_root"},
// 客户端IP列表,选填参数
HostIp: []string{"localhost"},
// SQL 语句,选填参数
Sql: "update heartbeat set id=?, value=?",
}

slowLogsResponse, err := client.GetSlowLogs(args)
if err != nil {
fmt.Printf("get slow logs error: %+v\n", err)
return
}
fmt.Println("get slow logs success.")
fmt.Println("slow logs count: ", slowLogsResponse.Count)
for _, slowLog := range slowLogsResponse.SlowLogs {
fmt.Println("=================================================")
fmt.Println("slow log instanceId: ", slowLog.InstanceId)
fmt.Println("slow log userName: ", slowLog.UserName)
fmt.Println("slow log dbName: ", slowLog.DbName)
fmt.Println("slow log hostIp: ", slowLog.HostIp)
// 执行时长,单位:秒
fmt.Println("slow log queryTime: ", slowLog.QueryTime)
// 加锁时长,单位:秒
fmt.Println("slow log lockTime: ", slowLog.LockTime)
// 解析行数
fmt.Println("slow log rowsExamined: ", slowLog.RowsExamined)
// 返回行数
fmt.Println("slow log rowsSent: ", slowLog.RowsSent)
fmt.Println("slow log sql: ", slowLog.Sql)
// 执行时间
fmt.Println("slow log executeTime: ", slowLog.ExecuteTime)
}
```

# 其他
## VPC列表
使用以下代码可以查询vpc列表(仅支持DDC)。
Expand Down
8 changes: 8 additions & 0 deletions services/ddc/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ func getLogsUrlWithLogId(instanceId, logId string) string {
return URI_PREFIX + REQUEST_DDC_INSTANCE_URL + "/" + instanceId + REQUEST_DDC_LOG_URL + "/" + logId
}

func getErrorLogsUrlWithInstanceId(instanceId string) string {
return URI_PREFIX + REQUEST_DDC_INSTANCE_URL + "/" + instanceId + REQUEST_DDC_LOG_URL + "/logErrorDetail"
}

func getSlowLogsUrlWithInstanceId(instanceId string) string {
return URI_PREFIX + REQUEST_DDC_INSTANCE_URL + "/" + instanceId + REQUEST_DDC_LOG_URL + "/logSlowDetail"
}

func getCreateTableHardLinkUrl(instanceId, dbName string) string {
return URI_PREFIX + REQUEST_DDC_INSTANCE_URL + "/" + instanceId +
REQUEST_DDC_DATABASE_URL + "/" + dbName +
Expand Down
61 changes: 61 additions & 0 deletions services/ddc/v2/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1582,3 +1582,64 @@ func TestClient_GetAccessLog(t *testing.T) {
fmt.Println("bbc access logs link: ", downloadInfo.Downloadurl.Bbc)
fmt.Println("bos access logs link: ", downloadInfo.Downloadurl.Bos)
}

func TestClient_GetErrorLogs(t *testing.T) {
args := &GetErrorLogsArgs{
InstanceId: "ddc-mp8lme9w",
StartTime: "2021-08-16T02:28:51Z",
EndTime: "2021-08-17T02:28:51Z",
PageNo: 1,
PageSize: 10,
Role: "master",
KeyWord: "Aborted",
}
errorLogsResponse, err := client.GetErrorLogs(args)
if err != nil {
fmt.Printf("get error logs error: %+v\n", err)
return
}
fmt.Println("get error logs success.")
fmt.Println("error logs count: ", errorLogsResponse.Count)
for _, errorLog := range errorLogsResponse.ErrorLogs {
fmt.Println("=================================================")
fmt.Println("error log instanceId: ", errorLog.InstanceId)
fmt.Println("error log executeTime: ", errorLog.ExecuteTime)
fmt.Println("error log logLevel: ", errorLog.LogLevel)
fmt.Println("error log logText: ", errorLog.LogText)
}
}

func TestClient_GetSlowLogs(t *testing.T) {
args := &GetSlowLogsArgs{
InstanceId: "ddc-mp8lme9w",
StartTime: "2021-08-16T02:28:51Z",
EndTime: "2021-08-17T02:28:51Z",
PageNo: 1,
PageSize: 10,
Role: "master",
DbName: []string{"baidu_dba"},
UserName: []string{"_root"},
HostIp: []string{"localhost"},
Sql: "update heartbeat set id=?, value=?",
}
slowLogsResponse, err := client.GetSlowLogs(args)
if err != nil {
fmt.Printf("get slow logs error: %+v\n", err)
return
}
fmt.Println("get slow logs success.")
fmt.Println("slow logs count: ", slowLogsResponse.Count)
for _, slowLog := range slowLogsResponse.SlowLogs {
fmt.Println("=================================================")
fmt.Println("slow log instanceId: ", slowLog.InstanceId)
fmt.Println("slow log userName: ", slowLog.UserName)
fmt.Println("slow log dbName: ", slowLog.DbName)
fmt.Println("slow log hostIp: ", slowLog.HostIp)
fmt.Println("slow log queryTime: ", slowLog.QueryTime)
fmt.Println("slow log lockTime: ", slowLog.LockTime)
fmt.Println("slow log rowsExamined: ", slowLog.RowsExamined)
fmt.Println("slow log rowsSent: ", slowLog.RowsSent)
fmt.Println("slow log sql: ", slowLog.Sql)
fmt.Println("slow log executeTime: ", slowLog.ExecuteTime)
}
}
82 changes: 82 additions & 0 deletions services/ddc/v2/ddc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2151,3 +2151,85 @@ func (c *DDCClient) GetAccessLog(date string) (*AccessLog, error) {

return result, err
}

// GetErrorLog - get error logs
//
// PARAMS:
// RETURNS:
// - *ErrorLogsResponse: the error logs
// - error: nil if success otherwise the specific error
func (c *DDCClient) GetErrorLogs(args *GetErrorLogsArgs) (*ErrorLogsResponse, error) {
if args == nil {
return nil, fmt.Errorf("unset args")
}
if len(args.InstanceId) < 1 {
return nil, fmt.Errorf("unset instanceId")
}
if len(args.StartTime) < 1 {
return nil, fmt.Errorf("unset startTime")
}
if len(args.EndTime) < 1 {
return nil, fmt.Errorf("unset endTime")
}
if args.PageNo < 1 {
return nil, fmt.Errorf("unset pageNo")
}
if args.PageSize < 1 {
return nil, fmt.Errorf("unset pageSize")
}
if len(args.Role) < 1 {
return nil, fmt.Errorf("unset role")
}

result := &ErrorLogsResponse{}
err := bce.NewRequestBuilder(c).
WithMethod(http.POST).
WithURL(getErrorLogsUrlWithInstanceId(args.InstanceId)).
WithHeader(http.CONTENT_TYPE, bce.DEFAULT_CONTENT_TYPE).
WithBody(args).
WithResult(result).
Do()

return result, err
}

// GetSlowLog - get slow logs
//
// PARAMS:
// RETURNS:
// - *SlowLogsResponse: the slow logs
// - error: nil if success otherwise the specific error
func (c *DDCClient) GetSlowLogs(args *GetSlowLogsArgs) (*SlowLogsResponse, error) {
if args == nil {
return nil, fmt.Errorf("unset args")
}
if len(args.InstanceId) < 1 {
return nil, fmt.Errorf("unset instanceId")
}
if len(args.StartTime) < 1 {
return nil, fmt.Errorf("unset startTime")
}
if len(args.EndTime) < 1 {
return nil, fmt.Errorf("unset endTime")
}
if args.PageNo < 1 {
return nil, fmt.Errorf("unset pageNo")
}
if args.PageSize < 1 {
return nil, fmt.Errorf("unset pageSize")
}
if len(args.Role) < 1 {
return nil, fmt.Errorf("unset role")
}

result := &SlowLogsResponse{}
err := bce.NewRequestBuilder(c).
WithMethod(http.POST).
WithURL(getSlowLogsUrlWithInstanceId(args.InstanceId)).
WithHeader(http.CONTENT_TYPE, bce.DEFAULT_CONTENT_TYPE).
WithBody(args).
WithResult(result).
Do()

return result, err
}
20 changes: 20 additions & 0 deletions services/ddc/v2/ddcrds.go
Original file line number Diff line number Diff line change
Expand Up @@ -1433,3 +1433,23 @@ func (c *Client) CancelMaintainTask(taskId string) error {
func (c *Client) GetAccessLog(date string) (*AccessLog, error) {
return c.ddcClient.GetAccessLog(date)
}

// GetErrorLogs - get error logs
//
// PARAMS:
// RETURNS:
// - *ErrorLogsResponse: the error logs
// - error: nil if success otherwise the specific error
func (c *Client) GetErrorLogs(args *GetErrorLogsArgs) (*ErrorLogsResponse, error) {
return c.ddcClient.GetErrorLogs(args)
}

// GetSlowLogs - get slow logs
//
// PARAMS:
// RETURNS:
// - *SlowLogsResponse: the slow logs
// - error: nil if success otherwise the specific error
func (c *Client) GetSlowLogs(args *GetSlowLogsArgs) (*SlowLogsResponse, error) {
return c.ddcClient.GetSlowLogs(args)
}
53 changes: 53 additions & 0 deletions services/ddc/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,3 +1039,56 @@ type AccessLog struct {
Mysql string `json:"mysql"`
} `json:"downloadurl"`
}

type GetErrorLogsArgs struct {
InstanceId string `json:"instanceId"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
PageNo int `json:"pageNo"`
PageSize int `json:"pageSize"`
Role string `json:"role"`
KeyWord string `json:"keyWord,omitempty"`
}

type ErrorLog struct {
InstanceId string `json:"instanceId"`
LogLevel string `json:"logLevel"`
LogText string `json:"logText"`
ExecuteTime string `json:"executeTime"`
}

type ErrorLogsResponse struct {
Count int `json:"count"`
ErrorLogs []ErrorLog `json:"errorLogs"`
}

type GetSlowLogsArgs struct {
InstanceId string `json:"instanceId"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
PageNo int `json:"pageNo"`
PageSize int `json:"pageSize"`
Role string `json:"role"`
DbName []string `json:"dbName,omitempty"`
UserName []string `json:"userName,omitempty"`
HostIp []string `json:"hostIp,omitempty"`
Sql string `json:"sql,omitempty"`
}

type SlowLog struct {
InstanceId string `json:"instanceId"`
UserName string `json:"userName"`
DbName string `json:"dbName"`
HostIp string `json:"hostIp"`
QueryTime float64 `json:"queryTime"`
LockTime float64 `json:"lockTime"`
RowsExamined int `json:"rowsExamined"`
RowsSent int `json:"rowsSent"`
Sql string `json:"sql"`
ExecuteTime string `json:"executeTime"`
}

type SlowLogsResponse struct {
Count int `json:"count"`
SlowLogs []SlowLog `json:"slowLogs"`
}

0 comments on commit 4d48c76

Please sign in to comment.