Skip to content

Commit

Permalink
OTEL wiring in grpcserver
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed May 30, 2024
1 parent df7a478 commit 436de0d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
22 changes: 21 additions & 1 deletion baseapp/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"fmt"
"strconv"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"

gogogrpc "github.com/cosmos/gogoproto/grpc"
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
Expand All @@ -18,14 +23,19 @@ import (
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
)

const tracerName = "cosmos-sdk"

// GRPCQueryRouter returns the GRPCQueryRouter of a BaseApp.
func (app *BaseApp) GRPCQueryRouter() *GRPCQueryRouter { return app.grpcQueryRouter }

// RegisterGRPCServer registers gRPC services directly with the gRPC server.
func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server, logQueries bool) {

tracer := otel.Tracer(tracerName)

// Define an interceptor for all gRPC queries: this interceptor will create
// a new sdk.Context, and pass it into the query handler.
interceptor := func(grpcCtx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
interceptor := func(grpcCtx context.Context, req interface{}, grpcInfo *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
// If there's some metadata in the context, retrieve it.
md, ok := metadata.FromIncomingContext(grpcCtx)
if !ok {
Expand Down Expand Up @@ -70,6 +80,16 @@ func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server, logQueries bool)
app.logger.Info("gRPC query received of type: " + fmt.Sprintf("%#v", req))
}

// Extract the existing span context from the incoming request
parentCtx := otel.GetTextMapPropagator().Extract(grpcCtx, propagation.HeaderCarrier(md))

// Start a new span representing the request
// The span ends when the request is complete
grpcCtx, span := tracer.Start(parentCtx, grpcInfo.FullMethod, trace.WithSpanKind(trace.SpanKindServer))
defer span.End()

span.SetAttributes(attribute.String("http.method", grpcInfo.FullMethod))

return handler(grpcCtx, req)
}

Expand Down
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
go 1.20
go 1.21

toolchain go1.21.5

module github.com/cosmos/cosmos-sdk

Expand Down Expand Up @@ -54,9 +56,10 @@ require (
github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.14.0
github.com/stretchr/testify v1.8.4
github.com/stretchr/testify v1.9.0
github.com/tendermint/go-amino v0.16.0
github.com/tidwall/btree v1.6.0
go.opentelemetry.io/otel/trace v1.27.0
golang.org/x/crypto v0.18.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
google.golang.org/genproto/googleapis/api v0.0.0-20230726155614-23370e0ffb3e
Expand Down Expand Up @@ -109,6 +112,8 @@ require (
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
Expand Down Expand Up @@ -168,6 +173,8 @@ require (
github.com/zondax/ledger-go v0.14.0 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.27.0 // indirect
go.opentelemetry.io/otel/metric v1.27.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sync v0.3.0 // indirect
Expand Down
Loading

0 comments on commit 436de0d

Please sign in to comment.