Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add profiling #53

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions util/errhandler/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright 2023 The KubeVela Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package errhandler

// ErrorHandler handler for error
type ErrorHandler func(error)

// NotifyOrPanic if given errChan is nil, panic on error, otherwise send error
// to errChan
func NotifyOrPanic(errChan chan error) ErrorHandler {
return func(err error) {
if err == nil {
return
}
if errChan != nil {
errChan <- err
} else {
panic(err)
}
}
}
44 changes: 44 additions & 0 deletions util/errhandler/handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2023 The KubeVela Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package errhandler_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/kubevela/pkg/util/errhandler"
)

func TestNotifyOrPanic(t *testing.T) {
ch := make(chan error, 1)
h1 := errhandler.NotifyOrPanic(ch)
h2 := errhandler.NotifyOrPanic(nil)
h1(nil)
require.Equal(t, 0, len(ch))
e := fmt.Errorf("err")
h1(e)
require.Equal(t, 1, len(ch))
func() {
defer func() {
err := recover()
require.NotNil(t, err)
}()
h2(e)
}()
}
29 changes: 29 additions & 0 deletions util/profiling/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2023 The KubeVela Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package profiling

import "github.com/spf13/pflag"

var (
// Addr the address for starting profiling server
Addr = ""
)

// AddFlags .
func AddFlags(fs *pflag.FlagSet) {
fs.StringVarP(&Addr, "profiling-addr", "", Addr, "if not empty, start the profiling server at the given address")
}
59 changes: 59 additions & 0 deletions util/profiling/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
Copyright 2023 The KubeVela Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package profiling

import (
"encoding/json"
"net/http"
"net/http/pprof"
"runtime"

"k8s.io/klog/v2"

"github.com/kubevela/pkg/util/errhandler"
)

// NewProfilingHandler create a profiling handler
func NewProfilingHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.HandleFunc("/mem/stat", func(writer http.ResponseWriter, request *http.Request) {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
bs, _ := json.Marshal(ms)
_, _ = writer.Write(bs)
})
mux.HandleFunc("/gc", func(writer http.ResponseWriter, request *http.Request) {
runtime.GC()
})
return mux
}

// StartProfilingServer listen to the pprofAddr and export the profiling results
// If the errChan is nil, this function will panic when the listening error occurred.
func StartProfilingServer(errChan chan error) {
if Addr == "" {
return
}
klog.Infof("start profiling server at %s", Addr)
err := http.ListenAndServe(Addr, NewProfilingHandler())
errhandler.NotifyOrPanic(errChan)(err)
}
41 changes: 41 additions & 0 deletions util/profiling/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 The KubeVela Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package profiling_test

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/spf13/pflag"
"github.com/stretchr/testify/require"

"github.com/kubevela/pkg/util/profiling"
)

func TestStartProfilingServer(t *testing.T) {
fn := profiling.NewProfilingHandler()
server := httptest.NewServer(fn)
defer server.Close()
_, err := http.Get(server.URL + "/mem/stat")
require.NoError(t, err)
_, err = http.Get(server.URL + "/gc")
require.NoError(t, err)

profiling.AddFlags(pflag.NewFlagSet("", pflag.ExitOnError))
profiling.StartProfilingServer(nil)
}