Skip to content

Commit

Permalink
Add pprof module for profiling (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrproliu authored Jul 14, 2024
1 parent d045da1 commit a916a69
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion configs/rover_configs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}
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}
18 changes: 18 additions & 0 deletions docs/en/setup/configuration/pprof.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions docs/en/setup/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions pkg/boot/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -33,4 +34,5 @@ func init() {
module.Register(process.NewModule())
module.Register(profiling.NewModule())
module.Register(accesslog.NewModule())
module.Register(pprof.NewModule())
}
30 changes: 30 additions & 0 deletions pkg/pprof/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 "github.com/apache/skywalking-rover/pkg/module"

type Config struct {
module.Config `mapstructure:",squash"`

Port int `mapstructure:"port"`
}

func (c *Config) IsActive() bool {
return c.Active
}
93 changes: 93 additions & 0 deletions pkg/pprof/module.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a916a69

Please sign in to comment.