Skip to content

Commit

Permalink
feat(github): allow setting personal access tokens on GitHub queries
Browse files Browse the repository at this point in the history
  • Loading branch information
freak12techno committed Feb 13, 2022
1 parent 0be99b5 commit c450b8f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Here's the example of the Prometheus config you can use for scraping data:
```yaml
scrape-configs:
- job_name: 'tendermint-exporter'
scrape_interval: 120s
scrape_interval: 10s
static_configs:
- targets: ['<your IP>:9500']
```
Expand All @@ -93,10 +93,11 @@ You can pass the artuments to the executable file to configure it. Here is the p

- `--listen-address` - the address with port the node would listen to. For example, you can use it to redefine port or to make the exporter accessible from the outside by listening on `127.0.0.1`. Defaults to `:9500` (so it's accessible from the outside on port 9500)
- `--local-tendermint-rpc` - local Tendermint RPC URL to query node stats. Defaults to `http://localhost:26657`
- `--remote-tendermint-rpc` - remote Tendermint RPC URL to query node stats. Defaults to `http://rpc.cosmos.network:443`. Optional, if not provided, the exporter won't scrape data from the remote node.
- `--binary-path` - path to a fullnode binary to query version from. It may fail if it's located in $GOPATH and the path is relative, so better to explicitly provide the absolute path. Optional, if not provided the exporter won't collect data on binary version.
- `--remote-tendermint-rpc` - remote Tendermint RPC URL to query node stats. Optional, if not provided, the exporter won't scrape data from the remote node.
- `--binary-path` - path to a fullnode binary to query version from. It may fail if it's located in $GOPATH and the path is relative, so better to explicitly provide the absolute path. Optional, if not provided the exporter won't collect data on binary version. This won't work on chains where `<binary-path> version --long --output json` doesn't work or returns data not in JSON.
- `--github-org` - GitHub organization name.
- `--github-repo` - Github repository. This param and `--github-org` are used to specify the repository hosting the full node binary sources. If one or both of these are not provided, the exporter won't fetch the remote GitHub version.
- `--github-repo` - GitHub repository. This param and `--github-org` are used to specify the repository hosting the full node binary sources. If one or both of these are not provided, the exporter won't fetch the remote GitHub version.
- `--github-token` - GitHub personal access token. You can get one [here](https://github.com/settings/tokens). If this isn't provided, GitHub only allows you to do 60 requests per hour, if you do more requests than that it'll start throwing errors, causing all requests to this exporter to fail.
- `--log-devel` - logger level. Defaults to `info`. You can set it to `debug` to make it more verbose.
Additionally, you can pass a `--config` flag with a path to your config file (I use `.toml`, but anything supported by [viper](https://github.com/spf13/viper) should work). Here's the example of such config:
Expand Down
29 changes: 20 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ var (
BinaryPath string
LogLevel string

GithubOrg string
GithubRepo string
GithubOrg string
GithubRepo string
GithubToken string
)

type VersionInfo struct {
Expand All @@ -57,8 +58,6 @@ type Data struct {

var log = zerolog.New(zerolog.ConsoleWriter{Out: os.Stdout}).With().Timestamp().Logger()

var myClient = &http.Client{Timeout: 10 * time.Second}

var rootCmd = &cobra.Command{
Use: "tendermint-exporter",
Long: "Scrape the data on Tendermint node.",
Expand Down Expand Up @@ -284,7 +283,7 @@ func GetAllData() Data {
}

latestReleaseUrl := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", GithubOrg, GithubRepo)
releaseInfoError = GetJson(latestReleaseUrl, &releaseInfo)
releaseInfoError = GetGithubRelease(latestReleaseUrl, &releaseInfo)
wg.Done()
}()
wg.Add(1)
Expand Down Expand Up @@ -331,14 +330,25 @@ func GetAllData() Data {
}
}

func GetJson(url string, target interface{}) error {
r, err := myClient.Get(url)
func GetGithubRelease(url string, target interface{}) error {
client := &http.Client{Timeout: 10 * time.Second}

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}

if GithubToken != "" {
req.Header.Set("Authorization", "Bearer "+GithubToken)
}

res, err := client.Do(req)
if err != nil {
return err
}
defer r.Body.Close()
defer res.Body.Close()

return json.NewDecoder(r.Body).Decode(target)
return json.NewDecoder(res.Body).Decode(target)
}

func GetNodeStatus(nodeUrl string) (*coretypes.ResultStatus, error) {
Expand Down Expand Up @@ -384,6 +394,7 @@ func main() {
rootCmd.PersistentFlags().StringVar(&BinaryPath, "binary-path", "", "Binary path to get version from")
rootCmd.PersistentFlags().StringVar(&GithubOrg, "github-org", "", "Github organization name")
rootCmd.PersistentFlags().StringVar(&GithubRepo, "github-repo", "", "Github repository name")
rootCmd.PersistentFlags().StringVar(&GithubToken, "github-token", "", "Github personal access token")

if err := rootCmd.Execute(); err != nil {
log.Fatal().Err(err).Msg("Could not start application")
Expand Down

0 comments on commit c450b8f

Please sign in to comment.