Coder Social home page Coder Social logo

tracegen's Introduction

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)
}

tracegen's People

Contributors

hexdigest avatar mkabischev avatar regeda avatar vpbarb avatar ybubnov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tracegen's Issues

Generated code fails lint because public structs and methods aren't commented

Observed
Generated code fails lint because the structs and methods in the template do not have comments, this causes go lint to fail.

Expected
Generated code does not have lint warnings

Recommendation
Use the existing template logic to generate some basic comments:
https://github.com/gojuno/tracegen/blob/master/main.go#L24

// {{ $structName }} provides tracing for a {{ $interfaceName}}

// New{{$structName}} creates a {{$structName}}

// {{$methodName}} is a tracing decorator for {{$methodName}}

Support usage with explicit tracer

Hi,

It would be nice to support an opentracing.Tracer given in constructor instead of using opentracing.StartSpanFromContext() which uses opentracing.GlobalTracer() underneath. This could be an optional feature controlled by an extra flag. I'd be happy to fork the project, add that functionality and make a PR if you would think something like that would be useful.

As an implementation detail, that tracer would be added as an extra field to the generated struct and be used when starting new spans.

Adrian

Support unnamed parameters in interfaces

Go supports unnamed parameters for interfaces. This looks like the following.

type FooService interface {
    Bar(context.Context, string) error
}

When I use tracegen to generate a decorator with an interface with unnamed parameters I get the following

return t.next.Bar(, , )

It would be a nice to have to support this, though the work around is trivial. Also thanks for opensourcing such a wonderful little utility!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.