Skip to content

Commit

Permalink
Linter & test fixes (#204)
Browse files Browse the repository at this point in the history
* fix some linter issues and make AMQP test conditional on address env. variable
* remove broken rabbitmq bootstrap from circleci
  • Loading branch information
basvanbeek authored Oct 25, 2021
1 parent 8b5f895 commit 4319f91
Show file tree
Hide file tree
Showing 13 changed files with 170 additions and 72 deletions.
9 changes: 5 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
run:
deadline: 5m
skip-dirs:
- zipkin_proto3

linters:
disable-all: true
Expand All @@ -8,10 +10,9 @@ linters:
- goconst
- gocyclo
- gofmt
- golint
- revive
- govet
- ineffassign
- interfacer
- lll
- misspell
- nakedret
Expand All @@ -25,6 +26,6 @@ linters-settings:
lll:
line-length: 170
gocyclo:
min-complexity: 15
min-complexity: 20
golint:
min-confidence: 0.85
min-confidence: 0.85
15 changes: 9 additions & 6 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ jobs:
- image: circleci/golang
steps:
- checkout
- run: echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list
- run: wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
- run: sudo apt-get update
- run: sudo apt-get install rabbitmq-server
- run: sudo service rabbitmq-server start
- run: go get -t -v -d ./...
- run: go mod download
- run: make vet test bench
# RabbitMQ install currently broken
# - run: echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list
# - run: wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -
# - run: sudo apt-get update
# - run: sudo apt-get install rabbitmq-server
# - run: sudo service rabbitmq-server start
# - run: go get -t -v -d ./...
# - run: AMQP_ADDR=amqp://guest:guest@localhost:5672/ make vet test bench
2 changes: 1 addition & 1 deletion middleware/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var _ = ginkgo.Describe("gRPC Client", func() {
reporter = recorder.NewReporter()
ep, _ := zipkin.NewEndpoint("grpcClient", "")
tracer, err = zipkin.NewTracer(
reporter, zipkin.WithLocalEndpoint(ep), zipkin.WithIDGenerator(newSequentialIdGenerator(1)))
reporter, zipkin.WithLocalEndpoint(ep), zipkin.WithIDGenerator(newSequentialIDGenerator(1)))
gomega.Expect(tracer, err).ToNot(gomega.BeNil())
})

Expand Down
70 changes: 45 additions & 25 deletions middleware/grpc/grpc_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import (
)

