From 85c7131d43b02ea7c0c777354ee1bd5c538de202 Mon Sep 17 00:00:00 2001 From: rghetia Date: Tue, 8 Jan 2019 11:08:20 -0800 Subject: [PATCH] Add jaeger support for frontend. (#113) --- kubernetes-manifests/frontend.yaml | 2 + src/frontend/Gopkg.lock | 111 +++++++++++++++++++---------- src/frontend/main.go | 36 +++++++++- 3 files changed, 111 insertions(+), 38 deletions(-) diff --git a/kubernetes-manifests/frontend.yaml b/kubernetes-manifests/frontend.yaml index e4b8e89..c83a1a7 100644 --- a/kubernetes-manifests/frontend.yaml +++ b/kubernetes-manifests/frontend.yaml @@ -58,6 +58,8 @@ spec: value: "checkoutservice:5050" - name: AD_SERVICE_ADDR value: "adservice:9555" + # - name: JAEGER_SERVICE_ADDR + # value: "jaeger-collector:14268" resources: requests: cpu: 100m diff --git a/src/frontend/Gopkg.lock b/src/frontend/Gopkg.lock index fa48498..53289b0 100644 --- a/src/frontend/Gopkg.lock +++ b/src/frontend/Gopkg.lock @@ -16,16 +16,33 @@ version = "v0.27.0" [[projects]] - digest = "1:9fe70def8f0ceb3d455a0acad9dadd6632287cdbf9c8c2ea50e8dabe2ade40c4" + digest = "1:4b96dcd8534bc6450a922bd16a76360ba3381f0d1daf40abbaec91c053fbfeb5" name = "contrib.go.opencensus.io/exporter/stackdriver" - packages = [ - ".", - "propagation", - ] + packages = ["."] pruneopts = "UT" revision = "37aa2801fbf0205003e15636096ebf0373510288" version = "v0.5.0" +[[projects]] + branch = "master" + digest = "1:1d8f3cb93c42c5652bb509fde29ecdd1feede9334e355e8bbdc0f9f95b40c254" + name = "git.apache.org/thrift.git" + packages = ["lib/go/thrift"] + pruneopts = "UT" + revision = "6503043bc42ab96da14c25f3aee2bb4add719774" + source = "github.com/apache/thrift" + +[[projects]] + branch = "master" + digest = "1:6cbe7676244a1429f4c22601f799d377a70449469ef636f91d992d719b559ff3" + name = "github.com/GoogleCloudPlatform/microservices-demo" + packages = [ + "src/frontend/genproto", + "src/frontend/money", + ] + pruneopts = "UT" + revision = "d944092100696aa4a5974ef5c2e710547a824622" + [[projects]] digest = "1:72856926f8208767b837bf51e3373f49139f61889b67dc7fd3c2a0fd711e3f7a" name = "github.com/golang/protobuf" @@ -50,23 +67,26 @@ name = "github.com/google/pprof" packages = ["profile"] pruneopts = "UT" - revision = "84b7d314e22c8d12334e52726f68517973b6027b" + revision = "3ea8567a2e5752420fc544d2e2ad249721768934" [[projects]] - digest = "1:3a26588bc48b96825977c1b3df964f8fd842cd6860cc26370588d3563433cf11" + digest = "1:236d7e1bdb50d8f68559af37dbcf9d142d56b431c9b2176d41e2a009b664cda8" name = "github.com/google/uuid" packages = ["."] pruneopts = "UT" - revision = "d460ce9f8df2e77fb1ba55ca87fafed96c607494" - version = "v1.0.0" + revision = "9b3b1e0f5f99ae461456d768e7d301a7acdaa2d8" + version = "v1.1.0" [[projects]] - digest = "1:e145e9710a10bc114a6d3e2738aadf8de146adaa031854ffdf7bbfe15da85e63" + digest = "1:cd9864c6366515827a759931746738ede6079faa08df9c584596370d6add135c" name = "github.com/googleapis/gax-go" - packages = ["."] + packages = [ + ".", + "v2", + ] pruneopts = "UT" - revision = "317e0006254c44a0ac427cc52a0e083ff0b9622f" - version = "v2.0.0" + revision = "c8a15bac9b9fe955bd9f900272f9a306465d28cf" + version = "v2.0.3" [[projects]] digest = "1:c79fb010be38a59d657c48c6ba1d003a8aa651fa56b579d959d74573b7dff8e1" @@ -84,6 +104,14 @@ revision = "e3702bed27f0d39777b0b37b664b6280e8ef8fbf" version = "v1.6.2" +[[projects]] + digest = "1:0a69a1c0db3591fcefb47f115b224592c8dfa4368b7ba9fae509d5e16cdc95c8" + name = "github.com/konsorten/go-windows-terminal-sequences" + packages = ["."] + pruneopts = "UT" + revision = "5c8c8bd35d3832f5d134ae1e1e375b69a4d25242" + version = "v1.0.1" + [[projects]] digest = "1:40e195917a951a8bf867cd05de2a46aaf1806c50cf92eebf4c16f78cd196f747" name = "github.com/pkg/errors" @@ -93,18 +121,20 @@ version = "v0.8.0" [[projects]] - digest = "1:d867dfa6751c8d7a435821ad3b736310c2ed68945d05b50fb9d23aee0540c8cc" + digest = "1:69b1cc331fca23d702bd72f860c6a647afd0aa9fcbc1d0659b1365e26546dd70" name = "github.com/sirupsen/logrus" packages = ["."] pruneopts = "UT" - revision = "3e01752db0189b9157070a0e1668a620f9a85da2" - version = "v1.0.6" + revision = "bcd833dfe83d3cebad139e4a29ed79cb2318bf95" + version = "v1.2.0" [[projects]] - digest = "1:1bb914cfb78f68f488a91cd7872d3d06a5f83c5bbacf0296dbef44e120b00a91" + digest = "1:a5154dfd6b37bef5a3eab759e13296348e639dc8c7604f538368167782b08ccd" name = "go.opencensus.io" packages = [ ".", + "exporter/jaeger", + "exporter/jaeger/internal/gen-go/jaeger", "internal", "internal/tagencoding", "plugin/ocgrpc", @@ -125,15 +155,15 @@ [[projects]] branch = "master" - digest = "1:3f3a05ae0b95893d90b9b3b5afdb79a9b3d96e4e36e099d841ae602e4aca0da8" + digest = "1:38f553aff0273ad6f367cb0a0f8b6eecbaef8dc6cb8b50e57b6a81c1d5b1e332" name = "golang.org/x/crypto" packages = ["ssh/terminal"] pruneopts = "UT" - revision = "0e37d006457bf46f9e6692014ba72ef82c33022c" + revision = "ff983b9c42bc9fbf91556e191cc8efb585c16908" [[projects]] branch = "master" - digest = "1:1c14517b2f106c61d75006199b46a46576058661d469658cb0f90739919641d2" + digest = "1:8ecb828bb550a8c6b7d75b8261a42c369461311616ebe5451966d067f5f993bf" name = "golang.org/x/net" packages = [ "context", @@ -146,11 +176,11 @@ "trace", ] pruneopts = "UT" - revision = "26e67e76b6c3f6ce91f7c52def5af501b4e0f3a2" + revision = "927f97764cc334a6575f4b7a1584a147864d5723" [[projects]] branch = "master" - digest = "1:f645667d687fc8bf228865a2c5455824ef05bad08841e673673ef2bb89ac5b90" + digest = "1:23443edff0740e348959763085df98400dcfd85528d7771bb0ce9f5f2754ff4a" name = "golang.org/x/oauth2" packages = [ ".", @@ -160,26 +190,26 @@ "jwt", ] pruneopts = "UT" - revision = "d2e6202438beef2727060aa7cabdd924d92ebfd9" + revision = "d668ce993890a79bda886613ee587a69dd5da7a6" [[projects]] branch = "master" - digest = "1:e0140c0c868c6e0f01c0380865194592c011fe521d6e12d78bfd33e756fe018a" + digest = "1:75515eedc0dc2cb0b40372008b616fa2841d831c63eedd403285ff286c593295" name = "golang.org/x/sync" packages = ["semaphore"] pruneopts = "UT" - revision = "1d60e4601c6fd243af51cc01ddf169918a5407ca" + revision = "37e7f081c4d4c64e13b10787722085407fe5d15f" [[projects]] branch = "master" - digest = "1:374fc90fcb026e9a367e3fad29e988e5dd944b68ca3f24a184d77abc5307dda4" + digest = "1:191cccd950a4aeadb60306062f2bdc2f924d750d0156ec6c691b17211bfd7349" name = "golang.org/x/sys" packages = [ "unix", "windows", ] pruneopts = "UT" - revision = "d0be0721c37eeb5299f245a996a483160fc36940" + revision = "82a175fd1598e8a172e58ebdf5ed262bb29129e5" [[projects]] digest = "1:a2ab62866c75542dd18d2b069fec854577a20211d7c0ea6ae746072a1dccdd18" @@ -206,7 +236,7 @@ [[projects]] branch = "master" - digest = "1:e9e388241f9f0f02000dddaeeb91153d53f0cd09dcec33879cc7e043a2e65d75" + digest = "1:2e81813e8e072aa700e101369890e55539729d817d32dbc3fab228d6b40c4d83" name = "google.golang.org/api" packages = [ "googleapi/transport", @@ -217,12 +247,13 @@ "transport", "transport/grpc", "transport/http", + "transport/http/internal/propagation", ] pruneopts = "UT" - revision = "7ca32eb868bf53ea2fc406698eb98583a8073d19" + revision = "19e022d8cf43ce81f046bae8cc18c5397cc7732f" [[projects]] - digest = "1:26619fcd2452b4044174d26acd8b09c09dffee9a1c3a22d2383b873aa9a0131f" + digest = "1:c4eaa5f79d36f76ef4bd0c4f96e36bc1b7b5a359528d1267f0cb7a5d58b7b5bb" name = "google.golang.org/appengine" packages = [ ".", @@ -239,12 +270,12 @@ "urlfetch", ] pruneopts = "UT" - revision = "b1f26356af11148e710935ed1ac8a7f5702c7612" - version = "v1.1.0" + revision = "e9657d882bb81064595ca3b56cbe2546bbabf7b1" + version = "v1.4.0" [[projects]] branch = "master" - digest = "1:7a3da01a8f840fbbef1f027dc091ae52a29c6ab9374e126b6bdc5bf3b0ff2687" + digest = "1:3552e7267a98c605e6cbfe6b03c34589265ab72e6078b95fb2e10e0cb44f5cc8" name = "google.golang.org/genproto" packages = [ "googleapis/api/annotations", @@ -260,28 +291,33 @@ "protobuf/field_mask", ] pruneopts = "UT" - revision = "36d5787dc5356b711fe8f3040271aaf51c35955b" + revision = "bd9b4fb69e2ffd37621a6caa54dcbead29b546f2" [[projects]] - digest = "1:4ad047d772a7d4841687df9534a767a9e243885ed6d7d2ced6af5995a5dc44b3" + digest = "1:3fc54ad826c0183f803bb97e0927dfc05fcb7b7a6ddabed646ee8184d861fa9b" name = "google.golang.org/grpc" packages = [ ".", "balancer", "balancer/base", "balancer/roundrobin", + "binarylog/grpc_binarylog_v1", "codes", "connectivity", "credentials", + "credentials/internal", "credentials/oauth", "encoding", "encoding/proto", "grpclog", "internal", "internal/backoff", + "internal/binarylog", "internal/channelz", "internal/envconfig", "internal/grpcrand", + "internal/grpcsync", + "internal/syscall", "internal/transport", "keepalive", "metadata", @@ -295,8 +331,8 @@ "tap", ] pruneopts = "UT" - revision = "8dea3dc473e90c8179e519d91302d0597c0ca1d1" - version = "v1.15.0" + revision = "df014850f6dee74ba2fc94874043a9f3f75fbfd8" + version = "v1.17.0" [solve-meta] analyzer-name = "dep" @@ -311,6 +347,7 @@ "github.com/gorilla/mux", "github.com/pkg/errors", "github.com/sirupsen/logrus", + "go.opencensus.io/exporter/jaeger", "go.opencensus.io/plugin/ocgrpc", "go.opencensus.io/plugin/ochttp", "go.opencensus.io/plugin/ochttp/propagation/b3", diff --git a/src/frontend/main.go b/src/frontend/main.go index 5046432..61ac8ce 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -26,6 +26,7 @@ import ( "github.com/gorilla/mux" "github.com/pkg/errors" "github.com/sirupsen/logrus" + "go.opencensus.io/exporter/jaeger" "go.opencensus.io/plugin/ocgrpc" "go.opencensus.io/plugin/ochttp" "go.opencensus.io/plugin/ochttp/propagation/b3" @@ -142,6 +143,29 @@ func main() { log.Fatal(http.ListenAndServe(addr+":"+srvPort, handler)) } +func initJaegerTracing(log logrus.FieldLogger) { + + svcAddr := os.Getenv("JAEGER_SERVICE_ADDR") + if svcAddr == "" { + log.Info("jaeger initialization disabled.") + return + } + + // Register the Jaeger exporter to be able to retrieve + // the collected spans. + exporter, err := jaeger.NewExporter(jaeger.Options{ + Endpoint: fmt.Sprintf("http://%s", svcAddr), + Process: jaeger.Process{ + ServiceName: "frontend", + }, + }) + if err != nil { + log.Fatal(err) + } + trace.RegisterExporter(exporter) + log.Info("jaeger initialization completed.") +} + func initStats(log logrus.FieldLogger, exporter *stackdriver.Exporter) { view.SetReportingPeriod(60 * time.Second) view.RegisterExporter(exporter) @@ -158,16 +182,26 @@ func initStats(log logrus.FieldLogger, exporter *stackdriver.Exporter) { } func initTracing(log logrus.FieldLogger) { + // This is a demo app with low QPS. trace.AlwaysSample() is used here + // to make sure traces are available for observation and analysis. + // In a production environment or high QPS setup please use + // trace.ProbabilitySampler set at the desired probability. + trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()}) + + initJaegerTracing(log) + // TODO(ahmetb) this method is duplicated in other microservices using Go // since they are not sharing packages. for i := 1; i <= 3; i++ { log = log.WithField("retry", i) exporter, err := stackdriver.NewExporter(stackdriver.Options{}) if err != nil { + // log.Warnf is used since there are multiple backends (stackdriver & jaeger) + // to store the traces. In production setup most likely you would use only one backend. + // In that case you should use log.Fatalf. log.Warnf("failed to initialize stackdriver exporter: %+v", err) } else { trace.RegisterExporter(exporter) - trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()}) log.Info("registered stackdriver tracing") // Register the views to collect server stats.