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

[NH-78392] Add golangci-lint to GHA #74

Merged
merged 10 commits into from
Apr 19, 2024
Merged
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
25 changes: 25 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: golangci-lint
on:
push:
branches:
- main
pull_request:

permissions:
contents: read
checks: write

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v4
with:
version: v1.54
7 changes: 1 addition & 6 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request: null
push:
branches:
- master
- main

jobs:
license-check:
Expand Down Expand Up @@ -42,8 +42,3 @@ jobs:
run: go vet ./...
- name: Run tests
run: make test
- uses: dominikh/[email protected]
if: matrix.goversion == '1.21'
with:
install-go: false
cache-key: ${{ matrix.goversion }}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Run all tests with `make test`.

We enforce the following in CI:

* [`staticcheck ./...`](https://staticcheck.dev/docs/)
* [`golangci-lint run`](https://golangci-lint.run/)
* [`go vet ./...`](https://pkg.go.dev/cmd/vet)
* [`gofmt`](https://pkg.go.dev/cmd/gofmt)

Expand Down
39 changes: 29 additions & 10 deletions internal/hdrhist/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hdrhist

import (
Expand All @@ -27,10 +28,13 @@ func EncodeCompressed(h *Hist) ([]byte, error) {
var buf bytes.Buffer
b64w := base64.NewEncoder(base64.StdEncoding, &buf)
if err := encodeCompressed(h, b64w, h.Max()); err != nil {
b64w.Close()
_ = b64w.Close()
return nil, errors.Wrap(err, "unable to encode histogram")
}
b64w.Close() // DO NOT defer this close, otherwise that could prevent bytes from getting flushed
// DO NOT defer this close, otherwise that could prevent bytes from getting flushed
if err := b64w.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

Expand All @@ -39,15 +43,22 @@ func encodeCompressed(h *Hist, w io.Writer, histMax int64) error {
var buf bytes.Buffer

var cookie int32 = compressedEncodingCookie
binary.Write(&buf, binary.BigEndian, cookie)
err := binary.Write(&buf, binary.BigEndian, cookie)
if err != nil {
return err
}
buf.WriteString("\x00\x00\x00\x00")
preCompressed := buf.Len()
zw, _ := zlib.NewWriterLevel(&buf, zlib.BestCompression)
encodeInto(h, zw, histMax) // won't error, not io device
zw.Close()
if err = encodeInto(h, zw, histMax); err != nil {
return err
}
if err = zw.Close(); err != nil {
return err
}
binary.BigEndian.PutUint32(buf.Bytes()[4:], uint32(buf.Len()-preCompressed))

_, err := buf.WriteTo(w)
_, err = buf.WriteTo(w)
return errors.Wrap(err, "unable to write compressed hist")
}

Expand All @@ -57,13 +68,21 @@ func encodeInto(h *Hist, w io.Writer, max int64) error {
importantLen := h.b.countsIndex(max) + 1
var buf bytes.Buffer
var cookie int32 = encodingCookie
binary.Write(&buf, binary.BigEndian, cookie)
if err := binary.Write(&buf, binary.BigEndian, cookie); err != nil {
return err
}
buf.WriteString("\x00\x00\x00\x00") // length will go here
buf.WriteString("\x00\x00\x00\x00") // normalizing index offset
cfg := h.Config()
binary.Write(&buf, binary.BigEndian, int32(cfg.SigFigs))
binary.Write(&buf, binary.BigEndian, int64(cfg.LowestDiscernible))
binary.Write(&buf, binary.BigEndian, int64(cfg.HighestTrackable))
if err := binary.Write(&buf, binary.BigEndian, cfg.SigFigs); err != nil {
return err
}
if err := binary.Write(&buf, binary.BigEndian, cfg.LowestDiscernible); err != nil {
return err
}
if err := binary.Write(&buf, binary.BigEndian, cfg.HighestTrackable); err != nil {
return err
}
// int to double conversion ratio
buf.WriteString("\x3f\xf0\x00\x00\x00\x00\x00\x00")
payloadStart := buf.Len()
Expand Down
4 changes: 0 additions & 4 deletions internal/hdrhist/hist.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,7 @@ func (b *buckets) valueFor(i int) int64 {

func (b *buckets) sizeOfEquivalentValueRange(v int64) int64 {
bi := bucketIndex(v, b.subMask, b.leadZeroCountBase)
sbi := subBucketIndex(v, bi, b.unitMag)
t := bi
if sbi >= int(b.subCount) {
bi++
}
nextDist := int64(1) << uint64(int64(b.unitMag)+int64(t))
return nextDist
}
Expand Down
8 changes: 6 additions & 2 deletions internal/hdrhist/log_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ func (l *LogWriter) writeHist(h *Hist, start time.Time, end time.Time) error {
float64(end.Sub(start)/1e6)/1e3,
float64(max)/MaxValueUnitRatio)
b64w := base64.NewEncoder(base64.StdEncoding, &l.buf)
encodeCompressed(h, b64w, max) // not writing to disk yet, won't fail
b64w.Close()
if err := encodeCompressed(h, b64w, max); err != nil {
return err
}
if err := b64w.Close(); err != nil {
return err
}
l.buf.WriteString("\n")
_, err := l.buf.WriteTo(l.w)
return errors.Wrap(err, "unable to write hist")
Expand Down
2 changes: 1 addition & 1 deletion internal/host/sys_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func initDistro() (distro string) {
distro = utils.GetStrByKeyword(SUSE, "PRETTY_NAME")
if distro != "" {
// TODO
//lint:ignore SA1024 This is untested so until we have coverage I'm not inclined to fix this
//nolint:staticcheck // This is untested so until we have coverage I'm not inclined to fix this
distro = strings.TrimLeft(distro, "PRETTY_NAME=")
distro = strings.Trim(distro, "\"")
return distro
Expand Down
2 changes: 1 addition & 1 deletion internal/log/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func logIt(level LogLevel, msg string, args []interface{}) {

buffer.WriteString(pre)

s := msg
var s string
if msg == "" {
s = fmt.Sprint(args...)
} else {
Expand Down
3 changes: 1 addition & 2 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,6 @@ func BuildServerlessMessage(span HTTPSpanMessage, rcs map[string]*RateCounts, ra
}
if ttTraced != 0 {
bbuf.AppendString(strconv.Itoa(i), "Triggered")
i++
tammy-baylis-swi marked this conversation as resolved.
Show resolved Hide resolved
}

bbuf.AppendFinishObject(start)
Expand Down Expand Up @@ -1038,7 +1037,7 @@ func RecordSpan(span sdktrace.ReadOnlySpan, isAppoptics bool) {
Method: method,
}

var tagsList []map[string]string = nil
var tagsList []map[string]string
var metricName string
if !isAppoptics {
tagsList = []map[string]string{swoTags}
Expand Down
4 changes: 3 additions & 1 deletion internal/metrics/metrics_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package metrics
import (
"github.com/solarwinds/apm-go/internal/bson"
"github.com/solarwinds/apm-go/internal/utils"
"github.com/stretchr/testify/require"
"strings"
"syscall"
"testing"
Expand All @@ -29,7 +30,8 @@ func TestAppendUname(t *testing.T) {
bbuf := bson.NewBuffer()
appendUname(bbuf)
bbuf.Finish()
m := bsonToMap(bbuf)
m, err := bsonToMap(bbuf)
require.NoError(t, err)

var sysname, release string

Expand Down
39 changes: 26 additions & 13 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/solarwinds/apm-go/internal/log"
"github.com/solarwinds/apm-go/internal/swotel/semconv"
"github.com/solarwinds/apm-go/internal/testutils"
"github.com/stretchr/testify/require"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"math"
Expand All @@ -38,10 +39,12 @@ import (
mbson "gopkg.in/mgo.v2/bson"
)

func bsonToMap(bbuf *bson.Buffer) map[string]interface{} {
func bsonToMap(bbuf *bson.Buffer) (map[string]interface{}, error) {
m := make(map[string]interface{})
mbson.Unmarshal(bbuf.GetBuf(), m)
return m
if err := mbson.Unmarshal(bbuf.GetBuf(), m); err != nil {
return nil, err
}
return m, nil
}

func round(val float64, roundOn float64, places int) (newVal float64) {
Expand All @@ -62,7 +65,8 @@ func TestAppendIPAddresses(t *testing.T) {
bbuf := bson.NewBuffer()
appendIPAddresses(bbuf)
bbuf.Finish()
m := bsonToMap(bbuf)
m, err := bsonToMap(bbuf)
require.NoError(t, err)

ifaces, _ := host.FilteredIfaces()
var addresses []string
Expand Down Expand Up @@ -94,7 +98,8 @@ func TestAppendMACAddresses(t *testing.T) {
bbuf := bson.NewBuffer()
appendMACAddresses(bbuf, host.CurrentID().MAC())
bbuf.Finish()
m := bsonToMap(bbuf)
m, err := bsonToMap(bbuf)
require.NoError(t, err)

ifaces, _ := host.FilteredIfaces()
var macs []string
Expand Down Expand Up @@ -131,7 +136,8 @@ func TestAddMetricsValue(t *testing.T) {
addMetricsValue(bbuf, &index, "name4", float64(444.44))
addMetricsValue(bbuf, &index, "name5", "hello")
bbuf.Finish()
m := bsonToMap(bbuf)
m, err := bsonToMap(bbuf)
require.NoError(t, err)

assert.NotZero(t, m["0"])
m2 := m["0"].(map[string]interface{})
Expand Down Expand Up @@ -223,8 +229,10 @@ func TestRecordMeasurement(t *testing.T) {
t1 := make(map[string]string)
t1["t1"] = "tag1"
t1["t2"] = "tag2"
me.recordWithSoloTags("name1", t1, 111.11, 1, false)
me.recordWithSoloTags("name1", t1, 222, 1, false)
err := me.recordWithSoloTags("name1", t1, 111.11, 1, false)
require.NoError(t, err)
err = me.recordWithSoloTags("name1", t1, 222, 1, false)
require.NoError(t, err)
assert.NotNil(t, me.m["name1&false&t1:tag1&t2:tag2&"])
m := me.m["name1&false&t1:tag1&t2:tag2&"]
assert.Equal(t, "tag1", m.Tags["t1"])
Expand All @@ -235,7 +243,8 @@ func TestRecordMeasurement(t *testing.T) {

t2 := make(map[string]string)
t2["t3"] = "tag3"
me.recordWithSoloTags("name2", t2, 123.456, 3, true)
err = me.recordWithSoloTags("name2", t2, 123.456, 3, true)
require.NoError(t, err)
assert.NotNil(t, me.m["name2&true&t3:tag3&"])
m = me.m["name2&true&t3:tag3&"]
assert.Equal(t, "tag3", m.Tags["t3"])
Expand Down Expand Up @@ -305,7 +314,8 @@ func TestAddMeasurementToBSON(t *testing.T) {
addMeasurementToBSON(bbuf, &index, measurement1)
addMeasurementToBSON(bbuf, &index, measurement2)
bbuf.Finish()
m := bsonToMap(bbuf)
m, err := bsonToMap(bbuf)
require.NoError(t, err)

assert.NotZero(t, m["0"])
m1 := m["0"].(map[string]interface{})
Expand Down Expand Up @@ -365,7 +375,8 @@ func TestAddHistogramToBSON(t *testing.T) {
addHistogramToBSON(bbuf, &index, h1)
addHistogramToBSON(bbuf, &index, h2)
bbuf.Finish()
m := bsonToMap(bbuf)
m, err := bsonToMap(bbuf)
require.NoError(t, err)

assert.NotZero(t, m["0"])
m1 := m["0"].(map[string]interface{})
Expand All @@ -392,7 +403,8 @@ func TestGenerateMetricsMessage(t *testing.T) {
RCRegular: {10, 2, 5, 5, 1},
RCRelaxedTriggerTrace: {3, 0, 1, 2, 0},
RCStrictTriggerTrace: {4, 0, 3, 1, 0}}, true))
m := bsonToMap(bbuf)
m, err := bsonToMap(bbuf)
require.NoError(t, err)

_, ok := m["Hostname"]
assert.False(t, ok)
Expand Down Expand Up @@ -477,8 +489,9 @@ func TestGenerateMetricsMessage(t *testing.T) {
}
}

m = bsonToMap(bson.WithBuf(BuildBuiltinMetricsMessage(testMetrics, &EventQueueStats{},
m, err = bsonToMap(bson.WithBuf(BuildBuiltinMetricsMessage(testMetrics, &EventQueueStats{},
map[string]*RateCounts{RCRegular: {}, RCRelaxedTriggerTrace: {}, RCStrictTriggerTrace: {}}, true)))
require.NoError(t, err)

assert.NotNil(t, m["TransactionNameOverflow"])
assert.True(t, m["TransactionNameOverflow"].(bool))
Expand Down
2 changes: 1 addition & 1 deletion internal/reporter/log_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (lr *logWriter) flush() error {
}

if file, ok := lr.dest.(*os.File); ok {
file.Sync()
return file.Sync()
}

return nil
Expand Down
17 changes: 11 additions & 6 deletions internal/reporter/log_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package reporter

import (
"github.com/solarwinds/apm-go/internal/utils"
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -23,14 +24,16 @@ import (
func TestLogWriter(t *testing.T) {
sb := &utils.SafeBuffer{}
eventWriter := newLogWriter(false, sb, 1e6)
eventWriter.Write(EventWT, []byte("hello event"))
_, err := eventWriter.Write(EventWT, []byte("hello event"))
require.NoError(t, err)
assert.Equal(t, 0, sb.Len())
eventWriter.Flush()
require.NoError(t, eventWriter.Flush())
assert.Equal(t, "{\"apm-data\":{\"events\":[\"aGVsbG8gZXZlbnQ=\"]}}\n", sb.String())

sb.Reset()
metricWriter := newLogWriter(true, sb, 1e6)
metricWriter.Write(MetricWT, []byte("hello metric"))
_, err = metricWriter.Write(MetricWT, []byte("hello metric"))
require.NoError(t, err)
assert.Equal(t, "{\"apm-data\":{\"metrics\":[\"aGVsbG8gbWV0cmlj\"]}}\n", sb.String())
assert.NotNil(t, metricWriter.Flush())

Expand All @@ -40,12 +43,14 @@ func TestLogWriter(t *testing.T) {
assert.Zero(t, n)
assert.Error(t, err)

writer.Write(EventWT, []byte("hello"))
_, err = writer.Write(EventWT, []byte("hello"))
require.NoError(t, err)
assert.Zero(t, sb.Len())
writer.Write(EventWT, []byte(" event"))
_, err = writer.Write(EventWT, []byte(" event"))
require.NoError(t, err)
assert.Equal(t, 37, sb.Len())
assert.Equal(t, "{\"apm-data\":{\"events\":[\"aGVsbG8=\"]}}\n", sb.String())
writer.Flush()
require.NoError(t, writer.Flush())
assert.Equal(t, "{\"apm-data\":{\"events\":[\"aGVsbG8=\"]}}\n{\"apm-data\":{\"events\":[\"IGV2ZW50\"]}}\n",
sb.String())

Expand Down
2 changes: 1 addition & 1 deletion internal/reporter/methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func TestGenericMethod(t *testing.T) {
err := errors.New("err connection aborted")
mockTC.On("Ping", mock.Anything, mock.Anything).
Return(nil, err)
pe.Call(context.Background(), mockTC)
_ = pe.Call(context.Background(), mockTC)
assert.Contains(t, pe.CallSummary(), err.Error())

}
Loading
Loading