Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OTEL wiring in grpcserver interceptor (DataDog POC) #612

Merged
merged 17 commits into from
Jul 22, 2024
32 changes: 30 additions & 2 deletions baseapp/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import (
"fmt"
"strconv"

"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
otelcodes "go.opentelemetry.io/otel/codes"
"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 +24,18 @@ 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,7 +80,25 @@ func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server, logQueries bool)
app.logger.Info("gRPC query received of type: " + fmt.Sprintf("%#v", req))
}

return handler(grpcCtx, 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))

resp, err = handler(grpcCtx, req)

if err != nil {
span.SetStatus(otelcodes.Error, err.Error())
} else {
span.SetStatus(otelcodes.Ok, "OK")
}

return resp, err
Comment on lines -73 to +101
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Link

}

// Loop through all services and methods, add the interceptor, and register
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,11 @@ 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 v1.27.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 +113,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 +174,7 @@ 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/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
Loading