Skip to content

Commit

Permalink
dev: initial support for metrics + updated comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arkits committed Mar 24, 2021
1 parent b154dcb commit c6c4cdb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ var prometheusCmd = &cobra.Command{
Run: handleKnownActuatorCmd,
}

var metricsCmd = &cobra.Command{
Use: "metrics",
Short: "Interface with /actuator/metrics",
Run: handleKnownActuatorCmd,
}

func handleKnownActuatorCmd(cmd *cobra.Command, args []string) {

domain.SetupConfig(cmd)
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
addCommonFlags(customCmd)
addCommonFlags(logfileCmd)
addCommonFlags(prometheusCmd)
addCommonFlags(metricsCmd)

// Custom Flags
customCmd.Flags().StringP("endpoint", "E", "", "Endpoint prefix of the custom endpoint")
Expand All @@ -44,6 +45,7 @@ func init() {
rootCmd.AddCommand(customCmd)
rootCmd.AddCommand(logfileCmd)
rootCmd.AddCommand(prometheusCmd)
rootCmd.AddCommand(metricsCmd)

}

Expand Down
2 changes: 2 additions & 0 deletions domain/actuator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func GetAndPrintKnownActuator(inventory Inventory, cmdName string) error {
PrettyPrintActuatorHealthResponse(strResponse)
case "info":
PrettyPrintActuatorInfoResponse(strResponse)
case "metrics":
PrettyPrintActuatorMetricsResponse(strResponse)
case "prometheus":
fmt.Println(strResponse)
default:
Expand Down
4 changes: 4 additions & 0 deletions domain/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,7 @@ type ActuatorInfoGitProperties struct {
} `json:"commit"`
} `json:"total"`
}

type ActuatorMetricsProperties struct {
Names []string `json:"names"`
}
35 changes: 35 additions & 0 deletions domain/prettyPrinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import (
"github.com/jedib0t/go-pretty/v6/text"
)

// PrintInventoryHeader is a util function that prints a pretty Header text
// This is intended to be run for each Inventory - Prior execution of the domain specific function
func PrintInventoryHeader(inventory Inventory) {
fmt.Printf(">>> %v \n", inventory.Name)
}

// PrintInventoryFooter is a util function that prints a pretty Header text
// This is intended to be run for each Inventory - Post execution of the domain specific function
func PrintInventoryFooter(inventory Inventory) {
fmt.Print("\n")
}
Expand Down Expand Up @@ -196,6 +200,7 @@ func PrettyPrintActuatorEnvResponse(actuatorEnvResponseStr string) {

}

// PrettyPrintActuatorLinksResponse pretty prints the response from /actuator
func PrettyPrintActuatorLinksResponse(actuatorResponse string) {

reader := MakeDynamicStructReader(ActuatorLinks{}, actuatorResponse)
Expand Down Expand Up @@ -401,6 +406,7 @@ func PrettyPrintActuatorInfoResponse(actuatorResponse string) {

}

// renderAndResetTable is a util function that will render and reset the table - a commonly used set of functions
func renderAndResetTable(t table.Writer) {

t.Render()
Expand All @@ -410,3 +416,32 @@ func renderAndResetTable(t table.Writer) {
t.ResetFooters()

}

// PrettyPrintActuatorMetricsResponse pretty prints the response from /actuator/metrics
func PrettyPrintActuatorMetricsResponse(actuatorResponse string) {

reader := MakeDynamicStructReader(ActuatorMetricsProperties{}, actuatorResponse)

rowConfigAutoMerge := table.RowConfig{AutoMerge: true}

t := MakeTable()

t.AppendHeader(table.Row{
text.Bold.Sprint("Available Metrics"),
}, rowConfigAutoMerge)

if reader.HasField("Names") {

names := reader.GetField("Names").Interface().([]string)

for _, name := range names {
t.AppendRow(table.Row{
name,
}, rowConfigAutoMerge)
}

}

renderAndResetTable(t)

}

0 comments on commit c6c4cdb

Please sign in to comment.