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. diff --git a/lib/tracing/setup.go b/lib/tracing/setup.go index 141683b393a..1ace962e54e 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,19 +11,64 @@ 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" + envJaegerUser = "LOTUS_JAEGER_USERNAME" + envJaegerCred = "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 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(envJaegerUser); ok { + if p, ok := os.LookupEnv(envJaegerCred); 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) + log.Errorw("failed to create the jaeger exporter", "error", err) return nil }