Skip to content

Commit ed68ff7

Browse files
committed
feat: add labels for cache and puppetdb api usage
1 parent 25609b0 commit ed68ff7

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

cmd/puppet-report-exporter/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,11 @@ func (app *application) puppetdbLogMetricCollectorBuilder(refreshNotify chan any
119119
var report []puppet.ReportLogEntry
120120

121121
if item := applicationInstance.reportLogCache.Get(node.LatestReportHash); item != nil {
122+
go metrics.PuppetDBReportCacheAccess.With(prometheus.Labels{metrics.LabelType: "hit"}).Add(1)
122123
report = item.Value()
123124
} else {
124125
var reportFetchError error
126+
go metrics.PuppetDBReportCacheAccess.With(prometheus.Labels{metrics.LabelType: "miss"}).Add(1)
125127
report, reportFetchError = app.puppetDb.GetReportHashInfo(node.LatestReportHash)
126128
if reportFetchError != nil {
127129
continue

internal/metrics/metrics.go

+28
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ func init() {
1010
NodeCount,
1111
NodeLogEntries,
1212
PuppetDBReportCacheEntries,
13+
PuppetDBReportCacheAccess,
14+
PuppetDBQueries,
1315
)
1416
}
1517

1618
const (
1719
LabelEnvironment = "environment"
1820
LabelNode = "node"
1921
LabelLevel = "level"
22+
LabelType = "type"
23+
LabelEndpoint = "endpoint"
2024
)
2125

2226
var (
@@ -54,4 +58,28 @@ var (
5458
Help: "Number of entries in the report log cache",
5559
},
5660
)
61+
62+
PuppetDBReportCacheAccess = prometheus.NewCounterVec(
63+
prometheus.CounterOpts{
64+
Namespace: "puppet",
65+
Subsystem: "report_exporter",
66+
Name: "puppetdb_report_cache_access",
67+
Help: "Number of accesses in the report log cache",
68+
},
69+
[]string{
70+
LabelType,
71+
},
72+
)
73+
74+
PuppetDBQueries = prometheus.NewCounterVec(
75+
prometheus.CounterOpts{
76+
Namespace: "puppet",
77+
Subsystem: "report_exporter",
78+
Name: "puppetdb_queries",
79+
Help: "Number of queries to the PuppetDB API",
80+
},
81+
[]string{
82+
LabelEndpoint,
83+
},
84+
)
5785
)

pkg/puppet/api_client.go

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import (
66
"log"
77
"net/http"
88
"net/url"
9+
10+
"github.com/prometheus/client_golang/prometheus"
11+
12+
"github.com/bonsai-oss/puppet-report-exporter/internal/metrics"
913
)
1014

1115
type ApiClient struct {
@@ -40,6 +44,7 @@ func NewApiClient(options ...ApiClientOption) *ApiClient {
4044
// GetNodes - Get all nodes from the PuppetDB API
4145
func (client *ApiClient) GetNodes() ([]Node, error) {
4246
var nodes []Node
47+
go metrics.PuppetDBQueries.With(prometheus.Labels{metrics.LabelEndpoint: "nodes"}).Inc()
4348
response, err := http.Get(client.url.JoinPath("pdb/query/v4/nodes").String())
4449
if err != nil {
4550
return nil, err
@@ -57,6 +62,7 @@ func (client *ApiClient) GetNodes() ([]Node, error) {
5762
}
5863

5964
func (client *ApiClient) GetReportHashInfo(hash string) ([]ReportLogEntry, error) {
65+
go metrics.PuppetDBQueries.With(prometheus.Labels{metrics.LabelEndpoint: "reports"}).Inc()
6066
response, reportFetchError := http.Get(client.url.JoinPath("pdb/query/v4/reports", hash, "logs").String())
6167
if reportFetchError != nil {
6268
return nil, reportFetchError

0 commit comments

Comments
 (0)