Skip to content

Commit 2dc541d

Browse files
committed
squash: feat: add metric for node report status
Squashed commit of the following: * ci: use pipeline templates Signed-off-by: Florian Bauer <florian@fsrv.xyz> * doc: add note for enum values to metric help Signed-off-by: Florian Bauer <florian@fsrv.xyz> * feat: add metric for node report status Signed-off-by: Florian Bauer <florian@fsrv.xyz> See merge request https://gitlab.com/bonsai-oss/exporter/puppet-report-exporter/-/merge_requests/39
1 parent 044de2b commit 2dc541d

File tree

4 files changed

+73
-94
lines changed

4 files changed

+73
-94
lines changed

.gitlab-ci.yml

+9-77
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,16 @@
11
---
22
stages:
33
- test
4-
- build
54
- release
5+
- build
66

7-
lint:
8-
image: registry.gitlab.com/gitlab-org/gitlab-build-images:golangci-lint-alpine
9-
allow_failure: true
10-
stage: test
11-
script:
12-
- golangci-lint run ./... --issues-exit-code 0 --out-format code-climate | tee gl-code-quality-report.json | jq -r '.[] | "\(.location.path):\(.location.lines.begin) \(.description)"'
13-
artifacts:
14-
reports:
15-
codequality: gl-code-quality-report.json
16-
paths:
17-
- gl-code-quality-report.json
18-
19-
vulnerability_check:
20-
image: golang:latest
21-
stage: test
22-
before_script:
23-
- go install golang.org/x/vuln/cmd/govulncheck@latest
24-
script:
25-
- govulncheck ./...
26-
needs:
27-
- lint
28-
29-
go_fmt:
30-
image: golang
31-
stage: test
32-
script:
33-
- go fmt ./...
34-
35-
go_test:
36-
image: golang:latest
7+
.go_template_defaults:
378
stage: test
38-
before_script:
39-
- go install gotest.tools/gotestsum@latest
40-
script:
41-
- go test -v ./... -coverprofile=coverage.txt -covermode count
42-
- go tool cover -func coverage.txt
43-
- go get github.com/boumenot/gocover-cobertura
44-
- go run github.com/boumenot/gocover-cobertura < coverage.txt > coverage.xml
45-
- /go/bin/gotestsum --junitfile report.xml --format testname
46-
coverage: '/^total:\t+\(statements\)\t+(\d+\.\d+)%/'
47-
artifacts:
48-
when: always
49-
reports:
50-
junit: report.xml
51-
coverage_report:
52-
coverage_format: cobertura
53-
path: coverage.xml
54-
55-
build:
56-
stage: build
57-
image:
58-
name: gcr.io/kaniko-project/executor:debug
59-
entrypoint: [ "" ]
60-
script:
61-
- mkdir -p /kaniko/.docker
62-
- echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${CI_REGISTRY_USER}" "${CI_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json
63-
- >-
64-
/kaniko/executor
65-
--context "${CI_PROJECT_DIR}"
66-
--dockerfile "${CI_PROJECT_DIR}/Dockerfile"
67-
--destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHORT_SHA}"
68-
--destination "${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}"
69-
--destination "${CI_REGISTRY_IMAGE}:latest"
70-
71-
semantic_release:
72-
image: registry.fsrv.services/fsrvcorp/container/payipi:latest
9+
.semver_template_defaults:
7310
stage: release
74-
variables:
75-
GIT_STRATEGY: clone
76-
GIT_DEPTH: 0
77-
rules:
78-
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
79-
script:
80-
- get-next-version -f json
81-
- VERSION="$(get-next-version -f json | jq -r 'select(.hasNextVersion==true) | .version')"
82-
- echo $VERSION
83-
- test -z "$VERSION" || curl --silent --request POST --header "PRIVATE-TOKEN:${CI_BOT_TOKEN}" "https://${CI_SERVER_HOST}/api/v4/projects/${CI_PROJECT_ID}/repository/tags?tag_name=${VERSION}&ref=${CI_DEFAULT_BRANCH}" | jq
84-
11+
.kaniko_template_defaults:
12+
stage: build
13+
include:
14+
- { project: bonsai-oss/organization/automate/ci-templates, file: templates/language/go.yml, ref: 1.0.5 }
15+
- { project: bonsai-oss/organization/automate/ci-templates, file: templates/release/semver.yml, ref: 1.0.5 }
16+
- { project: bonsai-oss/organization/automate/ci-templates, file: templates/release/kaniko.yml, ref: 1.0.5 }

cmd/puppet-report-exporter/main.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ func (app *application) puppetdbNodesCrawlerBuilder(refreshNotify chan any) work
129129
app.nodeCache = nodes
130130
metrics.NodeCount.Reset()
131131
for _, node := range nodes {
132-
metrics.NodeCount.With(prometheus.Labels{metrics.LabelEnvironment: node.CatalogEnvironment}).Add(1)
132+
metrics.NodeCount.With(prometheus.Labels{
133+
metrics.LabelEnvironment: node.CatalogEnvironment,
134+
}).Add(1)
135+
metrics.NodeStatus.With(prometheus.Labels{
136+
metrics.LabelEnvironment: node.CatalogEnvironment,
137+
metrics.LabelNode: node.Certname,
138+
}).Set(float64(node.LatestReportStatus.ToNumeric()))
133139
}
134140
app.nodeCacheLock.Unlock()
135141
refreshNotify <- nil
@@ -148,6 +154,12 @@ func (app *application) httpReportMetricCollectorBuilder(messageChan chan puppet
148154
return
149155
case report := <-messageChan:
150156
app.reportLogCache.Set(report.Host, report.Logs, 1*time.Hour)
157+
158+
metrics.NodeStatus.With(prometheus.Labels{
159+
metrics.LabelEnvironment: report.Environment,
160+
metrics.LabelNode: report.Host,
161+
}).Set(float64(report.Status.ToNumeric()))
162+
151163
for _, l := range puppet.Levels {
152164
metrics.NodeLogEntries.With(prometheus.Labels{
153165
metrics.LabelEnvironment: report.Environment,

internal/metrics/metrics.go

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ func init() {
88
// Register the metrics to prometheus.
99
prometheus.MustRegister(
1010
NodeCount,
11+
NodeStatus,
1112
NodeLogEntries,
1213
PuppetDBReportCacheEntries,
1314
PuppetDBReportCacheAccess,
@@ -39,6 +40,19 @@ var (
3940
},
4041
)
4142

43+
NodeStatus = prometheus.NewGaugeVec(
44+
prometheus.GaugeOpts{
45+
Namespace: "puppet",
46+
Subsystem: "report_exporter",
47+
Name: "node_status",
48+
Help: "Report status of nodes; 0 = undefined, 1 = changed, 2 = failed, 3 = unchanged",
49+
},
50+
[]string{
51+
LabelEnvironment,
52+
LabelNode,
53+
},
54+
)
55+
4256
NodeLogEntries = prometheus.NewGaugeVec(
4357
prometheus.GaugeOpts{
4458
Namespace: "puppet",

pkg/puppet/types.go

+37-16
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,46 @@ type ReportData struct {
88
Host string `yaml:"host"`
99
Logs []ReportLogEntry `yaml:"logs"`
1010
Environment string `yaml:"environment"`
11+
Status ReportStatus `yaml:"status"`
12+
}
13+
14+
type ReportStatus string
15+
16+
const (
17+
ReportStatusChanged ReportStatus = "changed"
18+
ReportStatusFailed ReportStatus = "failed"
19+
ReportStatusUnchanged ReportStatus = "unchanged"
20+
)
21+
22+
func (s ReportStatus) ToNumeric() uint8 {
23+
switch s {
24+
case ReportStatusChanged:
25+
return 1
26+
case ReportStatusFailed:
27+
return 2
28+
case ReportStatusUnchanged:
29+
return 3
30+
}
31+
return 0
1132
}
1233

1334
type Node struct {
14-
Deactivated interface{} `json:"deactivated"`
15-
LatestReportHash string `json:"latest_report_hash"`
16-
FactsEnvironment string `json:"facts_environment"`
17-
CachedCatalogStatus string `json:"cached_catalog_status"`
18-
ReportEnvironment string `json:"report_environment"`
19-
LatestReportCorrectiveChange interface{} `json:"latest_report_corrective_change"`
20-
CatalogEnvironment string `json:"catalog_environment"`
21-
FactsTimestamp time.Time `json:"facts_timestamp"`
22-
LatestReportNoop bool `json:"latest_report_noop"`
23-
Expired interface{} `json:"expired"`
24-
LatestReportNoopPending bool `json:"latest_report_noop_pending"`
25-
ReportTimestamp time.Time `json:"report_timestamp"`
26-
Certname string `json:"certname"`
27-
CatalogTimestamp time.Time `json:"catalog_timestamp"`
28-
LatestReportJobId interface{} `json:"latest_report_job_id"`
29-
LatestReportStatus string `json:"latest_report_status"`
35+
Deactivated interface{} `json:"deactivated"`
36+
LatestReportHash string `json:"latest_report_hash"`
37+
FactsEnvironment string `json:"facts_environment"`
38+
CachedCatalogStatus string `json:"cached_catalog_status"`
39+
ReportEnvironment string `json:"report_environment"`
40+
LatestReportCorrectiveChange interface{} `json:"latest_report_corrective_change"`
41+
CatalogEnvironment string `json:"catalog_environment"`
42+
FactsTimestamp time.Time `json:"facts_timestamp"`
43+
LatestReportNoop bool `json:"latest_report_noop"`
44+
Expired interface{} `json:"expired"`
45+
LatestReportNoopPending bool `json:"latest_report_noop_pending"`
46+
ReportTimestamp time.Time `json:"report_timestamp"`
47+
Certname string `json:"certname"`
48+
CatalogTimestamp time.Time `json:"catalog_timestamp"`
49+
LatestReportJobId interface{} `json:"latest_report_job_id"`
50+
LatestReportStatus ReportStatus `json:"latest_report_status"`
3051
}
3152

3253
// ReportLogEntry - representation of https://puppet.com/docs/puppet/7/format_report.html#format_report-puppet-util-log

0 commit comments

Comments
 (0)