Skip to content

Commit

Permalink
Merge pull request #31 from beatlabs/upgrades
Browse files Browse the repository at this point in the history
Upgrades
  • Loading branch information
mantzas authored Jan 19, 2025
2 parents 33624b9 + 0c0c092 commit fe5931f
Show file tree
Hide file tree
Showing 1,566 changed files with 513,708 additions and 105 deletions.
17 changes: 7 additions & 10 deletions .github/workflows/golang.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ jobs:
name: Linting Golang Code
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.15
uses: actions/[email protected]
with:
go-version: 1.15

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: './go.mod'

- name: Download module dependencies
env:
Expand All @@ -25,10 +25,7 @@ jobs:
run: make fmt

- name: Run golangci-lint
uses: golangci/[email protected]
with:
version: v1.33.0
args: --enable scopelint,bodyclose,gofmt,golint --exclude-use-default=false --modules-download-mode=vendor --build-tags integration
uses: golangci/golangci-lint-action@v6

- name: Run go tests
run: make test
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@
*.swp
*.swo

# Dependency directories (remove the comment below to include it)
vendor/

81 changes: 81 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
run:
timeout: 5m
issues-exit-code: 1
tests: true

#modules-download-mode: vendor

# list of build tags, all linters use it. Default is empty list
build-tags:
- integration

# output configuration options
output:
# print lines of code with issue, default is true
print-issued-lines: true

# print linter name in the end of issue text, default is true
print-linter-name: true

issues:
exclude-dirs:
- vendor
max-same-issues: 10
uniq-by-line: false

linters:
disable-all: true
enable:
- gofmt
- gosimple
- unparam
- goconst
- prealloc
- stylecheck
- unconvert
- unused
- staticcheck
- ineffassign
- gosec
- tparallel
- whitespace
- revive
- godot
- errorlint
- gocritic
- errname
- govet
- predeclared
- nestif
- exhaustive
- tenv
- gofumpt
- forcetypeassert
- nilerr
- errcheck
- bodyclose
- goimports
- durationcheck
- errchkjson
- sloglint
- dupword
- noctx
- makezero
- nilnil
- reassign
- spancheck
- testifylint
- wastedassign
- rowserrcheck
- sqlclosecheck
- goprintffuncname
- tagalign
- testableexamples
- wastedassign
- nonamedreturns
- perfsprint
- dogsled
- protogetter
- usestdlibvars
- testableexamples
fast: false
7 changes: 4 additions & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ func (a *app) run() error {
// Initialize HTTP server
hostPort := fmt.Sprintf(":%d", a.port)
server := &http.Server{
Addr: hostPort,
Addr: hostPort,
ReadHeaderTimeout: 5 * time.Second,
}
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/live", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
w.WriteHeader(http.StatusOK)
})
http.HandleFunc("/ready", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(200)
w.WriteHeader(http.StatusOK)
})

// Start listening asynchronously
Expand Down
6 changes: 0 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (r *YamlRequests) getCleanRequests() ([]*dnsStream, error) {
continue
}
cleanRequests = append(cleanRequests, c)

}
if len(cleanRequests) == 0 {
return []*dnsStream{}, errors.Errorf("No valid requests found inside the request sections coming from yaml config")
Expand Down Expand Up @@ -81,17 +80,13 @@ func (r *YamlRequest) getCleanRequest() (*dnsStream, error) {
return newDNSStream(dr, interval), nil
}

// Config holds all our configuration coming from user that our app needs
type config struct {
appPort int
logLevel string
watchdogRequests []*dnsStream
}

// newConfig constructs and returns the struct that will host
// all our configuration variables
func newConfig() (*config, error) {

initViper()
r, err := getYamlConfig()
if err != nil {
Expand Down Expand Up @@ -138,7 +133,6 @@ func initViper() {
// the funciton return a YamlRequests struct that contains all info from
// the file.
func getYamlConfig() (*YamlRequests, error) {

if err := viper.ReadInConfig(); err != nil {
return nil, errors.Wrap(err, "Error reading config file")
}
Expand Down
22 changes: 2 additions & 20 deletions dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ import (
)

const (
// DefaultTimeout is default timeout for the DNS requests
// DefaultTimeout is default timeout for the DNS requests.
DefaultTimeout time.Duration = 5 * time.Second
)

// rCode is a new int type to encapsulate the different response code
// in a more UX friendly way.
type rCode int

// rCode constants to represent response code in more
// UX friendly way
const (
NOERROR rCode = iota
NXDOMAIN
Expand Down Expand Up @@ -54,16 +50,12 @@ func newRCode(rc string) (rCode, error) {
return OTHER, fmt.Errorf("%s is not a supported response code", rc)
}

// dnsResponse holds all the information that
// have to do with a DNS response
type dnsResponse struct {
rawResponse *dns.Msg
code rCode
answers []string
}

// dnsRequest holds all infromation that have to do
// with a DNS request and its expected answers
type dnsRequest struct {
domain string
queryType string
Expand All @@ -72,9 +64,6 @@ type dnsRequest struct {
expectedResponseCode *rCode
}

// dnsStream encapsulates the info related with monitoring DNS
// request and its response. Hence the stream in the name although
// UDP is stream-less.
type dnsStream struct {
request dnsRequest
response dnsResponse
Expand All @@ -83,7 +72,6 @@ type dnsStream struct {
verificationStatus float64
}

// newDNSStream constructs a new dnsStream struct
func newDNSStream(r *dnsRequest, interval int) *dnsStream {
return &dnsStream{request: *r, rtt: 0, verificationStatus: 0, interval: interval}
}
Expand Down Expand Up @@ -139,7 +127,7 @@ func (d *dnsStream) query(dnsClient dnsClientInterface) error {
// first one that is in the resolv.conf of the system.
func (d *dnsStream) constructResolver() (string, error) {
if d.request.resolver != nil {
return fmt.Sprintf("%s:53", *d.request.resolver), nil
return *d.request.resolver + ":53", nil
}

conf, err := dns.ClientConfigFromFile("/etc/resolv.conf")
Expand Down Expand Up @@ -179,7 +167,6 @@ func (d *dnsStream) constructQuery() *dns.Msg {
// storing different answers based on type and also the response
// code.
func (d *dnsStream) parseResponse() {

switch d.response.rawResponse.Rcode {
case dns.RcodeSuccess:
d.response.code = NOERROR
Expand Down Expand Up @@ -218,7 +205,6 @@ func (d *dnsStream) parseResponse() {
// is what user has set to be expected in terms of answers and response
// code.
func (d *dnsStream) isResponseLegit() bool {

// If we have expectations for RC check it against the expected one
if d.request.expectedResponseCode != nil {
if *d.request.expectedResponseCode != d.response.code {
Expand All @@ -240,8 +226,6 @@ func (d *dnsStream) isResponseLegit() bool {
return true
}

// areEqual is a helper function that provides us a way to
// compare if two lists have the same elements regardless their order
func areEqual(a, b []string) bool {
if len(a) != len(b) {
return false
Expand All @@ -262,8 +246,6 @@ func areEqual(a, b []string) bool {
return true
}

// updateStats updates the prometheus stats for specific dnsStream struct
// after we have gotten the DNS response
func (d *dnsStream) updateStats() {
increaseRequestsCounter(d.request.domain, d.request.queryType)
updateRTTHistogram(d.request.domain, d.request.queryType, d.rtt.Seconds())
Expand Down
Loading

0 comments on commit fe5931f

Please sign in to comment.