Skip to content

Commit 7c24c5c

Browse files
authored
Merge pull request #13 from ninech/fix-import-issue
build: upgrade dependencies
2 parents 6fd1055 + 92510e5 commit 7c24c5c

8 files changed

Lines changed: 208 additions & 406 deletions

File tree

‎.github/workflows/go.yml‎

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,68 @@
1-
name: Go
1+
name: go
22

3-
on: [push, pull_request]
3+
on: [pull_request]
4+
5+
permissions:
6+
contents: read
47

58
jobs:
6-
test:
7-
name: Test
9+
lint:
10+
name: lint
811
runs-on: ubuntu-latest
912
steps:
10-
- name: Set up Go 1.x
11-
uses: actions/setup-go@v2
12-
with:
13-
go-version: ^1.14
14-
id: go
13+
- uses: actions/checkout@v4
14+
- uses: actions/setup-go@v5
15+
with:
16+
go-version-file: go.mod
17+
- run: go mod tidy -diff
18+
- run: go vet ./...
19+
- uses: golangci/golangci-lint-action@v9
20+
with:
21+
version: latest
22+
args: --timeout=10m
23+
- uses: dominikh/staticcheck-action@v1
24+
with:
25+
install-go: false
26+
- uses: golang/govulncheck-action@v1
27+
with:
28+
repo-checkout: false
29+
cache: false
1530

16-
- name: Check out code into the Go module directory
17-
uses: actions/checkout@v2
31+
spell-check:
32+
name: spell-check
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@v4
36+
- uses: crate-ci/typos@v1.44.0
1837

19-
- name: Get dependencies
20-
run: go get -v -t -d ./...
38+
test:
39+
name: test
40+
runs-on: ubuntu-latest
41+
steps:
42+
- uses: actions/checkout@v4
43+
- uses: actions/setup-go@v5
44+
with:
45+
go-version-file: go.mod
46+
- run: go test -v ./...
47+
- name: Test Cloud Function build (simulate GCP buildpack)
48+
run: bash scripts/test-cloudfn-build.sh
2149

22-
- name: Test
23-
run: go test -v ./...
50+
terraform:
51+
name: terraform
52+
runs-on: ubuntu-latest
53+
steps:
54+
- uses: actions/checkout@v4
55+
- uses: hashicorp/setup-terraform@v3
56+
- run: terraform fmt -check -recursive
57+
working-directory: terraform
58+
- run: terraform init -backend=false
59+
working-directory: terraform
60+
- run: terraform validate
61+
working-directory: terraform
62+
- uses: terraform-linters/setup-tflint@v4
63+
with:
64+
tflint_version: latest
65+
- run: tflint --init
66+
working-directory: terraform
67+
- run: tflint
68+
working-directory: terraform

‎exporter.go‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
"strings"
1414
"time"
1515

16-
"github.com/go-kit/kit/log"
17-
"github.com/go-kit/kit/log/level"
16+
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
17+
"github.com/go-kit/log"
18+
"github.com/go-kit/log/level"
1819
"github.com/prometheus/blackbox_exporter/config"
1920
"github.com/prometheus/blackbox_exporter/prober"
2021
"github.com/prometheus/client_golang/prometheus"
@@ -45,11 +46,15 @@ var (
4546
})
4647
)
4748

48-
// Handler is a http.Handler which will be called by the
49+
func init() {
50+
functions.HTTP("Handler", handler)
51+
}
52+
53+
// handler is a http.handler which will be called by the
4954
// GCP Cloud Function on every request.
5055
// It has been adapted from the original blackbox_exporter probeHandler:
5156
// https://github.com/prometheus/blackbox_exporter/blob/63678419a6a274ac6d43d3d4088cad2a1d06371f/main.go#L70
52-
func Handler(w http.ResponseWriter, r *http.Request) {
57+
func handler(w http.ResponseWriter, r *http.Request) {
5358
ctx, cancel := context.WithTimeout(r.Context(), timeoutSeconds*time.Second)
5459
defer cancel()
5560
r = r.WithContext(ctx)
@@ -92,7 +97,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
9297
}
9398
}
9499

95-
level.Info(logger).Log("msg", "Beginning probe", "probe", module.Prober, "timeout_seconds", timeoutSeconds)
100+
_ = level.Info(logger).Log("msg", "Beginning probe", "probe", module.Prober, "timeout_seconds", timeoutSeconds)
96101

