-
Notifications
You must be signed in to change notification settings - Fork 3
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: support profiling #250
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 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 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,142 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/url" | ||
"time" | ||
|
||
"github.com/grafana/pyroscope-go" | ||
"github.com/hamba/logger/v2" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var allProfilingTypes = []pyroscope.ProfileType{ | ||
pyroscope.ProfileCPU, | ||
pyroscope.ProfileInuseObjects, | ||
pyroscope.ProfileAllocObjects, | ||
pyroscope.ProfileInuseSpace, | ||
pyroscope.ProfileAllocSpace, | ||
pyroscope.ProfileGoroutines, | ||
pyroscope.ProfileMutexCount, | ||
pyroscope.ProfileMutexDuration, | ||
pyroscope.ProfileBlockCount, | ||
pyroscope.ProfileBlockDuration, | ||
} | ||
|
||
// Tracing flag constants declared for CLI use. | ||
const ( | ||
FlagProfilingDSN = "profiling.dsn" | ||
FlagProfileUploadRate = "profiling.upload-rate" | ||
FlagProfilingTags = "profiling.tags" | ||
FlagProfilingTypes = "profiling.types" | ||
) | ||
|
||
// ProfilingFlags are flags that configure profiling. | ||
var ProfilingFlags = Flags{ | ||
&cli.StringFlag{ | ||
Name: FlagProfilingDSN, | ||
Usage: "The address to the Pyroscope server, in the format " + | ||
"'http://basic:auth@server:port?token=auth-token&tenantid=tenant-id'.", | ||
EnvVars: []string{"PROFILING_DSN"}, | ||
}, | ||
&cli.DurationFlag{ | ||
Name: FlagProfileUploadRate, | ||
Usage: "The rate at which profiles are uploaded.", | ||
Value: 15 * time.Second, | ||
EnvVars: []string{"PROFILING_UPLOAD_RATE"}, | ||
}, | ||
&cli.StringSliceFlag{ | ||
Name: FlagProfilingTags, | ||
Usage: "A list of tags appended to every profile. Format: key=value.", | ||
EnvVars: []string{"PROFILING_TAGS"}, | ||
}, | ||
&cli.StringSliceFlag{ | ||
Name: FlagProfilingTypes, | ||
Usage: "The type of profiles to include. Defaults to all.", | ||
EnvVars: []string{"PROFILING_TYPES"}, | ||
}, | ||
} | ||
|
||
// NewProfiler returns a profiler configured from the cli. | ||
// If no profiler is configured, nil is returned. | ||
func NewProfiler(c *cli.Context, svc string, log *logger.Logger) (*pyroscope.Profiler, error) { | ||
dsn := c.String(FlagProfilingDSN) | ||
if dsn == "" { | ||
//nolint:nilnil // There is no sentinel in this case. | ||
return nil, nil | ||
} | ||
|
||
u, err := url.Parse(dsn) | ||
if err != nil { | ||
return nil, fmt.Errorf("parsing profiling DSN: %w", err) | ||
} | ||
|
||
tenantID := u.Query().Get("tenantid") | ||
|
||
authToken := u.Query().Get("token") | ||
var username, password string | ||
if u.User != nil { | ||
username = u.User.Username() | ||
password, _ = u.User.Password() | ||
} | ||
if (username != "" || password != "") && authToken != "" { | ||
return nil, errors.New("cannot set auth token and basic auth") | ||
} | ||
|
||
srvURL := &url.URL{ | ||
Scheme: u.Scheme, | ||
Host: u.Host, | ||
Path: u.Path, | ||
} | ||
|
||
tags := map[string]string{} | ||
if pairs := c.StringSlice(FlagProfilingTags); len(pairs) > 0 { | ||
strTags, err := Split(pairs, "=") | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, kv := range strTags { | ||
tags[kv[0]] = kv[1] | ||
} | ||
} | ||
|
||
types := allProfilingTypes | ||
if newTypes := c.StringSlice(FlagProfilingTypes); len(newTypes) > 0 { | ||
types = make([]pyroscope.ProfileType, len(newTypes)) | ||
for i, typ := range newTypes { | ||
types[i] = pyroscope.ProfileType(typ) | ||
} | ||
} | ||
|
||
cfg := pyroscope.Config{ | ||
ApplicationName: svc, | ||
Tags: tags, | ||
ServerAddress: srvURL.String(), | ||
AuthToken: authToken, | ||
BasicAuthUser: username, | ||
BasicAuthPassword: password, | ||
TenantID: tenantID, | ||
UploadRate: c.Duration(FlagProfileUploadRate), | ||
Logger: pyroLogAdapter{log: log}, | ||
ProfileTypes: types, | ||
} | ||
|
||
return pyroscope.Start(cfg) | ||
} | ||
|
||
type pyroLogAdapter struct { | ||
log *logger.Logger | ||
} | ||
|
||
func (a pyroLogAdapter) Infof(format string, args ...any) { | ||
a.log.Info(fmt.Sprintf(format, args...)) | ||
} | ||
|
||
func (a pyroLogAdapter) Debugf(format string, args ...any) { | ||
a.log.Debug(fmt.Sprintf(format, args...)) | ||
} | ||
|
||
func (a pyroLogAdapter) Errorf(format string, args ...any) { | ||
a.log.Error(fmt.Sprintf(format, args...)) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
strcase.ToSNAKE
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current version doesn't at the moment, but the next major will.