lotus/lib/tracing/setup.go

92 lines
2.9 KiB
Go
Raw Permalink Normal View History

package tracing
import (
"os"
2021-05-25 08:23:33 +00:00
"strings"
2022-06-14 15:00:51 +00:00
logging "github.com/ipfs/go-log/v2"
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"
2021-12-02 13:44:59 +00:00
"go.uber.org/zap"
)
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
)
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.
func jaegerOptsFromEnv() jaeger.EndpointOption {
2021-05-25 08:23:33 +00:00
var e string
var ok bool
2021-05-25 08:23:33 +00:00
if e, ok = os.LookupEnv(envCollectorEndpoint); ok {
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)
return jaeger.WithCollectorEndpoint(options...)
2021-05-25 08:23:33 +00:00
}
2021-05-25 08:23:33 +00:00
if e, ok = os.LookupEnv(envAgentHost); ok {
2021-12-02 13:44:59 +00:00
options := []jaeger.AgentEndpointOption{jaeger.WithAgentHost(e), jaeger.WithLogger(zap.NewStdLog(log.Desugar()))}
var ep string
2021-05-25 08:23:33 +00:00
if p, ok := os.LookupEnv(envAgentPort); ok {
options = append(options, jaeger.WithAgentPort(p))
ep = strings.Join([]string{e, p}, ":")
2021-05-25 08:23:33 +00:00
} else {
ep = strings.Join([]string{e, "6831"}, ":")
2021-05-25 08:23:33 +00:00
}
log.Infof("jaeger traces will be sent to agent %s", ep)
return jaeger.WithAgentEndpoint(options...)
}
return nil
2021-05-25 08:23:33 +00:00
}
func SetupJaegerTracing(serviceName string) *tracesdk.TracerProvider {
jaegerEndpoint := jaegerOptsFromEnv()
if jaegerEndpoint == nil {
2021-05-25 08:23:33 +00:00
return nil
}
je, err := jaeger.New(jaegerEndpoint)
if err != nil {
2021-05-26 06:55:46 +00:00
log.Errorw("failed to create the jaeger exporter", "error", err)
return nil
}
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),
)),
2021-12-02 13:44:59 +00:00
tracesdk.WithSampler(tracesdk.AlwaysSample()),
)
otel.SetTracerProvider(tp)
tracer := tp.Tracer(serviceName)
octrace.DefaultTracer = opencensus.NewTracer(tracer)
return tp
}