From 273368ed6a684f318027fce16ae4198f9c522862 Mon Sep 17 00:00:00 2001 From: Cory Schwartz Date: Tue, 25 May 2021 01:23:33 -0700 Subject: [PATCH 1/3] separate tracing environment variables --- lib/tracing/setup.go | 61 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 8 deletions(-) diff --git a/lib/tracing/setup.go b/lib/tracing/setup.go index 141683b393a..9f30b7714f6 100644 --- a/lib/tracing/setup.go +++ b/lib/tracing/setup.go @@ -2,6 +2,7 @@ package tracing import ( "os" + "strings" "contrib.go.opencensus.io/exporter/jaeger" logging "github.com/ipfs/go-log/v2" @@ -10,17 +11,61 @@ import ( var log = logging.Logger("tracing") -func SetupJaegerTracing(serviceName string) *jaeger.Exporter { +const ( + // environment variable names + envCollectorEndpoint = "LOTUS_JAEGER_COLLECTOR_ENDPOINT" + envAgentEndpoint = "LOTUS_JAEGER_AGENT_ENDPOINT" + envAgentHost = "LOTUS_JAEGER_AGENT_HOST" + envAgentPort = "LOTUS_JAEGER_AGENT_PORT" + envUsername = "LOTUS_JAEGER_USERNAME" + envPassword = "LOTUS_JAEGER_PASSWORD" +) - if _, ok := os.LookupEnv("LOTUS_JAEGER"); !ok { - return nil +// When sending directly to the collector, agent options are ignored. +// The collector endpoint is an HTTP or HTTPs URL. +// The agent endpoint is a thrift/udp protocol given like "hostname:port" +// or separate host and port environment variables. +func jaegerOptsFromEnv(opts *jaeger.Options) bool { + var e string + var ok bool + if e, ok = os.LookupEnv(envUsername); ok { + if p, ok := os.LookupEnv(envPassword); ok { + opts.Username = e + opts.Password = p + } else { + log.Warn("jaeger username supplied with no password. authentication will not be used.") + } + } + if e, ok = os.LookupEnv(envCollectorEndpoint); ok { + opts.CollectorEndpoint = e + log.Infof("jaeger tracess will send to collector %s", e) + return true + } + if e, ok = os.LookupEnv(envAgentEndpoint); ok { + log.Infof("jaeger traces will be sent to agent %s", e) + opts.AgentEndpoint = e + return true + } + if e, ok = os.LookupEnv(envAgentHost); ok { + if p, ok := os.LookupEnv(envAgentPort); ok { + opts.AgentEndpoint = strings.Join([]string{e, p}, ":") + } else { + opts.AgentEndpoint = strings.Join([]string{e, "6831"}, ":") + } + log.Infof("jaeger traces will be sent to agent %s", opts.AgentEndpoint) + return true } - agentEndpointURI := os.Getenv("LOTUS_JAEGER") + log.Infof("jaeger tracing is not configured.") + return false +} - je, err := jaeger.NewExporter(jaeger.Options{ - AgentEndpoint: agentEndpointURI, - ServiceName: serviceName, - }) +func SetupJaegerTracing(serviceName string) *jaeger.Exporter { + opts := jaeger.Options{} + if !jaegerOptsFromEnv(&opts) { + return nil + } + opts.ServiceName = serviceName + je, err := jaeger.NewExporter(opts) if err != nil { log.Errorw("Failed to create the Jaeger exporter", "error", err) return nil From e6b631078f2da81471bcbac07fed02c2db805f93 Mon Sep 17 00:00:00 2001 From: Cory Schwartz Date: Tue, 25 May 2021 23:53:08 -0700 Subject: [PATCH 2/3] update jaeger documentation --- documentation/en/jaeger-tracing.md | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/documentation/en/jaeger-tracing.md b/documentation/en/jaeger-tracing.md index bbe4d30523a..ec9351d5322 100644 --- a/documentation/en/jaeger-tracing.md +++ b/documentation/en/jaeger-tracing.md @@ -12,7 +12,20 @@ Currently it is set up to use Jaeger, though other tracing backends should be fa To easily run and view tracing locally, first, install jaeger. The easiest way to do this is to [download the binaries](https://www.jaegertracing.io/download/) and then run the `jaeger-all-in-one` binary. This will start up jaeger, listen for spans on `localhost:6831`, and expose a web UI for viewing traces on `http://localhost:16686/`. -Now, to start sending traces from Lotus to Jaeger, set the environment variable `LOTUS_JAEGER` to `localhost:6831`, and start the `lotus daemon`. +Now, to start sending traces from Lotus to Jaeger, set the environment variable and start the daemon. + +```bash +export LOTUS_JAEGER_AGENT_ENDPOINT=127.0.0.1:6831 +lotus daemon +``` + +Alternatively, the agent endpoint can also be configured by a pair of environemnt variables to provide the host and port. The following snipit is functionally equivilent to the previous. + +```bash +export LOTUS_JAEGER_AGENT_HOST=127.0.0.1 +export LOTUS_JAEGER_AGENT_PORT=6831 +lotus daemon +``` Now, to view any generated traces, open up `http://localhost:16686/` in your browser. From d50b59b4c70fd0428a46bf2c28662f4be1be9f53 Mon Sep 17 00:00:00 2001 From: Cory Schwartz Date: Tue, 25 May 2021 23:55:46 -0700 Subject: [PATCH 3/3] don't scare the gosec linter --- lib/tracing/setup.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/tracing/setup.go b/lib/tracing/setup.go index 9f30b7714f6..1ace962e54e 100644 --- a/lib/tracing/setup.go +++ b/lib/tracing/setup.go @@ -17,19 +17,20 @@ const ( envAgentEndpoint = "LOTUS_JAEGER_AGENT_ENDPOINT" envAgentHost = "LOTUS_JAEGER_AGENT_HOST" envAgentPort = "LOTUS_JAEGER_AGENT_PORT" - envUsername = "LOTUS_JAEGER_USERNAME" - envPassword = "LOTUS_JAEGER_PASSWORD" + envJaegerUser = "LOTUS_JAEGER_USERNAME" + envJaegerCred = "LOTUS_JAEGER_PASSWORD" ) // When sending directly to the collector, agent options are ignored. // The collector endpoint is an HTTP or HTTPs URL. -// The agent endpoint is a thrift/udp protocol given like "hostname:port" -// or separate host and port environment variables. +// The agent endpoint is a thrift/udp protocol and should be given +// as a string like "hostname:port". The agent can also be configured +// with separate host and port variables. func jaegerOptsFromEnv(opts *jaeger.Options) bool { var e string var ok bool - if e, ok = os.LookupEnv(envUsername); ok { - if p, ok := os.LookupEnv(envPassword); ok { + if e, ok = os.LookupEnv(envJaegerUser); ok { + if p, ok := os.LookupEnv(envJaegerCred); ok { opts.Username = e opts.Password = p } else { @@ -67,7 +68,7 @@ func SetupJaegerTracing(serviceName string) *jaeger.Exporter { opts.ServiceName = serviceName je, err := jaeger.NewExporter(opts) if err != nil { - log.Errorw("Failed to create the Jaeger exporter", "error", err) + log.Errorw("failed to create the jaeger exporter", "error", err) return nil }