Skip to content

Commit b1afb76

Browse files
feat(api): add gitlab host metric (#4)
* feature: add gitlab host metric * docs + changelog * tests --------- Co-authored-by: lexander023 <aleksander@scensei.com>
1 parent a800e64 commit b1afb76

File tree

7 files changed

+64
-3
lines changed

7 files changed

+64
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
## [UNRELEASED] prometheus-gitlabci-exporter 0.1.1
1+
## [UNRELEASED] prometheus-gitlabci-exporter 0.2.0
2+
3+
### Features
4+
- add GitLab host name metrics ([#4](https://github.com/AleksanderWWW/prometheus-gitlabci-exporter/pull/4))
25

36
### Changes
47
- refactor to make code more testable ([#3](https://github.com/AleksanderWWW/prometheus-gitlabci-exporter/pull/3))

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ scrape_configs:
162162
| `gitlab_pipeline_total` | Counter | Number of pipelines by status |
163163
| `gitlab_pipeline_last_duration_seconds` | Gauge | Duration of the latest pipeline |
164164
| `gitlab_probe_success` | Gauge | Indicates if the exporter successfully queried GitLab |
165+
| `gitlab_host` | Gauge | Host name of the GitLab instance |
166+
| `exporter_probe_duration_seconds` | Gauge | Duration of the probe in seconds |
165167

166168
---
167169

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ go 1.24.2
55
require (
66
github.com/alecthomas/kingpin/v2 v2.4.0
77
github.com/prometheus/client_golang v1.23.2
8+
github.com/stretchr/testify v1.11.1
89
gitlab.com/gitlab-org/api/client-go v0.157.0
910
)
1011

1112
require (
1213
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
1314
github.com/beorn7/perks v1.0.1 // indirect
1415
github.com/cespare/xxhash/v2 v2.3.0 // indirect
16+
github.com/davecgh/go-spew v1.1.1 // indirect
1517
github.com/google/go-querystring v1.1.0 // indirect
1618
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
1719
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
1820
github.com/kr/text v0.2.0 // indirect
1921
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
22+
github.com/pmezard/go-difflib v1.0.0 // indirect
2023
github.com/prometheus/client_model v0.6.2 // indirect
2124
github.com/prometheus/common v0.66.1 // indirect
2225
github.com/prometheus/procfs v0.16.1 // indirect
@@ -27,4 +30,5 @@ require (
2730
golang.org/x/sys v0.35.0 // indirect
2831
golang.org/x/time v0.12.0 // indirect
2932
google.golang.org/protobuf v1.36.10 // indirect
33+
gopkg.in/yaml.v3 v3.0.1 // indirect
3034
)

internal/desc.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ func Describe(ch chan<- *prometheus.Desc) {
77
ch <- successDesc
88
ch <- latestDurationDesc
99
ch <- probeDurationDesc
10+
ch <- gitlabHostDesc
1011
}
1112

1213
var pipelineTotalDesc = prometheus.NewDesc(
@@ -36,3 +37,10 @@ var probeDurationDesc = prometheus.NewDesc(
3637
nil,
3738
nil,
3839
)
40+
41+
var gitlabHostDesc = prometheus.NewDesc(
42+
"gitlab_host",
43+
"Host name of the GitLab instance",
44+
[]string{"instance"},
45+
nil,
46+
)

internal/metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Metrics struct {
2525
Count PipelineCount
2626
LatestDuration float64
2727
ProbeDuration float64
28+
GitlabHost string
2829
}
2930

3031
func GetMetrics(client *gitlab.Client, opts *GitlabScrapeOpts) (*Metrics, error) {
@@ -82,5 +83,6 @@ func GetMetrics(client *gitlab.Client, opts *GitlabScrapeOpts) (*Metrics, error)
8283
Count: pc,
8384
LatestDuration: duration,
8485
ProbeDuration: probeDuration.Seconds(),
86+
GitlabHost: client.BaseURL().Hostname(),
8587
}, nil
8688
}

internal/send.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func (df *DefaultMetricsSender) SendMetrics(ch chan<- prometheus.Metric, metrics
1313
sendProbeStatus(ch, true)
1414
sendLatestDuration(ch, metrics.LatestDuration)
1515
sendProbeDuration(ch, metrics.ProbeDuration)
16+
sendGitlabHost(ch, metrics.GitlabHost)
1617
}
1718

1819
func sendPipelineCountByStatus(ch chan<- prometheus.Metric, metrics *Metrics, opts *GitlabScrapeOpts) {
@@ -80,3 +81,12 @@ func sendProbeDuration(ch chan<- prometheus.Metric, duration float64) {
8081
duration,
8182
)
8283
}
84+
85+
func sendGitlabHost(ch chan<- prometheus.Metric, host string) {
86+
ch <- prometheus.MustNewConstMetric(
87+
gitlabHostDesc,
88+
prometheus.GaugeValue,
89+
1,
90+
host,
91+
)
92+
}

main_test.go

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import (
77
"os"
88
"strings"
99
"testing"
10+
11+
"github.com/stretchr/testify/assert"
1012
)
1113

12-
func TestAPI(t *testing.T) {
13-
client := &http.Client{}
14+
var client *http.Client = &http.Client{}
1415

16+
func TestAPI(t *testing.T) {
1517
baseUrl := os.Getenv("TEST_BASE_URL")
1618
if baseUrl == "" {
1719
baseUrl = "http://localhost:9115/probe"
@@ -64,6 +66,36 @@ func TestAPI(t *testing.T) {
6466
}
6567
}
6668

69+
func TestResponseContent(t *testing.T) {
70+
baseUrl := os.Getenv("TEST_BASE_URL")
71+
if baseUrl == "" {
72+
baseUrl = "http://localhost:9115/probe"
73+
}
74+
75+
url := fmt.Sprintf("%s?group=%s&project=%s", baseUrl, "alwojnarowicz", "some-project1")
76+
req, err := http.NewRequest("GET", url, nil)
77+
if err != nil {
78+
t.Fatalf("creating request: %v", err)
79+
}
80+
81+
resp, err := client.Do(req)
82+
if err != nil {
83+
t.Fatalf("making request: %v", err)
84+
}
85+
defer saveCloseRespBody(resp)
86+
87+
bodyText, err := io.ReadAll(resp.Body)
88+
if err != nil {
89+
t.Fatalf("reading response body: %v", err)
90+
}
91+
92+
assert.Contains(t, string(bodyText), "gitlab_probe_success")
93+
assert.Contains(t, string(bodyText), "gitlab_host")
94+
assert.Contains(t, string(bodyText), "gitlab_pipeline_total")
95+
assert.Contains(t, string(bodyText), "gitlab_pipeline_total")
96+
assert.Contains(t, string(bodyText), "exporter_probe_duration_seconds")
97+
}
98+
6799
func saveCloseRespBody(resp *http.Response) {
68100
err := resp.Body.Close()
69101
if err != nil {

0 commit comments

Comments
 (0)