Skip to content

Commit 63b9efc

Browse files
committed
Use go 1.19 and add prometheus server
1 parent 8e0ce7b commit 63b9efc

File tree

7 files changed

+52
-27
lines changed

7 files changed

+52
-27
lines changed

.github/workflows/build-binary-package.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ jobs:
1010
name: Build and upload binary package
1111
runs-on: ubuntu-latest
1212
steps:
13-
- name: Set up Go 1.13
13+
- name: Set up Go 1.19
1414
uses: actions/setup-go@v1
1515
with:
16-
go-version: 1.13
16+
go-version: 1.19
1717
id: go
1818
- name: Check out code into the Go module directory
1919
uses: actions/checkout@v2

.github/workflows/go.yml

-5
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,3 @@ jobs:
2323
run: make build
2424
- name: Test
2525
run: go test -v
26-
27-
28-
29-
30-

config.go

+26-19
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,33 @@ import (
1313
"gopkg.in/yaml.v2"
1414
)
1515

16+
type PrometheusConfig struct {
17+
Enabled bool `yaml:"enabled"`
18+
ListenAddress string `yaml:"listen_addr"`
19+
ListenPort string `yaml:"listen_port"`
20+
}
21+
1622
type bouncerConfig struct {
17-
BinPath string `yaml:"bin_path"` // path to binary
18-
PidDir string `yaml:"piddir"`
19-
UpdateFrequency string `yaml:"update_frequency"`
20-
IncludeScenariosContaining []string `yaml:"include_scenarios_containing"`
21-
ExcludeScenariosContaining []string `yaml:"exclude_scenarios_containing"`
22-
OnlyIncludeDecisionsFrom []string `yaml:"only_include_decisions_from"`
23-
Daemon bool `yaml:"daemonize"`
24-
LogMode string `yaml:"log_mode"`
25-
LogDir string `yaml:"log_dir"`
26-
LogLevel log.Level `yaml:"log_level"`
27-
LogMaxSize int `yaml:"log_max_size,omitempty"`
28-
LogMaxFiles int `yaml:"log_max_files,omitempty"`
29-
LogMaxAge int `yaml:"log_max_age,omitempty"`
30-
CompressLogs *bool `yaml:"compress_logs,omitempty"`
31-
APIUrl string `yaml:"api_url"`
32-
APIKey string `yaml:"api_key"`
33-
CacheRetentionDuration time.Duration `yaml:"cache_retention_duration"`
34-
FeedViaStdin bool `yaml:"feed_via_stdin"`
35-
TotalRetries int `yaml:"total_retries"`
23+
BinPath string `yaml:"bin_path"` // path to binary
24+
PidDir string `yaml:"piddir"`
25+
UpdateFrequency string `yaml:"update_frequency"`
26+
IncludeScenariosContaining []string `yaml:"include_scenarios_containing"`
27+
ExcludeScenariosContaining []string `yaml:"exclude_scenarios_containing"`
28+
OnlyIncludeDecisionsFrom []string `yaml:"only_include_decisions_from"`
29+
Daemon bool `yaml:"daemonize"`
30+
LogMode string `yaml:"log_mode"`
31+
LogDir string `yaml:"log_dir"`
32+
LogLevel log.Level `yaml:"log_level"`
33+
LogMaxSize int `yaml:"log_max_size,omitempty"`
34+
LogMaxFiles int `yaml:"log_max_files,omitempty"`
35+
LogMaxAge int `yaml:"log_max_age,omitempty"`
36+
CompressLogs *bool `yaml:"compress_logs,omitempty"`
37+
APIUrl string `yaml:"api_url"`
38+
APIKey string `yaml:"api_key"`
39+
CacheRetentionDuration time.Duration `yaml:"cache_retention_duration"`
40+
FeedViaStdin bool `yaml:"feed_via_stdin"`
41+
TotalRetries int `yaml:"total_retries"`
42+
PrometheusConfig PrometheusConfig `yaml:"prometheus"`
3643
}
3744

3845
func NewConfig(configPath string) (*bouncerConfig, error) {

config/crowdsec-custom-bouncer.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ log_max_backups: 3
1515
log_max_age: 30
1616
api_url: http://localhost:8080/
1717
api_key: ${API_KEY}
18+
19+
prometheus:
20+
enabled: true
21+
listen_addr: 127.0.0.1
22+
listen_port: 60602

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/crowdsecurity/crowdsec v1.4.1
88
github.com/crowdsecurity/go-cs-bouncer v0.0.0-20220817075151-29237cbe9873
99
github.com/go-openapi/swag v0.22.3 // indirect
10+
github.com/prometheus/client_golang v1.13.0
1011
github.com/sirupsen/logrus v1.9.0
1112
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c // indirect
1213
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
@@ -38,7 +39,6 @@ require (
3839
github.com/mitchellh/mapstructure v1.5.0 // indirect
3940
github.com/oklog/ulid v1.3.1 // indirect
4041
github.com/pkg/errors v0.9.1 // indirect
41-
github.com/prometheus/client_golang v1.13.0 // indirect
4242
github.com/prometheus/client_model v0.2.0 // indirect
4343
github.com/prometheus/common v0.37.0 // indirect
4444
github.com/prometheus/procfs v0.8.0 // indirect

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA
180180
github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
181181
github.com/certifi/gocertifi v0.0.0-20191021191039-0944d244cd40/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
182182
github.com/certifi/gocertifi v0.0.0-20200922220541-2c3bb06c6054/go.mod h1:sGbDF6GwGcLpkNXPUTkMRoywsNa/ol15pxFe6ERfguA=
183+
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
183184
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
184185
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
185186
github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE=

main.go

+17
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"net"
7+
"net/http"
68
"os"
79
"os/exec"
810
"os/signal"
911
"syscall"
1012
"time"
1113

1214
"github.com/coreos/go-systemd/daemon"
15+
"github.com/prometheus/client_golang/prometheus"
16+
"github.com/prometheus/client_golang/prometheus/promhttp"
1317
log "github.com/sirupsen/logrus"
1418
"github.com/sirupsen/logrus/hooks/writer"
1519

@@ -118,6 +122,19 @@ func main() {
118122
bouncer.Run()
119123
return fmt.Errorf("stream api init failed")
120124
})
125+
if config.PrometheusConfig.Enabled {
126+
prometheus.MustRegister(csbouncer.TotalLAPICalls, csbouncer.TotalLAPIError)
127+
go func() {
128+
http.Handle("/metrics", promhttp.Handler())
129+
listenOn := net.JoinHostPort(
130+
config.PrometheusConfig.ListenAddress,
131+
config.PrometheusConfig.ListenPort,
132+
)
133+
log.Infof("Serving metrics at %s", listenOn+"/metrics")
134+
log.Error(http.ListenAndServe(listenOn, nil))
135+
}()
136+
}
137+
go bouncer.Run()
121138
if config.FeedViaStdin {
122139
t.Go(
123140
func() error {

0 commit comments

Comments
 (0)