Skip to content

Commit

Permalink
Merge pull request #140 from taosdata/feat/TD-31615
Browse files Browse the repository at this point in the history
fix log bug
  • Loading branch information
sheyanjie-qq authored Sep 14, 2024
2 parents 861e9e7 + 10ce388 commit b0475e6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
10 changes: 5 additions & 5 deletions api/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ func (r *Reporter) Init(c gin.IRouter) {
}

func (r *Reporter) getConn() *db.Connector {
qid := util.GetQidOwn()

logger := logger.WithFields(
logrus.Fields{config.ReqIDKey: qid},
)

conn, err := db.NewConnector(r.username, r.password, r.host, r.port, r.usessl)
if err != nil {
qid := util.GetQidOwn()

logger := logger.WithFields(
logrus.Fields{config.ReqIDKey: qid},
)
logger.Errorf("connect to database error, msg:%s", err)
panic(err)
}
Expand Down
37 changes: 28 additions & 9 deletions db/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
_ "github.com/taosdata/driver-go/v3/taosRestful"
"github.com/taosdata/taoskeeper/infrastructure/config"
"github.com/taosdata/taoskeeper/infrastructure/log"
"github.com/taosdata/taoskeeper/util"
)

type Connector struct {
Expand All @@ -35,10 +36,16 @@ func NewConnector(username, password, host string, port int, usessl bool) (*Conn
} else {
protocol = "http"
}
dbLogger := dbLogger.WithFields(logrus.Fields{config.ReqIDKey: util.GetQidOwn()})
dbLogger.Tracef("connect to adapter, host:%s, port:%d, usessl:%v", host, port, usessl)

db, err := sql.Open("taosRestful", fmt.Sprintf("%s:%s@%s(%s:%d)/?skipVerify=true", username, password, protocol, host, port))
if err != nil {
dbLogger.Errorf("connect to adapter failed, host:%s, port:%d, usessl:%v, error:%s", host, port, usessl, err)
return nil, err
}

dbLogger.Tracef("connect to adapter success, host:%s, port:%d, usessl:%v", host, port, usessl)
return &Connector{db: db}, nil
}

Expand All @@ -49,17 +56,25 @@ func NewConnectorWithDb(username, password, host string, port int, dbname string
} else {
protocol = "http"
}

dbLogger := dbLogger.WithFields(logrus.Fields{config.ReqIDKey: util.GetQidOwn()})
dbLogger.Tracef("connect to adapter, host:%s, port:%d, usessl:%v", host, port, usessl)

db, err := sql.Open("taosRestful", fmt.Sprintf("%s:%s@%s(%s:%d)/%s?skipVerify=true", username, password, protocol, host, port, dbname))
if err != nil {
dbLogger.Errorf("connect to adapter failed, host:%s, port:%d, db:%s, usessl:%v, error:%s", host, port, dbname, usessl, err)
return nil, err
}

dbLogger.Tracef("connect to adapter success, host:%s, port:%d, db:%s, usessl:%v", host, port, dbname, usessl)
return &Connector{db: db}, nil
}

func (c *Connector) Exec(ctx context.Context, sql string, qid uint64) (int64, error) {
dbLogger = dbLogger.WithFields(logrus.Fields{config.ReqIDKey: qid})
dbLogger := dbLogger.WithFields(logrus.Fields{config.ReqIDKey: qid})
ctx = context.WithValue(ctx, common.ReqIDKey, int64(qid))

dbLogger.Tracef("call adapter to execute sql:%s", sql)
startTime := time.Now()
res, err := c.db.ExecContext(ctx, sql)

Expand All @@ -78,11 +93,15 @@ func (c *Connector) Exec(ctx context.Context, sql string, qid uint64) (int64, er
return 0, err
}

if dbLogger.Logger.IsLevelEnabled(logrus.TraceLevel) {
dbLogger.Tracef("latency:%v, sql:%s", latency, sql)
rowsAffected, err := res.RowsAffected()
if err != nil {
dbLogger.Errorf("latency:%v, err:%s", latency, err)
return rowsAffected, err
}

return res.RowsAffected()
dbLogger.Tracef("response ok, rowsAffected:%v, latency:%v", rowsAffected, latency)

return rowsAffected, err
}

func logData(data *Data, logger *logrus.Entry) {
Expand All @@ -96,13 +115,15 @@ func logData(data *Data, logger *logrus.Entry) {
logger.Errorf("Failed to marshal data to JSON: %v", err)
return
}
logger.Tracef("%s", jsonData)
logger.Tracef("query result data:%s", jsonData)
}

func (c *Connector) Query(ctx context.Context, sql string, qid uint64) (*Data, error) {
dbLogger = dbLogger.WithFields(logrus.Fields{config.ReqIDKey: qid})
dbLogger := dbLogger.WithFields(logrus.Fields{config.ReqIDKey: qid})
ctx = context.WithValue(ctx, common.ReqIDKey, int64(qid))

dbLogger.Tracef("call adapter to execute query, sql:%s", sql)

startTime := time.Now()
rows, err := c.db.QueryContext(ctx, sql)

Expand All @@ -121,9 +142,7 @@ func (c *Connector) Query(ctx context.Context, sql string, qid uint64) (*Data, e
return nil, err
}

if dbLogger.Logger.IsLevelEnabled(logrus.TraceLevel) {
dbLogger.Tracef("latency:%v, sql:%s", latency, sql)
}
dbLogger.Tracef("response ok, latency:%v, sql:%s", latency, sql)

data := &Data{}
data.Head, err = rows.Columns()
Expand Down
3 changes: 2 additions & 1 deletion infrastructure/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

var logger = logrus.New()
var ServerID = randomID()
var globalLogFormatter = &TaosLogFormatter{}
var finish = make(chan struct{})
var exist = make(chan struct{})
Expand Down Expand Up @@ -179,7 +180,7 @@ func (t *TaosLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
b.Reset()
b.WriteString(entry.Time.Format("01/02 15:04:05.000000"))
b.WriteByte(' ')
b.WriteString(fmt.Sprintf("%d", config.Conf.InstanceID))
b.WriteString(ServerID)
b.WriteByte(' ')
v, exist := entry.Data[config.ModelKey]
if exist && v != nil {
Expand Down
1 change: 1 addition & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func GetQid(qidStr string) uint64 {
func GetQidOwn() uint64 {

id := atomic.AddUint64(&globalCounter64, 1)

if id > 0x00ffffffffffffff {
atomic.StoreUint64(&globalCounter64, 1)
id = 1
Expand Down

0 comments on commit b0475e6

Please sign in to comment.