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

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


The following commit(s) were added to refs/heads/main by this push:
     new a916a69  Add `pprof` module for profiling (#133)
a916a69 is described below

commit a916a690939c561ced18462095c00d0c7314e3e2
Author: mrproliu <[email protected]>
AuthorDate: Sun Jul 14 22:02:27 2024 +0800

    Add `pprof` module for profiling (#133)
---
 CHANGES.md                                |  1 +
 configs/rover_configs.yaml                |  8 ++-
 docs/en/setup/configuration/pprof.md      | 18 ++++++
 docs/en/setup/overview.md                 |  1 +
 pkg/boot/register.go                      |  2 +
 pkg/{boot/register.go => pprof/config.go} | 26 ++++-----
 pkg/pprof/module.go                       | 93 +++++++++++++++++++++++++++++++
 7 files changed, 132 insertions(+), 17 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 926cdd6..bdebbfe 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -7,6 +7,7 @@ Release Notes.
 #### Features
 * Upgrade LLVM to `18`.
 * Support propagation the excluding namespaces in the access log to the 
backend.
+* Add `pprof` module for observe self.
 
 #### Bug Fixes
 * Fixed the issue where `conntrack` could not find the Reply IP in the access 
log module.
diff --git a/configs/rover_configs.yaml b/configs/rover_configs.yaml
index 3c3547e..2c5d243 100644
--- a/configs/rover_configs.yaml
+++ b/configs/rover_configs.yaml
@@ -146,4 +146,10 @@ access_log:
     # The count of parallel protocol analyzer
     parallels: ${ROVER_ACCESS_LOG_PROTOCOL_ANALYZE_PARALLELS:2}
     # The size of per paralleled analyzer queue
-    queue_size: ${ROVER_ACCESS_LOG_PROTOCOL_ANALYZE_QUEUE_SIZE:5000}
\ No newline at end of file
+    queue_size: ${ROVER_ACCESS_LOG_PROTOCOL_ANALYZE_QUEUE_SIZE:5000}
+
+pprof:
+  # Is active the pprof
+  active: ${ROVER_PPROF_ACTIVE:false}
+  # The bind port of the pprof HTTP server
+  port: ${ROVER_PPROF_PORT:6060}
\ No newline at end of file
diff --git a/docs/en/setup/configuration/pprof.md 
b/docs/en/setup/configuration/pprof.md
new file mode 100644
index 0000000..98e5675
--- /dev/null
+++ b/docs/en/setup/configuration/pprof.md
@@ -0,0 +1,18 @@
+# Pprof 
+
+Pprof is a feature to collect self runtime profiling data through `pprof` 
module.
+
+## Configuration
+
+| Name      | Default | Environment Key      | Description                     
    |
+|-----------|---------|----------------------|-------------------------------------|
+| `enabled` | `false` | `ROVER_PPROF_ACTIVE` | Enable pprof module.            
    |
+| `port`    | `6060`  | `ROVER_PPROF_PORT`   | The HTTP port to expose pprof 
data. |
+
+## Expose Paths
+
+- `/debug/pprof/`: The root path to access pprof data.
+- `/debug/pprof/cmdline`: The command line invocation of the current program.
+- `/debug/pprof/profile`: A pprof-formatted snapshot of the current program.
+- `/debug/pprof/symbol`: The symbol table of the current program.
+- `/debug/pprof/trace`: A trace of the current program.
\ No newline at end of file
diff --git a/docs/en/setup/overview.md b/docs/en/setup/overview.md
index 8297577..73f108e 100644
--- a/docs/en/setup/overview.md
+++ b/docs/en/setup/overview.md
@@ -27,6 +27,7 @@ The SkyWalking Rover requires specialized protocols to 
communicate with SkyWalki
 2. [Service Discovery](configuration/service-discovery.md) includes advanced 
setups about the ways of discovering services on your Kubernetes cluster. 
 3. [Access logs](./configuration/traffic.md) reports L2 to L4 network traffic 
relative information through access logs, to help OAP backend to do topology 
and metrics analysis.
 4. [Profiling](./configuration/profiling.md) is an on-demand feature to 
enhance general observability besides access logs. It provides eBPF powered 
process ON_CPU, OFF_CPU profiling and network advanced profiling to link HTTP 
traffic with SkyWalking and Zipkin traces. 
+5. [Pprof](./configuration/pprof.md) is a feature to collect self runtime 
profiling data.
 
 To adjust the configurations, refer to [Overriding 
Setting](./configuration/override-settings.md) document for more details.
 
diff --git a/pkg/boot/register.go b/pkg/boot/register.go
index f383c90..03b3142 100644
--- a/pkg/boot/register.go
+++ b/pkg/boot/register.go
@@ -22,6 +22,7 @@ import (
        "github.com/apache/skywalking-rover/pkg/core"
        "github.com/apache/skywalking-rover/pkg/logger"
        "github.com/apache/skywalking-rover/pkg/module"
+       "github.com/apache/skywalking-rover/pkg/pprof"
        "github.com/apache/skywalking-rover/pkg/process"
        "github.com/apache/skywalking-rover/pkg/profiling"
 )
@@ -33,4 +34,5 @@ func init() {
        module.Register(process.NewModule())
        module.Register(profiling.NewModule())
        module.Register(accesslog.NewModule())
+       module.Register(pprof.NewModule())
 }
diff --git a/pkg/boot/register.go b/pkg/pprof/config.go
similarity index 59%
copy from pkg/boot/register.go
copy to pkg/pprof/config.go
index f383c90..496957c 100644
--- a/pkg/boot/register.go
+++ b/pkg/pprof/config.go
@@ -15,22 +15,16 @@
 // specific language governing permissions and limitations
 // under the License.
 
-package boot
+package pprof
 
-import (
-       "github.com/apache/skywalking-rover/pkg/accesslog"
-       "github.com/apache/skywalking-rover/pkg/core"
-       "github.com/apache/skywalking-rover/pkg/logger"
-       "github.com/apache/skywalking-rover/pkg/module"
-       "github.com/apache/skywalking-rover/pkg/process"
-       "github.com/apache/skywalking-rover/pkg/profiling"
-)
+import "github.com/apache/skywalking-rover/pkg/module"
 
-func init() {
-       // register all active module
-       module.Register(logger.NewModule())
-       module.Register(core.NewModule())
-       module.Register(process.NewModule())
-       module.Register(profiling.NewModule())
-       module.Register(accesslog.NewModule())
+type Config struct {
+       module.Config `mapstructure:",squash"`
+
+       Port int `mapstructure:"port"`
+}
+
+func (c *Config) IsActive() bool {
+       return c.Active
 }
diff --git a/pkg/pprof/module.go b/pkg/pprof/module.go
new file mode 100644
index 0000000..b7842f3
--- /dev/null
+++ b/pkg/pprof/module.go
@@ -0,0 +1,93 @@
+// 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 pprof
+
+import (
+       "context"
+       "fmt"
+       "net/http"
+       "net/http/pprof"
+       "sync"
+       "time"
+
+       "github.com/apache/skywalking-rover/pkg/module"
+)
+
+const ModuleName = "pprof"
+
+type Module struct {
+       config *Config
+
+       mutex  sync.Mutex
+       server *http.Server
+
+       shutdown bool
+}
+
+func NewModule() *Module {
+       return &Module{config: &Config{}}
+}
+
+func (m *Module) Name() string {
+       return ModuleName
+}
+
+func (m *Module) RequiredModules() []string {
+       return nil
+}
+
+func (m *Module) Config() module.ConfigInterface {
+       return m.config
+}
+
+func (m *Module) Start(ctx context.Context, mgr *module.Manager) error {
+       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)
+
+       m.mutex.Lock()
+       defer m.mutex.Unlock()
+
+       m.server = &http.Server{
+               Addr:              fmt.Sprintf(":%d", m.config.Port),
+               ReadHeaderTimeout: 3 * time.Second,
+               Handler:           mux,
+       }
+       go func() {
+               m.shutdown = false
+               err := m.server.ListenAndServe()
+               if err != nil && !m.shutdown {
+                       mgr.ShutdownModules(err)
+               }
+       }()
+       return nil
+}
+
+func (m *Module) NotifyStartSuccess() {
+}
+
+func (m *Module) Shutdown(ctx context.Context, mgr *module.Manager) error {
+       m.shutdown = true
+       if m.server != nil {
+               return m.server.Shutdown(ctx)
+       }
+       return nil
+}

Reply via email to