-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
reparo: support pprof and metrics (#1184)
* reparo support pprof (#1182) * fix prometheus.registry (#1183) * reparo support pprof * fix * update go.mod * update * update
- Loading branch information
Showing
9 changed files
with
175 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package reparo | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"net/http/pprof" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/log" | ||
"github.com/prometheus/client_golang/prometheus/promhttp" | ||
"github.com/soheilhy/cmux" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var cmuxReadTimeout = 10 * time.Second | ||
|
||
func startHTTPServer(lis net.Listener) { | ||
router := http.NewServeMux() | ||
router.Handle("/metrics", promhttp.Handler()) | ||
|
||
router.HandleFunc("/debug/pprof/", pprof.Index) | ||
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline) | ||
router.HandleFunc("/debug/pprof/profile", pprof.Profile) | ||
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol) | ||
router.HandleFunc("/debug/pprof/trace", pprof.Trace) | ||
|
||
httpServer := &http.Server{ | ||
Handler: router, | ||
} | ||
err := httpServer.Serve(lis) | ||
err = errors.Cause(err) | ||
if err != nil && !isErrNetClosing(err) && err != http.ErrServerClosed { | ||
log.Info("reparo http handler return with error", zap.String("error", err.Error())) | ||
} | ||
} | ||
|
||
func startReparoService(addr string) error { | ||
rootLis, err := net.Listen("tcp", addr) | ||
if err != nil { | ||
return errors.Annotate(err, "start listening") | ||
} | ||
|
||
// create a cmux | ||
m := cmux.New(rootLis) | ||
m.SetReadTimeout(cmuxReadTimeout) // set a timeout, ref: https://github.com/pingcap/tidb-binlog/pull/352 | ||
|
||
httpL := m.Match(cmux.HTTP1Fast()) | ||
go startHTTPServer(httpL) | ||
|
||
err = m.Serve() // start serving, block | ||
if err != nil && isErrNetClosing(err) { | ||
err = nil | ||
} | ||
return err | ||
} | ||
|
||
var useOfClosedErrMsg = "use of closed network connection" | ||
|
||
// isErrNetClosing checks whether is an ErrNetClosing error | ||
func isErrNetClosing(err error) bool { | ||
if err == nil { | ||
return false | ||
} | ||
return strings.Contains(err.Error(), useOfClosedErrMsg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package reparo | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
var ( | ||
eventCounter = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Namespace: "binlog", | ||
Subsystem: "drainer", | ||
Name: "event", | ||
Help: "the count of sql event(dml, ddl).", | ||
}, []string{"type"}) | ||
|
||
checkpointTSOGauge = prometheus.NewGauge( | ||
prometheus.GaugeOpts{ | ||
Namespace: "binlog", | ||
Subsystem: "drainer", | ||
Name: "checkpoint_tso", | ||
Help: "save checkpoint tso of drainer.", | ||
}) | ||
executeHistogram = prometheus.NewHistogram( | ||
prometheus.HistogramOpts{ | ||
Namespace: "binlog", | ||
Subsystem: "drainer", | ||
Name: "execute_duration_time", | ||
Help: "Bucketed histogram of processing time (s) of a execute to sync data to downstream.", | ||
Buckets: prometheus.ExponentialBuckets(0.00005, 2, 18), | ||
}) | ||
|
||
queryHistogramVec = prometheus.NewHistogramVec( | ||
prometheus.HistogramOpts{ | ||
Namespace: "binlog", | ||
Subsystem: "drainer", | ||
Name: "query_duration_time", | ||
Help: "Bucketed histogram of processing time (s) of a query to sync data to downstream.", | ||
Buckets: prometheus.ExponentialBuckets(0.00005, 2, 18), | ||
}, []string{"type"}) | ||
|
||
readBinlogSizeHistogram = prometheus.NewHistogram( | ||
prometheus.HistogramOpts{ | ||
Namespace: "binlog", | ||
Subsystem: "drainer", | ||
Name: "read_binlog_size", | ||
Help: "Bucketed histogram of size of a binlog.", | ||
Buckets: prometheus.ExponentialBuckets(16, 2, 25), | ||
}) | ||
|
||
queueSizeGauge = prometheus.NewGaugeVec( | ||
prometheus.GaugeOpts{ | ||
Namespace: "binlog", | ||
Subsystem: "drainer", | ||
Name: "queue_size", | ||
Help: "the size of queue", | ||
}, []string{"name"}) | ||
) | ||
|
||
func init() { | ||
registry := prometheus.NewRegistry() | ||
registry.MustRegister(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{})) | ||
registry.MustRegister(prometheus.NewGoCollector()) | ||
registry.MustRegister(checkpointTSOGauge) | ||
registry.MustRegister(eventCounter) | ||
registry.MustRegister(executeHistogram) | ||
registry.MustRegister(readBinlogSizeHistogram) | ||
registry.MustRegister(queryHistogramVec) | ||
registry.MustRegister(queueSizeGauge) | ||
|
||
prometheus.DefaultGatherer = registry | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters