Skip to content

Commit e45e3fa

Browse files
authored
feat: build migrated to Bazel, project refactored
feat: build migrated to Bazel, project refactored
2 parents 2bb032a + 61745aa commit e45e3fa

25 files changed

+1739
-246
lines changed

.env.example

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
REGCLOUD_TOKEN=ace6ahvaezuqueh3Eec9eikaeth2Oonaiwo3ohzoongu2oom1aigae9phahgae2oi4Oof6oda2naiqueh6eshe0die0Zaibaufoo
2-
REGCLOUD_HTTP_PORT=322
3-
TZ=Europe/Moscow
1+
COLLECTORS_CLOUD_REG_RU_AUTH_TOKEN={{https://help.reg.ru/support/servery-vps/oblachnyye-servery/rabota-s-serverom/api-dlya-oblachnykh-serverov}}
2+
COLLECTORS_SELECTEL_AUTH_TOKEN={{https://developers.selectel.ru/docs/control-panel/authorization/}}

.github/workflows/on_push_main.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: on_push_main_stage
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
jobs:
8+
update_stage:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Login to GitHub Container Registry
14+
uses: docker/login-action@v3
15+
with:
16+
registry: ghcr.io
17+
username: ${{ github.actor }}
18+
password: ${{ secrets.GITHUB_TOKEN }}
19+
- uses: bazelbuild/setup-bazelisk@v3
20+
- name: Mount bazel cache
21+
uses: actions/cache@v4
22+
with:
23+
path: "~/.cache/bazel"
24+
key: bazel-${{ hashFiles('.bazelversion', '.bazelrc', 'WORKSPACE', 'WORKSPACE.bazel', 'MODULE.bazel', '**/BUILD.bazel') }}
25+
restore-keys: |
26+
bazel-
27+
- run: bazel run push

.gitignore

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
# Environment
2-
.env
3-
4-
# Golang
5-
go.sum
1+
#----Environment----
2+
/**/.env
3+
#-------------------
4+
#-----Intellij------
5+
.idea/
6+
.idea_modules/
7+
#-------------------
8+
#-------Bazel-------
9+
bazel-*
10+
.ijwb
11+
#-------------------
12+
#-------VSCode------
13+
.vscode
14+
#-------------------

BUILD.bazel

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
load("@gazelle//:def.bzl", "gazelle")
2+
load("@rules_oci//oci:defs.bzl", "oci_push", "oci_tarball")
3+
4+
gazelle(name = "gazelle")
5+
6+
oci_tarball(
7+
name = "bundle",
8+
image = "//cmd/http_exporter:oci",
9+
repo_tags = ["local/mrdan4es/http_exporter:latest"],
10+
)
11+
12+
oci_push(
13+
name = "push",
14+
image = "//cmd/http_exporter:oci",
15+
remote_tags = ["latest"],
16+
repository = "ghcr.io/mrdan4es/http_exporter",
17+
)

MODULE.bazel

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module(name = "http_exporter")
2+
3+
bazel_dep(name = "rules_go", version = "0.48.0")
4+
bazel_dep(name = "gazelle", version = "0.37.0")
5+
bazel_dep(name = "rules_oci", version = "2.0.0-alpha5")
6+
bazel_dep(name = "rules_pkg", version = "0.9.1")
7+
8+
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
9+
oci.pull(
10+
name = "distroless_base_debian12",
11+
digest = "sha256:2102ce121ff1448316b93c5f347118a8e604f5ba7ec9dd7a5c2d8b0eac2941fe",
12+
image = "gcr.io/distroless/base-debian12",
13+
platforms = [
14+
"linux/amd64",
15+
],
16+
)
17+
use_repo(oci, "distroless_base_debian12", "distroless_base_debian12_linux_amd64")
18+
19+
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
20+
go_sdk.download(version = "1.22.5")
21+
22+
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
23+
go_deps.from_file(go_mod = "//:go.mod")
24+
use_repo(
25+
go_deps,
26+
"com_github_jmespath_go_jmespath",
27+
"com_github_prometheus_client_golang",
28+
"com_github_rs_zerolog",
29+
"com_github_spf13_pflag",
30+
"com_github_spf13_viper",
31+
)

MODULE.bazel.lock

Lines changed: 1085 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Whitespace-only changes.

cmd/http_exporter/BUILD.bazel

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
load("@rules_go//go:def.bzl", "go_binary", "go_library")
2+
load("@rules_oci//oci:defs.bzl", "oci_image")
3+
load("@rules_pkg//:pkg.bzl", "pkg_tar")
4+
5+
go_library(
6+
name = "http_exporter_lib",
7+
srcs = ["http_exporter.go"],
8+
importpath = "github.com/mrdan4es/http_exporter/cmd/http_exporter",
9+
visibility = ["//visibility:private"],
10+
deps = [
11+
"//pkg/collector",
12+
"//pkg/config",
13+
"@com_github_prometheus_client_golang//prometheus",
14+
"@com_github_prometheus_client_golang//prometheus/promhttp",
15+
"@com_github_rs_zerolog//:zerolog",
16+
"@com_github_rs_zerolog//log",
17+
"@com_github_spf13_pflag//:pflag",
18+
],
19+
)
20+
21+
go_binary(
22+
name = "http_exporter",
23+
embed = [":http_exporter_lib"],
24+
visibility = ["//visibility:public"],
25+
)
26+
27+
pkg_tar(
28+
name = "layer",
29+
srcs = ["http_exporter"],
30+
)
31+
32+
oci_image(
33+
name = "oci",
34+
base = "@distroless_base_debian12",
35+
entrypoint = ["/http_exporter"],
36+
tars = ["layer"],
37+
visibility = ["//visibility:public"],
38+
)

cmd/http_exporter/http_exporter.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"flag"
8+
"fmt"
9+
"net/http"
10+
"os/signal"
11+
"syscall"
12+
"time"
13+
14+
"github.com/prometheus/client_golang/prometheus"
15+
"github.com/prometheus/client_golang/prometheus/promhttp"
16+
"github.com/rs/zerolog"
17+
zlog "github.com/rs/zerolog/log"
18+
"github.com/spf13/pflag"
19+
20+
"github.com/mrdan4es/http_exporter/pkg/collector"
21+
"github.com/mrdan4es/http_exporter/pkg/config"
22+
)
23+
24+
func main() {
25+
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
26+
defer cancel()
27+
28+
configPath := pflag.StringP("config", "c", "", "path to config file")
29+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
30+
pflag.Parse()
31+
32+
cfg, err := config.Load(*configPath)
33+
if err != nil {
34+
panic(err)
35+
}
36+
37+
log := zlog.Output(
38+
zerolog.NewConsoleWriter(
39+
func(w *zerolog.ConsoleWriter) {
40+
w.TimeFormat = time.RFC3339Nano
41+
},
42+
),
43+
)
44+
ctx = log.WithContext(ctx)
45+
46+
msg, err := json.Marshal(cfg)
47+
if err != nil {
48+
log.Fatal().
49+
Err(err).
50+
Msg("marshal config")
51+
}
52+
53+
log.Info().
54+
RawJSON("config", msg).
55+
Send()
56+
57+
if err := run(ctx, cfg); err != nil {
58+
log.Fatal().
59+
Err(err).
60+
Send()
61+
}
62+
}
63+
64+
func run(ctx context.Context, cfg *config.Config) error {
65+
log := zerolog.Ctx(ctx)
66+
67+
for _, collectorCfg := range cfg.Collectors {
68+
c, err := collector.New(ctx, collectorCfg)
69+
if err != nil {
70+
return fmt.Errorf("create %s collector: %w", collectorCfg.Name, err)
71+
}
72+
73+
prometheus.MustRegister(c)
74+
}
75+
76+
mux := http.NewServeMux()
77+
mux.Handle("/metrics", promhttp.Handler())
78+
79+
httpServer := &http.Server{
80+
Addr: cfg.HTTP.ListenAddr,
81+
Handler: mux,
82+
}
83+
84+
go func() {
85+
<-ctx.Done()
86+
87+
log.Info().Msg("Shutting down HTTP server...")
88+
89+
shutdownCtx := context.Background()
90+
shutdownCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
91+
defer cancel()
92+
93+
if err := httpServer.Shutdown(shutdownCtx); err != nil {
94+
log.Err(err).Send()
95+
}
96+
}()
97+
98+
log.Info().Msgf("Serving /metrics on addr %s...", cfg.HTTP.ListenAddr)
99+
100+
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
101+
return err
102+
}
103+
104+
return nil
105+
}

config.example.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
HTTP:
2+
listenAddr: ":322"
3+
4+
collectors:
5+
- name: cloud_reg_ru
6+
url: https://api.cloudvps.reg.ru/v1/balance_data
7+
auth:
8+
method: bearer
9+
token: ${COLLECTORS_CLOUD_REG_RU_AUTH_TOKEN}
10+
fields:
11+
- name: balance
12+
description: Balance
13+
query: balance_data.balance
14+
- name: hours_left
15+
description: Hours left
16+
query: balance_data.hours_left
17+
- name: selectel
18+
url: https://api.selectel.ru/v3/balances
19+
auth:
20+
method: x-token
21+
token: ${COLLECTORS_SELECTEL_AUTH_TOKEN}
22+
fields:
23+
- name: balance
24+
description: Balance
25+
query: data.billings[0].final_sum

docker-compose.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
version: "3.8"
21
services:
3-
regcloud-exporter:
4-
build: src/
2+
http_exporter:
3+
image: local/mrdan4es/http_exporter
54
ports:
6-
- "${REGCLOUD_HTTP_PORT}:${REGCLOUD_HTTP_PORT}"
5+
- "322:322"
6+
volumes:
7+
- ./config.example.yml:/tmp/config.yml
8+
command: ["--config", "/tmp/config.yml"]
79
env_file:
8-
- .env
10+
- .env.example

go.mod

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module github.com/mrdan4es/http_exporter
2+
3+
go 1.22
4+
5+
require (
6+
github.com/jmespath/go-jmespath v0.4.0
7+
github.com/prometheus/client_golang v1.19.1
8+
github.com/rs/zerolog v1.33.0
9+
github.com/spf13/pflag v1.0.5
10+
github.com/spf13/viper v1.19.0
11+
)
12+
13+
require (
14+
github.com/beorn7/perks v1.0.1 // indirect
15+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
16+
github.com/fsnotify/fsnotify v1.7.0 // indirect
17+
github.com/hashicorp/hcl v1.0.0 // indirect
18+
github.com/magiconair/properties v1.8.7 // indirect
19+
github.com/mattn/go-colorable v0.1.13 // indirect
20+
github.com/mattn/go-isatty v0.0.20 // indirect
21+
github.com/mitchellh/mapstructure v1.5.0 // indirect
22+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
23+
github.com/prometheus/client_model v0.5.0 // indirect
24+
github.com/prometheus/common v0.48.0 // indirect
25+
github.com/prometheus/procfs v0.12.0 // indirect
26+
github.com/sagikazarmark/locafero v0.4.0 // indirect
27+
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
28+
github.com/sourcegraph/conc v0.3.0 // indirect
29+
github.com/spf13/afero v1.11.0 // indirect
30+
github.com/spf13/cast v1.6.0 // indirect
31+
github.com/subosito/gotenv v1.6.0 // indirect
32+
go.uber.org/multierr v1.11.0 // indirect
33+
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
34+
golang.org/x/sys v0.23.0 // indirect
35+
golang.org/x/text v0.14.0 // indirect
36+
google.golang.org/protobuf v1.33.0 // indirect
37+
gopkg.in/ini.v1 v1.67.0 // indirect
38+
gopkg.in/yaml.v3 v3.0.1 // indirect
39+
)

0 commit comments

Comments
 (0)