Skip to content

Commit

Permalink
Add Debug logs to help troubleshooting (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
peimanja authored Feb 8, 2020
1 parent 5a08b0f commit ae5c73a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 30 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Flags:
| `artifactory.scrape-uri`<br/>`ARTI_SCRAPE_URI` | No | `http://localhost:8081/artifactory` | URI on which to scrape JFrog Artifactory. |
| `artifactory.ssl-verify`<br/>`ARTI_SSL_VERIFY` | No | `true` | Flag that enables SSL certificate verification for the scrape URI. |
| `artifactory.timeout`<br/>`ARTI_TIMEOUT` | No | `5s` | Timeout for trying to get stats from JFrog Artifactory. |
| `log.level` | No | `info` | Only log messages with the given severity or above. One of: [debug, info, warn, error]. |
| `log.format` | No | `logfmt` | Output format of log messages. One of: [logfmt, json]. |
| `ARTI_USERNAME` | *No | | User to access Artifactory |
| `ARTI_PASSWORD` | *No | | Password of the user accessing the Artifactory |
| `ARTI_ACCESS_TOKEN` | *No | | Access token for accessing the Artifactory |
Expand Down
7 changes: 4 additions & 3 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,15 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
ch <- e.jsonParseFailures
}

func fetchHTTP(uri string, path string, cred config.Credentials, authMethod string, sslVerify bool, timeout time.Duration) ([]byte, error) {
func (e *Exporter) fetchHTTP(uri string, path string, cred config.Credentials, authMethod string, sslVerify bool, timeout time.Duration) ([]byte, error) {
fullPath := fmt.Sprintf("%s/api/%s", uri, path)
tr := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: !sslVerify}}
client := http.Client{
Timeout: timeout,
Transport: tr,
}

req, err := http.NewRequest("GET", uri+"/api/"+path, nil)
level.Debug(e.logger).Log("msg", "Fetching http", "path", fullPath)
req, err := http.NewRequest("GET", fullPath, nil)
if err != nil {
return nil, err
}
Expand Down
14 changes: 11 additions & 3 deletions collector/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"strings"

"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -22,11 +23,13 @@ type replication struct {

