Skip to content

Commit

Permalink
Merge pull request #16 from EladLeev/add-golangci-lint
Browse files Browse the repository at this point in the history
feat: add lint file, fix and fix errors
  • Loading branch information
EladLeev authored Jul 1, 2023
2 parents 4d6f967 + 9b7b8bd commit e57876a
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 22 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: Build Package
on:
push:
branches:
- main
branches: [main]
pull_request:
branches:
- main
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: golangci-lint
on:
branches: [main]
pull_request:

permissions:
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# IDEs
.idea

# Dependency directories (remove the comment below to include it)
# vendor/

Expand Down
19 changes: 19 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
run:
timeout: 5m

linters:
enable:
- errcheck
- goimports
- govet
- staticcheck
- durationcheck
- revive

issues:
exclude-use-default: false
max-issues-per-linter: 0
max-same-issues: 0

output:
sort-results: true
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,40 @@ SHELL := /bin/bash
export GOBIN := $(CWD)/.bin
NAME=schema-registry-statistics

.PHONY: build
build:
GOARCH=amd64 GOOS=darwin go build -o ${NAME}-darwin main.go
GOARCH=amd64 GOOS=linux go build -o ${NAME}-linux main.go

.PHONY: clean
clean:
go clean
rm ${NAME}-darwin
rm ${NAME}-linux

.PHONY: test
test:
go test -v ./...

.PHONY: test_coverage
test_coverage:
go test -v ./... -coverprofile=coverage.out

.PHONY: dep
dep:
go mod download

.PHONY: tidy
tidy:
go mod tidy

.PHONY: safe
safe:
go vet
go test -race .
go build -race .

.PHONY: test_race
test_race:
go run -race . --bootstrap "localhost:9092" \
--topic "payments-topic" \
Expand All @@ -36,3 +44,7 @@ test_race:
--user "USERNAME" \
--password "PASSWORD" \
--oldest --verbose

.PHONY: lint
lint:
golangci-lint run
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// main ...
package main

