Skip to content

Instantly share code, notes, and snippets.

@strangnet
Last active July 19, 2017 07:06
Show Gist options
  • Save strangnet/37ee7c4ac38b982fa7ce310293191a35 to your computer and use it in GitHub Desktop.
Save strangnet/37ee7c4ac38b982fa7ce310293191a35 to your computer and use it in GitHub Desktop.
go-kit with zipkin
import (
kitlog "github.com/go-kit/kit/log"
stdopentracing "github.com/opentracing/opentracing-go"
)
func ServeAPI(ctx context.Context, tracer stdopentracing.Tracer, logger kitlog.Logger, ...) http.Handler {
// ...
var hs health.Service
hs = health.NewService(signalBroker)
hs = health.LoggingMiddleware(kitlog.With(logger, "component", "health"))(hs)
healthEndpoints := health.MakeEndpoints(hs, tracer)
httpLogger := kitlog.With(logger, "component", "http")
mux := http.NewServeMux()
mux.Handle("/health", health.MakeHandler(ctx, healthEndpoints, tracer, httpLogger))
return mux
}
import (
"github.com/go-kit/kit/endpoint"
opentracing "github.com/go-kit/kit/tracing/opentracing"
stdopentracing "github.com/opentracing/opentracing-go"
)
type Endpoints struct {
GetEndpoint endpoint.Endpoint
}
func MakeEndpoints(s Service, tracer stdopentracing.Tracer) Endpoints {
return Endpoints{
GetEndpoint: opentracing.TraceServer(tracer, "GetHealth")(MakeGetHealthEndpoint(s)),
}
}
// ...
func MakeGetHealthEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
status, details, err := s.Get()
return GetHealthResponse{
StatusMessage: status,
Details: details,
}, err
}
}
func main() {
// ...
var tracer stdopentracing.Tracer
{
if zipkinEndpoint != "" {
logger := log.With(logger, "tracer", "zipkinhttp")
// zipkingEndpoint: http://127.0.0.1:9411
collector, err := zipkin.NewHTTPCollector(zipkinEndpoint)
if err != nil {
logger.Log("err", err)
}
defer collector.Close()
tracer, err = zipkin.NewTracer(
zipkin.NewRecorder(collector, true, serviceHostPort, "auth"),
zipkin.ClientServerSameSpan(true),
zipkin.TraceID128Bit(true),
)
if err != nil {
logger.Log("err", err)
}
}
}
// ...
mux := api.ServeAPI(ctx, tracer, logger, ...)
http.Handle("/", mux)
errs := make(chan error, 2)
// Launch http server
go func() {
logger.Log("transport", "http", "address", *httpAddr, "msg", "listening")
errs <- http.ListenAndServe(*httpAddr, nil)
}()
}
import (
kitlog "github.com/go-kit/kit/log"
"github.com/go-kit/kit/tracing/opentracing"
kithttp "github.com/go-kit/kit/transport/http"
stdopentracing "github.com/opentracing/opentracing-go"
)
func MakeHandler(ctx context.Context, e Endpoints, tracer stdopentracing.Tracer, logger kitlog.Logger) http.Handler {
opts := []kithttp.ServerOption{
kithttp.ServerErrorLogger(logger),
kithttp.ServerErrorEncoder(encodeError),
}
getHealthHandler := kithttp.NewServer(
e.GetEndpoint,
decodeGetHealthRequest,
encodeResponse,
append(opts, kithttp.ServerBefore(opentracing.FromHTTPRequest(tracer, "GetHealth", logger)))...,
)
r := mux.NewRouter().StrictSlash(false)
r.Handle("/health", getHealthHandler).Methods(http.MethodGet)
return r
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment