Skip to content

Commit 49463a9

Browse files
Add more static checks (#4)
* Add more static checks
1 parent 3d99d11 commit 49463a9

File tree

13 files changed

+86
-74
lines changed

13 files changed

+86
-74
lines changed

.github/workflows/code-quality.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ name: Check code quality
22

33
on:
44
push:
5+
branches:
6+
- main
57
pull_request:
68
branches:
79
- main

.github/workflows/secrets-scanner.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
name: TruffleHog Secrets Scan
2-
on: [push, pull_request]
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches:
8+
- main
39

410
permissions:
511
actions: write # Needed for skip-duplicate-jobs job

.pre-commit-config.yaml

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,28 @@ repos:
2626
rev: v0.5.0
2727
hooks:
2828
- id: go-fmt
29-
# - id: go-vet
3029
- id: go-imports
3130
- id: go-cyclo
3231
- id: validate-toml
3332
- id: no-go-testing
34-
# - id: golangci-lint
33+
- id: golangci-lint
3534
# - id: go-critic
3635
# # args: [-disable=sloppyTypeAssert]
3736
# args: ["-v", "-enable='yodaStyleExpr'", "-disable=sloppyTypeAssert"]
3837
- id: go-unit-tests
3938
- id: go-build
4039
- id: go-mod-tidy
4140
- id: go-mod-vendor
42-
#
43-
# - repo: https://github.com/Bahjat/pre-commit-golang
44-
# rev: v1.0.2
45-
# hooks:
46-
# - id: go-fmt-import
47-
# - id: go-lint
48-
# - id: go-unit-tests
49-
# - id: gofumpt
50-
# - id: go-err-check
51-
# - id: go-static-check # install https://staticcheck.io/docs/
52-
# - id: golangci-lint # requires github.com/golangci/golangci-lint
53-
# args: [--config=.github/linters/.golangci.yml] # optional
54-
# - id: go-ruleguard # requires https://github.com/quasilyte/go-ruleguard
55-
# args: [rules/rules.go] # required
5641

57-
# - repo: https://github.com/pre-commit/mirrors-prettier
58-
# rev: "v2.7.1"
59-
# hooks:
60-
# - id: prettier
42+
- repo: https://github.com/Bahjat/pre-commit-golang
43+
rev: v1.0.2
44+
hooks:
45+
- id: go-fmt-import
46+
- id: go-lint
47+
# - id: go-unit-tests
48+
- id: gofumpt
49+
- id: go-err-check
50+
- id: go-static-check
51+
- id: golangci-lint
52+
# - id: go-ruleguard # requires https://github.com/quasilyte/go-ruleguard
53+
# args: [rules/rules.go] # required

examples/client/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import (
2727
"go.uber.org/zap"
2828
)
2929

30-
const BundleCount = 10000
31-
const BatchSize = 100
32-
const BatchDelay = time.Second
30+
const (
31+
BundleCount = 10000
32+
BatchSize = 100
33+
BatchDelay = time.Second
34+
)
3335

3436
func main() {
3537
// read configuration from env variables

pkg/api/add_events/add_events.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ type SessionInfo struct {
6666
}
6767

6868
type AddEventsRequestParams struct {
69-
Token string `json:"token,omitempty"`
7069
Session string `json:"session,omitempty"`
7170
SessionInfo *SessionInfo `json:"sessionInfo,omitempty"`
7271
Events []*Event `json:"events,omitempty"`
@@ -89,7 +88,6 @@ func (response *AddEventsResponse) SetResponseObj(resp *http.Response) {
8988
}
9089

9190
func TrimAttrs(attrs map[string]interface{}, remaining int) map[string]interface{} {
92-
9391
keys := make([]string, 0, len(attrs))
9492
lengths := make(map[string]int)
9593

@@ -161,16 +159,15 @@ func TrimAttrs(attrs map[string]interface{}, remaining int) map[string]interface
161159
zap.Int("remaining", remaining),
162160
)
163161
*/
164-
} else {
165-
/* TODO: IMP - Log this info
162+
} /* else {
163+
// TODO: IMP - Log this info
166164
add_events.Logger.Error(
167165
"Event attribute too long - skipping",
168166
zap.String("key", key),
169167
zap.Int("originalLen", lengths[key]),
170168
zap.Int("remaining", remaining),
171169
)
172-
*/
173-
}
170+
} */
174171
}
175172
}
176173
}

