From bceda660b47e09a88aef5b511e94e0b91259f2b6 Mon Sep 17 00:00:00 2001 From: K-Yo Date: Mon, 21 Oct 2024 11:42:04 +0200 Subject: [PATCH 1/5] update workflows to test package --- .github/workflows/go-coverage.yml | 42 +++++++++++++++++++++++++++++++ .github/workflows/go.yml | 3 --- 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/go-coverage.yml diff --git a/.github/workflows/go-coverage.yml b/.github/workflows/go-coverage.yml new file mode 100644 index 0000000..6ad6f89 --- /dev/null +++ b/.github/workflows/go-coverage.yml @@ -0,0 +1,42 @@ +# workflow taken from https://github.com/marketplace/actions/go-coveragev +name: "Go Coverage" +on: + pull_request: + push: + branches: + # It's important that the action also runs on merge to main + - main + +jobs: + coverage: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + # default fetch-depth is insufficent to find previous coverage notes + fetch-depth: 10 + + - uses: gwatts/go-coverage-action@v2 + id: coverage + with: + # Optional coverage threshold + # use fail-coverage to determine what should happen below this threshold + coverage-threshold: 80 + + # collect coverage for all packages beyond the one under test + cover-pkg: ./... + + # Ignore code-generated files when calculating coverage totals + ignore-pattern: | + \.pb\.go$ + \_string\.go$ + + # A url that the html report will be accessible at, once your + # workflow uploads it. Used in the pull request comment. + # report-url: https://artifacts.example.com/go-coverage/${{ github.ref_name}}.html + + # - name: Upload coverage to s3 + # # ensure this runs regardless of whether the threshold is met using always() + # if: always() && steps.coverage.outputs.report-pathname != '' + # run: | + # aws s3 cp ${{ steps.coverage.outputs.report-pathname }} s3://artifacts.example.com-bucket/go-coverage/${{ github.ref_name}}.html \ No newline at end of file diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index d381dd7..da656e2 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -29,6 +29,3 @@ jobs: - name: Build run: go build -v ./... - - - name: Test - run: go test -v ./... From b20c1f4a5d20eb337b2432618f91481d7dd6de22 Mon Sep 17 00:00:00 2001 From: K-Yo Date: Mon, 21 Oct 2024 12:02:27 +0200 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=94=87=20remove=20unused=20logs=20fro?= =?UTF-8?q?m=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exporter/health_manager_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/exporter/health_manager_test.go b/exporter/health_manager_test.go index e14328a..05d38c5 100644 --- a/exporter/health_manager_test.go +++ b/exporter/health_manager_test.go @@ -2,7 +2,6 @@ package exporter import ( "encoding/json" - "fmt" "os" "testing" @@ -47,7 +46,6 @@ func TestDeployment(t *testing.T) { // empty chan go func() { for x := <-ch; x != nil; x = <-ch { - fmt.Fprintln(os.Stdout, x) } }() From c5a20974b430a65352eb71d75552a43afff08c4f Mon Sep 17 00:00:00 2001 From: K-Yo Date: Mon, 21 Oct 2024 12:32:47 +0200 Subject: [PATCH 3/5] =?UTF-8?q?=F0=9F=9A=A7=20=E2=99=BB=EF=B8=8F=20start?= =?UTF-8?q?=20refactoring=20run()=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 94 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 35 deletions(-) diff --git a/main.go b/main.go index 89c2a11..2b9f635 100644 --- a/main.go +++ b/main.go @@ -13,8 +13,10 @@ import ( "syscall" "github.com/alecthomas/kingpin/v2" + "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/common/promlog" "github.com/prometheus/common/promlog/flag" "github.com/prometheus/common/version" @@ -67,49 +69,16 @@ func run() int { return 1 } - // register exporter - opts := exporter.SplunkOpts{ - URI: sc.C.URL, - Token: sc.C.Token, - Username: sc.C.Username, - Password: sc.C.Password, - Insecure: sc.C.Insecure, - } - exp, err := exporter.New(opts, logger, sc.C.Metrics) + exp, err := createAndRegisterExporter(logger) if err != nil { - level.Error(logger).Log("msg", "could not create exporter", "err", err) return 1 } - prometheus.MustRegister(exp) - - // Infer or set Splunk exporter externalURL - listenAddrs := toolkitFlags.WebListenAddresses - if *externalURL == "" && *toolkitFlags.WebSystemdSocket { - level.Error(logger).Log("msg", "Cannot automatically infer external URL with systemd socket listener. Please provide --web.external-url") - return 1 - } else if *externalURL == "" && len(*listenAddrs) > 1 { - level.Info(logger).Log("msg", "Inferring external URL from first provided listen address") - } - beURL, err := computeExternalURL(*externalURL, (*listenAddrs)[0]) + beURL, err := getRoutePrefix(logger) if err != nil { - level.Error(logger).Log("msg", "failed to determine external URL", "err", err) return 1 } - level.Debug(logger).Log("externalURL", beURL.String()) - - // Default -web.route-prefix to path of -web.external-url. - if *routePrefix == "" { - *routePrefix = beURL.Path - } - // routePrefix must always be at least '/'. - *routePrefix = "/" + strings.Trim(*routePrefix, "/") - // routePrefix requires path to have trailing "/" in order - // for browsers to interpret the path-relative path correctly, instead of stripping it. - if *routePrefix != "/" { - *routePrefix = *routePrefix + "/" - } level.Debug(logger).Log("routePrefix", *routePrefix) hup := make(chan os.Signal, 1) @@ -246,6 +215,61 @@ func run() int { } +// createAndRegisterExporter creates an exporter from configuration and returns it. +func createAndRegisterExporter(logger log.Logger) (*exporter.Exporter, error) { + // register exporter + opts := exporter.SplunkOpts{ + URI: sc.C.URL, + Token: sc.C.Token, + Username: sc.C.Username, + Password: sc.C.Password, + Insecure: sc.C.Insecure, + } + exp, err := exporter.New(opts, logger, sc.C.Metrics) + if err != nil { + level.Error(logger).Log("msg", "could not create exporter", "err", err) + return nil, err + } + + prometheus.MustRegister(exp) + + return exp, nil +} + +// getRoutePrefix computes the route prefix from parameters. +// it returns the base URL. +func getRoutePrefix(logger log.Logger) (*url.URL, error) { + + // Infer or set Splunk exporter externalURL + listenAddrs := toolkitFlags.WebListenAddresses + if *externalURL == "" && *toolkitFlags.WebSystemdSocket { + level.Error(logger).Log("msg", "Cannot automatically infer external URL with systemd socket listener. Please provide --web.external-url") + return nil, fmt.Errorf("Cannot automatically infer external URL with systemd socket listener. Please provide --web.external-url") + } else if *externalURL == "" && len(*listenAddrs) > 1 { + level.Info(logger).Log("msg", "Inferring external URL from first provided listen address") + } + beURL, err := computeExternalURL(*externalURL, (*listenAddrs)[0]) + if err != nil { + level.Error(logger).Log("msg", "failed to determine external URL", "err", err) + return nil, fmt.Errorf("failed to determine external URL") + } + level.Debug(logger).Log("externalURL", beURL.String()) + + // Default -web.route-prefix to path of -web.external-url. + if *routePrefix == "" { + *routePrefix = beURL.Path + } + + // routePrefix must always be at least '/'. + *routePrefix = "/" + strings.Trim(*routePrefix, "/") + // routePrefix requires path to have trailing "/" in order + // for browsers to interpret the path-relative path correctly, instead of stripping it. + if *routePrefix != "/" { + *routePrefix = *routePrefix + "/" + } + return beURL, nil +} + func startsOrEndsWithQuote(s string) bool { return strings.HasPrefix(s, "\"") || strings.HasPrefix(s, "'") || strings.HasSuffix(s, "\"") || strings.HasSuffix(s, "'") From 6f27a3234003aed558930b58a52e4fc5b14e7cd1 Mon Sep 17 00:00:00 2001 From: K-Yo Date: Fri, 6 Dec 2024 15:58:13 +0100 Subject: [PATCH 4/5] removing coverage threshold --- .github/workflows/go-coverage.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/go-coverage.yml b/.github/workflows/go-coverage.yml index 6ad6f89..ece861d 100644 --- a/.github/workflows/go-coverage.yml +++ b/.github/workflows/go-coverage.yml @@ -1,4 +1,4 @@ -# workflow taken from https://github.com/marketplace/actions/go-coveragev +# workflow taken from https://github.com/marketplace/actions/go-coverage name: "Go Coverage" on: pull_request: @@ -21,7 +21,7 @@ jobs: with: # Optional coverage threshold # use fail-coverage to determine what should happen below this threshold - coverage-threshold: 80 + # coverage-threshold: 80 # collect coverage for all packages beyond the one under test cover-pkg: ./... From 4eba67789cd016c7ce7f1ca69d87a7f55e525dd5 Mon Sep 17 00:00:00 2001 From: K-Yo Date: Fri, 6 Dec 2024 15:58:28 +0100 Subject: [PATCH 5/5] add coverage doc --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 5b7b7e3..04e21ed 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,13 @@ All metrics are **Gauge**. go test -v ./... ``` +To check coverage: + +```shell +go test -v ./... --coverprofile cover.out +go tool cover -html=cover.out +``` + ## ✨ Roadmap | Item | Status |