Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init changes to include artifactory openmetrics #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ Supported optional metrics:
* `artifacts` - Extracts number of artifacts created/downloaded for each repository. Enabling this will add `artifactory_artifacts_*` metrics. Please note that on large Artifactory instances, this may impact the performance.
* `replication_status` - Extracts status of replication for each repository which has replication enabled. Enabling this will add the `status` label to `artifactory_replication_enabled` metric.
* `federation_status` - Extracts federation metrics. Enabling this will add two new metrics: `artifactory_federation_mirror_lag`, and `artifactory_federation_unavailable_mirror`. Please note that these metrics are only available in Artifactory Enterprise Plus and version 7.18.3 and above.
* `open_metrics` - Exposes Open Metrics from the JFrog Platform. For more information about Open Metrics, please refer to [JFrog Platform Open Metrics](https://jfrog.com/help/r/jfrog-platform-administration-documentation/open-metrics).

### Grafana Dashboard

Expand Down
32 changes: 32 additions & 0 deletions artifactory/openmetrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package artifactory

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

const openMetricsEndpoint = "v1/metrics"

type OpenMetrics struct {
PromMetrics string
NodeId string
}

// FetchOpenMetrics makes the API call to open metrics endpoint and returns all the open metrics
func (c *Client) FetchOpenMetrics() (OpenMetrics, error) {
var openMetrics OpenMetrics
level.Debug(c.logger).Log("msg", "Fetching openMetrics")
resp, err := c.FetchHTTP(openMetricsEndpoint)
if err != nil {
if err.(*APIError).status == 404 {
return openMetrics, nil
}
return openMetrics, err
}

level.Debug(c.logger).Log("msg", "OpenMetrics from Artifactory", "body", string(resp.Body))

openMetrics.NodeId = resp.NodeId
openMetrics.PromMetrics = string(resp.Body)

return openMetrics, nil
}
1 change: 0 additions & 1 deletion collector/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package collector
import (
"encoding/json"
"fmt"

"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
)
Expand Down
16 changes: 16 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ var (
"mirrorLag": newMetric("mirror_lag", "federation", "Federation mirror lag in milliseconds.", federationLabelNames),
"unavailableMirror": newMetric("unavailable_mirror", "federation", "Unsynchronized federated mirror status", append([]string{"status"}, federationLabelNames...)),
}
openMetrics = metrics{
"openMetrics": newMetric("open_metrics", "openmetrics", "OpenMetrics proxied from JFrog Platform", defaultLabelNames),
}
)

func init() {
Expand Down Expand Up @@ -99,6 +102,11 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- m
}
}
if e.optionalMetrics.OpenMetrics {
for _, m := range openMetrics {
ch <- m
}
}

ch <- e.up.Desc()
ch <- e.totalScrapes.Desc()
Expand Down Expand Up @@ -154,6 +162,14 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) (up float64) {
}
}

// Collect and export open metrics
if e.optionalMetrics.OpenMetrics {
err = e.exportOpenMetrics(ch)
if err != nil {
return 0
}
}

// Collect and export system metrics
err = e.exportSystem(license, ch)
if err != nil {
Expand Down
60 changes: 60 additions & 0 deletions collector/openMetrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package collector

import (
"strings"

"github.com/go-kit/log/level"
"github.com/prometheus/client_golang/prometheus"
io_prometheus_client "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
)

func (e *Exporter) exportOpenMetrics(ch chan<- prometheus.Metric) error {
// Fetch Open Metrics
openMetrics, err := e.client.FetchOpenMetrics()
if err != nil {
level.Error(e.logger).Log("msg", "There was an issue when try to fetch openMetrics")
e.totalAPIErrors.Inc()
return err
}

level.Debug(e.logger).Log("msg", "OpenMetrics from Artifactory util", "body", openMetrics.PromMetrics)

// assign openMetrics.Metric to a string variable
openMetricsString := openMetrics.PromMetrics

parser := expfmt.TextParser{}
metrics, err := parser.TextToMetricFamilies(strings.NewReader(openMetricsString))
if err != nil {
// handle the error
return err
}

for _, family := range metrics {
for _, metric := range family.Metric {
// create labels map
labels := make(map[string]string)
for _, label := range metric.Label {
labels[*label.Name] = *label.Value
}

// create a new descriptor
desc := prometheus.NewDesc(
family.GetName(),
family.GetHelp(),
nil,
labels,
)

// create a new metric and collect it
switch family.GetType() {
case io_prometheus_client.MetricType_COUNTER:
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, metric.GetCounter().GetValue())
case io_prometheus_client.MetricType_GAUGE:
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, metric.GetGauge().GetValue())
}
}
}

return nil
}
5 changes: 4 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
optionalMetrics = kingpin.Flag("optional-metric", "optional metric to be enabled. Pass multiple times to enable multiple optional metrics.").PlaceHolder("metric-name").Strings()
)

var optionalMetricsList = []string{"artifacts", "replication_status", "federation_status"}
var optionalMetricsList = []string{"artifacts", "replication_status", "federation_status", "open_metrics"}

// Credentials represents Username and Password or API Key for
// Artifactory Authentication
Expand All @@ -37,6 +37,7 @@ type OptionalMetrics struct {
Artifacts bool
ReplicationStatus bool
FederationStatus bool
OpenMetrics bool
davidshadix marked this conversation as resolved.
Show resolved Hide resolved
}

// Config represents all configuration options for running the Exporter.
Expand Down Expand Up @@ -88,6 +89,8 @@ func NewConfig() (*Config, error) {
optMetrics.ReplicationStatus = true
case "federation_status":
optMetrics.FederationStatus = true
case "open_metrics":
optMetrics.OpenMetrics = true
default:
return nil, fmt.Errorf("unknown optional metric: %s. Valid optional metrics are: %s", metric, optionalMetricsList)
}
Expand Down