var (
serverIdGenerator *sequentialIdGenerator
serverIDGenerator *sequentialIDGenerator
serverReporter *recorder.ReporterRecorder

server *grpc.Server
Expand All @@ -52,15 +52,25 @@ func TestGrpc(t *testing.T) {
}

var _ = ginkgo.BeforeSuite(func() {
var err error
var (
err error
tracer *zipkin.Tracer
lis net.Listener
customLis net.Listener
)

serverReporter = recorder.NewReporter()
ep, _ := zipkin.NewEndpoint("grpcServer", "")
serverIdGenerator = newSequentialIdGenerator(0x1000000)
tracer, err := zipkin.NewTracer(
serverReporter, zipkin.WithLocalEndpoint(ep), zipkin.WithIDGenerator(serverIdGenerator), zipkin.WithSharedSpans(false))
serverIDGenerator = newSequentialIDGenerator(0x1000000)

lis, err := net.Listen("tcp", ":0")
tracer, err = zipkin.NewTracer(
serverReporter, zipkin.WithLocalEndpoint(ep),
zipkin.WithIDGenerator(serverIDGenerator),
zipkin.WithSharedSpans(false),
)
gomega.Expect(tracer, err).ToNot(gomega.BeNil(), "failed to create Zipkin tracer")

lis, err = net.Listen("tcp", ":0")
gomega.Expect(lis, err).ToNot(gomega.BeNil(), "failed to listen to tcp port")

server = grpc.NewServer(grpc.StatsHandler(zipkingrpc.NewServerHandler(tracer)))
Expand All @@ -70,14 +80,24 @@ var _ = ginkgo.BeforeSuite(func() {
}()
serverAddr = lis.Addr().String()

customLis, err := net.Listen("tcp", ":0")
customLis, err = net.Listen("tcp", ":0")
gomega.Expect(customLis, err).ToNot(gomega.BeNil(), "failed to listen to tcp port")

tracer, err = zipkin.NewTracer(
serverReporter, zipkin.WithLocalEndpoint(ep), zipkin.WithIDGenerator(serverIdGenerator), zipkin.WithSharedSpans(true))
customServer = grpc.NewServer(grpc.StatsHandler(zipkingrpc.NewServerHandler(
tracer,
zipkingrpc.ServerTags(map[string]string{"default": "tag"}))))
serverReporter,
zipkin.WithLocalEndpoint(ep),
zipkin.WithIDGenerator(serverIDGenerator),
zipkin.WithSharedSpans(true),
)
gomega.Expect(tracer, err).ToNot(gomega.BeNil(), "failed to create Zipkin tracer")

customServer = grpc.NewServer(
grpc.StatsHandler(
zipkingrpc.NewServerHandler(
tracer, zipkingrpc.ServerTags(map[string]string{"default": "tag"}),
),
),
)
service.RegisterHelloServiceServer(customServer, &TestHelloService{})
go func() {
_ = customServer.Serve(customLis)
Expand All @@ -91,34 +111,34 @@ var _ = ginkgo.AfterSuite(func() {
_ = serverReporter.Close()
})

type sequentialIdGenerator struct {
nextTraceId uint64
nextSpanId uint64
type sequentialIDGenerator struct {
nextTraceID uint64
nextSpanID uint64
start uint64
}

func newSequentialIdGenerator(start uint64) *sequentialIdGenerator {
return &sequentialIdGenerator{start, start, start}
func newSequentialIDGenerator(start uint64) *sequentialIDGenerator {
return &sequentialIDGenerator{start, start, start}
}

func (g *sequentialIdGenerator) SpanID(traceID model.TraceID) model.ID {
id := model.ID(g.nextSpanId)
g.nextSpanId++
func (g *sequentialIDGenerator) SpanID(_ model.TraceID) model.ID {
id := model.ID(g.nextSpanID)
g.nextSpanID++
return id
}

func (g *sequentialIdGenerator) TraceID() model.TraceID {
func (g *sequentialIDGenerator) TraceID() model.TraceID {
id := model.TraceID{
High: 0,
Low: g.nextTraceId,
Low: g.nextTraceID,
}
g.nextTraceId++
g.nextTraceID++
return id
}

func (g *sequentialIdGenerator) reset() {
g.nextTraceId = g.start
g.nextSpanId = g.start
func (g *sequentialIDGenerator) reset() {
g.nextTraceID = g.start
g.nextSpanID = g.start
}

type TestHelloService struct {
Expand Down
14 changes: 10 additions & 4 deletions middleware/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var _ = ginkgo.Describe("gRPC Server", func() {
)

ginkgo.BeforeEach(func() {
serverIdGenerator.reset()
serverIDGenerator.reset()
serverReporter.Flush()
})

Expand Down Expand Up @@ -80,7 +80,9 @@ var _ = ginkgo.Describe("gRPC Server", func() {
// Manually create a client context
tracer, err := zipkin.NewTracer(
reporter.NewNoopReporter(),
zipkin.WithIDGenerator(newSequentialIdGenerator(1)))
zipkin.WithIDGenerator(newSequentialIDGenerator(1)))
gomega.Expect(tracer, err).ToNot(gomega.BeNil(), "failed to create Zipkin tracer")

testSpan := tracer.StartSpan("test")
md := metadata.New(nil)
_ = b3.InjectGRPC(&md)(testSpan.Context())
Expand Down Expand Up @@ -158,13 +160,17 @@ var _ = ginkgo.Describe("gRPC Server", func() {
// Manually create a client context
tracer, err := zipkin.NewTracer(
reporter.NewNoopReporter(),
zipkin.WithIDGenerator(newSequentialIdGenerator(1)))
zipkin.WithIDGenerator(newSequentialIDGenerator(1)))
gomega.Expect(tracer, err).ToNot(gomega.BeNil(), "failed to create Zipkin tracer")

testSpan := tracer.StartSpan("test")

md := metadata.New(nil)
_ = b3.InjectGRPC(&md)(testSpan.Context())
ctx := metadata.NewOutgoingContext(context.Background(), md)

resp, err := client.Hello(ctx, &service.HelloRequest{Payload: "Hello"})
var resp *service.HelloResponse
resp, err = client.Hello(ctx, &service.HelloRequest{Payload: "Hello"})
gomega.Expect(err).ToNot(gomega.HaveOccurred())

spanCtx := resp.GetSpanContext()
Expand Down
2 changes: 1 addition & 1 deletion middleware/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,5 @@ func (c *Client) DoWithAppSpan(req *http.Request, name string) (res *http.Respon
sp: appSpan,
traceEnabled: c.httpTrace,
}
return
return res, err
}
2 changes: 1 addition & 1 deletion middleware/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (r *rwInterceptor) getResponseSize() string {
return strconv.FormatUint(atomic.LoadUint64(&r.size), 10)
}

func (r *rwInterceptor) wrap() http.ResponseWriter {
func (r *rwInterceptor) wrap() http.ResponseWriter { // nolint:gocyclo
var (
hj, i0 = r.w.(http.Hijacker)
cn, i1 = r.w.(http.CloseNotifier)
Expand Down
4 changes: 2 additions & 2 deletions middleware/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (t *transport) RoundTrip(req *http.Request) (res *http.Response, err error)
if err != nil {
t.errHandler(sp, err, 0)
sp.Finish()
return
return nil, err
}

if res.ContentLength > 0 {
Expand All @@ -231,5 +231,5 @@ func (t *transport) RoundTrip(req *http.Request) (res *http.Response, err error)
}
}
sp.Finish()
return
return res, err
}
18 changes: 9 additions & 9 deletions propagation/b3/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ func TestHTTPInjectSampledAndDebugTrace(t *testing.T) {

sampled := true
sc := model.SpanContext{
TraceID: model.TraceID{Low: 1},
ID: model.ID(2),
TraceID: model.TraceID{Low: 3},
ID: model.ID(4),
Debug: true,
Sampled: &sampled,
}
Expand All @@ -373,8 +373,8 @@ func TestHTTPInjectWithSingleOnlyHeaders(t *testing.T) {

sampled := true
sc := model.SpanContext{
TraceID: model.TraceID{Low: 1},
ID: model.ID(2),
TraceID: model.TraceID{Low: 5},
ID: model.ID(6),
Debug: true,
Sampled: &sampled,
}
Expand All @@ -385,7 +385,7 @@ func TestHTTPInjectWithSingleOnlyHeaders(t *testing.T) {
t.Errorf("TraceID want empty, have %s", have)
}

if want, have := "0000000000000001-0000000000000002-d", r.Header.Get(b3.Context); want != have {
if want, have := "0000000000000005-0000000000000006-d", r.Header.Get(b3.Context); want != have {
t.Errorf("Context want %s, have %s", want, have)
}
}
Expand All @@ -394,19 +394,19 @@ func TestHTTPInjectWithBothSingleAndMultipleHeaders(t *testing.T) {

sampled := true
sc := model.SpanContext{
TraceID: model.TraceID{Low: 1},
ID: model.ID(2),
TraceID: model.TraceID{Low: 7},
ID: model.ID(8),
Debug: true,
Sampled: &sampled,
}

b3.InjectHTTP(r, b3.WithSingleAndMultiHeader())(sc)

if want, have := "0000000000000001", r.Header.Get(b3.TraceID); want != have {
if want, have := "0000000000000007", r.Header.Get(b3.TraceID); want != have {
t.Errorf("Trace ID want %s, have %s", want, have)
}

if want, have := "0000000000000001-0000000000000002-d", r.Header.Get(b3.Context); want != have {
if want, have := "0000000000000007-0000000000000008-d", r.Header.Get(b3.Context); want != have {
t.Errorf("Context want %s, have %s", want, have)
}
}
Expand Down
Loading

0 comments on commit 4319f91

Please sign in to comment.