Skip to content

Commit

Permalink
prometheus: Added build_info metric.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpds committed Apr 17, 2024
1 parent 4eb600b commit cd63fef
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var runCmd = &cobra.Command{
}

metrics := prometheus.New()
metrics.SetBuildInfo(cmd.Version)
manager := manager.New(repository, metrics, gitConfig.Path, cfg.Hostname, machineId)
go poller.Poller(manager, cfg.Remotes)
http.Serve(manager,
Expand Down
12 changes: 12 additions & 0 deletions internal/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import (

type Prometheus struct {
promRegistry *prometheus.Registry
buildInfo *prometheus.GaugeVec
deploymentInfo *prometheus.GaugeVec
fetchCounter *prometheus.CounterVec
}

func New() Prometheus {
promReg := prometheus.NewRegistry()
buildInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "comin_build_info",
Help: "Build info for comin.",
}, []string{"version"})
deploymentInfo := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "comin_deployment_info",
Help: "Info of the last deployment.",
Expand All @@ -23,10 +28,12 @@ func New() Prometheus {
Name: "comin_fetch_count",
Help: "Number of fetches per status",
}, []string{"remote_name", "status"})
promReg.MustRegister(buildInfo)
promReg.MustRegister(deploymentInfo)
promReg.MustRegister(fetchCounter)
return Prometheus{
promRegistry: promReg,
buildInfo: buildInfo,
deploymentInfo: deploymentInfo,
fetchCounter: fetchCounter,
}
Expand All @@ -44,6 +51,11 @@ func (m Prometheus) IncFetchCounter(remoteName, status string) {
m.fetchCounter.With(prometheus.Labels{"remote_name": remoteName, "status": status}).Inc()
}

func (m Prometheus) SetBuildInfo(version string) {
m.buildInfo.Reset()
m.buildInfo.With(prometheus.Labels{"version": version}).Set(1)
}

func (m Prometheus) SetDeploymentInfo(commitId, status string) {
m.deploymentInfo.Reset()
m.deploymentInfo.With(prometheus.Labels{"commit_id": commitId, "status": status}).Set(1)
Expand Down

0 comments on commit cd63fef

Please sign in to comment.