Last active
September 4, 2018 02:33
-
-
Save masroorhasan/f221798918cdbbdc1eac6b31f502584e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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