Skip to content

Commit

Permalink
telemetry: fix the bug of annoying log caused by telemetry (#25907)
Browse files Browse the repository at this point in the history
  • Loading branch information
YinWeiling authored Jul 15, 2021
1 parent d56be06 commit 9d234b6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion telemetry/data_slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func updateCurrentSQB(ctx sessionctx.Context) (err error) {
pQueryCtx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
pQueryTs := time.Now().Add(-time.Minute)
promQL := "tidb_server_slow_query_process_duration_seconds_bucket{sql_type=\"general\"}"
promQL := "avg(tidb_server_slow_query_process_duration_seconds_bucket{sql_type=\"general\"}) by (le)"
value, err := querySQLMetric(pQueryCtx, pQueryTs, promQL)

if err != nil && err != infosync.ErrPrometheusAddrIsNotSet {
Expand Down
27 changes: 14 additions & 13 deletions telemetry/data_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
promv1 "github.com/prometheus/client_golang/api/prometheus/v1"
pmodel "github.com/prometheus/common/model"
"go.uber.org/atomic"
"go.uber.org/zap"
)

var (
Expand Down Expand Up @@ -69,10 +68,10 @@ type windowData struct {
SQLUsage sqlUsageData `json:"SQLUsage"`
}

type sqlType map[string]int64
type sqlType map[string]uint64

type sqlUsageData struct {
SQLTotal int64 `json:"total"`
SQLTotal uint64 `json:"total"`
SQLType sqlType `json:"type"`
}

Expand All @@ -96,8 +95,8 @@ var (
subWindowsLock = sync.RWMutex{}
)

func getSQLSum(sqlTypeData *sqlType) int64 {
result := int64(0)
func getSQLSum(sqlTypeData *sqlType) uint64 {
result := uint64(0)
for _, v := range *sqlTypeData {
result += v
}
Expand All @@ -106,12 +105,13 @@ func getSQLSum(sqlTypeData *sqlType) int64 {

func readSQLMetric(timepoint time.Time, SQLResult *sqlUsageData) error {
ctx := context.TODO()
promQL := "sum(tidb_executor_statement_total{}) by (instance,type)"
promQL := "avg(tidb_executor_statement_total{}) by (type)"
result, err := querySQLMetric(ctx, timepoint, promQL)
if err != nil {
logutil.BgLogger().Warn("querySQLMetric got error")
analysisSQLUsage(result, SQLResult)
} else {
analysisSQLUsage(result, SQLResult)
}
anylisSQLUsage(result, SQLResult)
return nil
}

Expand Down Expand Up @@ -149,7 +149,7 @@ func querySQLMetric(ctx context.Context, queryTime time.Time, promQL string) (re
return result, err
}

func anylisSQLUsage(promResult pmodel.Value, SQLResult *sqlUsageData) {
func analysisSQLUsage(promResult pmodel.Value, SQLResult *sqlUsageData) {
if promResult == nil {
return
}
Expand All @@ -159,7 +159,7 @@ func anylisSQLUsage(promResult pmodel.Value, SQLResult *sqlUsageData) {
for _, m := range matrix {
v := m.Value
promLable := string(m.Metric[pmodel.LabelName("type")])
SQLResult.SQLType[promLable] = int64(float64(v))
SQLResult.SQLType[promLable] = uint64(v)
}
}
}
Expand Down Expand Up @@ -188,10 +188,11 @@ func RotateSubWindow() {
},
}

if err := readSQLMetric(time.Now(), &thisSubWindow.SQLUsage); err != nil {
logutil.BgLogger().Error("Error exists when calling prometheus", zap.Error(err))

err := readSQLMetric(time.Now(), &thisSubWindow.SQLUsage)
if err != nil {
logutil.BgLogger().Info("Error exists when getting the SQL Metric.")
}

thisSubWindow.SQLUsage.SQLTotal = getSQLSum(&thisSubWindow.SQLUsage.SQLType)

subWindowsLock.Lock()
Expand Down

0 comments on commit 9d234b6

Please sign in to comment.