2019-07-25 20:19:43 +00:00
|
|
|
package tracing
|
|
|
|
|
|
|
|
import (
|
2019-10-03 18:01:23 +00:00
|
|
|
"os"
|
2021-05-25 08:23:33 +00:00
|
|
|
"strings"
|
2019-10-03 18:01:23 +00:00
|
|
|
|
2021-12-02 00:07:56 +00:00
|
|
|
octrace "go.opencensus.io/trace"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/bridge/opencensus"
|
|
|
|
"go.opentelemetry.io/otel/exporters/jaeger"
|
|
|
|
"go.opentelemetry.io/otel/sdk/resource"
|
|
|
|
tracesdk "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
|
|
|
|
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-07-25 20:19:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("tracing")
|
|
|
|
|
2021-05-25 08:23:33 +00:00
|
|
|
const (
|
|
|
|
// environment variable names
|
|
|
|
envCollectorEndpoint = "LOTUS_JAEGER_COLLECTOR_ENDPOINT"
|
|
|
|
envAgentHost = "LOTUS_JAEGER_AGENT_HOST"
|
|
|
|
envAgentPort = "LOTUS_JAEGER_AGENT_PORT"
|
2021-05-26 06:55:46 +00:00
|
|
|
envJaegerUser = "LOTUS_JAEGER_USERNAME"
|
|
|
|
envJaegerCred = "LOTUS_JAEGER_PASSWORD"
|
2021-05-25 08:23:33 +00:00
|
|
|
)
|
2019-07-25 20:19:43 +00:00
|
|
|
|
2021-05-25 08:23:33 +00:00
|
|
|
// When sending directly to the collector, agent options are ignored.
|
|
|
|
// The collector endpoint is an HTTP or HTTPs URL.
|
2021-05-26 06:55:46 +00:00
|
|
|
// 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.
|
2021-12-02 00:07:56 +00:00
|
|
|
func jaegerOptsFromEnv() jaeger.EndpointOption {
|
2021-05-25 08:23:33 +00:00
|
|
|
var e string
|
|
|
|
var ok bool
|
2021-12-02 00:07:56 +00:00
|
|
|
|
2021-05-25 08:23:33 +00:00
|
|
|
if e, ok = os.LookupEnv(envCollectorEndpoint); ok {
|
2021-12-02 00:07:56 +00:00
|
|
|
options := []jaeger.CollectorEndpointOption{jaeger.WithEndpoint(e)}
|
|
|
|
if u, ok := os.LookupEnv(envJaegerUser); ok {
|
|
|
|
if p, ok := os.LookupEnv(envJaegerCred); ok {
|
|
|
|
options = append(options, jaeger.WithUsername(u))
|
|
|
|
options = append(options, jaeger.WithPassword(p))
|
|
|
|
} else {
|
|
|
|
log.Warn("jaeger username supplied with no password. authentication will not be used.")
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 08:23:33 +00:00
|
|
|
log.Infof("jaeger tracess will send to collector %s", e)
|
2021-12-02 00:07:56 +00:00
|
|
|
return jaeger.WithCollectorEndpoint(options...)
|
2021-05-25 08:23:33 +00:00
|
|
|
}
|
2021-12-02 00:07:56 +00:00
|
|
|
|
2021-05-25 08:23:33 +00:00
|
|
|
if e, ok = os.LookupEnv(envAgentHost); ok {
|
2021-12-02 00:07:56 +00:00
|
|
|
options := []jaeger.AgentEndpointOption{jaeger.WithAgentHost(e)}
|
|
|
|
var ep string
|
2021-05-25 08:23:33 +00:00
|
|
|
if p, ok := os.LookupEnv(envAgentPort); ok {
|
2021-12-02 00:07:56 +00:00
|
|
|
options = append(options, jaeger.WithAgentPort(p))
|
|
|
|
ep = strings.Join([]string{e, p}, ":")
|
2021-05-25 08:23:33 +00:00
|
|
|
} else {
|
2021-12-02 00:07:56 +00:00
|
|
|
ep = strings.Join([]string{e, "6831"}, ":")
|
2021-05-25 08:23:33 +00:00
|
|
|
}
|
2021-12-02 00:07:56 +00:00
|
|
|
log.Infof("jaeger traces will be sent to agent %s", ep)
|
|
|
|
return jaeger.WithAgentEndpoint(options...)
|
2019-10-03 18:01:23 +00:00
|
|
|
}
|
2021-12-02 00:07:56 +00:00
|
|
|
return nil
|
2021-05-25 08:23:33 +00:00
|
|
|
}
|
2019-07-25 20:19:43 +00:00
|
|
|
|
2021-12-02 00:07:56 +00:00
|
|
|
func SetupJaegerTracing(serviceName string) *tracesdk.TracerProvider {
|
|
|
|
jaegerEndpoint := jaegerOptsFromEnv()
|
|
|
|
if jaegerEndpoint == nil {
|
2021-05-25 08:23:33 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-02 00:07:56 +00:00
|
|
|
je, err := jaeger.New(jaegerEndpoint)
|
2019-07-25 20:19:43 +00:00
|
|
|
if err != nil {
|
2021-05-26 06:55:46 +00:00
|
|
|
log.Errorw("failed to create the jaeger exporter", "error", err)
|
2019-07-25 20:19:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-12-02 00:07:56 +00:00
|
|
|
tp := tracesdk.NewTracerProvider(
|
|
|
|
// Always be sure to batch in production.
|
|
|
|
tracesdk.WithBatcher(je),
|
|
|
|
// Record information about this application in an Resource.
|
|
|
|
tracesdk.WithResource(resource.NewWithAttributes(
|
|
|
|
semconv.SchemaURL,
|
|
|
|
semconv.ServiceNameKey.String(serviceName),
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
otel.SetTracerProvider(tp)
|
|
|
|
tracer := tp.Tracer(serviceName)
|
|
|
|
octrace.DefaultTracer = opencensus.NewTracer(tracer)
|
|
|
|
return tp
|
2019-07-25 20:19:43 +00:00
|
|
|
}
|