From 55c3422813343c6bccaa826565c098abb11d42bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=A5=E5=89=91?= <1045931706@qq.com> Date: Mon, 30 Dec 2024 15:13:57 +0800 Subject: [PATCH 1/3] support pyroscope MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 童剑 <1045931706@qq.com> --- cmd/pd-server/main.go | 1 + go.mod | 1 + go.sum | 1 + pkg/utils/metricutil/metricutil.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/cmd/pd-server/main.go b/cmd/pd-server/main.go index 165bcd2a12f..d77eeb304a1 100644 --- a/cmd/pd-server/main.go +++ b/cmd/pd-server/main.go @@ -232,6 +232,7 @@ func start(cmd *cobra.Command, args []string, services ...string) { grpcprometheus.EnableHandlingTimeHistogram() metricutil.Push(&cfg.Metric) + metricutil.EnablePyroscope() err = join.PrepareJoinCluster(cfg) if err != nil { diff --git a/go.mod b/go.mod index 9c8a7bc90a5..7d7555fea4e 100644 --- a/go.mod +++ b/go.mod @@ -63,6 +63,7 @@ require ( golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d google.golang.org/grpc v1.62.1 gotest.tools/gotestsum v1.7.0 + github.com/grafana/pyroscope-go v1.2.0 ) require ( diff --git a/go.sum b/go.sum index a2f070397d1..e3184396651 100644 --- a/go.sum +++ b/go.sum @@ -245,6 +245,7 @@ github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/grafana/pyroscope-go v1.2.0/go.mod h1:2GHr28Nr05bg2pElS+dDsc98f3JTUh2f6Fz1hWXrqwk= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= diff --git a/pkg/utils/metricutil/metricutil.go b/pkg/utils/metricutil/metricutil.go index 32583248d5b..486e03d2168 100644 --- a/pkg/utils/metricutil/metricutil.go +++ b/pkg/utils/metricutil/metricutil.go @@ -15,7 +15,10 @@ package metricutil import ( + "github.com/grafana/pyroscope-go" + "go.uber.org/zap" "os" + "runtime" "time" "unicode" @@ -102,3 +105,28 @@ func instanceName() string { } return hostname } + +// EnablePyroscope enables pyroscope if pyroscope is enabled. +func EnablePyroscope() { + if os.Getenv("PYROSCOPE_SERVER_ADDRESS") != "" { + runtime.SetMutexProfileFraction(5) + runtime.SetBlockProfileRate(5) + _, err := pyroscope.Start(pyroscope.Config{ + ApplicationName: "pd", + ServerAddress: os.Getenv("PYROSCOPE_SERVER_ADDRESS"), + Logger: pyroscope.StandardLogger, + AuthToken: os.Getenv("PYROSCOPE_AUTH_TOKEN"), + TenantID: os.Getenv("PYROSCOPE_TENANT_ID"), + BasicAuthUser: os.Getenv("PYROSCOPE_BASIC_AUTH_USER"), + BasicAuthPassword: os.Getenv("PYROSCOPE_BASIC_AUTH_PASSWORD"), + ProfileTypes: []pyroscope.ProfileType{ + pyroscope.ProfileCPU, + pyroscope.ProfileAllocSpace, + }, + UploadRate: 30 * time.Second, + }) + if err != nil { + log.Fatal("fail to start pyroscope", zap.Error(err)) + } + } +} From 2a6220a38e7e5cbec45e25226c439264a724814c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=A5=E5=89=91?= <1045931706@qq.com> Date: Mon, 30 Dec 2024 15:16:41 +0800 Subject: [PATCH 2/3] go mod tidy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 童剑 <1045931706@qq.com> --- go.mod | 3 ++- go.sum | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 7d7555fea4e..6f784620a9a 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/gogo/protobuf v1.3.2 github.com/google/btree v1.1.2 github.com/gorilla/mux v1.7.4 + github.com/grafana/pyroscope-go v1.2.0 github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 github.com/joho/godotenv v1.4.0 github.com/mailru/easyjson v0.7.6 @@ -63,7 +64,6 @@ require ( golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d google.golang.org/grpc v1.62.1 gotest.tools/gotestsum v1.7.0 - github.com/grafana/pyroscope-go v1.2.0 ) require ( @@ -127,6 +127,7 @@ require ( github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.5.1 // indirect + github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect diff --git a/go.sum b/go.sum index e3184396651..60d61b785ab 100644 --- a/go.sum +++ b/go.sum @@ -245,7 +245,10 @@ github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/grafana/pyroscope-go v1.2.0 h1:aILLKjTj8CS8f/24OPMGPewQSYlhmdQMBmol1d3KGj8= github.com/grafana/pyroscope-go v1.2.0/go.mod h1:2GHr28Nr05bg2pElS+dDsc98f3JTUh2f6Fz1hWXrqwk= +github.com/grafana/pyroscope-go/godeltaprof v0.1.8 h1:iwOtYXeeVSAeYefJNaxDytgjKtUuKQbJqgAIjlnicKg= +github.com/grafana/pyroscope-go/godeltaprof v0.1.8/go.mod h1:2+l7K7twW49Ct4wFluZD3tZ6e0SjanjcUUBPVD/UuGU= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= From 3aa96f199e981a6f9a895b95dd0789f16d186ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=A5=E5=89=91?= <1045931706@qq.com> Date: Mon, 30 Dec 2024 15:17:50 +0800 Subject: [PATCH 3/3] format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 童剑 <1045931706@qq.com> --- pkg/utils/metricutil/metricutil.go | 4 ++-- tests/integrations/go.mod | 2 ++ tests/integrations/go.sum | 4 ++++ tools/go.mod | 2 ++ tools/go.sum | 4 ++++ 5 files changed, 14 insertions(+), 2 deletions(-) diff --git a/pkg/utils/metricutil/metricutil.go b/pkg/utils/metricutil/metricutil.go index 486e03d2168..8e636c20b87 100644 --- a/pkg/utils/metricutil/metricutil.go +++ b/pkg/utils/metricutil/metricutil.go @@ -15,15 +15,15 @@ package metricutil import ( - "github.com/grafana/pyroscope-go" - "go.uber.org/zap" "os" "runtime" "time" "unicode" + "github.com/grafana/pyroscope-go" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/push" + "go.uber.org/zap" "github.com/pingcap/log" diff --git a/tests/integrations/go.mod b/tests/integrations/go.mod index c5fcb617014..82c633088b0 100644 --- a/tests/integrations/go.mod +++ b/tests/integrations/go.mod @@ -101,6 +101,8 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/gorilla/mux v1.7.4 // indirect github.com/gorilla/websocket v1.5.1 // indirect + github.com/grafana/pyroscope-go v1.2.0 // indirect + github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect diff --git a/tests/integrations/go.sum b/tests/integrations/go.sum index 5a48549ef65..69797bb70fb 100644 --- a/tests/integrations/go.sum +++ b/tests/integrations/go.sum @@ -237,6 +237,10 @@ github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/grafana/pyroscope-go v1.2.0 h1:aILLKjTj8CS8f/24OPMGPewQSYlhmdQMBmol1d3KGj8= +github.com/grafana/pyroscope-go v1.2.0/go.mod h1:2GHr28Nr05bg2pElS+dDsc98f3JTUh2f6Fz1hWXrqwk= +github.com/grafana/pyroscope-go/godeltaprof v0.1.8 h1:iwOtYXeeVSAeYefJNaxDytgjKtUuKQbJqgAIjlnicKg= +github.com/grafana/pyroscope-go/godeltaprof v0.1.8/go.mod h1:2+l7K7twW49Ct4wFluZD3tZ6e0SjanjcUUBPVD/UuGU= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho= diff --git a/tools/go.mod b/tools/go.mod index ded2e2e82c8..ea801e05602 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -106,6 +106,8 @@ require ( github.com/google/uuid v1.6.0 // indirect github.com/gorilla/mux v1.7.4 // indirect github.com/gorilla/websocket v1.5.1 // indirect + github.com/grafana/pyroscope-go v1.2.0 // indirect + github.com/grafana/pyroscope-go/godeltaprof v0.1.8 // indirect github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect diff --git a/tools/go.sum b/tools/go.sum index bd68a4f0ca1..ad1bdebea7a 100644 --- a/tools/go.sum +++ b/tools/go.sum @@ -236,6 +236,10 @@ github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= +github.com/grafana/pyroscope-go v1.2.0 h1:aILLKjTj8CS8f/24OPMGPewQSYlhmdQMBmol1d3KGj8= +github.com/grafana/pyroscope-go v1.2.0/go.mod h1:2GHr28Nr05bg2pElS+dDsc98f3JTUh2f6Fz1hWXrqwk= +github.com/grafana/pyroscope-go/godeltaprof v0.1.8 h1:iwOtYXeeVSAeYefJNaxDytgjKtUuKQbJqgAIjlnicKg= +github.com/grafana/pyroscope-go/godeltaprof v0.1.8/go.mod h1:2+l7K7twW49Ct4wFluZD3tZ6e0SjanjcUUBPVD/UuGU= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0/go.mod h1:g5qyo/la0ALbONm6Vbp88Yd8NsDy6rZz+RcrMPxvld8= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 h1:Ovs26xHkKqVztRpIrF/92BcuyuQ/YW4NSIpoGtfXNho=