Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update workflows to test package #29

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/go-coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# workflow taken from https://github.com/marketplace/actions/go-coverage
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
3 changes: 0 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,3 @@ jobs:

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 0 additions & 2 deletions exporter/health_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package exporter

import (
"encoding/json"
"fmt"
"os"
"testing"

Expand Down Expand Up @@ -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)
}
}()

Expand Down
94 changes: 59 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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, "'")
Expand Down
Loading