Skip to content

Commit 39011e5

Browse files
committed
only server side spans should be recorded
Signed-off-by: Roman Dmytrenko <[email protected]>
1 parent 222d16c commit 39011e5

File tree

7 files changed

+426
-4
lines changed

7 files changed

+426
-4
lines changed

.mockery.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ packages:
4444
interfaces:
4545
Provider: {}
4646
Manager: {}
47+
google.golang.org/grpc/stats:
48+
interfaces:
49+
Handler: {}
50+
config:
51+
pkgname: otel
52+
dir: "./internal/otel/"

cmd/flipt/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"go.flipt.io/flipt/internal/secrets"
3535
"go.flipt.io/flipt/internal/telemetry"
3636
"go.opentelemetry.io/contrib/bridges/otelzap"
37-
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
3837
"go.opentelemetry.io/otel/log/global"
3938
"go.opentelemetry.io/otel/sdk/log"
4039
"go.uber.org/zap"
@@ -457,7 +456,7 @@ func run(ctx context.Context, logger *zap.Logger, cfg *config.Config) error {
457456

458457
// in-process client connection for grpc services
459458
ipch := &inprocgrpc.Channel{}
460-
ipch = ipch.WithStatsHandler(otelgrpc.NewServerHandler())
459+
ipch = ipch.WithStatsHandler(otel.NewInprocStatsHandler())
461460

462461
var grpcOptions []cmd.GRPCServerOption
463462
if forceMigrate {

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,8 @@ require (
546546
replace (
547547
github.com/codahale/hdrhistogram => github.com/HdrHistogram/hdrhistogram-go v0.9.0
548548
github.com/dgrijalva/jwt-go v3.2.0+incompatible => github.com/golang-jwt/jwt/v4 v4.2.0
549+
// replace with fork while changes are in review
550+
// https://github.com/fullstorydev/grpchan/pull/83
549551
github.com/fullstorydev/grpchan => github.com/erka/grpchan v0.0.0-20251014172820-9b4242e94974
550552
)
551553

internal/cmd/grpc.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
rpcanalytics "go.flipt.io/flipt/rpc/v2/analytics"
4747
rpcenv "go.flipt.io/flipt/rpc/v2/environments"
4848
rpcevaluationv2 "go.flipt.io/flipt/rpc/v2/evaluation"
49-
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
5049
opentelemetry "go.opentelemetry.io/otel"
5150
metricsdk "go.opentelemetry.io/otel/sdk/metric"
5251
tracesdk "go.opentelemetry.io/otel/sdk/trace"
@@ -372,7 +371,6 @@ func NewGRPCServer(
372371
grpcOpts := []grpc.ServerOption{
373372
grpc.ChainUnaryInterceptor(unaryInterceptors...),
374373
grpc.ChainStreamInterceptor(streamInterceptors...),
375-
grpc.StatsHandler(otelgrpc.NewServerHandler()),
376374
grpc.KeepaliveParams(keepalive.ServerParameters{
377375
MaxConnectionIdle: cfg.Server.GRPCConnectionMaxIdleTime,
378376
MaxConnectionAge: cfg.Server.GRPCConnectionMaxAge,

internal/otel/stats.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package otel
2+
3+
import (
4+
"context"
5+
6+
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
7+
"google.golang.org/grpc/stats"
8+
)
9+
10+
var _ stats.Handler = (*statsHandler)(nil)
11+
12+
type statsHandler struct {
13+
stats.Handler
14+
}
15+
16+
// HandleRPC processes the RPC stats.
17+
// It ignores client stats to avoid double spans with the same GRPC call.
18+
func (w *statsHandler) HandleRPC(ctx context.Context, s stats.RPCStats) {
19+
if s.IsClient() {
20+
return
21+
}
22+
w.Handler.HandleRPC(ctx, s)
23+
}
24+
25+
// NewInprocStatsHandler creates a new gRPC stats handler for telemetry.
26+
func NewInprocStatsHandler() stats.Handler {
27+
return &statsHandler{Handler: otelgrpc.NewServerHandler()}
28+
}

internal/otel/stats_mock.go

Lines changed: 205 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)