Skip to content

Commit

Permalink
github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydeluca committed Sep 29, 2023
1 parent f8f2677 commit 044e0a5
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 57 deletions.
Empty file added .github/badges/.gitkeep
Empty file.
52 changes: 52 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 8 additions & 13 deletions .github/workflows/build.yml → .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Goals
name: Test

on:
push:
Expand All @@ -13,23 +13,18 @@ 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

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

- name: Static Analysis
run: |
go install honnef.co/go/tools/cmd/[email protected]
staticcheck ./...
- name: Test
run: |
CVPKG=$(go list ./... | grep -v mocks | tr '\n' ',')
Expand All @@ -46,11 +41,11 @@ 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
uses: EndBug/add-and-commit@v9
with:
default_author: github_actions
default_author: github_actor
message: 'commit badge'
add: '*.svg'
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
run:
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=http://localhost:4317
go run main.go

test:
go test
golangci-lint run
9 changes: 4 additions & 5 deletions file_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/json"
"io/ioutil"
"os"
)

Expand All @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
17 changes: 14 additions & 3 deletions file_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
6 changes: 3 additions & 3 deletions github_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion grafana_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
25 changes: 11 additions & 14 deletions report_parser_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"fmt"
"reflect"
"github.com/stretchr/testify/assert"
"testing"
)

Expand Down Expand Up @@ -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)
}
12 changes: 8 additions & 4 deletions utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}

Expand Down
15 changes: 4 additions & 11 deletions utilities_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package main

import (
"fmt"
"reflect"
"github.com/stretchr/testify/assert"
"testing"
)

Expand All @@ -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)
}

0 comments on commit 044e0a5

Please sign in to comment.