Skip to content

Commit

Permalink
Merge pull request pathwar#443 from pathwar/dev/moul/tracing
Browse files Browse the repository at this point in the history
feat: configure opentracing
  • Loading branch information
moul authored Mar 12, 2020
2 parents 4e95c9b + 2254bcf commit 1f3983d
Show file tree
Hide file tree
Showing 16 changed files with 333 additions and 169 deletions.
1 change: 1 addition & 0 deletions api/errcode.proto
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ enum ErrCode {
ErrConfigureDB = 6013;
ErrInitSSOClient = 6014;
ErrInitService = 6015;
ErrInitTracer = 6016;

//// Pathwar Agent (starting at 7001)

Expand Down
2 changes: 1 addition & 1 deletion docs/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ require (
github.com/oklog/run v1.0.0
github.com/olekukonko/tablewriter v0.0.4
github.com/opencontainers/go-digest v1.0.0-rc1 // indirect
github.com/opentracing/opentracing-go v1.1.0
github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5
github.com/openzipkin/zipkin-go v0.2.1
github.com/peterbourgon/ff v1.7.0
github.com/pkg/errors v0.8.1
github.com/rogpeppe/go-internal v1.5.1 // indirect
Expand Down
20 changes: 20 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ include ../rules.mk # see https://github.com/moul/rules.mk
mkdir -p out
GOOS=linux GOARCH=amd64 $(GO) build -mod=readonly -o $@ ./cmd/pwinit

##
## mysql
##

MYSQL_CONFIG ?= -h127.0.0.1 -P3306 -uroot -puns3cur3

.PHONY: up
Expand Down Expand Up @@ -57,7 +53,7 @@ mysql.down:

.PHONY: mysql.logs
mysql.logs:
docker-compose logs -f mysql
docker-compose logs --tail=1000 -f mysql

.PHONY: mysql.shell
mysql.shell:
Expand All @@ -67,9 +63,18 @@ mysql.shell:
mysql.dump:
mysqldump $(MYSQL_CONFIG) pathwar

.PHONY: packr
packr:
cd pkg/pwinit && packr2
.PHONY: zipkin.up
zipkin.up:
docker-compose $(COMPOSE_OPTS) up -d zipkin

.PHONY: zipkin.logs
zipkin.logs:
docker-compose $(COMPOSE_OPTS) logs --tail=1000 -f zipkin

.PHONY: zipkin.down
zipkin.down:
docker-compose $(COMPOSE_OPTS) stop zipkin || true
docker-compose $(COMPOSE_OPTS) rm -f -v zipkin || true

##
## generate
Expand Down Expand Up @@ -110,3 +115,7 @@ generate_local:
.PHONY: clean
clean:
rm -f gen.sum $(wildcard */*/*.pb.go */*/*.pb.gw.go) $(wildcard out/*) $(wildcard */*/packrd/*) $(wildcard */*/*-packr.go)

.PHONY: packr
packr:
cd pkg/pwinit && packr2
30 changes: 29 additions & 1 deletion go/cmd/pathwar/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
_ "github.com/go-sql-driver/mysql" // required by gorm
"github.com/jinzhu/gorm"
"github.com/oklog/run"
opentracing "github.com/opentracing/opentracing-go"
zipkinot "github.com/openzipkin-contrib/zipkin-go-opentracing"
zipkin "github.com/openzipkin/zipkin-go"
"github.com/openzipkin/zipkin-go/model"
reporterhttp "github.com/openzipkin/zipkin-go/reporter/http"
"github.com/peterbourgon/ff"
"github.com/peterbourgon/ff/ffcli"
"go.uber.org/zap"
Expand Down Expand Up @@ -51,7 +56,9 @@ const (
)

var (
logger *zap.Logger
logger *zap.Logger
tracer opentracing.Tracer

flagOutput = os.Stderr

// flag vars
Expand Down Expand Up @@ -86,6 +93,7 @@ var (
composeUpForceRecreate bool
composeUpInstanceKey string
httpAPIAddr string
zipkinEndpoint string
serverBind string
serverCORSAllowedOrigins string
serverRequestTimeout time.Duration
Expand Down Expand Up @@ -128,6 +136,7 @@ func main() {
)
globalFlags.SetOutput(flagOutput) // used in main_test.go
globalFlags.BoolVar(&globalDebug, "debug", false, "debug mode")
globalFlags.StringVar(&zipkinEndpoint, "zipkin-endpoint", "", "optional opentracing server")
globalFlags.StringVar(&bearerSecretKey, "bearer-secretkey", "", "bearer.sh secret key")

agentFlags.BoolVar(&agentClean, "clean", false, "remove all pathwar instances before executing")
Expand Down Expand Up @@ -238,6 +247,7 @@ func main() {
RequestTimeout: serverRequestTimeout,
ShutdownTimeout: serverShutdownTimeout,
WithPprof: serverWithPprof,
Tracer: tracer,
}
var err error
server, err = pwapi.NewServer(ctx, svc, opts)
Expand Down Expand Up @@ -790,6 +800,24 @@ func globalPreRun() error {
return errcode.ErrInitLogger.Wrap(err)
}
}
if zipkinEndpoint != "" {
reporter := reporterhttp.NewReporter(zipkinEndpoint)
localEndpoint := &model.Endpoint{ServiceName: "pathwar"}
sampler, err := zipkin.NewCountingSampler(1)
if err != nil {
return errcode.ErrInitTracer.Wrap(err)
}
nativeTracer, err := zipkin.NewTracer(
reporter,
zipkin.WithSampler(sampler),
zipkin.WithLocalEndpoint(localEndpoint),
)
if err != nil {
return errcode.ErrInitTracer.Wrap(err)
}
tracer = zipkinot.Wrap(nativeTracer)
opentracing.SetGlobalTracer(tracer)
}
return nil
}

Expand Down
1 change: 1 addition & 0 deletions go/cmd/pathwar/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ func Example() {
// FLAGS
// -bearer-secretkey ... bearer.sh secret key
// -debug false debug mode
// -zipkin-endpoint ... optional opentracing server
}
4 changes: 4 additions & 0 deletions go/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ services:
mysql:
ports:
- 3306:3306

zipkin:
ports:
- 9411:9411
5 changes: 5 additions & 0 deletions go/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ services:
MYSQL_USER: pathwar
MYSQL_PASSWORD: uns3cur3

zipkin:
image: openzipkin/zipkin-slim:latest
ports:
- 9411

mailserver:
image: mailhog/mailhog
restart: unless-stopped
Expand Down
4 changes: 2 additions & 2 deletions go/gen.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1f3983d

Please sign in to comment.