forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 70
internal/debug: add integration with Grafana Pyroscope #33623 #1983
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
Merged
AnilChinchawale
merged 1 commit into
XinFinOrg:dev-upgrade
from
gzliudan:support-grafana-pyroscope
Jan 29, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| // Copyright 2026 The go-ethereum Authors | ||
| // This file is part of the go-ethereum library. | ||
| // | ||
| // The go-ethereum library is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Lesser General Public License as published by | ||
| // the Free Software Foundation, either version 3 of the License, or | ||
| // (at your option) any later version. | ||
| // | ||
| // The go-ethereum library is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Lesser General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Lesser General Public License | ||
| // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package debug | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/XinFinOrg/XDPoSChain/internal/flags" | ||
| "github.com/XinFinOrg/XDPoSChain/log" | ||
| "github.com/grafana/pyroscope-go" | ||
| "github.com/urfave/cli/v2" | ||
| ) | ||
|
|
||
| var ( | ||
| pyroscopeFlag = &cli.BoolFlag{ | ||
| Name: "pyroscope", | ||
| Usage: "Enable Pyroscope profiling", | ||
| Value: false, | ||
| Category: flags.LoggingCategory, | ||
| } | ||
| pyroscopeServerFlag = &cli.StringFlag{ | ||
| Name: "pyroscope-server", | ||
| Usage: "Pyroscope server URL to push profiling data to", | ||
| Value: "http://localhost:4040", | ||
| Category: flags.LoggingCategory, | ||
| } | ||
| pyroscopeAuthUsernameFlag = &cli.StringFlag{ | ||
| Name: "pyroscope-username", | ||
| Usage: "Pyroscope basic authentication username", | ||
| Value: "", | ||
| Category: flags.LoggingCategory, | ||
| } | ||
| pyroscopeAuthPasswordFlag = &cli.StringFlag{ | ||
| Name: "pyroscope-password", | ||
| Usage: "Pyroscope basic authentication password (prefer setting PYROSCOPE_PASSWORD env var instead of CLI flag)", | ||
| Value: "", | ||
| EnvVars: []string{"PYROSCOPE_PASSWORD"}, | ||
| Category: flags.LoggingCategory, | ||
| } | ||
| pyroscopeTagsFlag = &cli.StringFlag{ | ||
| Name: "pyroscope-tags", | ||
| Usage: "Comma separated list of key=value tags to add to profiling data", | ||
| Value: "", | ||
| Category: flags.LoggingCategory, | ||
| } | ||
| ) | ||
|
|
||
| // This holds the globally-configured Pyroscope instance. | ||
| var pyroscopeProfiler *pyroscope.Profiler | ||
|
|
||
| func startPyroscope(ctx *cli.Context) error { | ||
| server := ctx.String(pyroscopeServerFlag.Name) | ||
| authUsername := ctx.String(pyroscopeAuthUsernameFlag.Name) | ||
| authPassword := ctx.String(pyroscopeAuthPasswordFlag.Name) | ||
|
|
||
| rawTags := ctx.String(pyroscopeTagsFlag.Name) | ||
| tags := make(map[string]string) | ||
| for tag := range strings.SplitSeq(rawTags, ",") { | ||
| tag = strings.TrimSpace(tag) | ||
| if tag == "" { | ||
| continue | ||
| } | ||
| k, v, ok := strings.Cut(tag, "=") | ||
| if !ok || k == "" { | ||
| // Skip tags that do not conform to the expected key=value format | ||
| continue | ||
| } | ||
| tags[k] = v | ||
| } | ||
|
|
||
| config := pyroscope.Config{ | ||
| ApplicationName: " XDC", | ||
| ServerAddress: server, | ||
| BasicAuthUser: authUsername, | ||
| BasicAuthPassword: authPassword, | ||
| Logger: &pyroscopeLogger{Logger: log.Root()}, | ||
| Tags: tags, | ||
| ProfileTypes: []pyroscope.ProfileType{ | ||
| // Enabling all profile types | ||
| pyroscope.ProfileCPU, | ||
| pyroscope.ProfileAllocObjects, | ||
| pyroscope.ProfileAllocSpace, | ||
| pyroscope.ProfileInuseObjects, | ||
| pyroscope.ProfileInuseSpace, | ||
| pyroscope.ProfileGoroutines, | ||
| pyroscope.ProfileMutexCount, | ||
| pyroscope.ProfileMutexDuration, | ||
| pyroscope.ProfileBlockCount, | ||
| pyroscope.ProfileBlockDuration, | ||
| }, | ||
| } | ||
|
|
||
| profiler, err := pyroscope.Start(config) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| pyroscopeProfiler = profiler | ||
| log.Info("Enabled Pyroscope", "server", server) | ||
| return nil | ||
| } | ||
|
|
||
| func stopPyroscope() { | ||
| if pyroscopeProfiler != nil { | ||
| pyroscopeProfiler.Stop() | ||
| pyroscopeProfiler = nil | ||
| } | ||
| } | ||
|
|
||
| // Small wrapper for log.Logger to satisfy pyroscope.Logger interface | ||
| type pyroscopeLogger struct { | ||
| Logger log.Logger | ||
| } | ||
|
|
||
| func (l *pyroscopeLogger) Infof(format string, v ...any) { | ||
| l.Logger.Info(fmt.Sprintf("Pyroscope: "+format, v...)) | ||
| } | ||
|
|
||
| func (l *pyroscopeLogger) Debugf(format string, v ...any) { | ||
| l.Logger.Debug(fmt.Sprintf("Pyroscope: "+format, v...)) | ||
| } | ||
|
|
||
| func (l *pyroscopeLogger) Errorf(format string, v ...any) { | ||
| l.Logger.Error(fmt.Sprintf("Pyroscope: "+format, v...)) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.