Skip to content

Commit

Permalink
OTEL wiring in grpcserver interceptor (DataDog POC) (#612)
Browse files Browse the repository at this point in the history
* OTEL wiring in grpcserver

* updates

* updates

* grpc headers

* updates

* updates

* updates

* go 1.21

* updates

* test go1.21

* update lint go

* go mod for tests

* more updates

* bump linter version

* updates

* remove nakedret

* codeql go version

(cherry picked from commit 5c3a933)

# Conflicts:
#	.github/workflows/lint.yml
#	.golangci.yml
#	baseapp/grpcserver.go
#	go.mod
#	go.sum
#	simapp/go.mod
#	simapp/go.sum
#	tests/go.mod
#	tests/go.sum
  • Loading branch information
p0mvn authored and mergify[bot] committed Jul 23, 2024
1 parent e899ad6 commit 795abb7
Show file tree
Hide file tree
Showing 11 changed files with 1,070 additions and 3 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ jobs:
name: golangci-lint
runs-on: ubuntu-latest
steps:
<<<<<<< HEAD
=======
- uses: actions/setup-go@v3
with:
go-version: "1.21"
check-latest: true
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
<<<<<<< HEAD
go-version: "1.21"
check-latest: true
- uses: technote-space/get-diff-action@v6.1.2
Expand Down Expand Up @@ -47,3 +55,7 @@ jobs:
env:
GIT_DIFF: ${{ env.GIT_DIFF }}
LINT_DIFF: 1
=======
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.54.0
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.20"
go-version: "1.21"
check-latest: true
cache: true
cache-dependency-path: core/go.sum
Expand Down
4 changes: 3 additions & 1 deletion .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,8 +27,11 @@ linters:
- govet
- ineffassign
- misspell
<<<<<<< HEAD
- nakedret
- nolintlint
=======
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
- staticcheck
- revive
- stylecheck
Expand Down
46 changes: 45 additions & 1 deletion baseapp/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"context"
"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 @@ -19,11 +25,23 @@ import (
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
)

<<<<<<< HEAD
// RegisterGRPCServer registers gRPC services directly with the gRPC server.
func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) {
=======
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)

>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
// 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 +82,33 @@ func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) {
app.logger.Error("failed to set gRPC header", "err", err)
}

<<<<<<< HEAD
return handler(grpcCtx, req)
=======
if logQueries {
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))

resp, err = handler(grpcCtx, req)

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

return resp, err
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
}

// Loop through all services and methods, add the interceptor, and register
Expand Down
71 changes: 71 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.4
<<<<<<< HEAD
github.com/google/go-cmp v0.6.0
=======
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
github.com/google/gofuzz v1.2.0
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
Expand All @@ -50,6 +53,7 @@ require (
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
<<<<<<< HEAD
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
github.com/tendermint/go-amino v0.16.0
Expand All @@ -65,6 +69,30 @@ require (
)

require (
=======
github.com/spf13/viper v1.14.0
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/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-20231006140011-7918f672742d
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.0
pgregory.net/rapid v0.5.5
sigs.k8s.io/yaml v1.3.0
)

require (
cloud.google.com/go v0.110.6 // indirect
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/iam v1.1.1 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/DataDog/datadog-go v3.2.0+incompatible // indirect
Expand All @@ -75,6 +103,10 @@ require (
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
<<<<<<< HEAD
=======
github.com/cockroachdb/errors v1.10.0 // indirect
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v1.1.0 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
Expand All @@ -97,14 +129,32 @@ require (
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/go-kit/kit v0.12.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
<<<<<<< HEAD
github.com/go-logfmt/logfmt v0.6.0 // 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/go-logfmt/logfmt v0.5.1 // 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.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/orderedcode v0.0.1 // indirect
<<<<<<< HEAD
=======
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/gax-go/v2 v2.11.0 // indirect
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
Expand Down Expand Up @@ -145,6 +195,7 @@ require (
github.com/spf13/afero v1.11.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d // indirect
<<<<<<< HEAD
github.com/tidwall/btree v1.7.0 // indirect
github.com/zondax/hid v0.9.2 // indirect
github.com/zondax/ledger-go v0.14.3 // indirect
Expand All @@ -156,6 +207,26 @@ require (
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect
=======
github.com/tecbot/gorocksdb v0.0.0-20191217155057-f0fad39f321c // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/zondax/hid v0.9.1 // indirect
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.28.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/oauth2 v0.20.0 // indirect
golang.org/x/sync v0.7.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
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/api v0.126.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240701130421-f6361c86f094 // indirect
>>>>>>> 5c3a9339e (OTEL wiring in grpcserver interceptor (DataDog POC) (#612))
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

0 comments on commit 795abb7

Please sign in to comment.