Skip to content

Commit

Permalink
Merge pull request #6323 from filecoin-project/feat/separate-tracing-…
Browse files Browse the repository at this point in the history
…env-vars

separate tracing environment variables
  • Loading branch information
magik6k committed May 26, 2021
2 parents 930f2b4 + d50b59b commit 409f96d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
15 changes: 14 additions & 1 deletion documentation/en/jaeger-tracing.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
64 changes: 55 additions & 9 deletions lib/tracing/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tracing

import (
"os"
"strings"

"contrib.go.opencensus.io/exporter/jaeger"
logging "github.com/ipfs/go-log/v2"
Expand All @@ -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
}

Expand Down

0 comments on commit 409f96d

Please sign in to comment.