Skip to content

Instantly share code, notes, and snippets.

@masroorhasan
Last active September 4, 2018 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masroorhasan/f221798918cdbbdc1eac6b31f502584e to your computer and use it in GitHub Desktop.
Save masroorhasan/f221798918cdbbdc1eac6b31f502584e to your computer and use it in GitHub Desktop.
package grpc_server
import (
"github.com/opentracing/opentracing-go"
"github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
"github.com/grpc-ecosystem/go-grpc-middleware"
"google.golang.org/grpc"
"github.com/masroorhasan/myapp/tracer"
)
func NewServer() (*grpc.Server, error) {
// initialize tracer
tracer, closer, err := tracer.NewTracer()
defer closer.Close()
if err != nil {
return &grpc.Server{}, err
}
opentracing.SetGlobalTracer(tracer)
// initialize grpc server with chained interceptors
s := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
// add opentracing stream interceptor to chain
grpc_opentracing.StreamServerInterceptor(grpc_opentracing.WithTracer(tracer)),
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
// add opentracing unary interceptor to chain
grpc_opentracing.UnaryServerInterceptor(grpc_opentracing.WithTracer(tracer)),
)),
)
return s, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment