You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by ha...@apache.org on 2022/01/28 02:33:08 UTC

[skywalking-banyandb] branch main updated: Add pprof (#74)

This is an automated email from the ASF dual-hosted git repository.

hanahmily pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-banyandb.git


The following commit(s) were added to refs/heads/main by this push:
     new 673e839  Add pprof (#74)
673e839 is described below

commit 673e83922bb967173ae39d5d1755730fd5b732ed
Author: Jiajing LU <lu...@gmail.com>
AuthorDate: Fri Jan 28 10:33:04 2022 +0800

    Add pprof (#74)
    
    * support pprof
    
    Co-authored-by: 吴晟 Wu Sheng <wu...@foxmail.com>
---
 banyand/internal/cmd/standalone.go |  3 ++
 banyand/prof/pprof.go              | 74 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)

diff --git a/banyand/internal/cmd/standalone.go b/banyand/internal/cmd/standalone.go
index 5bf1058..4fdb06e 100644
--- a/banyand/internal/cmd/standalone.go
+++ b/banyand/internal/cmd/standalone.go
@@ -26,6 +26,7 @@ import (
 	"github.com/apache/skywalking-banyandb/banyand/discovery"
 	"github.com/apache/skywalking-banyandb/banyand/liaison"
 	"github.com/apache/skywalking-banyandb/banyand/metadata"
+	"github.com/apache/skywalking-banyandb/banyand/prof"
 	"github.com/apache/skywalking-banyandb/banyand/query"
 	"github.com/apache/skywalking-banyandb/banyand/queue"
 	"github.com/apache/skywalking-banyandb/banyand/stream"
@@ -68,6 +69,7 @@ func newStandaloneCmd() *cobra.Command {
 	if err != nil {
 		l.Fatal().Err(err).Msg("failed to initiate Endpoint transport layer")
 	}
+	profSvc := prof.NewProfService()
 
 	// Meta the run Group units.
 	g.Register(
@@ -78,6 +80,7 @@ func newStandaloneCmd() *cobra.Command {
 		streamSvc,
 		q,
 		tcp,
+		profSvc,
 	)
 	logging := logger.Logging{}
 	standaloneCmd := &cobra.Command{
diff --git a/banyand/prof/pprof.go b/banyand/prof/pprof.go
new file mode 100644
index 0000000..4ca357d
--- /dev/null
+++ b/banyand/prof/pprof.go
@@ -0,0 +1,74 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you 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 prof
+
+import (
+	"net/http"
+	// Register pprof package
+	_ "net/http/pprof"
+
+	"github.com/apache/skywalking-banyandb/pkg/logger"
+	"github.com/apache/skywalking-banyandb/pkg/run"
+)
+
+var (
+	_ run.Service = (*pprofService)(nil)
+	_ run.Config  = (*pprofService)(nil)
+)
+
+func NewProfService() run.Service {
+	return &pprofService{
+		stopCh: make(chan struct{}),
+	}
+}
+
+type pprofService struct {
+	listenAddr string
+	stopCh     chan struct{}
+	l          *logger.Logger
+}
+
+func (p *pprofService) FlagSet() *run.FlagSet {
+	flagSet := run.NewFlagSet("prof")
+	flagSet.StringVar(&p.listenAddr, "pprof-listener-addr", "127.0.0.1:6060", "listen addr for pprof")
+	return flagSet
+}
+
+func (p *pprofService) Validate() error {
+	return nil
+}
+
+func (p *pprofService) Name() string {
+	return "pprof-service"
+}
+
+func (p *pprofService) Serve() error {
+	p.l = logger.GetLogger(p.Name())
+	go func() {
+		p.l.Info().Str("listenAddr", p.listenAddr).Msg("Start pprof server")
+		_ = http.ListenAndServe(p.listenAddr, nil)
+	}()
+
+	<-p.stopCh
+
+	return nil
+}
+
+func (p *pprofService) GracefulStop() {
+	close(p.stopCh)
+}