func (e *Exporter) fetchReplications() ([]replication, error) {
var replications []replication
resp, err := fetchHTTP(e.URI, "replications", e.cred, e.authMethod, e.sslVerify, e.timeout)
level.Debug(e.logger).Log("msg", "Fetching replications stats")
resp, err := e.fetchHTTP(e.URI, "replications", e.cred, e.authMethod, e.sslVerify, e.timeout)
if err != nil {
return nil, err
}
if err := json.Unmarshal(resp, &replications); err != nil {
level.Debug(e.logger).Log("msg", "There was an issue getting replication respond")
e.jsonParseFailures.Inc()
return replications, err
}
Expand All @@ -35,14 +38,19 @@ func (e *Exporter) fetchReplications() ([]replication, error) {

func (e *Exporter) exportReplications(replications []replication, ch chan<- prometheus.Metric) {
if len(replications) == 0 {
level.Debug(e.logger).Log("msg", "No replications stats found")
return
}
for _, replication := range replications {
for metricName, metric := range replicationMetrics {
switch metricName {
case "enabled":
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, b2f(replication.Enabled), replication.RepoKey, strings.ToLower(replication.ReplicationType), replication.CronExp)

enabled := b2f(replication.Enabled)
repo := replication.RepoKey
rType := strings.ToLower(replication.ReplicationType)
cronExp := replication.CronExp
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "repo", replication.RepoKey, "type", rType, "cron", cronExp, "value", enabled)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, enabled, repo, rType, cronExp)
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions collector/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package collector
import (
"encoding/json"

"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -13,7 +14,8 @@ type user struct {

func (e *Exporter) fetchUsers() ([]user, error) {
var users []user
resp, err := fetchHTTP(e.URI, "security/users", e.cred, e.authMethod, e.sslVerify, e.timeout)
level.Debug(e.logger).Log("msg", "Fetching users stats")
resp, err := e.fetchHTTP(e.URI, "security/users", e.cred, e.authMethod, e.sslVerify, e.timeout)
if err != nil {
return nil, err
}
Expand All @@ -30,6 +32,7 @@ type usersCount struct {
}

func (e *Exporter) countUsers(metricName string, metric *prometheus.Desc, users []user, ch chan<- prometheus.Metric) {
level.Debug(e.logger).Log("msg", "Counting users")
userCount := []usersCount{
{0, "saml"},
{0, "internal"},
Expand All @@ -48,9 +51,11 @@ func (e *Exporter) countUsers(metricName string, metric *prometheus.Desc, users
func (e *Exporter) exportUsersCount(metricName string, metric *prometheus.Desc, users []usersCount, ch chan<- prometheus.Metric) {
if users[0].count == 0 && users[1].count == 0 {
e.jsonParseFailures.Inc()
level.Debug(e.logger).Log("msg", "There was an issue getting users respond")
return
}
for _, user := range users {
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "realm", user.realm, "value", user.count)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, user.count, user.realm)
}
}
Expand All @@ -62,11 +67,13 @@ type group struct {

func (e *Exporter) fetchGroups() ([]group, error) {
var groups []group
resp, err := fetchHTTP(e.URI, "security/groups", e.cred, e.authMethod, e.sslVerify, e.timeout)
level.Debug(e.logger).Log("msg", "Fetching groups stats")
resp, err := e.fetchHTTP(e.URI, "security/groups", e.cred, e.authMethod, e.sslVerify, e.timeout)
if err != nil {
return nil, err
}
if err := json.Unmarshal(resp, &groups); err != nil {
level.Debug(e.logger).Log("msg", "There was an issue getting groups respond")
e.jsonParseFailures.Inc()
return groups, err
}
Expand Down
52 changes: 34 additions & 18 deletions collector/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"
"strings"

"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -67,19 +68,21 @@ type storageInfo struct {

func (e *Exporter) fetchStorageInfo() (storageInfo, error) {
var storageInfo storageInfo
resp, err := fetchHTTP(e.URI, "storageinfo", e.cred, e.authMethod, e.sslVerify, e.timeout)
level.Debug(e.logger).Log("msg", "Fetching storage info stats")
resp, err := e.fetchHTTP(e.URI, "storageinfo", e.cred, e.authMethod, e.sslVerify, e.timeout)
if err != nil {
return storageInfo, err
}
if err := json.Unmarshal(resp, &storageInfo); err != nil {
level.Debug(e.logger).Log("msg", "There was an issue getting storageInfo respond")
e.jsonParseFailures.Inc()
return storageInfo, err
}
return storageInfo, nil
}

func removeCommas(str string) (float64, error) {

func (e *Exporter) removeCommas(str string) (float64, error) {
level.Debug(e.logger).Log("msg", "Removing other characters to extract number from string")
reg, err := regexp.Compile("[^0-9.]+")
if err != nil {
return 0, err
Expand All @@ -88,39 +91,45 @@ func removeCommas(str string) (float64, error) {
if err != nil {
return 0, err
}

level.Debug(e.logger).Log("msg", "Successfully converted string to number", "string", str, "number", convertedStr)
return convertedStr, nil
}

func bytesConverter(str string) (float64, error) {
func (e *Exporter) bytesConverter(str string) (float64, error) {
type errorString struct {
s string
}
num, err := removeCommas(str)
var bytesValue float64
level.Debug(e.logger).Log("msg", "Converting size to bytes")
num, err := e.removeCommas(str)
if err != nil {
return 0, err
}

if strings.Contains(str, "bytes") {
return num, nil
bytesValue = num
} else if strings.Contains(str, "KB") {
return num * 1024, nil
bytesValue = num * 1024
} else if strings.Contains(str, "MB") {
return num * 1024 * 1024, nil
bytesValue = num * 1024 * 1024
} else if strings.Contains(str, "GB") {
return num * 1024 * 1024 * 1024, nil
bytesValue = num * 1024 * 1024 * 1024
} else if strings.Contains(str, "TB") {
return num * 1024 * 1024 * 1024 * 1024, nil
bytesValue = num * 1024 * 1024 * 1024 * 1024
} else {
return 0, fmt.Errorf("Could not convert %s to bytes", str)
}
return 0, fmt.Errorf("Could not convert %s to bytes", str)
level.Debug(e.logger).Log("msg", "Successfully converted string to bytes", "string", str, "value", bytesValue)
return bytesValue, nil
}

func (e *Exporter) exportCount(metricName string, metric *prometheus.Desc, count string, ch chan<- prometheus.Metric) {
if count == "" {
e.jsonParseFailures.Inc()
return
}
value, _ := removeCommas(count)
value, _ := e.removeCommas(count)
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "value", value)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, value)
}

Expand All @@ -129,7 +138,8 @@ func (e *Exporter) exportSize(metricName string, metric *prometheus.Desc, size s
e.jsonParseFailures.Inc()
return
}
value, _ := bytesConverter(size)
value, _ := e.bytesConverter(size)
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "value", value)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, value)
}

Expand All @@ -138,7 +148,8 @@ func (e *Exporter) exportFilestore(metricName string, metric *prometheus.Desc, s
e.jsonParseFailures.Inc()
return
}
value, _ := bytesConverter(size)
value, _ := e.bytesConverter(size)
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "type", fileStoreType, "directory", fileStoreDir, "value", value)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, value, fileStoreType, fileStoreDir)
}

Expand All @@ -157,6 +168,7 @@ func (e *Exporter) extractRepoSummary(storageInfo storageInfo, ch chan<- prometh
var err error
rs := repoSummary{}
repoSummaryList := []repoSummary{}
level.Debug(e.logger).Log("msg", "Extracting repo summeriest")
for _, repo := range storageInfo.StorageSummary.RepositoriesSummaryList {
if repo.RepoKey == "TOTAL" {
continue
Expand All @@ -167,12 +179,12 @@ func (e *Exporter) extractRepoSummary(storageInfo storageInfo, ch chan<- prometh
rs.FilesCount = float64(repo.FilesCount)
rs.ItemsCount = float64(repo.ItemsCount)
rs.PackageType = strings.ToLower(repo.PackageType)
rs.UsedSpace, err = bytesConverter(repo.UsedSpace)
rs.UsedSpace, err = e.bytesConverter(repo.UsedSpace)
if err != nil {
e.jsonParseFailures.Inc()
return
}
rs.Percentage, err = removeCommas(repo.Percentage)
rs.Percentage, err = e.removeCommas(repo.Percentage)
if err != nil {
e.jsonParseFailures.Inc()
return
Expand All @@ -183,19 +195,23 @@ func (e *Exporter) extractRepoSummary(storageInfo storageInfo, ch chan<- prometh
}

func (e *Exporter) exportRepo(repoSummaries []repoSummary, ch chan<- prometheus.Metric) {

for _, repoSummary := range repoSummaries {
for metricName, metric := range storageMetrics {
switch metricName {
case "repoUsed":
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "repo", repoSummary.Name, "type", repoSummary.Type, "package_type", repoSummary.PackageType, "value", repoSummary.UsedSpace)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, repoSummary.UsedSpace, repoSummary.Name, repoSummary.Type, repoSummary.PackageType)
case "repoFolders":
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "repo", repoSummary.Name, "type", repoSummary.Type, "package_type", repoSummary.PackageType, "value", repoSummary.FoldersCount)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, repoSummary.FoldersCount, repoSummary.Name, repoSummary.Type, repoSummary.PackageType)
case "repoItems":
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "repo", repoSummary.Name, "type", repoSummary.Type, "package_type", repoSummary.PackageType, "value", repoSummary.ItemsCount)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, repoSummary.ItemsCount, repoSummary.Name, repoSummary.Type, repoSummary.PackageType)
case "repoFiles":
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "repo", repoSummary.Name, "type", repoSummary.Type, "package_type", repoSummary.PackageType, "value", repoSummary.FilesCount)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, repoSummary.FilesCount, repoSummary.Name, repoSummary.Type, repoSummary.PackageType)
case "repoPercentage":
level.Debug(e.logger).Log("msg", "Registering metric", "metric", metricName, "repo", repoSummary.Name, "type", repoSummary.Type, "package_type", repoSummary.PackageType, "value", repoSummary.Percentage)
ch <- prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, repoSummary.Percentage, repoSummary.Name, repoSummary.Type, repoSummary.PackageType)
}
}
Expand Down
18 changes: 14 additions & 4 deletions collector/system.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package collector

import "encoding/json"
import (
"encoding/json"

"github.com/go-kit/kit/log/level"
)

func (e *Exporter) fetchHealth() (float64, error) {
resp, err := fetchHTTP(e.URI, "system/ping", e.cred, e.authMethod, e.sslVerify, e.timeout)
level.Debug(e.logger).Log("msg", "Fetching health stats")
resp, err := e.fetchHTTP(e.URI, "system/ping", e.cred, e.authMethod, e.sslVerify, e.timeout)
if err != nil {
return 0, err
}
bodyString := string(resp)
if bodyString == "OK" {
level.Debug(e.logger).Log("msg", "System ping returned OK")
return 1, nil
}
return 0, err
Expand All @@ -21,11 +27,13 @@ type buildInfo struct {

func (e *Exporter) fetchBuildInfo() (buildInfo, error) {
var buildInfo buildInfo
resp, err := fetchHTTP(e.URI, "system/version", e.cred, e.authMethod, e.sslVerify, e.timeout)
level.Debug(e.logger).Log("msg", "Fetching build stats")
resp, err := e.fetchHTTP(e.URI, "system/version", e.cred, e.authMethod, e.sslVerify, e.timeout)
if err != nil {
return buildInfo, err
}
if err := json.Unmarshal(resp, &buildInfo); err != nil {
level.Debug(e.logger).Log("msg", "There was an issue getting builds respond")
e.jsonParseFailures.Inc()
return buildInfo, err
}
Expand All @@ -40,11 +48,13 @@ type licenseInfo struct {

func (e *Exporter) fetchLicense() (licenseInfo, error) {
var licenseInfo licenseInfo
resp, err := fetchHTTP(e.URI, "system/license", e.cred, e.authMethod, e.sslVerify, e.timeout)
level.Debug(e.logger).Log("msg", "Fetching license stats")
resp, err := e.fetchHTTP(e.URI, "system/license", e.cred, e.authMethod, e.sslVerify, e.timeout)
if err != nil {
return licenseInfo, err
}
if err := json.Unmarshal(resp, &licenseInfo); err != nil {
level.Debug(e.logger).Log("msg", "There was an issue getting license respond")
e.jsonParseFailures.Inc()
return licenseInfo, err
}
Expand Down

0 comments on commit ae5c73a

Please sign in to comment.