Skip to content

Commit

Permalink
versions updates
Browse files Browse the repository at this point in the history
minor checkstyle improvements
  • Loading branch information
avarabyeu committed May 24, 2024
1 parent 2ecae79 commit 4aac8ed
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 491 deletions.
25 changes: 3 additions & 22 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,9 @@ linters-settings:
- whyNoLint

linters:
enable-all: true
disable:
- prealloc
- gochecknoglobals
- gocyclo
# - gocritic
- interfacer
- wsl
- funlen
- unused
- gomnd
# - maligned
- goerr113
- godot
- nestif
- nlreturn
- sqlclosecheck
- exhaustive
- gci
- exhaustivestruct
- godox
- gochecknoglobals
presets:
- bugs
- performance

run:
concurrency: 1
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ build:
.PHONY: build

lint:
docker run --rm -v $(CURR_DIR):/app -w /app golangci/golangci-lint:v1.32.2 golangci-lint run --enable-all --deadline 10m ./...
docker run --rm -v $(CURR_DIR):/app -w /app golangci/golangci-lint:v1.58.2 golangci-lint run ./...

fmt:
gofumpt -extra -l -w -s ${GOFILES_NOVENDOR}
gofumports -local github.com/fleetframework/sonarqube-prometheus-exporter -l -w ${GOFILES_NOVENDOR}
gci -local github.com/fleetframework/sonarqube-prometheus-exporter -w ${GOFILES_NOVENDOR}
gofumpt -extra -l -w ${GOFILES_NOVENDOR}
goimports -local github.com/fleetframework/goga -w ${GOFILES_NOVENDOR}
gci write --skip-generated --section Standard --section Default --section "Prefix(github.com/fleetframework/sonarqube-prometheus-exporter)" ${GOFILES_NOVENDOR}

test:
$(GO) test ${GODIRS_NOVENDOR}
Expand Down
16 changes: 13 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
module github.com/fleetframework/sonarqube-prometheus-exporter

go 1.16
go 1.22

require (
github.com/prometheus/client_golang v1.11.0
github.com/sirupsen/logrus v1.8.1
github.com/prometheus/client_golang v1.19.1
github.com/sirupsen/logrus v1.9.3
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
golang.org/x/sys v0.20.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
467 changes: 23 additions & 444 deletions go.sum

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
helpCmd bool
)

// nolint:gochecknoinits
//nolint:gochecknoinits
func init() {
var scrapeTimeoutStr string
var labelsStr string
Expand Down Expand Up @@ -140,7 +140,11 @@ func main() {

m := http.NewServeMux()
m.Handle("/metrics", promhttp.Handler())
server := &http.Server{Addr: fmt.Sprintf(":%s", port), Handler: m}
server := &http.Server{
Addr: ":" + port,
Handler: m,
ReadTimeout: 5 * time.Second,
}

go func() {
if err := server.ListenAndServe(); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pkg

import (
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -49,7 +50,7 @@ func (c *Collector) Schedule(done <-chan struct{}, initialDelay, timeout time.Du
}
log.Debugf("Metrics to be collected\n: %s", strings.Join(metricNames, ","))
if len(metricNames) == 0 {
return fmt.Errorf("no metrics to gather detected")
return errors.New("no metrics to gather detected")
}

go c.schedule(done, initialDelay, timeout, func() error {
Expand Down
21 changes: 11 additions & 10 deletions pkg/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ type promMetric struct {

// NewPrometheusExporter creates new exporter instance
func NewPrometheusExporter(ns string, staticLabels map[string]string, labels []string, exportEmptyLabels bool) *PrometheusExporter {
p := &PrometheusExporter{
ns: ns,
metrics: map[string]*promMetric{},
componentLabels: map[string]prometheus.Labels{},
mut: sync.RWMutex{},
staticLabels: staticLabels,
exportEmptyLabels: exportEmptyLabels,
}

// make sure names are escaped
for i, label := range labels {
labels[i] = escapeName(label)
Expand All @@ -54,7 +45,16 @@ func NewPrometheusExporter(ns string, staticLabels map[string]string, labels []s
// adds default component name label
labels = append(labels, componentNameLabel)
sort.Strings(labels)
p.labels = labels

p := &PrometheusExporter{
ns: ns,
metrics: map[string]*promMetric{},
componentLabels: map[string]prometheus.Labels{},
mut: sync.RWMutex{},
staticLabels: staticLabels,
exportEmptyLabels: exportEmptyLabels,
labels: labels,
}

return p
}
Expand Down Expand Up @@ -103,6 +103,7 @@ func (pe *PrometheusExporter) registerMetrics(metrics []*Metric) ([]string, erro
pe.mut.RLock()
defer pe.mut.RUnlock()

//nolint:prealloc
var mNames []string
for _, m := range metrics {
if _, ok := pe.metrics[m.Key]; ok {
Expand Down
10 changes: 5 additions & 5 deletions pkg/sonar_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"

Expand All @@ -24,7 +24,7 @@ func NewSonarClient(url, user, password string) *SonarClient {

func (s *SonarClient) SearchComponents() ([]*ComponentInfo, error) {
var c Components
err := s.executeGet(fmt.Sprintf("%s/api/components/search?qualifiers=TRK", s.url), &c)
err := s.executeGet(s.url+"/api/components/search?qualifiers=TRK", &c)
if err != nil {
return nil, err
}
Expand All @@ -41,7 +41,7 @@ func (s *SonarClient) GetComponent(key string) (*Component, error) {

func (s *SonarClient) GetMetrics() ([]*Metric, error) {
var m Metrics
err := s.executeGet(fmt.Sprintf("%s/api/metrics/search?ps=500", s.url), &m)
err := s.executeGet(s.url+"/api/metrics/search?ps=500", &m)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -77,8 +77,8 @@ func (s *SonarClient) executeGet(u string, res interface{}) error {
}
}
}()
if rs.StatusCode >= 400 {
body, _ := ioutil.ReadAll(rs.Body)
if rs.StatusCode >= http.StatusBadRequest {
body, _ := io.ReadAll(rs.Body)
return fmt.Errorf("request failed. status code %d. Error: %s", rs.StatusCode, string(body))
}

Expand Down

0 comments on commit 4aac8ed

Please sign in to comment.