This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metrics API endpoint and command to retrive server telemetry.
In addition to sending telemetry to an endpoint, this commit now allows metrics to be viewed by calling the Sherpa API. A command can also be used, which wraps the API, to output the telemetry information to the terminal. The API and command are nested under the system commands and endpoints.
- Loading branch information
Showing
11 changed files
with
167 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package metrics | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/jrasell/sherpa/pkg/api" | ||
clientCfg "github.com/jrasell/sherpa/pkg/config/client" | ||
"github.com/ryanuber/columnize" | ||
"github.com/sean-/sysexits" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
outputHeader = "Name|Type|Value" | ||
) | ||
|
||
func RegisterCommand(rootCmd *cobra.Command) error { | ||
cmd := &cobra.Command{ | ||
Use: "metrics", | ||
Short: "Retrieve metrics from a Sherpa server", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
runInfo(cmd, args) | ||
}, | ||
} | ||
rootCmd.AddCommand(cmd) | ||
|
||
return nil | ||
} | ||
|
||
func runInfo(_ *cobra.Command, _ []string) { | ||
clientConfig := clientCfg.GetConfig() | ||
mergedConfig := api.DefaultConfig(&clientConfig) | ||
|
||
client, err := api.NewClient(mergedConfig) | ||
if err != nil { | ||
fmt.Println("Error setting up Sherpa client:", err) | ||
os.Exit(sysexits.Software) | ||
} | ||
|
||
metrics, err := client.System().Metrics() | ||
if err != nil { | ||
fmt.Println("Error calling server metrics:", err) | ||
os.Exit(sysexits.Software) | ||
} | ||
|
||
out := []string{outputHeader} | ||
|
||
for i := range metrics.Gauges { | ||
out = append(out, fmt.Sprintf("%s|%s|%v", | ||
metrics.Gauges[i].Name, "Gauge", metrics.Gauges[i].Value)) | ||
} | ||
|
||
for i := range metrics.Counters { | ||
out = append(out, fmt.Sprintf("%s|%s|%v", | ||
metrics.Counters[i].Name, "Counter", metrics.Counters[i].Mean)) | ||
} | ||
|
||
for i := range metrics.Samples { | ||
out = append(out, fmt.Sprintf("%s|%s|%v", | ||
metrics.Samples[i].Name, "Counter", metrics.Samples[i].Mean)) | ||
} | ||
|
||
// If there are no metrics to print (happens during initial server startup) | ||
// then we don't want to just print the header so perform a check so the | ||
// CLI is nice and tidy. | ||
if len(out) > 1 { | ||
fmt.Println(formatList(out)) | ||
} | ||
} | ||
|
||
func formatList(in []string) string { | ||
columnConf := columnize.DefaultConfig() | ||
columnConf.Empty = "<none>" | ||
return columnize.Format(in, columnConf) | ||
} |
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
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