pkg/api/add_events/add_events_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (s *SuiteAddEvents) TestTrimAttrsTrimLongest(assert, require *td.T) {
5555

5656
assert.Cmp(TrimAttrs(attrs, 44), expected)
5757
}
58+
5859
func (s *SuiteAddEvents) TestTrimAttrsSkipLongest(assert, require *td.T) {
5960
attrs := map[string]interface{}{
6061
"foo": "aaaaaa",
@@ -84,6 +85,7 @@ func (s *SuiteAddEvents) TestTrimAttrsSkipLongestAndTrim(assert, require *td.T)
8485

8586
assert.Cmp(TrimAttrs(attrs, 26), expected)
8687
}
88+
8789
func (s *SuiteAddEvents) TestTrimAttrsSkipLongestAndTrimFully(assert, require *td.T) {
8890
attrs := map[string]interface{}{
8991
"foo": "aaaaaa",
@@ -98,6 +100,7 @@ func (s *SuiteAddEvents) TestTrimAttrsSkipLongestAndTrimFully(assert, require *t
98100

99101
assert.Cmp(TrimAttrs(attrs, 24), expected)
100102
}
103+
101104
func (s *SuiteAddEvents) TestTrimAttrsSkipAll(assert, require *td.T) {
102105
attrs := map[string]interface{}{
103106
"foo": "aaaaaa",

pkg/buffer/buffer.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ import (
2121
"fmt"
2222
"time"
2323

24+
"github.com/scalyr/dataset-go/pkg/api/request"
25+
2426
"github.com/scalyr/dataset-go/pkg/api/add_events"
2527

2628
"github.com/google/uuid"
2729

2830
"go.uber.org/zap"
2931
)
3032

31-
const ShouldSentBufferSize = 5 * 1024 * 1024
32-
const LimitBufferSize = 5*1024*1024 + 960*1024
33+
const (
34+
ShouldSentBufferSize = 5 * 1024 * 1024
35+
LimitBufferSize = 5*1024*1024 + 960*1024
36+
)
3337

3438
const (
3539
Ready = /*Status*/ iota
@@ -74,10 +78,7 @@ type Buffer struct {
7478
}
7579

7680
func NewEmptyBuffer(session string, token string) *Buffer {
77-
id, err := uuid.NewRandom()
78-
if err != nil {
79-
//return nil, fmt.Errorf(err, "buffer: it was not possible to generate UUID")
80-
}
81+
id, _ := uuid.NewRandom()
8182

8283
return &Buffer{Id: id, Session: session, Token: token, Status: Ready, Attempt: 0}
8384
}
@@ -141,7 +142,6 @@ func (buffer *Buffer) Reset() {
141142
}
142143

143144
func (buffer *Buffer) AddBundle(bundle *add_events.EventBundle) (AddStatus, error) {
144-
145145
// append thread
146146
addT, errT := buffer.addThread(bundle.Thread)
147147
if errT != nil {
@@ -316,7 +316,7 @@ func (buffer *Buffer) ShouldSendSize() bool {
316316
}
317317

318318
func (buffer *Buffer) ShouldSendAge(delay time.Duration) bool {
319-
return buffer.countEvents > 0 && time.Now().Sub(buffer.bufferSent) > delay
319+
return buffer.countEvents > 0 && time.Since(buffer.bufferSent) > delay
320320
}
321321

322322
func (buffer *Buffer) BufferLengths() int {
@@ -346,13 +346,17 @@ func (buffer *Buffer) Payload() ([]byte, error) {
346346
logs = append(logs, l)
347347
}
348348

349-
reqObject := add_events.AddEventsRequestParams{
350-
Token: buffer.Token,
351-
Session: buffer.Session,
352-
SessionInfo: buffer.sessionInfo,
353-
Events: buffer.events,
354-
Threads: threads,
355-
Logs: logs,
349+
reqObject := add_events.AddEventsRequest{
350+
AuthParams: request.AuthParams{
351+
Token: buffer.Token,
352+
},
353+
AddEventsRequestParams: add_events.AddEventsRequestParams{
354+
Session: buffer.Session,
355+
SessionInfo: buffer.sessionInfo,
356+
Events: buffer.events,
357+
Threads: threads,
358+
Logs: logs,
359+
},
356360
}
357361

358362
payload, err := json.Marshal(reqObject)
@@ -374,7 +378,7 @@ func (buffer *Buffer) ZapStats(fields ...zap.Field) []zap.Field {
374378
zap.Int("events", buffer.countEvents),
375379
zap.Int("bufferLength", buffer.BufferLengths()),
376380
zap.Float64("bufferRatio", float64(buffer.BufferLengths())/ShouldSentBufferSize),
377-
zap.Int64("sinceLastMs", time.Now().Sub(buffer.bufferSent).Milliseconds()),
381+
zap.Int64("sinceLastMs", time.Since(buffer.bufferSent).Milliseconds()),
378382
}
379383
res = append(res, fields...)
380384
return res

pkg/buffer/buffer_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,27 @@ func loadJson(name string) string {
4646
}
4747

4848
func (s *SuiteBuffer) TestEmptyPayloadShouldFail(assert, require *td.T) {
49-
5049
buffer, err := NewBuffer("id", nil, "token")
5150
assert.Nil(err)
5251
_, err = buffer.Payload()
5352
assert.CmpError(err, "there is no event")
5453
}
55-
func (s *SuiteBuffer) TestEmptyTokenShouldFail(assert, require *td.T) {
5654

55+
func (s *SuiteBuffer) TestEmptyTokenShouldFail(assert, require *td.T) {
5756
buffer, err := NewBuffer("id", nil, "")
5857
assert.Nil(err)
5958
_, err = buffer.Payload()
6059
assert.CmpError(err, "token is missing")
6160
}
62-
func (s *SuiteBuffer) TestEmptySessionShouldFail(assert, require *td.T) {
6361

62+
func (s *SuiteBuffer) TestEmptySessionShouldFail(assert, require *td.T) {
6463
buffer, err := NewBuffer("", nil, "token")
6564
assert.Nil(err)
6665
_, err = buffer.Payload()
6766
assert.CmpError(err, "session is missing")
6867
}
69-
func (s *SuiteBuffer) TestPayloadFull(assert, require *td.T) {
7068

69+
func (s *SuiteBuffer) TestPayloadFull(assert, require *td.T) {
7170
sessionInfo := &add_events.SessionInfo{
7271
ServerId: "serverId",
7372
ServerType: "serverType",
@@ -110,7 +109,7 @@ func (s *SuiteBuffer) TestPayloadFull(assert, require *td.T) {
110109
payload, err := buffer.Payload()
111110
assert.Nil(err)
112111

113-
params := add_events.AddEventsRequestParams{}
112+
params := add_events.AddEventsRequest{}
114113
err = json.Unmarshal(payload, &params)
115114
assert.Nil(err)
116115
assert.Cmp(params.Session, session)
@@ -129,8 +128,8 @@ func (s *SuiteBuffer) TestPayloadFull(assert, require *td.T) {
129128
}
130129
assert.Cmp(payload, []byte(expected))
131130
}
132-
func (s *SuiteBuffer) TestPayloadInjection(assert, require *td.T) {
133131

132+
func (s *SuiteBuffer) TestPayloadInjection(assert, require *td.T) {
134133
sessionInfo := &add_events.SessionInfo{
135134
ServerId: "serverId\",\"sI\":\"I",
136135
ServerType: "serverType\",\"sT\":\"T",
@@ -150,7 +149,8 @@ func (s *SuiteBuffer) TestPayloadInjection(assert, require *td.T) {
150149
Attrs: map[string]interface{}{
151150
"LAttr1\",\"i\":\"i": "LVal1\",\"i\":\"i",
152151
"LAttr2\",\"i\":\"i": "LVal2\",\"i\":\"i",
153-
}},
152+
},
153+
},
154154
Thread: &add_events.Thread{Id: "TId\",\"i\":\"i", Name: "TName\",\"i\":\"i"},
155155
Event: &add_events.Event{
156156
Thread: "TId\",\"i\":\"i",
@@ -175,7 +175,7 @@ func (s *SuiteBuffer) TestPayloadInjection(assert, require *td.T) {
175175
payload, err := buffer.Payload()
176176
assert.Nil(err)
177177

178-
params := add_events.AddEventsRequestParams{}
178+
params := add_events.AddEventsRequest{}
179179
err = json.Unmarshal(payload, &params)
180180
assert.Nil(err)
181181
assert.Cmp(params.Session, session)

pkg/client/add_events.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ func (client *DataSetClient) DangerousNaiveAddEvents(events []*add_events.Event,
5151
"POST",
5252
client.Config.Endpoint+"/api/addEvents",
5353
).WithWriteLog(client.Config.Tokens).JsonRequest(apiRequest).HttpRequest()
54-
5554
if err != nil {
5655
return nil, fmt.Errorf("cannot create request: %w", err)
5756
}
@@ -138,7 +137,6 @@ func (client *DataSetClient) SendAddEventsBuffer(buf *buffer.Buffer) (*add_event
138137
httpRequest, err := request.NewRequest(
139138
"POST", client.Config.Endpoint+"/api/addEvents",
140139
).WithWriteLog(client.Config.Tokens).RawRequest(payload).HttpRequest()
141-
142140
if err != nil {
143141
return nil, fmt.Errorf("cannot create request: %w", err)
144142
}
@@ -163,7 +161,12 @@ func (client *DataSetClient) apiCall(req *http.Request, response response.SetRes
163161
return fmt.Errorf("unable to send request: %w", err)
164162
}
165163

166-
defer resp.Body.Close()
164+
defer func() {
165+
if err = resp.Body.Close(); err != nil {
166+
client.Logger.Error("Error when closing:", zap.Error(err))
167+
}
168+
}()
169+
167170
// foo
168171
client.Logger.Debug("Received response",
169172
zap.Int("statusCode", resp.StatusCode),

0 commit comments

Comments
 (0)