Skip to content

a-teisseire/gentools

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gentools

Gentools provides a collection of tools that help by generating Go interface implementations that add monitoring, logging and tracing.

Installation

In order to install all tools run:

go get -u github.com/Bo0mer/gentools/cmd/...

Using mongen

Given a path to a package and an interface name, you could generate monitoring implementation of the interface.

package service

import "context"

type Service interface {
    DoWork(context.Context, int, string) (string, error)
}
$ mongen path/to/service Service
Wrote monitoring implementation of "path/to/service.Service" to "path/to/service/servicews/monitoring_service.go"

By default the generated implementation uses go-kit metrics. It can be changed to use opencensus by providing it as a 3rd argument:

$ mongen path/to/service Service opencensus
Wrote monitoring implementation of "path/to/service.Service" to "path/to/service/servicews/monitoring_service.go"

Using monitoring implementation in your program

With Go-Kit

Instantiate monitoring implementations with NewMonitoring{InterfaceName}:

var svc Service = service.New()
svc = servicemws.NewMonitoringService(svc, totalOps, faildOps, opsDuration)

With Opencensus

Usage with opencensus is similar with the addition of the ctxFunc parameter. It can be used to add custom labels at run time.

ctxFunc := func(ctx context.Context) context.Context {
  // modify the context the way you need it
  ctx, _ = tag.New(ctx,
    tag.Insert("service_name", "payments"),
  )
  return ctx
}
var svc Service = service.New()
svc = servicemws.NewMonitoringService(svc, totalOps, faildOps, opsDuration, ctxFunc)

ctxFunc is optional and can be set to nil.

Examples

See cmd/mongen/examples for the files that mongen produces.

Using logen

Given a path to a package and an interface name, you could generate logging implementation of the interface. ATM only error logging is supported.

Using tracegen

Given a path to a package and an interface name, you could generate tracing implementation of the interface. Tracing will be added only to methods that take a context.Context as a first argument. All other methods will be proxied to the original implementation, without any modifications or additions.

Integration with go generate

The best way to integrate the tools within your project is to use the go:generate directive in your code.

$ cat path/to/service/file.go
package service

import "context"

//go:generate mongen . Service
//go:generate tracegen . Service
//go:generate logen . Service

type Service interface {
	DoWork(context.Context, int, string) (string, error)
}
$ go generate ./...
Wrote monitoring implementation of "path/to/service.Service" to "servicemws/monitoring_service.go"
Wrote tracing implementation of "path/to/service.Service" to "servicemws/tracing_service.go"
Wrote logging implementation of "path/to/service.Service" to "servicemws/logging_service.go"

Credits

  • Special thanks to Momchil Atanasov and his awesome project gostub, which code was used as a starting point for developing gentools v2.

About

Generate logging, tracing and monitoring Go interface implementations

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%