Skip to content

Commit

Permalink
Pass golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
swi-jared committed Apr 19, 2024
1 parent 2608e26 commit 6a69047
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 55 deletions.
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/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++
}

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
3 changes: 2 additions & 1 deletion internal/metrics/metrics_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func TestAppendUname(t *testing.T) {
bbuf := bson.NewBuffer()
appendUname(bbuf)
bbuf.Finish()
m := bsonToMap(bbuf)
m, err := bsonToMap(bbuf)
require.NoError(t, err)

Check failure on line 33 in internal/metrics/metrics_linux_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: require (typecheck)

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())

}
27 changes: 19 additions & 8 deletions internal/reporter/reporter_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func StartTestGRPCServer(t *testing.T, addr string) *TestGRPCServer {
pb.RegisterTraceCollectorServer(grpcServer, testServer)
require.NoError(t, err)

go grpcServer.Serve(lis)
go func() {
_ = grpcServer.Serve(lis)
}()
return testServer
}

Expand Down Expand Up @@ -176,10 +178,14 @@ func (p *TestProxyServer) Start() error {
}
switch p.url.Scheme {
case "http":
go srv.ListenAndServe()
go func() {
_ = srv.ListenAndServe()
}()
p.closeFunc = closeFunc
case "https":
go srv.ListenAndServeTLS(p.pemFile, p.keyFile)
go func() {
_ = srv.ListenAndServeTLS(p.pemFile, p.keyFile)
}()
p.closeFunc = closeFunc
// TODO: case "socks5":
default:
Expand Down Expand Up @@ -211,7 +217,9 @@ func (p *TestProxyServer) proxyHttpHandler(w http.ResponseWriter, r *http.Reques
subtle.ConstantTimeCompare([]byte(pwd), []byte(expectedPwd)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="wrong auth"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorised.\n"))
if _, err := w.Write([]byte("Unauthorised.\n")); err != nil {
panic(err)
}
return
}

Expand All @@ -231,18 +239,21 @@ func (p *TestProxyServer) proxyHttpHandler(w http.ResponseWriter, r *http.Reques
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
clientConn.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
_, err = clientConn.Write([]byte("HTTP/1.0 200 OK\r\n\r\n"))
if err != nil {
panic(err)
}

go forward(serverConn, clientConn)
go forward(clientConn, serverConn)
}

func forward(dst io.WriteCloser, src io.ReadCloser) {
defer func() {
dst.Close()
src.Close()
_ = dst.Close()
_ = src.Close()
}()
io.Copy(dst, src)
_, _ = io.Copy(dst, src)
}

func TestAppopticsCertificate(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import (
// SPrintBson prints the BSON message. It's not concurrent-safe and is for testing only
func SPrintBson(message []byte) string {
m := make(map[string]interface{})
bson.Unmarshal(message, m)
if err := bson.Unmarshal(message, m); err != nil {
// Since this is only used in testing/debug, we'll just return the error message
return err.Error()
}
b, _ := json.MarshalIndent(m, "", " ")
return string(b)
}
Expand Down
Loading

0 comments on commit 6a69047

Please sign in to comment.