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

feat: OTEL wiring in grpcserver interceptor (DataDog POC) (backport #612) #619

Merged
merged 8 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ run:
linters:
disable-all: true
enable:
- depguard
- dogsled
- exportloopref
- goconst
Expand All @@ -28,12 +27,8 @@ linters:
- govet
- ineffassign
- misspell
- nakedret
- nolintlint
- staticcheck
- revive
- stylecheck
- typecheck
- unconvert
- unused

Expand Down
30 changes: 28 additions & 2 deletions baseapp/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
gogogrpc "github.com/cosmos/gogoproto/grpc"
grpcmiddleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpcrecovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"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"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
Expand All @@ -19,11 +24,14 @@ import (
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
)

const tracerName = "cosmos-sdk"

// RegisterGRPCServer registers gRPC services directly with the gRPC server.
func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) {
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 @@ -64,7 +72,25 @@ func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) {
app.logger.Error("failed to set gRPC header", "err", err)
}

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
}

// Loop through all services and methods, add the interceptor, and register
Expand Down
29 changes: 18 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/chzyer/readline v1.5.1
github.com/cockroachdb/apd/v2 v2.0.2
github.com/cockroachdb/errors v1.11.1
github.com/cometbft/cometbft v0.38.7
github.com/cometbft/cometbft v0.38.8
github.com/cosmos/btcutil v1.0.5
github.com/cosmos/cosmos-db v1.0.2
github.com/cosmos/cosmos-proto v1.0.0-beta.5
Expand Down Expand Up @@ -53,12 +53,15 @@ require (
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
github.com/tendermint/go-amino v0.16.0
golang.org/x/crypto v0.22.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/trace v1.28.0
golang.org/x/crypto v0.24.0
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0
golang.org/x/sync v0.7.0
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.33.0
google.golang.org/genproto/googleapis/api v0.0.0-20240528184218-531527333157
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
gotest.tools/v3 v3.5.1
pgregory.net/rapid v1.1.0
sigs.k8s.io/yaml v1.4.0
Expand Down Expand Up @@ -98,10 +101,12 @@ 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.6.0 // indirect
github.com/go-logr/logr v1.4.2 // 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
github.com/golang/glog v1.2.0 // indirect
github.com/golang/glog v1.2.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/orderedcode v0.0.1 // indirect
Expand All @@ -110,6 +115,7 @@ require (
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-plugin v1.5.2 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
Expand Down Expand Up @@ -149,13 +155,14 @@ require (
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
go.etcd.io/bbolt v1.3.8 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
nhooyr.io/websocket v1.8.6 // indirect
Expand Down
Loading
Loading