Skip to content

Commit

Permalink
Add support for nats server request profile (nats-io/nats-server#3774)
Browse files Browse the repository at this point in the history
  • Loading branch information
neilalexander committed Jan 11, 2023
1 parent f776abd commit d8643e3
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cli/server_request_command.go
Expand Up @@ -14,6 +14,7 @@
package cli

import (
"encoding/json"
"fmt"

"github.com/choria-io/fisk"
Expand Down Expand Up @@ -50,6 +51,9 @@ type SrvRequestCmd struct {

jsServerOnly bool
jsEnabled bool

profileName string
profileDebug int
}

func configureServerRequestCommand(srv *fisk.CmdClause) {
Expand Down Expand Up @@ -113,6 +117,10 @@ func configureServerRequestCommand(srv *fisk.CmdClause) {
healthz := req.Command("jetstream-health", "Request JetStream health status").Alias("healthz").Action(c.healthz)
healthz.Flag("js-enabled", "Checks that JetStream should be enabled on all servers").Short('J').BoolVar(&c.jsEnabled)
healthz.Flag("server-only", "Restricts the health check to the JetStream server only, do not check streams and consumers").Short('S').BoolVar(&c.jsServerOnly)

profilez := req.Command("profile", "Run a profile").Action(c.profilez)
profilez.Flag("profile", "Specify the name of the profile to run").StringVar(&c.profileName)
profilez.Flag("debug", "Set the debug level of the profile").IntVar(&c.profileDebug)
}

func (c *SrvRequestCmd) healthz(_ *fisk.ParseContext) error {
Expand All @@ -138,6 +146,45 @@ func (c *SrvRequestCmd) healthz(_ *fisk.ParseContext) error {
return nil
}

func (c *SrvRequestCmd) profilez(_ *fisk.ParseContext) error {
nc, _, err := prepareHelper("", natsOpts()...)
if err != nil {
return err
}

opts := server.ProfilezEventOptions{
ProfilezOptions: server.ProfilezOptions{
Name: c.profileName,
Debug: c.profileDebug,
},
EventFilterOptions: c.reqFilter(),
}

res, err := c.doReq("PROFILEZ", &opts, nc)
if err != nil {
return err
}

var response struct {
Resp server.ProfilezStatus `json:"data"`
}

var b []byte
for _, r := range res {
b = append(b, r...)
}
if err := json.Unmarshal(b, &response); err != nil {
return fmt.Errorf("no response")
}

if response.Resp.Error != "" {
return fmt.Errorf(response.Resp.Error)
}
fmt.Print(string(response.Resp.Profile))

return nil
}

func (c *SrvRequestCmd) jsz(_ *fisk.ParseContext) error {
nc, _, err := prepareHelper("", natsOpts()...)
if err != nil {
Expand Down

0 comments on commit d8643e3

Please sign in to comment.