Skip to content

Commit

Permalink
Chore: Replace deprecated prometheus grpc middleware (#961)
Browse files Browse the repository at this point in the history
* Chore: Replace deprecated prometheus grpc middleware

* prometheus grpc middleware v1.0.1 and manually merge require blocks in go.mod

* fix grafana comp. check to replace any existing sdk replace in go.mod

* check grafana comp. with go 1.22
  • Loading branch information
marefr authored Apr 25, 2024
1 parent 3bbfa89 commit 897690f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 125 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check-grafana-compatibility.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ jobs:
- uses: actions/setup-go@v5
with:
cache: false
go-version: '~1.22'
check-latest: true
go-version-file: 'grafana-plugin-sdk-go/go.mod'

- name: Check if branch exists in Grafana
working-directory: './grafana'
Expand All @@ -42,7 +42,7 @@ jobs:
- name: Link sdk
working-directory: './grafana'
run: echo 'replace github.com/grafana/grafana-plugin-sdk-go => ../grafana-plugin-sdk-go' >> go.mod
run: go mod edit -replace github.com/grafana/grafana-plugin-sdk-go=../grafana-plugin-sdk-go

- name: Automatic updates
working-directory: './grafana'
Expand Down
18 changes: 8 additions & 10 deletions backend/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import (
"syscall"
"time"

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus"
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -85,7 +84,6 @@ func GRPCServeOpts(opts ServeOpts) grpcplugin.ServeOpts {
// - otel grpc stats handler (see otelgrpc.NewServerHandler)
func grpcServerOptions(serveOpts ServeOpts, customOpts ...grpc.ServerOption) []grpc.ServerOption {
options := defaultGRPCMiddlewares(serveOpts)
options = append(options, grpc.StatsHandler(otelgrpc.NewServerHandler()))
options = append(options, customOpts...)
return options
}
Expand All @@ -94,14 +92,15 @@ func defaultGRPCMiddlewares(opts ServeOpts) []grpc.ServerOption {
if opts.GRPCSettings.MaxReceiveMsgSize <= 0 {
opts.GRPCSettings.MaxReceiveMsgSize = defaultServerMaxReceiveMessageSize
}

srvMetrics := grpc_prometheus.NewServerMetrics(grpc_prometheus.WithServerHandlingTimeHistogram())
prometheus.MustRegister(srvMetrics)

grpcMiddlewares := []grpc.ServerOption{
grpc.MaxRecvMsgSize(opts.GRPCSettings.MaxReceiveMsgSize),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpc_prometheus.StreamServerInterceptor,
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_prometheus.UnaryServerInterceptor,
)),
grpc.StatsHandler(otelgrpc.NewServerHandler()),
grpc.UnaryInterceptor(srvMetrics.UnaryServerInterceptor()),
grpc.StreamInterceptor(srvMetrics.StreamServerInterceptor()),
}
if opts.GRPCSettings.MaxSendMsgSize > 0 {
grpcMiddlewares = append([]grpc.ServerOption{grpc.MaxSendMsgSize(opts.GRPCSettings.MaxSendMsgSize)}, grpcMiddlewares...)
Expand All @@ -111,7 +110,6 @@ func defaultGRPCMiddlewares(opts ServeOpts) []grpc.ServerOption {

// Serve starts serving the plugin over gRPC.
func Serve(opts ServeOpts) error {
grpc_prometheus.EnableHandlingTimeHistogram()
pluginOpts := GRPCServeOpts(opts)
pluginOpts.GRPCServer = func(customOptions []grpc.ServerOption) *grpc.Server {
return grpc.NewServer(grpcServerOptions(opts, customOptions...)...)
Expand Down
45 changes: 21 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,25 @@ go 1.21
replace github.com/getkin/kin-openapi => github.com/getkin/kin-openapi v0.120.0

require (
github.com/apache/arrow/go/v15 v15.0.2
github.com/cheekybits/genny v1.0.0
github.com/golang/protobuf v1.5.4 // indirect
github.com/chromedp/cdproto v0.0.0-20220208224320-6efb837e6bc2
github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027
github.com/getkin/kin-openapi v0.120.0
github.com/google/go-cmp v0.6.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/google/uuid v1.6.0
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.1
github.com/hashicorp/go-hclog v1.6.3
github.com/hashicorp/go-plugin v1.6.0
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/invopop/jsonschema v0.12.0 // for schema codgen+extraction
github.com/json-iterator/go v1.1.12
github.com/magefile/mage v1.15.0
github.com/mattetti/filebuffer v1.0.1
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/reflectwalk v1.0.2
github.com/olekukonko/tablewriter v0.0.5
github.com/prometheus/client_golang v1.18.0
github.com/prometheus/common v0.46.0
github.com/prometheus/client_golang v1.19.0
github.com/prometheus/common v0.53.0
github.com/stretchr/testify v1.9.0
golang.org/x/sys v0.18.0
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.33.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/apache/arrow/go/v15 v15.0.2
github.com/chromedp/cdproto v0.0.0-20220208224320-6efb837e6bc2
github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027
github.com/getkin/kin-openapi v0.120.0
github.com/google/uuid v1.6.0
github.com/unknwon/bra v0.0.0-20200517080246-1e3013ecaff8
github.com/urfave/cli v1.22.14
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0
Expand All @@ -48,9 +37,12 @@ require (
go.opentelemetry.io/otel/sdk v1.24.0
go.opentelemetry.io/otel/trace v1.24.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/net v0.23.0
golang.org/x/net v0.24.0
golang.org/x/oauth2 v0.18.0
golang.org/x/sys v0.19.0
golang.org/x/text v0.14.0
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.33.0
k8s.io/kube-openapi v0.0.0-20240220201932-37d671a357a5 // @grafana/grafana-app-platform-squad
)

Expand All @@ -61,7 +53,7 @@ require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/elazarl/goproxy/ext v0.0.0-20220115173737-adb46da277ac // indirect
Expand All @@ -74,11 +66,14 @@ require (
github.com/go-openapi/swag v0.22.4 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/flatbuffers v23.5.26+incompatible // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.0 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.16.7 // indirect
Expand All @@ -87,15 +82,16 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/procfs v0.14.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/unknwon/com v1.0.1 // indirect
Expand All @@ -110,7 +106,8 @@ require (
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240415180920-8c6c420018be // indirect
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
)
Loading

0 comments on commit 897690f

Please sign in to comment.