github.com/gojuno/tracegen
go get github.com/gojuno/tracegen

github.com/gojuno/tracegen

Simple tool to generate opentracing decorators

by gojuno

v1.0.0 (see all)
go get github.com/gojuno/tracegen
Readme

Tracegen

Generates interface decorators with opentracing support.

Installation

go get github.com/gojuno/tracegen

Example

type Cache interface {
    Set(ctx context.Context, key, value []byte) error
    Get(ctx context.Context, key []byte) (value []byte, err error)
}
tracegen -i Cache -o example/cache_trace.go ./example

Will generate:

type CacheTracer struct {
    next   Cache
    prefix string
}

func NewCacheTracer(next Cache, prefix string) *CacheTracer {
    return &CacheTracer{
        next:   next,
        prefix: prefix,
    }
}

func (t *CacheTracer) Get(in context.Context, in1 []byte) (out []byte, out1 error) {
    span, in := opentracing.StartSpanFromContext(in, t.prefix+".Cache.Get")
    defer func() {
        if out1 != nil {
            ext.Error.Set(span, true)
            span.LogFields(
                log.String("event", "error"),
                log.String("message", out1.Error()),
            )
        }
        span.Finish()
    }()

    return t.next.Get(in, in1)
}

func (t *CacheTracer) Set(in context.Context, in1 []byte, in2 []byte) (out error) {
    span, in := opentracing.StartSpanFromContext(in, t.prefix+".Cache.Set")
    defer func() {
        if out != nil {
            ext.Error.Set(span, true)
            span.LogFields(
                log.String("event", "error"),
                log.String("message", out.Error()),
            )
        }
        span.Finish()
    }()

    return t.next.Set(in, in1, in2)
}

GitHub Stars

12

LAST COMMIT

3yrs ago

MAINTAINERS

0

CONTRIBUTORS

7

OPEN ISSUES

0

OPEN PRs

0
VersionTagPublished
v1.0.1-0.20191118102949-0cb23d383f65
9mos ago
v1.0.0
2yrs ago
v0.0.0-20180906143659-e899a20dade1
3yrs ago
No alternatives found
No tutorials found
Add a tutorial