From fb10d4c54f16296dba7be673baa9e71ebafa7b0e Mon Sep 17 00:00:00 2001 From: Eundoo Song Date: Sat, 13 Oct 2018 03:32:53 +0900 Subject: [PATCH] Support tracer env based initialization in hotrod (#1115) --- examples/hotrod/README.md | 7 ++-- examples/hotrod/cmd/customer.go | 3 +- examples/hotrod/cmd/driver.go | 3 +- examples/hotrod/cmd/frontend.go | 2 +- examples/hotrod/cmd/root.go | 2 -- examples/hotrod/cmd/route.go | 2 +- examples/hotrod/pkg/tracing/init.go | 40 ++++++--------------- examples/hotrod/services/customer/server.go | 4 +-- examples/hotrod/services/driver/redis.go | 4 +-- examples/hotrod/services/driver/server.go | 4 +-- glide.lock | 19 +++++----- glide.yaml | 6 ++-- 12 files changed, 38 insertions(+), 58 deletions(-) diff --git a/examples/hotrod/README.md b/examples/hotrod/README.md index 05a18eceab0..0542f54c0c0 100644 --- a/examples/hotrod/README.md +++ b/examples/hotrod/README.md @@ -49,10 +49,11 @@ go run ./main.go all docker run \ --rm \ --link jaeger \ + --env JAEGER_AGENT_HOST=jaeger \ + --env JAEGER_AGENT_PORT=6831 \ -p8080-8083:8080-8083 \ - jaegertracing/example-hotrod:1.6 \ - all \ - --jaeger-agent.host-port=jaeger:6831 + jaegertracing/example-hotrod:latest \ + all ``` Then open http://127.0.0.1:8080 diff --git a/examples/hotrod/cmd/customer.go b/examples/hotrod/cmd/customer.go index ffec7ddf348..5a6363521ad 100644 --- a/examples/hotrod/cmd/customer.go +++ b/examples/hotrod/cmd/customer.go @@ -36,10 +36,9 @@ var customerCmd = &cobra.Command{ logger := log.NewFactory(zapLogger) server := customer.NewServer( net.JoinHostPort("0.0.0.0", strconv.Itoa(customerPort)), - tracing.Init("customer", metricsFactory.Namespace("customer", nil), logger, jAgentHostPort), + tracing.Init("customer", metricsFactory, logger), metricsFactory, logger, - jAgentHostPort, ) return logError(zapLogger, server.Run()) }, diff --git a/examples/hotrod/cmd/driver.go b/examples/hotrod/cmd/driver.go index b4225931b76..6b808cff20a 100644 --- a/examples/hotrod/cmd/driver.go +++ b/examples/hotrod/cmd/driver.go @@ -36,10 +36,9 @@ var driverCmd = &cobra.Command{ logger := log.NewFactory(zapLogger) server := driver.NewServer( net.JoinHostPort("0.0.0.0", strconv.Itoa(driverPort)), - tracing.Init("driver", metricsFactory.Namespace("driver", nil), logger, jAgentHostPort), + tracing.Init("driver", metricsFactory, logger), metricsFactory, logger, - jAgentHostPort, ) return logError(zapLogger, server.Run()) }, diff --git a/examples/hotrod/cmd/frontend.go b/examples/hotrod/cmd/frontend.go index fc58091f370..7cf4f355b56 100644 --- a/examples/hotrod/cmd/frontend.go +++ b/examples/hotrod/cmd/frontend.go @@ -42,7 +42,7 @@ var frontendCmd = &cobra.Command{ logger := log.NewFactory(zapLogger) server := frontend.NewServer( options, - tracing.Init("frontend", metricsFactory.Namespace("frontend", nil), logger, jAgentHostPort), + tracing.Init("frontend", metricsFactory, logger), logger, ) return logError(zapLogger, server.Run()) diff --git a/examples/hotrod/cmd/root.go b/examples/hotrod/cmd/root.go index 8f67303bd58..fe19127f3fd 100644 --- a/examples/hotrod/cmd/root.go +++ b/examples/hotrod/cmd/root.go @@ -31,7 +31,6 @@ import ( var ( metricsBackend string - jAgentHostPort string logger *zap.Logger metricsFactory metrics.Factory @@ -63,7 +62,6 @@ func Execute() { func init() { RootCmd.PersistentFlags().StringVarP(&metricsBackend, "metrics", "m", "expvar", "Metrics backend (expvar|prometheus)") - RootCmd.PersistentFlags().StringVarP(&jAgentHostPort, "jaeger-agent.host-port", "a", "0.0.0.0:6831", "String representing jaeger-agent UDP host:port, or jaeger-collector HTTP endpoint address, e.g. http://localhost:14268/api/traces.") RootCmd.PersistentFlags().DurationVarP(&fixDBConnDelay, "fix-db-query-delay", "D", 300*time.Millisecond, "Average lagency of MySQL DB query") RootCmd.PersistentFlags().BoolVarP(&fixDBConnDisableMutex, "fix-disable-db-conn-mutex", "M", false, "Disables the mutex guarding db connection") RootCmd.PersistentFlags().IntVarP(&fixRouteWorkerPoolSize, "fix-route-worker-pool-size", "W", 3, "Default worker pool size") diff --git a/examples/hotrod/cmd/route.go b/examples/hotrod/cmd/route.go index 22102af6bc7..3fdcc189135 100644 --- a/examples/hotrod/cmd/route.go +++ b/examples/hotrod/cmd/route.go @@ -36,7 +36,7 @@ var routeCmd = &cobra.Command{ logger := log.NewFactory(zapLogger) server := route.NewServer( net.JoinHostPort("0.0.0.0", strconv.Itoa(routePort)), - tracing.Init("route", metricsFactory.Namespace("route", nil), logger, jAgentHostPort), + tracing.Init("route", metricsFactory, logger), logger, ) return logError(zapLogger, server.Run()) diff --git a/examples/hotrod/pkg/tracing/init.go b/examples/hotrod/pkg/tracing/init.go index 25b12629c09..83444fa4f18 100644 --- a/examples/hotrod/pkg/tracing/init.go +++ b/examples/hotrod/pkg/tracing/init.go @@ -16,14 +16,11 @@ package tracing import ( "fmt" - "strings" "time" "github.com/opentracing/opentracing-go" - "github.com/uber/jaeger-client-go" "github.com/uber/jaeger-client-go/config" "github.com/uber/jaeger-client-go/rpcmetrics" - "github.com/uber/jaeger-client-go/transport" "github.com/uber/jaeger-lib/metrics" "go.uber.org/zap" @@ -31,36 +28,21 @@ import ( ) // Init creates a new instance of Jaeger tracer. -func Init(serviceName string, metricsFactory metrics.Factory, logger log.Factory, backendHostPort string) opentracing.Tracer { - cfg := config.Configuration{ - Sampler: &config.SamplerConfig{ - Type: "const", - Param: 1, - }, +func Init(serviceName string, metricsFactory metrics.Factory, logger log.Factory) opentracing.Tracer { + cfg, err := config.FromEnv() + if err != nil { + logger.Bg().Fatal("cannot parse Jaeger env vars", zap.Error(err)) } + cfg.ServiceName = serviceName + cfg.Sampler.Type = "const" + cfg.Sampler.Param = 1 + // TODO(ys) a quick hack to ensure random generators get different seeds, which are based on current time. time.Sleep(100 * time.Millisecond) jaegerLogger := jaegerLoggerAdapter{logger.Bg()} - var sender jaeger.Transport - if strings.HasPrefix(backendHostPort, "http://") { - sender = transport.NewHTTPTransport( - backendHostPort, - transport.HTTPBatchSize(1), - ) - } else { - if s, err := jaeger.NewUDPTransport(backendHostPort, 0); err != nil { - logger.Bg().Fatal("cannot initialize UDP sender", zap.Error(err)) - } else { - sender = s - } - } - tracer, _, err := cfg.New( - serviceName, - config.Reporter(jaeger.NewRemoteReporter( - sender, - jaeger.ReporterOptions.BufferFlushInterval(1*time.Second), - jaeger.ReporterOptions.Logger(jaegerLogger), - )), + + metricsFactory = metricsFactory.Namespace(serviceName, nil) + tracer, _, err := cfg.NewTracer( config.Logger(jaegerLogger), config.Metrics(metricsFactory), config.Observer(rpcmetrics.NewObserver(metricsFactory, rpcmetrics.DefaultNameNormalizer)), diff --git a/examples/hotrod/services/customer/server.go b/examples/hotrod/services/customer/server.go index dd6709766ae..1b25ad2c58c 100644 --- a/examples/hotrod/services/customer/server.go +++ b/examples/hotrod/services/customer/server.go @@ -36,13 +36,13 @@ type Server struct { } // NewServer creates a new customer.Server -func NewServer(hostPort string, tracer opentracing.Tracer, metricsFactory metrics.Factory, logger log.Factory, jAgentHostPort string) *Server { +func NewServer(hostPort string, tracer opentracing.Tracer, metricsFactory metrics.Factory, logger log.Factory) *Server { return &Server{ hostPort: hostPort, tracer: tracer, logger: logger, database: newDatabase( - tracing.Init("mysql", metricsFactory.Namespace("mysql", nil), logger, jAgentHostPort), + tracing.Init("mysql", metricsFactory, logger), logger.With(zap.String("component", "mysql")), ), } diff --git a/examples/hotrod/services/driver/redis.go b/examples/hotrod/services/driver/redis.go index 350b5590c89..23d4ceca3ce 100644 --- a/examples/hotrod/services/driver/redis.go +++ b/examples/hotrod/services/driver/redis.go @@ -39,9 +39,9 @@ type Redis struct { errorSimulator } -func newRedis(metricsFactory metrics.Factory, logger log.Factory, jAgentHostPort string) *Redis { +func newRedis(metricsFactory metrics.Factory, logger log.Factory) *Redis { return &Redis{ - tracer: tracing.Init("redis", metricsFactory.Namespace("redis", nil), logger, jAgentHostPort), + tracer: tracing.Init("redis", metricsFactory, logger), logger: logger, } } diff --git a/examples/hotrod/services/driver/server.go b/examples/hotrod/services/driver/server.go index 97295c622f4..274ceb7db07 100644 --- a/examples/hotrod/services/driver/server.go +++ b/examples/hotrod/services/driver/server.go @@ -36,7 +36,7 @@ type Server struct { } // NewServer creates a new driver.Server -func NewServer(hostPort string, tracer opentracing.Tracer, metricsFactory metrics.Factory, logger log.Factory, jAgentHostPort string) *Server { +func NewServer(hostPort string, tracer opentracing.Tracer, metricsFactory metrics.Factory, logger log.Factory) *Server { channelOpts := &tchannel.ChannelOptions{ Tracer: tracer, } @@ -52,7 +52,7 @@ func NewServer(hostPort string, tracer opentracing.Tracer, metricsFactory metric logger: logger, ch: ch, server: server, - redis: newRedis(metricsFactory, logger, jAgentHostPort), + redis: newRedis(metricsFactory, logger), } } diff --git a/glide.lock b/glide.lock index 45f1de7af30..274f1ac51db 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 5ffbb7d077d2f42d30c8107c7c323b17e8fbc252fff78e24c5e19577cf942e04 -updated: 2018-08-13T15:02:55.899715-04:00 +hash: eb96c7fd56c8c6523990c19ba5bcf0d826922569a16e1019a88de57d46675775 +updated: 2018-10-12T23:27:30.163799+09:00 imports: - name: github.com/apache/thrift version: 53dd39833a08ce33582e5ff31fa18bb4735d6731 @@ -21,7 +21,7 @@ imports: - assert - require - name: github.com/davecgh/go-spew - version: 8991bc29aa16c548c550c7ff78260e27b9ab7c73 + version: d8f796af33cc11cb798c1aaeb27a4ebc5099927d subpackages: - spew - name: github.com/eapache/go-resiliency @@ -204,7 +204,7 @@ imports: - name: github.com/rcrowley/go-metrics version: e2704e165165ec55d062f5919b4b29494e9fa790 - name: github.com/Shopify/sarama - version: 35324cf48e33d8260e1c7c18854465a904ade249 + version: f7be6aa2bc7b2e38edf816b08b582782194a1c02 subpackages: - mocks - name: github.com/spf13/afero @@ -218,11 +218,11 @@ imports: - name: github.com/spf13/jwalterweatherman version: 12bd96e66386c1960ab0f74ced1362f66f552f7b - name: github.com/spf13/pflag - version: 9a97c102cda95a86cec2345a6f09f55a939babf5 + version: 298182f68c66c05229eb03ac171abe6e309ee79a - name: github.com/spf13/viper - version: 907c19d40d9a6c9bb55f040ff4ae45271a4754b9 + version: 2c12c60302a5a0e62ee102ca9bc996277c2f64f5 - name: github.com/stretchr/objx - version: b8b73a35e9830ae509858c10dec5866b4d5c8bff + version: ef50b0de28773081167c97fc27cf29a0bf8b8c71 - name: github.com/stretchr/testify version: f35b8ab0b5a2cef36673838d662e249dd9c94686 subpackages: @@ -233,7 +233,7 @@ imports: - name: github.com/uber-go/atomic version: 8474b86a5a6f79c443ce4b2992817ff32cf208b8 - name: github.com/uber/jaeger-client-go - version: b043381d944715b469fd6b37addfd30145ca1758 + version: 1a782e2da844727691fef1757c72eb190c2909f0 subpackages: - config - internal/baggage @@ -242,6 +242,7 @@ imports: - internal/throttler - internal/throttler/remote - log + - log/zap - rpcmetrics - thrift - thrift-gen/agent @@ -292,7 +293,7 @@ imports: - zapcore - zaptest - name: golang.org/x/net - version: c39426892332e1bb5ec0a434a079bf82f5d30c54 + version: 49bb7cea24b1df9410e1712aa6433dae904ff66a subpackages: - context - context/ctxhttp diff --git a/glide.yaml b/glide.yaml index f46039c9853..655375133f9 100644 --- a/glide.yaml +++ b/glide.yaml @@ -13,7 +13,7 @@ import: - package: go.uber.org/zap version: ^1 - package: github.com/uber/jaeger-client-go - version: ^2.11.0 + version: ^2.15.0 subpackages: - transport - package: github.com/uber/jaeger-lib @@ -58,9 +58,9 @@ import: subpackages: - fs - package: github.com/Shopify/sarama - version: ^1.16.0 + version: 1.16.0 - package: github.com/bsm/sarama-cluster - version: ^2.1.13 + version: 2.1.13 - package: github.com/gogo/googleapis version: b23578765ee54ff6bceff57f397d833bf4ca6869 - package: github.com/gogo/protobuf