github.com/TykTechnologies/tyk/trace
No package summary is available.
Package
Files: 4. Third party imports: 2. Imports from organisation: 0. Tests: 0. Benchmarks: 0.
Vars
ErrManagerDisabled is returned when trying to use global trace manager when it is disabled.
var ErrManagerDisabled = errors.New("trace: trace is diabled")
Stores status of tracing.
var enabled atomic.Value
var initializer = Init
var logger Logger = StdLogger{}
This stores a map of opentracer configurations.
var manager = &sync.Map{}
This stores a map of service name to initialized Tracer implementation.
var services = &sync.Map{}
Types
Config
This type doesn't have documentation.
| Field name | Field type | Comment |
|---|---|---|
| Name |
|
No comment on field. |
| Opts |
|
No comment on field. |
type Config struct {
Name string
Opts map[string]interface{}
}
InitFunc
InitFunc this is a function for initializing a Tracer
| Field name | Field type | Comment |
|---|---|---|
| type |
|
No comment on field. |
type InitFunc func(name string, service string, opts map[string]interface{}, logger Logger) (Tracer, error)
Logger
Logger defines api for logging messages by the OpenTracer struct. This is a workaround to avoid trying this to logrus
| Field name | Field type | Comment |
|---|---|---|
| type |
|
No comment on field. |
type Logger interface {
Errorf(format string, args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
}
Logrus
Logrus implements a subset of logrus api to reduce friction when we want to log both on opentracing and on logrus.
| Field name | Field type | Comment |
|---|---|---|
| type |
|
No comment on field. |
type Logrus interface {
Debug(args ...interface{})
Error(args ...interface{})
Warning(args ...interface{})
Info(args ...interface{})
}
NoopTracer
NoopTracer wraps opentracing.NoopTracer to satisfy Tracer interface.
| Field name | Field type | Comment |
|---|---|---|
|
No comment on field. |
type NoopTracer struct {
opentracing.NoopTracer
}
StdLogger
This type doesn't have documentation.
type StdLogger struct{}
Tracer
This type doesn't have documentation.
| Field name | Field type | Comment |
|---|---|---|
| type |
|
No comment on field. |
type Tracer interface {
Name() string
opentracing.Tracer
io.Closer
}
serviceID
serviceID key used to store the service name in request context.Context.
type serviceID = struct{}
Functions
func AddTracer
AddTracer initialize a tracer for the service.
func AddTracer(tracer, service string) error {
if !IsEnabled() {
return ErrManagerDisabled
}
if _, ok := services.Load(service); !ok {
if v, ok := manager.Load(tracer); ok {
c := v.(Config)
tr, err := initializer(c.Name, service, c.Opts, StdLogger{})
if err != nil {
return err
}
services.Store(service, tr)
}
}
return nil
}
Cognitive complexity: 9, Cyclomatic complexity: 5
func Close
Close calls Close on the global tace manager.
func Close() error {
var s []string
services.Range(func(k, v interface{}) bool {
s = append(s, k.(string))
v.(Tracer).Close()
return true
})
for _, v := range s {
services.Delete(v)
}
Disable()
return nil
}
Cognitive complexity: 5, Cyclomatic complexity: 2
func Debug
Debug creates debug log on both logrus and span.
func Debug(ctx context.Context, logrus Logrus, args ...interface{}) {
logrus.Debug(args...)
Log(ctx, opentracinglog.String("DEBUG", fmt.Sprint(args...)))
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func Disable
Disable disables the global trace manager.
func Disable() {
enabled.Store(false)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func Enable
Enable sets the global manager to enabled.
func Enable() {
enabled.Store(true)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func Error
func Error(ctx context.Context, logrus Logrus, args ...interface{}) {
logrus.Error(args...)
Log(ctx, opentracinglog.String("ERROR", fmt.Sprint(args...)))
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func Extract
func Extract(tr Tracer, h http.Header) (opentracing.SpanContext, error) {
return tr.Extract(
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(h),
)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func ExtractFromContext
func ExtractFromContext(ctx context.Context, h http.Header) (opentracing.SpanContext, error) {
return Extract(Get(GetServiceID(ctx)), h)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func Get
Get returns a tracer stored on the global trace manager.
func Get(service string) Tracer {
if t, ok := services.Load(service); ok {
return t.(Tracer)
}
return NoopTracer{}
}
Cognitive complexity: 3, Cyclomatic complexity: 2
func GetServiceID
GetServiceID returns service name attched to context returns an empty string if the service name key is not found.
func GetServiceID(ctx context.Context) string {
if v := ctx.Value(serviceID{}); v != nil {
return v.(string)
}
return ""
}
Cognitive complexity: 3, Cyclomatic complexity: 2
func Handle
Handle returns a http.Handler with root opentracting setup. This should be the topmost handler.
func Handle(service string, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
span, req := Root(service, r)
defer span.Finish()
h.ServeHTTP(w, req)
})
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func Info
func Info(ctx context.Context, logrus Logrus, args ...interface{}) {
logrus.Info(args...)
Log(ctx, opentracinglog.String("INFO", fmt.Sprint(args...)))
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func Init
Init returns a tracer for a given name.
func Init(name string, service string, opts map[string]interface{}, logger Logger) (Tracer, error) {
switch name {
case jaeger.Name:
return jaeger.Init(service, opts, logger)
case openzipkin.Name:
return openzipkin.Init(service, opts)
default:
return NoopTracer{}, nil
}
}
Cognitive complexity: 6, Cyclomatic complexity: 4
func Inject
func Inject(service string, span opentracing.Span, h http.Header) error {
tr := Get(service)
return tr.Inject(
span.Context(),
opentracing.HTTPHeaders,
opentracing.HTTPHeadersCarrier(h),
)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func InjectFromContext
func InjectFromContext(ctx context.Context, span opentracing.Span, h http.Header) error {
return Inject(GetServiceID(ctx), span, h)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func IsEnabled
IsEnabled returns true if the global trace manager is enabled.
func IsEnabled() bool {
if v := enabled.Load(); v != nil {
return v.(bool)
}
return false
}
Cognitive complexity: 2, Cyclomatic complexity: 2
func Log
Log tries to check if there is a span in ctx and adds logs fields on the span.
func Log(ctx context.Context, fields ...opentracinglog.Field) {
if span := opentracing.SpanFromContext(ctx); span != nil {
span.LogFields(fields...)
}
}
Cognitive complexity: 2, Cyclomatic complexity: 2
func Root
func Root(service string, r *http.Request) (opentracing.Span, *http.Request) {
tr := Get(service)
mainCtx, err := Extract(tr, r.Header)
tags := opentracing.Tags{
"from_ip": request.RealIP(r),
"method": r.Method,
"endpoint": r.URL.Path,
"raw_url": r.URL.String(),
"size": strconv.Itoa(int(r.ContentLength)),
}
if err != nil {
// TODO log this error?
// We just create a new span here so the log should be a warning.
span, ctx := opentracing.StartSpanFromContextWithTracer(r.Context(),
tr,
service, tags)
return span, r.WithContext(SetServiceID(ctx, service))
}
span, ctx := opentracing.StartSpanFromContextWithTracer(r.Context(),
tr,
service,
opentracing.ChildOf(mainCtx), tags)
return span, r.WithContext(SetServiceID(ctx, service))
}
Cognitive complexity: 3, Cyclomatic complexity: 2
func SetInit
func SetInit(fn InitFunc) {
initializer = fn
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func SetLogger
func SetLogger(log Logger) {
logger = log
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func SetServiceID
SetServiceID returns context with service assigned to it.
func SetServiceID(ctx context.Context, service string) context.Context {
return context.WithValue(ctx, serviceID{}, service)
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func SetupTracing
func SetupTracing(name string, opts map[string]interface{}) {
// We are using empty string as key since we only work with one opentracer at a
// time hence the default.
manager.Store("", Config{
Name: name,
Opts: opts,
})
enabled.Store(true)
}
Cognitive complexity: 2, Cyclomatic complexity: 1
func Span
Span creates a new span for the given ops. If tracing is disabled in this ctx then a noop span is created and the same ctx is returned.
Note that the returned context contains the returned span as active span. So any spans created form the returned context will be children of the returned span.
func Span(ctx context.Context, ops string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
return opentracing.StartSpanFromContextWithTracer(ctx,
Get(GetServiceID(ctx)),
ops, opts...)
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func Warning
func Warning(ctx context.Context, logrus Logrus, args ...interface{}) {
logrus.Warning(args...)
Log(ctx, opentracinglog.String("WARN", fmt.Sprint(args...)))
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func (NoopTracer) Close
Close implements io.Closer interface by doing nothing.
func (n NoopTracer) Close() error {
return nil
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (NoopTracer) Name
func (n NoopTracer) Name() string {
return "NoopTracer"
}
Cognitive complexity: 0, Cyclomatic complexity: 1
func (StdLogger) Error
func (StdLogger) Error(args ...interface{}) {
log.Println("[ERROR] trace: ", fmt.Sprint(args...))
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func (StdLogger) Errorf
func (StdLogger) Errorf(format string, args ...interface{}) {
log.Println("[ERROR] trace: ", fmt.Sprintf(format, args...))
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func (StdLogger) Info
func (StdLogger) Info(args ...interface{}) {
log.Println("[INFO] trace: ", fmt.Sprint(args...))
}
Cognitive complexity: 1, Cyclomatic complexity: 1
func (StdLogger) Infof
func (StdLogger) Infof(format string, args ...interface{}) {
log.Println("[INFO] trace: ", fmt.Sprintf(format, args...))
}
Cognitive complexity: 1, Cyclomatic complexity: 1
Tests
Files: 1. Third party imports: 0. Imports from organisation: 0. Tests: 1. Benchmarks: 0.