Skip to content

Commit

Permalink
*: spilt the internal SQLs in the transaction related metrics (#8255) (
Browse files Browse the repository at this point in the history
  • Loading branch information
jackysp authored and zimulala committed Nov 12, 2018
1 parent fba8fec commit 4beba41
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
8 changes: 4 additions & 4 deletions metrics/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ var (
Subsystem: "session",
Name: "retry_error_total",
Help: "Counter of session retry error.",
}, []string{LblType})
}, []string{LblSQLType, LblType})
TransactionCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "session",
Name: "transaction_total",
Help: "Counter of transactions.",
}, []string{LblType})
}, []string{LblSQLType, LblType})

SessionRestrictedSQLCounter = prometheus.NewCounter(
prometheus.CounterOpts{
Expand All @@ -86,7 +86,7 @@ var (
Name: "transaction_statement_num",
Help: "Buckated histogram of statements count in each transaction.",
Buckets: prometheus.ExponentialBuckets(1, 2, 12),
}, []string{LblType})
}, []string{LblSQLType, LblType})

TransactionDuration = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Expand All @@ -95,7 +95,7 @@ var (
Name: "transaction_duration_seconds",
Help: "Bucketed histogram of a transaction execution duration, including retry.",
Buckets: prometheus.ExponentialBuckets(0.001, 2, 16), // range 1ms ~ 64s
}, []string{LblType})
}, []string{LblSQLType, LblType})
)

// Label constants.
Expand Down
31 changes: 16 additions & 15 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,11 @@ func (s *session) doCommitWithRetry(ctx context.Context) error {
err = s.retry(ctx, uint(maxRetryCount))
}
}
label := s.getSQLLabel()
counter := s.sessionVars.TxnCtx.StatementCount
duration := time.Since(s.GetSessionVars().TxnCtx.CreateTime).Seconds()
metrics.StatementPerTransaction.WithLabelValues(metrics.RetLabel(err)).Observe(float64(counter))
metrics.TransactionDuration.WithLabelValues(metrics.RetLabel(err)).Observe(float64(duration))
metrics.StatementPerTransaction.WithLabelValues(label, metrics.RetLabel(err)).Observe(float64(counter))
metrics.TransactionDuration.WithLabelValues(label, metrics.RetLabel(err)).Observe(float64(duration))
s.cleanRetryInfo()

if isoLevelOneShot := &s.sessionVars.TxnIsolationLevelOneShot; isoLevelOneShot.State != 0 {
Expand Down Expand Up @@ -386,15 +387,15 @@ func (s *session) CommitTxn(ctx context.Context) error {
if err != nil {
label = metrics.LblError
}
metrics.TransactionCounter.WithLabelValues(label).Inc()
metrics.TransactionCounter.WithLabelValues(s.getSQLLabel(), label).Inc()
return errors.Trace(err)
}

func (s *session) RollbackTxn(ctx context.Context) error {
var err error
if s.txn.Valid() {
terror.Log(s.txn.Rollback())
metrics.TransactionCounter.WithLabelValues(metrics.LblRollback).Inc()
metrics.TransactionCounter.WithLabelValues(s.getSQLLabel(), metrics.LblRollback).Inc()
}
s.cleanRetryInfo()
s.txn.changeToInvalid()
Expand Down Expand Up @@ -439,6 +440,13 @@ const sqlLogMaxLen = 1024
// SchemaChangedWithoutRetry is used for testing.
var SchemaChangedWithoutRetry bool

func (s *session) getSQLLabel() string {
if s.sessionVars.InRestrictedSQL {
return metrics.LblInternal
}
return metrics.LblGeneral
}

func (s *session) isRetryableError(err error) bool {
if SchemaChangedWithoutRetry {
return kv.IsRetryableError(err)
Expand Down Expand Up @@ -508,13 +516,13 @@ func (s *session) retry(ctx context.Context, maxCnt uint) error {
}
if !s.isRetryableError(err) {
log.Warnf("con:%d session:%v, err:%v in retry", connID, s, err)
metrics.SessionRetryErrorCounter.WithLabelValues(metrics.LblUnretryable)
metrics.SessionRetryErrorCounter.WithLabelValues(s.getSQLLabel(), metrics.LblUnretryable)
return errors.Trace(err)
}
retryCnt++
if retryCnt >= maxCnt {
log.Warnf("con:%d Retry reached max count %d", connID, retryCnt)
metrics.SessionRetryErrorCounter.WithLabelValues(metrics.LblReachMax)
metrics.SessionRetryErrorCounter.WithLabelValues(s.getSQLLabel(), metrics.LblReachMax)
return errors.Trace(err)
}
log.Warnf("con:%d retryable error: %v, txn: %v", connID, err, s.txn)
Expand Down Expand Up @@ -748,11 +756,7 @@ func (s *session) executeStatement(ctx context.Context, connID uint64, stmtNode
}
return nil, errors.Trace(err)
}
label := metrics.LblGeneral
if s.sessionVars.InRestrictedSQL {
label = metrics.LblInternal
}
metrics.SessionExecuteRunDuration.WithLabelValues(label).Observe(time.Since(startTime).Seconds())
metrics.SessionExecuteRunDuration.WithLabelValues(s.getSQLLabel()).Observe(time.Since(startTime).Seconds())

if recordSet != nil {
recordSets = append(recordSets, recordSet)
Expand Down Expand Up @@ -786,10 +790,7 @@ func (s *session) execute(ctx context.Context, sql string) (recordSets []ast.Rec
log.Warnf("con:%d parse error:\n%v\n%s", connID, err, sql)
return nil, errors.Trace(err)
}
label := metrics.LblGeneral
if s.sessionVars.InRestrictedSQL {
label = metrics.LblInternal
}
label := s.getSQLLabel()
metrics.SessionExecuteParseDuration.WithLabelValues(label).Observe(time.Since(startTS).Seconds())

compiler := executor.Compiler{Ctx: s}
Expand Down

0 comments on commit 4beba41

Please sign in to comment.