97102
probeSuccessGauge := prometheus.NewGauge(prometheus.GaugeOpts{
98103
Name: "probe_success",
@@ -113,14 +118,14 @@ func Handler(w http.ResponseWriter, r *http.Request) {
113118
probeDurationGauge.Set(duration)
114119
if success {
115120
probeSuccessGauge.Set(1)
116-
level.Info(logger).Log("msg", "Probe succeeded", "duration_seconds", duration)
121+
_ = level.Info(logger).Log("msg", "Probe succeeded", "duration_seconds", duration)
117122
} else {
118-
level.Error(logger).Log("msg", "Probe failed", "duration_seconds", duration)
123+
_ = level.Error(logger).Log("msg", "Probe failed", "duration_seconds", duration)
119124
}
120125

121126
mfs, err := registry.Gather()
122127
if err != nil {
123-
level.Debug(logger).Log("msg", err.Error())
128+
_ = level.Debug(logger).Log("msg", err.Error())
124129
http.Error(w, err.Error(), http.StatusInternalServerError)
125130
return
126131
}
@@ -134,15 +139,15 @@ func Handler(w http.ResponseWriter, r *http.Request) {
134139
enc := expfmt.NewEncoder(buf, contentType)
135140
for _, mf := range mfs {
136141
if err := enc.Encode(mf); err != nil {
137-
level.Debug(logger).Log("msg", err.Error())
142+
_ = level.Debug(logger).Log("msg", err.Error())
138143
http.Error(w, err.Error(), http.StatusInternalServerError)
139144
return
140145
}
141146
}
142147

143148
w.Header().Add("Content-Type", string(contentType))
144149
w.Header().Add("Content-Length", fmt.Sprint(buf.Len()))
145-
fmt.Fprint(w, buf)
150+
_, _ = fmt.Fprint(w, buf)
146151
}
147152

148153
// If a google cloud function is experiencing a cold start, TLS handshakes were observed
@@ -157,7 +162,7 @@ func coldStartRequest(target string) error {
157162
return nil
158163
}
159164
cloudFunctionColdStartGauge.Set(1)
160-
level.Info(logger).Log("msg", "cold start detected, making an initial request to the target", "target", target)
165+
_ = level.Info(logger).Log("msg", "cold start detected, making an initial request to the target", "target", target)
161166
client = &http.Client{
162167
Timeout: 10 * time.Second,
163168
}
@@ -213,23 +218,21 @@ func overwriteHTTPModuleParams(params url.Values, conf *config.HTTPProbe) error
213218
// parseStatusCodes parses the given comma separated status codes
214219
// it is possible to use shortcuts like 2xx, 3xx, etc which will be expanded
215220
// to the corresponding whole range of status codes
221+
var statusCodeRangeRe = regexp.MustCompile("[0-9]xx")
222+
216223
func parseStatusCodes(codes string) ([]int, error) {
217224
var result []int
218-
splittedCodes := strings.Split(codes, ",")
219-
for _, code := range splittedCodes {
225+
splitCodes := strings.Split(codes, ",")
226+
for _, code := range splitCodes {
220227
code := strings.TrimSpace(code)
221-
match, err := regexp.MatchString("[0-9]xx", code)
222-
if err != nil {
223-
return nil, err
224-
}
225-
if match {
228+
if statusCodeRangeRe.MatchString(code) {
226229
mainCode, _ := strconv.Atoi(string(code[0]))
227230
result = append(result, generateCodeRange(mainCode*100, mainCode*100+99)...)
228231
continue
229232
}
230233
parsed, err := strconv.Atoi(code)
231234
if err != nil {
232-
return nil, fmt.Errorf("Can not convert status code \"%s\" to a number", code)
235+
return nil, fmt.Errorf("can not convert status code %q to a number", code)
233236
}
234237
result = append(result, parsed)
235238
}

‎exporter_test.go‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package exporter
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"math/rand"
77
"net/http"
88
"net/http/httptest"
99
"net/url"
1010
"os"
1111
"testing"
12-
"time"
1312

1413
"github.com/alecthomas/assert"
1514
)
@@ -30,14 +29,12 @@ func TestMain(m *testing.M) {
3029
func initTestServers() map[string]*httptest.Server {
3130
result := make(map[string]*httptest.Server)
3231
result["simple"] = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33-
fmt.Fprintln(w, "ok")
32+
_, _ = fmt.Fprintln(w, "ok")
3433
}))
3534
result["randomClientError"] = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
36-
rand.Seed(time.Now().UnixNano())
3735
http.Error(w, "client error", 400+rand.Intn(100))
3836
}))
3937
result["serverError"] = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
40-
rand.Seed(time.Now().UnixNano())
4138
errorCodes := []int{500, 501, 502, 503}
4239
http.Error(w, "server error", errorCodes[rand.Intn(len(errorCodes))])
4340
}))
@@ -81,9 +78,9 @@ func TestHandler(t *testing.T) {
8178
t.Logf("Using URL: %s\n", testURL)
8279
req := httptest.NewRequest("GET", testURL, nil)
8380
w := httptest.NewRecorder()
84-
Handler(w, req)
81+
handler(w, req)
8582
resp := w.Result()
86-
body, err := ioutil.ReadAll(resp.Body)
83+
body, err := io.ReadAll(resp.Body)
8784
assert.NoError(t, err)
8885

8986
assert.Equal(t, tt.expectedStatus, resp.StatusCode, fmt.Sprintf("Expected status code %v, but got %v", tt.expectedStatus, resp.StatusCode))

‎go.mod‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ module github.com/ninech/blackbox-exporter-cloudfunction
33
go 1.25.0
44

55
require (
6+
github.com/GoogleCloudPlatform/functions-framework-go v1.9.2
67
github.com/alecthomas/assert v0.0.0-20170929043011-405dbfeb8e38
7-
github.com/go-kit/kit v0.10.0
8+
github.com/go-kit/log v0.2.1
89
github.com/prometheus/blackbox_exporter v0.25.0
910
github.com/prometheus/client_golang v1.19.0
1011
github.com/prometheus/common v0.52.2
@@ -17,27 +18,33 @@ require (
1718
github.com/andybalholm/brotli v1.1.0 // indirect
1819
github.com/beorn7/perks v1.0.1 // indirect
1920
github.com/cespare/xxhash/v2 v2.3.0 // indirect
20-
github.com/go-kit/log v0.2.1 // indirect
21+
github.com/cloudevents/sdk-go/v2 v2.15.2 // indirect
2122
github.com/go-logfmt/logfmt v0.5.1 // indirect
23+
github.com/google/uuid v1.6.0 // indirect
2224
github.com/jpillora/backoff v1.0.0 // indirect
23-
github.com/kr/text v0.2.0 // indirect
24-
github.com/mattn/go-isatty v0.0.4 // indirect
25+
github.com/json-iterator/go v1.1.12 // indirect
26+
github.com/mattn/go-isatty v0.0.16 // indirect
2527
github.com/miekg/dns v1.1.58 // indirect
28+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
29+
github.com/modern-go/reflect2 v1.0.2 // indirect
2630
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
2731
github.com/prometheus/client_model v0.6.1 // indirect
2832
github.com/prometheus/procfs v0.12.0 // indirect
2933
github.com/sergi/go-diff v1.1.0 // indirect
3034
github.com/stretchr/testify v1.9.0 // indirect
35+
go.uber.org/atomic v1.4.0 // indirect
36+
go.uber.org/multierr v1.1.0 // indirect
37+
go.uber.org/zap v1.10.0 // indirect
3138
golang.org/x/mod v0.32.0 // indirect
3239
golang.org/x/net v0.51.0 // indirect
3340
golang.org/x/oauth2 v0.36.0 // indirect
3441
golang.org/x/sync v0.19.0 // indirect
3542
golang.org/x/sys v0.41.0 // indirect
3643
golang.org/x/text v0.34.0 // indirect
3744
golang.org/x/tools v0.41.0 // indirect
38-
google.golang.org/genproto/googleapis/rpc v0.0.0-20241209162323-e6fa225c2576 // indirect
39-
google.golang.org/grpc v1.67.3 // indirect
40-
google.golang.org/protobuf v1.35.2 // indirect
45+
google.golang.org/genproto/googleapis/rpc v0.0.0-20260217215200-42d3e9bedb6d // indirect
46+
google.golang.org/grpc v1.78.0 // indirect
47+
google.golang.org/protobuf v1.36.11 // indirect
4148
gopkg.in/yaml.v2 v2.4.0 // indirect
4249
gopkg.in/yaml.v3 v3.0.1 // indirect
4350
)

0 commit comments

Comments
 (0)