import (
Expand Down
7 changes: 6 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ func TestLoadCert(t *testing.T) {
if err != nil {
t.Fatalf("failed to create temporary file: %s", err)
}
defer os.Remove(caCertFile.Name())
defer func(name string) {
err := os.Remove(name)
if err != nil {
return
}
}(caCertFile.Name())

caCertData := []byte(`
-----BEGIN CERTIFICATE-----
Expand Down
11 changes: 7 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,13 @@ func main() {
}
}

// Setup is setting up a new CG session
func (consumer *Consumer) Setup(sarama.ConsumerGroupSession) error {
close(consumer.ready)
return nil
}

// Cleanup function to clean after the CG
func (consumer *Consumer) Cleanup(sarama.ConsumerGroupSession) error {
consumedMessages := consumer.stats.StatMap["TOTAL"]
log.Printf("Total messages consumed: %v\n", consumedMessages)
Expand All @@ -90,7 +92,7 @@ func (consumer *Consumer) Cleanup(sarama.ConsumerGroupSession) error {
// Print results
for k, v := range utils.PercentileMap {
c := color.New(color.FgGreen)
c.Printf("Schema ID %v => %v%%\n", k, v)
c.Sprintf("Schema ID %v => %v%%\n", k, v)
}

// Dump stats to file
Expand All @@ -105,6 +107,7 @@ func (consumer *Consumer) Cleanup(sarama.ConsumerGroupSession) error {
return nil
}

// ConsumeClaim processes Kafka messages from a given topic and partition within a consumer group.
func (consumer *Consumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for {
select {
Expand All @@ -115,10 +118,10 @@ func (consumer *Consumer) ConsumeClaim(session sarama.ConsumerGroupSession, clai
utils.CalcStat(consumer.stats, 4294967295, &consumer.consumerLock)
break
}
schemaId := binary.BigEndian.Uint32(message.Value[1:5])
utils.CalcStat(consumer.stats, schemaId, &consumer.consumerLock)
schemaID := binary.BigEndian.Uint32(message.Value[1:5])
utils.CalcStat(consumer.stats, schemaID, &consumer.consumerLock)
if consumer.config.store { // lock map, and build result for analysis
utils.AppendResult(consumer.stats, message.Offset, schemaId, &consumer.consumerLock)
utils.AppendResult(consumer.stats, message.Offset, schemaID, &consumer.consumerLock)
}
consumer.consumerLock.RLock()
if consumer.stats.StatMap["TOTAL"]%100 == 0 { // I'm still alive :)
Expand Down
1 change: 1 addition & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type appConfig struct {
chart bool
}

// Consumer represent a Kafka Consumer
type Consumer struct {
ready chan bool
stats utils.ResultStats
Expand Down
2 changes: 1 addition & 1 deletion types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestConsumerInitialization(t *testing.T) {
assert.Equal(t, config.chart, consumer.config.chart)
}

func TestConsumerLocking(t *testing.T) {
func TestConsumerLocking(_ *testing.T) {
config := appConfig{
bootstrapServers: "localhost:9092",
version: "2.6.0",
Expand Down
6 changes: 4 additions & 2 deletions utils/chart.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package utils provide some common utilities
package utils

import (
Expand All @@ -11,7 +12,7 @@ import (
"github.com/go-echarts/go-echarts/v2/opts"
)

func generatePieItems(stat map[string]float64) []opts.PieData {
func generatePieItems() []opts.PieData {
items := make([]opts.PieData, 0)
for k, v := range PercentileMap {
items = append(items, opts.PieData{
Expand All @@ -30,7 +31,7 @@ func createPieChart() *charts.Pie {
Subtitle: fmt.Sprintf("Snapshot: %s", time.Now().Format(time.RFC822)),
}),
)
pie.AddSeries("pie", generatePieItems(PercentileMap)).
pie.AddSeries("pie", generatePieItems()).
SetSeriesOptions(charts.WithLabelOpts(
opts.Label{
Show: true,
Expand All @@ -40,6 +41,7 @@ func createPieChart() *charts.Pie {
return pie
}

// GenChart will generate a Pie Chart based on data
func GenChart() {
page := components.NewPage()
page.AddCharts(
Expand Down
18 changes: 11 additions & 7 deletions utils/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"sync"
)

// PercentileMap ...
var PercentileMap = map[string]float64{}

// ResultStats ...
type ResultStats struct {
StatMap map[string]int
ResultStore map[uint32][]int
Expand All @@ -20,6 +22,7 @@ func calc(schema, total int) float64 {
return math.Round((float64(schema) / float64(total) * 100))
}

// BuildPercentileMap will calculate the percentile into a map
func BuildPercentileMap(s map[string]int) {
for k, v := range s {
if k == "TOTAL" {
Expand All @@ -33,25 +36,26 @@ func BuildPercentileMap(s map[string]int) {
}

// AppendResult will map the results to a storeable map
func AppendResult(stat ResultStats, offset int64, schemaId uint32, lock *sync.RWMutex) {
func AppendResult(stat ResultStats, offset int64, schemaID uint32, lock *sync.RWMutex) {
lock.Lock()
defer lock.Unlock()
stat.ResultStore[schemaId] = append(stat.ResultStore[schemaId], int(offset))
stat.ResultStore[schemaID] = append(stat.ResultStore[schemaID], int(offset))
}

// CalcStat keep on track the stats
func CalcStat(stat ResultStats, schemaId uint32, lock *sync.RWMutex) {
func CalcStat(stat ResultStats, schemaID uint32, lock *sync.RWMutex) {
lock.Lock()
defer lock.Unlock()
// 4294967295 represents error
if schemaId == 4294967295 {
stat.StatMap["ERROR"] += 1
if schemaID == 4294967295 {
stat.StatMap["ERROR"]++
return
}
stat.StatMap[fmt.Sprint(schemaId)] += 1
stat.StatMap["TOTAL"] += 1
stat.StatMap[fmt.Sprint(schemaID)]++
stat.StatMap["TOTAL"]++
}

// DumpStats will print the results into a file
func DumpStats(stat ResultStats, path string) {
j, err := json.Marshal(stat.ResultStore)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions utils/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestAppendResult(t *testing.T) {
name string
stat ResultStats
offset int64
schemaId uint32
schemaID uint32
wantResult ResultStats
}{
{
Expand All @@ -72,7 +72,7 @@ func TestAppendResult(t *testing.T) {
},
},
offset: 4,
schemaId: 1,
schemaID: 1,
wantResult: ResultStats{
ResultStore: map[uint32][]int{
1: {1, 2, 3, 4},
Expand All @@ -87,7 +87,7 @@ func TestAppendResult(t *testing.T) {
},
},
offset: 8,
schemaId: 2,
schemaID: 2,
wantResult: ResultStats{
ResultStore: map[uint32][]int{
2: {5, 6, 7, 8},
Expand All @@ -102,7 +102,7 @@ func TestAppendResult(t *testing.T) {
},
},
offset: 12,
schemaId: 3,
schemaID: 3,
wantResult: ResultStats{
ResultStore: map[uint32][]int{
3: {9, 10, 11, 12},
Expand All @@ -112,7 +112,7 @@ func TestAppendResult(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
AppendResult(tc.stat, tc.offset, tc.schemaId, &l)
AppendResult(tc.stat, tc.offset, tc.schemaID, &l)
if !reflect.DeepEqual(tc.stat, tc.wantResult) {
t.Errorf("got %+v, want %+v", tc.stat, tc.wantResult)
}
Expand Down

0 comments on commit e57876a

Please sign in to comment.