diff --git a/.github/badges/.gitkeep b/.github/badges/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..9cf5649 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,52 @@ +name: Analysis +on: + push: + branches: + - main + pull_request: + +permissions: + contents: read + +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.20' + cache: false + - name: golangci-lint + uses: golangci/golangci-lint-action@v3 + with: + # Require: The version of golangci-lint to use. + # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. + # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. + version: v1.54 + + # Optional: working directory, useful for monorepos + # working-directory: somedir + + # Optional: golangci-lint command line arguments. + # + # Note: By default, the `.golangci.yml` file should be at the root of the repository. + # The location of the configuration file can be changed by using `--config=` + # args: --timeout=30m --config=/my/path/.golangci.yml --issues-exit-code=0 + + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true + + # Optional: if set to true, then all caching functionality will be completely disabled, + # takes precedence over all other caching options. + # skip-cache: true + + # Optional: if set to true, then the action won't cache or restore ~/go/pkg. + # skip-pkg-cache: true + + # Optional: if set to true, then the action won't cache or restore ~/.cache/go-build. + # skip-build-cache: true + + # Optional: The mode to install golangci-lint. It can be 'binary' or 'goinstall'. + # install-mode: "goinstall" \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/test.yml similarity index 79% rename from .github/workflows/build.yml rename to .github/workflows/test.yml index 92b081e..a7f8d74 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: Goals +name: Test on: push: @@ -13,11 +13,11 @@ jobs: runs-on: ubuntu-latest steps: - - - name: Set up Go - uses: actions/setup-go@v2 + - uses: actions/checkout@v4 + - uses: actions/setup-go@v4 with: - go-version: 1.20 + go-version: '^1.20' + - run: go version - name: Check out code uses: actions/checkout@v2 @@ -25,11 +25,6 @@ jobs: - name: Build run: go build -v ./... - - name: Static Analysis - run: | - go install honnef.co/go/tools/cmd/staticcheck@2020.2.1 - staticcheck ./... - - name: Test run: | CVPKG=$(go list ./... | grep -v mocks | tr '\n' ',') @@ -46,7 +41,7 @@ jobs: else COLOR=orange fi - curl "https://img.shields.io/badge/coverage-$total%25-$COLOR" > ../.github/badges/coverage.svg + curl "https://img.shields.io/badge/coverage-$total%25-$COLOR" > .github/badges/coverage.svg - name: Commit and push the badge (if it changed) uses: EndBug/add-and-commit@v7 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..295837a --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +run: + export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=http://localhost:4317 + go run main.go + +test: + go test + golangci-lint run \ No newline at end of file diff --git a/file_cache.go b/file_cache.go index 6d30d48..6afb163 100644 --- a/file_cache.go +++ b/file_cache.go @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "io/ioutil" "os" ) @@ -14,7 +13,7 @@ func NewSingleFileCache(location string) *SingleFileCache { if _, err := os.Stat(location); os.IsNotExist(err) { // Create an empty JSON file if it doesn't exist data := []byte("{}") - err := ioutil.WriteFile(location, data, 0644) + err := os.WriteFile(location, data, 0644) if err != nil { panic(err) } @@ -27,7 +26,7 @@ func NewSingleFileCache(location string) *SingleFileCache { func (c *SingleFileCache) AddToCache(key string, value string) error { // Read the existing cache - data, err := ioutil.ReadFile(c.location) + data, err := os.ReadFile(c.location) if err != nil { return err } @@ -48,7 +47,7 @@ func (c *SingleFileCache) AddToCache(key string, value string) error { return err } - err = ioutil.WriteFile(c.location, updatedData, 0644) + err = os.WriteFile(c.location, updatedData, 0644) if err != nil { return err } @@ -58,7 +57,7 @@ func (c *SingleFileCache) AddToCache(key string, value string) error { func (c *SingleFileCache) RetrieveValue(key string) (string, error) { // Read the existing cache - data, err := ioutil.ReadFile(c.location) + data, err := os.ReadFile(c.location) if err != nil { return "", err } diff --git a/file_cache_test.go b/file_cache_test.go index c4a74a6..9ecb55a 100644 --- a/file_cache_test.go +++ b/file_cache_test.go @@ -7,10 +7,21 @@ import ( func TestCacheAdd(t *testing.T) { cache := NewSingleFileCache("test-cache") - cache.AddToCache("test", "value") - value, _ := cache.RetrieveValue("test") + err := cache.AddToCache("test", "value") + if err != nil { + t.Failed() + } + + value, err := cache.RetrieveValue("test") + if err != nil { + t.Failed() + } + assert.Equal(t, "value", value) empty, _ := cache.RetrieveValue("not existent") assert.Empty(t, empty) - cache.DeleteCache() + err = cache.DeleteCache() + if err != nil { + t.Failed() + } } diff --git a/github_client.go b/github_client.go index 97b87ee..50bbf6d 100644 --- a/github_client.go +++ b/github_client.go @@ -4,7 +4,7 @@ import ( "encoding/base64" "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" ) @@ -44,7 +44,7 @@ func (c *GitHubClient) GetMostRecentCommit(repo, timestamp, branch string) (stri if response.StatusCode == http.StatusOK { defer response.Body.Close() - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) if err != nil { return "", err } @@ -80,7 +80,7 @@ func (c *GitHubClient) GetFileAtCommit(repository, filepath, commitSHA string) ( if response.StatusCode == http.StatusOK { defer response.Body.Close() - body, err := ioutil.ReadAll(response.Body) + body, err := io.ReadAll(response.Body) if err != nil { return "", err } diff --git a/grafana_dashboard.go b/grafana_dashboard.go index 6e975b4..a19296e 100644 --- a/grafana_dashboard.go +++ b/grafana_dashboard.go @@ -26,7 +26,10 @@ func generateDashboard(metrics []string) { // Update Dashboard based on metrics dashboard := generateDashboardJson(strings.Join(panels, ",")) - os.WriteFile("grafana/dashboards/instrumentation-benchmarks.json", []byte(dashboard), 0644) + err := os.WriteFile("grafana/dashboards/instrumentation-benchmarks.json", []byte(dashboard), 0644) + if err != nil { + panic(err) + } } func generateDashboardJson(panels string) string { diff --git a/main.go b/main.go index 5c2d172..13ee579 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,10 @@ func main() { cached, _ := commitCache.RetrieveValue(timestamp) if cached == "" { commit, _ = client.GetMostRecentCommit(repo, timestamp, "gh-pages") - commitCache.AddToCache(timestamp, commit) + err := commitCache.AddToCache(timestamp, commit) + if err != nil { + fmt.Println("Error adding to cache") + } } else { commit = cached } @@ -39,7 +42,10 @@ func main() { cached, _ = reportCache.RetrieveValue(timestamp) if cached == "" { contents, _ = client.GetFileAtCommit(repo, filePath, commit) - reportCache.AddToCache(timestamp, contents) + err := reportCache.AddToCache(timestamp, contents) + if err != nil { + fmt.Println("Error adding to cache") + } } else { contents = cached } @@ -83,7 +89,7 @@ func main() { otel.SetMeterProvider(meterProvider) // export to collector - fmt.Sprintf("Exporting metrics") + fmt.Println("Exporting metrics") _ = exp.Export(ctx, resourceMetrics) // Update Dashboard based on metrics diff --git a/report_parser_test.go b/report_parser_test.go index 79a253c..28bcf95 100644 --- a/report_parser_test.go +++ b/report_parser_test.go @@ -1,8 +1,7 @@ package main import ( - "fmt" - "reflect" + "github.com/stretchr/testify/assert" "testing" ) @@ -36,29 +35,27 @@ Peak threads : 42 53 55` func TestParseDateFromSummary(t *testing.T) { expected := "2023-09-01" result := ParseReport(report) - if expected != result.Date.Format("2006-01-02") { - t.Errorf(fmt.Sprintf("should be equal %s %s", expected, result.Date)) - } + assert.Equal(t, expected, result.Date.Format("2006-01-02")) } func TestParseConfigsFromReport(t *testing.T) { - expected := []string{"none", "latest", "snapshot"} + expected := []string{"latest", "snapshot", "none"} result := ParseReport(report) - if reflect.DeepEqual(expected, result) { - t.Errorf(fmt.Sprintf("should be equal")) + + var entities []string + for entity := range result.Metrics { + entities = append(entities, entity) } + + assert.ElementsMatch(t, expected, entities) } func TestParseMetricsFromReport(t *testing.T) { expected := 92.64 result := ParseReport(report).Metrics["none"]["Min heap used (MB)"] - if expected != result { - t.Errorf(fmt.Sprintf("should be equal")) - } + assert.Equal(t, expected, result) expected = 53 result = ParseReport(report).Metrics["latest"]["Peak threads"] - if expected != result { - t.Errorf(fmt.Sprintf("should be equal")) - } + assert.Equal(t, expected, result) } diff --git a/utilities.go b/utilities.go index 2fb6ecf..e030d10 100644 --- a/utilities.go +++ b/utilities.go @@ -7,18 +7,22 @@ import ( "time" ) +var ( + layout = "2006-01-02" +) + func generateTimeframeToToday(start string, interval int) ([]string, error) { currentTime := time.Now() - return generateTimeframeSlice(start, currentTime.Format("2006-01-02"), interval) + return generateTimeframeSlice(start, currentTime.Format(layout), interval) } func generateTimeframeSlice(start, end string, interval int) ([]string, error) { - startDate, err := time.Parse("2006-01-02", start) + startDate, err := time.Parse(layout, start) if err != nil { return nil, fmt.Errorf("failed to parse start date: %v", err) } - endDate, err := time.Parse("2006-01-02", end) + endDate, err := time.Parse(layout, end) if err != nil { return nil, fmt.Errorf("failed to parse end date: %v", err) } @@ -28,7 +32,7 @@ func generateTimeframeSlice(start, end string, interval int) ([]string, error) { // Increment the start date by the interval until it reaches or exceeds the end date currentDate := startDate for currentDate.Before(endDate) || currentDate.Equal(endDate) { - dateList = append(dateList, currentDate.Format("2006-01-02")) + dateList = append(dateList, currentDate.Format(layout)) currentDate = currentDate.AddDate(0, 0, interval) } diff --git a/utilities_test.go b/utilities_test.go index 65bf94b..7b41a38 100644 --- a/utilities_test.go +++ b/utilities_test.go @@ -1,8 +1,7 @@ package main import ( - "fmt" - "reflect" + "github.com/stretchr/testify/assert" "testing" ) @@ -12,25 +11,19 @@ func TestGenerateTimeframeSlice(t *testing.T) { interval := 1 expected := []string{"2023-09-20", "2023-09-21", "2023-09-22", "2023-09-23"} result, _ := generateTimeframeSlice(start, end, interval) - if !reflect.DeepEqual(expected, result) { - t.Errorf(fmt.Sprintf("should be equal %s %s", expected, result)) - } + assert.Equal(t, expected, result) } func TestConvertDateFormat(t *testing.T) { start := "Fri Sep 01 05:16:59 UTC 2023" expected := "2023-09-01" result := convertDateFormat(start) - if !reflect.DeepEqual(expected, result) { - t.Errorf(fmt.Sprintf("should be equal %s %s", expected, result)) - } + assert.Equal(t, expected, result.Format("2006-01-02")) } func TestSplitByMultipleSpaces(t *testing.T) { start := ` 111.87 129.19 129.52` expected := []string{"111.87", "129.19", "129.52"} result := splitByMultipleSpaces(start) - if !reflect.DeepEqual(expected, result) { - t.Errorf(fmt.Sprintf("should be equal %s %s", expected, result)) - } + assert.Equal(t, expected, result) }