-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support streaming of rule logs
- Loading branch information
Showing
7 changed files
with
141 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package enaptercli | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
type cmdRuleEngineRuleLogs struct { | ||
cmdRuleEngineRule | ||
ruleID string | ||
follow bool | ||
} | ||
|
||
func buildCmdRuleEngineRuleLogs() *cli.Command { | ||
cmd := &cmdRuleEngineRuleLogs{} | ||
return &cli.Command{ | ||
Name: "logs", | ||
Usage: "Show rule logs", | ||
CustomHelpTemplate: cmd.HelpTemplate(), | ||
Flags: cmd.Flags(), | ||
Before: cmd.Before, | ||
Action: func(cliCtx *cli.Context) error { | ||
return cmd.do(cliCtx) | ||
}, | ||
} | ||
} | ||
|
||
func (c *cmdRuleEngineRuleLogs) Flags() []cli.Flag { | ||
return append(c.cmdRuleEngineRule.Flags(), | ||
&cli.StringFlag{ | ||
Name: "rule-id", | ||
Usage: "rule ID", | ||
Destination: &c.ruleID, | ||
Required: true, | ||
}, | ||
&cli.BoolFlag{ | ||
Name: "follow", | ||
Aliases: []string{"f"}, | ||
Usage: "follow log output", | ||
Destination: &c.follow, | ||
}, | ||
) | ||
} | ||
|
||
func (c *cmdRuleEngineRuleLogs) do(cliCtx *cli.Context) error { | ||
if !c.follow { | ||
return cli.Exit("Currently, only follow mode (--follow) is supported.", 1) | ||
} | ||
|
||
path := fmt.Sprintf("/site/rule_engine/rules/%s/logs/ws", c.ruleID) | ||
conn, err := c.dialWebSocket(cliCtx.Context, path) | ||
if err != nil { | ||
return fmt.Errorf("connect: %w", err) | ||
} | ||
|
||
go func() { | ||
<-cliCtx.Done() | ||
conn.Close() | ||
}() | ||
|
||
for { | ||
_, r, err := conn.NextReader() | ||
if err != nil { | ||
select { | ||
case <-cliCtx.Done(): | ||
return nil | ||
default: | ||
return fmt.Errorf("read: %w", err) | ||
} | ||
} | ||
|
||
var msg struct { | ||
Timestamp int64 `json:"timestamp"` | ||
Severity string `json:"severity"` | ||
Message string `json:"message"` | ||
} | ||
if err := json.NewDecoder(r).Decode(&msg); err != nil { | ||
return fmt.Errorf("parse payload: %w", err) | ||
} | ||
|
||
ts := time.Unix(msg.Timestamp, 0).Format(time.RFC3339) | ||
fmt.Fprintf(c.writer, "%s [%s] %s\n", ts, msg.Severity, msg.Message) | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
internal/app/enaptercli/testdata/helps/enapter rule-engine rule logs
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,16 @@ | ||
NAME: | ||
enaptercli.test rule-engine rule logs - Show rule logs | ||
|
||
USAGE: | ||
enaptercli.test rule-engine rule logs [command options] | ||
|
||
OPTIONS: | ||
--verbose log extra details about operation (default: false) | ||
--rule-id value rule ID | ||
--follow, -f follow log output (default: false) | ||
--help, -h show help | ||
|
||
ENVIRONMENT VARIABLES: | ||
ENAPTER3_API_TOKEN Enapter API access token | ||
ENAPTER3_API_HOST Enapter API base URL (https://api.enapter.com by default) | ||
|