Skip to content

stats/opentelemetry: separate out interceptors for tracing and metrics #8063

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

Merged
merged 67 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
7ddbe46
replace dial with newclient
janardhankrishna-sai Dec 23, 2024
0486355
Merge branch 'master' of https://github.com/janardhanvissa/grpc-go
janardhankrishna-sai Jan 1, 2025
53fa9cd
Merge branch 'grpc:master' into master
janardhanvissa Jan 8, 2025
4435b8a
Merge branch 'grpc:master' into master
janardhanvissa Jan 13, 2025
a413555
Merge branch 'grpc:master' into master
janardhanvissa Jan 20, 2025
4e203c3
Merge branch 'grpc:master' into master
janardhanvissa Jan 30, 2025
e9ad552
Merge branch 'grpc:master' into master
janardhanvissa Jan 30, 2025
71804b4
refactor tracing and metrics interceptors separately
janardhankrishna-sai Feb 3, 2025
4b3bd26
adding opentelemetry tracing for client-server
janardhankrishna-sai Jan 30, 2025
69df069
fixing vet issues
janardhankrishna-sai Feb 3, 2025
770f430
reverting newclient changes and fixing vet issues
janardhankrishna-sai Feb 3, 2025
b97a3ca
reverting otel trace for client/server changes
janardhankrishna-sai Feb 3, 2025
c89f3c9
fixing vet issue
janardhankrishna-sai Feb 3, 2025
3f07e48
renaming receiver name
janardhankrishna-sai Feb 5, 2025
5e8a4a5
unused param fix
janardhankrishna-sai Feb 5, 2025
76e422a
fixing vet issue
janardhankrishna-sai Feb 5, 2025
8fa0b03
refactor client interceptor separately for traces and metrics
janardhankrishna-sai Feb 12, 2025
1f41a49
moving tracing code to client_tracing.go file
janardhankrishna-sai Feb 12, 2025
d74c61d
revert previous commit
janardhankrishna-sai Feb 12, 2025
170eef6
adding separate interceptors for traces and metrics of server
janardhankrishna-sai Feb 17, 2025
7f5f539
separating HandleRPC interceptors of traces and metrics
janardhankrishna-sai Feb 17, 2025
50999a0
updating client and server metrics
janardhankrishna-sai Feb 18, 2025
68b8966
removing metrics code from tracingstatshandler and unused parameters
janardhankrishna-sai Feb 19, 2025
8b56f0f
addressing review comments
janardhankrishna-sai Feb 24, 2025
ac6080f
addressing review comments
janardhankrishna-sai Feb 25, 2025
ed5506c
Merge branch 'master' into refactor-tracing-metrics
janardhanvissa Feb 25, 2025
be72377
updating go.sum
janardhankrishna-sai Feb 25, 2025
a5ed115
fixing linter issue
janardhankrishna-sai Feb 25, 2025
f243b43
addressing review comments for client
janardhankrishna-sai Mar 7, 2025
efb738f
addressing review comments for client
janardhankrishna-sai Mar 7, 2025
7b97c65
Merge branch 'master' into refactor-tracing-metrics
janardhanvissa Mar 7, 2025
e8b0180
addressing review comments for server
janardhankrishna-sai Mar 7, 2025
f70274b
fixing vet issue
janardhankrishna-sai Mar 7, 2025
5e607c5
updating newServerStatsHandler
janardhankrishna-sai Mar 7, 2025
a850532
updating var name
janardhankrishna-sai Mar 7, 2025
0f39f8d
moving tracing code to a separate file and adding comments
janardhankrishna-sai Mar 10, 2025
2a914e3
removing unused isMetricsEnabled()
janardhankrishna-sai Mar 10, 2025
05b4278
addressing review comments and updating doc string
janardhankrishna-sai Mar 10, 2025
105efe9
addressing review comments
janardhankrishna-sai Mar 10, 2025
3d78d59
creating helper func for rpcinfo
janardhankrishna-sai Mar 10, 2025
44fce2d
addressing review comments
janardhankrishna-sai Mar 13, 2025
e5f50b7
updating logger
janardhankrishna-sai Mar 13, 2025
ffcc0fa
Merge branch 'master' into refactor-tracing-metrics
janardhanvissa Mar 13, 2025
67e5b24
update unaryInterceptor
janardhankrishna-sai Mar 13, 2025
8cf7f15
revert doc string
janardhankrishna-sai Mar 13, 2025
7949501
fixing vet issue
janardhankrishna-sai Mar 13, 2025
ad22a22
update clientStatsHandler unaryInterceptor
janardhankrishna-sai Mar 13, 2025
dd179fb
updating verbosity level and removing redundant code
janardhankrishna-sai Mar 13, 2025
3201805
reverting logger.Info from logger.Error
janardhankrishna-sai Mar 13, 2025
a3915ef
Update server_metrics.go
janardhanvissa Mar 13, 2025
1ff5159
addressing review comments
janardhankrishna-sai Mar 18, 2025
4133dd3
updating the comment
janardhankrishna-sai Mar 18, 2025
f8e0cac
addressing review comments
janardhankrishna-sai Mar 20, 2025
0b89cf6
fixing vet issue
janardhankrishna-sai Mar 20, 2025
42c08a0
fixing linter issue
janardhankrishna-sai Mar 20, 2025
5bb48b3
addressing review comments
janardhankrishna-sai Mar 25, 2025
fb8025d
addressing review comments
janardhankrishna-sai Mar 26, 2025
cc39d1a
addressing review comments
janardhankrishna-sai Apr 1, 2025
eb8321f
Initializes ri to &rpcInfo{} when it's nil, separating the initializa…
janardhankrishna-sai Apr 2, 2025
c858ed2
Merge branch 'master' into refactor-tracing-metrics
janardhanvissa Apr 4, 2025
5c57489
fixing vet issue
janardhankrishna-sai Apr 4, 2025
cc12a84
addressing review comments
janardhankrishna-sai Apr 4, 2025
aae5419
Merge branch 'master' into refactor-tracing-metrics
janardhanvissa Apr 17, 2025
18a9c18
fixing merge conflicts
janardhankrishna-sai Apr 17, 2025
130ca22
addressing review comments
janardhankrishna-sai Apr 17, 2025
397abf0
addressing review comments
janardhankrishna-sai May 5, 2025
f4b4fd6
update client_metrics.go
janardhankrishna-sai May 5, 2025
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
32 changes: 15 additions & 17 deletions stats/opentelemetry/client_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
"google.golang.org/grpc/status"
)

type clientStatsHandler struct {
type clientMetricsHandler struct {
estats.MetricsRecorder
options Options
clientMetrics clientMetrics
}

func (h *clientStatsHandler) initializeMetrics() {
func (h *clientMetricsHandler) initializeMetrics() {
// Will set no metrics to record, logically making this stats handler a
// no-op.
if h.options.MetricsOptions.MeterProvider == nil {
Expand Down Expand Up @@ -67,12 +67,12 @@
rm.registerMetrics(metrics, meter)
}

func (h *clientStatsHandler) unaryInterceptor(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
func (h *clientMetricsHandler) unaryInterceptor(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ci := getCallInfo(ctx)
if ci == nil {
if logger.V(2) {
logger.Info("Creating new CallInfo since its not present in context in clientStatsHandler unaryInterceptor")
}

Check warning on line 75 in stats/opentelemetry/client_metrics.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/client_metrics.go#L74-L75

Added lines #L74 - L75 were not covered by tests
ci = &callInfo{
target: cc.CanonicalTarget(),
method: determineMethod(method, opts...),
Expand Down Expand Up @@ -107,12 +107,12 @@
return "other"
}

func (h *clientStatsHandler) streamInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
func (h *clientMetricsHandler) streamInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ci := getCallInfo(ctx)
if ci == nil {
if logger.V(2) {
logger.Info("Creating new CallInfo since its not present in context in clientStatsHandler streamInterceptor")
}

Check warning on line 115 in stats/opentelemetry/client_metrics.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/client_metrics.go#L114-L115

Added lines #L114 - L115 were not covered by tests
ci = &callInfo{
target: cc.CanonicalTarget(),
method: determineMethod(method, opts...),
Expand All @@ -138,7 +138,7 @@
}

// perCallMetrics records per call metrics for both unary and stream calls.
func (h *clientStatsHandler) perCallMetrics(ctx context.Context, err error, startTime time.Time, ci *callInfo) {
func (h *clientMetricsHandler) perCallMetrics(ctx context.Context, err error, startTime time.Time, ci *callInfo) {
callLatency := float64(time.Since(startTime)) / float64(time.Second)
attrs := otelmetric.WithAttributeSet(otelattribute.NewSet(
otelattribute.String("grpc.method", ci.method),
Expand All @@ -149,15 +149,15 @@
}

// TagConn exists to satisfy stats.Handler.
func (h *clientStatsHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
func (h *clientMetricsHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
return ctx
}

// HandleConn exists to satisfy stats.Handler.
func (h *clientStatsHandler) HandleConn(context.Context, stats.ConnStats) {}
func (h *clientMetricsHandler) HandleConn(context.Context, stats.ConnStats) {}

// TagRPC implements per RPC attempt context management for metrics.
func (h *clientStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
func (h *clientMetricsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
// Numerous stats handlers can be used for the same channel. The cluster
// impl balancer which writes to this will only write once, thus have this
// stats handler's per attempt scoped context point to the same optional
Expand All @@ -175,21 +175,19 @@
ctx = istats.SetLabels(ctx, labels)
}
ri := getRPCInfo(ctx)
var ai *attemptInfo
if ri == nil {
ai = &attemptInfo{}
} else {
ai = ri.ai
ri = &rpcInfo{ai: &attemptInfo{}}
}
ai := ri.ai
ai.startTime = time.Now()
ai.xdsLabels = labels.TelemetryLabels
ai.method = removeLeadingSlash(info.FullMethodName)

return setRPCInfo(ctx, &rpcInfo{ai: ai})
return setRPCInfo(ctx, ri)
}

// HandleRPC handles per RPC stats implementation.
func (h *clientStatsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
func (h *clientMetricsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
ri := getRPCInfo(ctx)
if ri == nil {
logger.Error("ctx passed into client side stats handler metrics event handling has no client attempt data present")
Expand All @@ -198,7 +196,7 @@
h.processRPCEvent(ctx, rs, ri.ai)
}

func (h *clientStatsHandler) processRPCEvent(ctx context.Context, s stats.RPCStats, ai *attemptInfo) {
func (h *clientMetricsHandler) processRPCEvent(ctx context.Context, s stats.RPCStats, ai *attemptInfo) {
switch st := s.(type) {
case *stats.Begin:
ci := getCallInfo(ctx)
Expand Down Expand Up @@ -226,7 +224,7 @@
}
}

func (h *clientStatsHandler) setLabelsFromPluginOption(ai *attemptInfo, incomingMetadata metadata.MD) {
func (h *clientMetricsHandler) setLabelsFromPluginOption(ai *attemptInfo, incomingMetadata metadata.MD) {
if ai.pluginOptionLabels == nil && h.options.MetricsOptions.pluginOption != nil {
labels := h.options.MetricsOptions.pluginOption.GetLabels(incomingMetadata)
if labels == nil {
Expand All @@ -236,7 +234,7 @@
}
}

func (h *clientStatsHandler) processRPCEnd(ctx context.Context, ai *attemptInfo, e *stats.End) {
func (h *clientMetricsHandler) processRPCEnd(ctx context.Context, ai *attemptInfo, e *stats.End) {
ci := getCallInfo(ctx)
if ci == nil {
logger.Error("ctx passed into client side stats handler metrics event handling has no metrics data present")
Expand Down
10 changes: 5 additions & 5 deletions stats/opentelemetry/client_tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,54 +38,54 @@

func (h *clientTracingHandler) initializeTraces() {
if h.options.TraceOptions.TracerProvider == nil {
log.Printf("TracerProvider is not provided in client TraceOptions")
return
}

Check warning on line 43 in stats/opentelemetry/client_tracing.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/client_tracing.go#L41-L43

Added lines #L41 - L43 were not covered by tests
}

func (h *clientTracingHandler) unaryInterceptor(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ci := getCallInfo(ctx)
if ci == nil {
if logger.V(2) {
logger.Info("Creating new CallInfo since its not present in context in clientTracingHandler unaryInterceptor")
}
ci = &callInfo{
target: cc.CanonicalTarget(),
method: determineMethod(method, opts...),
}
ctx = setCallInfo(ctx, ci)

Check warning on line 56 in stats/opentelemetry/client_tracing.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/client_tracing.go#L49-L56

Added lines #L49 - L56 were not covered by tests
}

var span trace.Span
ctx, span = h.createCallTraceSpan(ctx, method)
err := invoker(ctx, method, req, reply, cc, opts...)
h.perCallTraces(err, span)
h.finishTrace(err, span)
return err
}

func (h *clientTracingHandler) streamInterceptor(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
ci := getCallInfo(ctx)
if ci == nil {
if logger.V(2) {
logger.Info("Creating new CallInfo since its not present in context in clientTracingHandler streamInterceptor")
}
ci = &callInfo{
target: cc.CanonicalTarget(),
method: determineMethod(method, opts...),
}
ctx = setCallInfo(ctx, ci)

Check warning on line 76 in stats/opentelemetry/client_tracing.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/client_tracing.go#L69-L76

Added lines #L69 - L76 were not covered by tests
}

var span trace.Span
ctx, span = h.createCallTraceSpan(ctx, method)
callback := func(err error) { h.perCallTraces(err, span) }
callback := func(err error) { h.finishTrace(err, span) }
opts = append([]grpc.CallOption{grpc.OnFinish(callback)}, opts...)
return streamer(ctx, desc, cc, method, opts...)
}

// perCallTraces sets the span status based on the RPC result and ends the span.
// finishTrace sets the span status based on the RPC result and ends the span.
// It is used to finalize tracing for both unary and streaming calls.
func (h *clientTracingHandler) perCallTraces(err error, ts trace.Span) {
func (h *clientTracingHandler) finishTrace(err error, ts trace.Span) {
s := status.Convert(err)
if s.Code() == grpccodes.OK {
ts.SetStatus(otelcodes.Ok, s.Message())
Expand Down Expand Up @@ -130,8 +130,8 @@
func (h *clientTracingHandler) TagRPC(ctx context.Context, _ *stats.RPCTagInfo) context.Context {
ri := getRPCInfo(ctx)
var ai *attemptInfo
if ri == nil {
if ri.ai == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think here also we can't assume ri is not nil if the order of stats handlers changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

ai = &attemptInfo{}

Check warning on line 134 in stats/opentelemetry/client_tracing.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/client_tracing.go#L134

Added line #L134 was not covered by tests
} else {
ai = ri.ai
}
Expand All @@ -143,8 +143,8 @@
func (h *clientTracingHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
ri := getRPCInfo(ctx)
if ri == nil {
logger.Error("ctx passed into client side tracing handler trace event handling has no client attempt data present")
return
}

Check warning on line 148 in stats/opentelemetry/client_tracing.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/client_tracing.go#L146-L148

Added lines #L146 - L148 were not covered by tests
populateSpan(rs, ri.ai)
}
4 changes: 2 additions & 2 deletions stats/opentelemetry/metricsregistry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ type metricsRecorderForTest interface {
}

func newClientStatsHandler(options MetricsOptions) metricsRecorderForTest {
return &clientStatsHandler{options: Options{MetricsOptions: options}}
return &clientMetricsHandler{options: Options{MetricsOptions: options}}
}

func newServerStatsHandler(options MetricsOptions) metricsRecorderForTest {
return &serverStatsHandler{options: Options{MetricsOptions: options}}
return &serverMetricsHandler{options: Options{MetricsOptions: options}}
}

// TestMetricsRegistryMetrics tests the OpenTelemetry behavior with respect to
Expand Down
56 changes: 40 additions & 16 deletions stats/opentelemetry/opentelemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ type Options struct {
TraceOptions experimental.TraceOptions
}

func (o *Options) isMetricsEnabled() bool {
return o.MetricsOptions.MeterProvider != nil
}

func (o *Options) isTracingEnabled() bool {
return o.TraceOptions.TracerProvider != nil
}
Expand Down Expand Up @@ -117,15 +121,31 @@ type MetricsOptions struct {
// For the traces supported by this instrumentation code, provide an
// implementation of a TextMapPropagator and OpenTelemetry TracerProvider.
func DialOption(o Options) grpc.DialOption {
csh := &clientStatsHandler{options: o}
csh.initializeMetrics()
do := joinDialOptions(grpc.WithChainUnaryInterceptor(csh.unaryInterceptor), grpc.WithChainStreamInterceptor(csh.streamInterceptor), grpc.WithStatsHandler(csh))
if !o.isTracingEnabled() {
return do
var unaryInterceptors []grpc.UnaryClientInterceptor
var streamInterceptors []grpc.StreamClientInterceptor
var do []grpc.DialOption

if o.isMetricsEnabled() {
metricsHandler := &clientMetricsHandler{options: o}
metricsHandler.initializeMetrics()
unaryInterceptors = append(unaryInterceptors, metricsHandler.unaryInterceptor)
streamInterceptors = append(streamInterceptors, metricsHandler.streamInterceptor)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is changing the current order. Let's avoid that. Keep only 2 variables metricsInterceptors and tracesInterceptors and add metricsInterceptors before traces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

do = append(do, grpc.WithStatsHandler(metricsHandler))
}
if o.isTracingEnabled() {
tracingHandler := &clientTracingHandler{options: o}
tracingHandler.initializeTraces()
unaryInterceptors = append(unaryInterceptors, tracingHandler.unaryInterceptor)
streamInterceptors = append(streamInterceptors, tracingHandler.streamInterceptor)
do = append(do, grpc.WithStatsHandler(tracingHandler))
}
if len(unaryInterceptors) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

these ifs will change to metrics and traces

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

do = append(do, grpc.WithChainUnaryInterceptor(unaryInterceptors...))
}
tracingHandler := &clientTracingHandler{options: o}
tracingHandler.initializeTraces()
return joinDialOptions(do, grpc.WithChainUnaryInterceptor(tracingHandler.unaryInterceptor), grpc.WithChainStreamInterceptor(tracingHandler.streamInterceptor), grpc.WithStatsHandler(tracingHandler))
if len(streamInterceptors) > 0 {
do = append(do, grpc.WithChainStreamInterceptor(streamInterceptors...))
}
return joinDialOptions(do...)
}

var joinServerOptions = internal.JoinServerOptions.(func(...grpc.ServerOption) grpc.ServerOption)
Expand All @@ -146,15 +166,19 @@ var joinServerOptions = internal.JoinServerOptions.(func(...grpc.ServerOption) g
// For the traces supported by this instrumentation code, provide an
// implementation of a TextMapPropagator and OpenTelemetry TracerProvider.
func ServerOption(o Options) grpc.ServerOption {
ssh := &serverStatsHandler{options: o}
ssh.initializeMetrics()
so := joinServerOptions(grpc.ChainUnaryInterceptor(ssh.unaryInterceptor), grpc.ChainStreamInterceptor(ssh.streamInterceptor), grpc.StatsHandler(ssh))
if !o.isTracingEnabled() {
return so
var so []grpc.ServerOption

if o.isMetricsEnabled() {
metricsHandler := &serverMetricsHandler{options: o}
metricsHandler.initializeMetrics()
so = append(so, grpc.ChainUnaryInterceptor(metricsHandler.unaryInterceptor), grpc.ChainStreamInterceptor(metricsHandler.streamInterceptor), grpc.StatsHandler(metricsHandler))
}
if o.isTracingEnabled() {
tracingHandler := &serverTracingHandler{options: o}
tracingHandler.initializeTraces()
so = append(so, grpc.StatsHandler(tracingHandler))
}
tracingHandler := &serverTracingHandler{options: o}
tracingHandler.initializeTraces()
return joinServerOptions(so, grpc.ChainUnaryInterceptor(tracingHandler.unaryInterceptor), grpc.ChainStreamInterceptor(tracingHandler.streamInterceptor), grpc.StatsHandler(tracingHandler))
return joinServerOptions(so...)
}

// callInfo is information pertaining to the lifespan of the RPC client side.
Expand Down
26 changes: 12 additions & 14 deletions stats/opentelemetry/server_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import (
"google.golang.org/grpc/status"
)

type serverStatsHandler struct {
type serverMetricsHandler struct {
estats.MetricsRecorder
options Options
serverMetrics serverMetrics
}

func (h *serverStatsHandler) initializeMetrics() {
func (h *serverMetricsHandler) initializeMetrics() {
// Will set no metrics to record, logically making this stats handler a
// no-op.
if h.options.MetricsOptions.MeterProvider == nil {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (s *attachLabelsTransportStream) SendHeader(md metadata.MD) error {
return s.ServerTransportStream.SendHeader(md)
}

func (h *serverStatsHandler) unaryInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
func (h *serverMetricsHandler) unaryInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
var metadataExchangeLabels metadata.MD
if h.options.MetricsOptions.pluginOption != nil {
metadataExchangeLabels = h.options.MetricsOptions.pluginOption.GetMetadata()
Expand Down Expand Up @@ -151,7 +151,7 @@ func (s *attachLabelsStream) SendMsg(m any) error {
return s.ServerStream.SendMsg(m)
}

func (h *serverStatsHandler) streamInterceptor(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
func (h *serverMetricsHandler) streamInterceptor(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
var metadataExchangeLabels metadata.MD
if h.options.MetricsOptions.pluginOption != nil {
metadataExchangeLabels = h.options.MetricsOptions.pluginOption.GetMetadata()
Expand All @@ -171,15 +171,15 @@ func (h *serverStatsHandler) streamInterceptor(srv any, ss grpc.ServerStream, _
}

// TagConn exists to satisfy stats.Handler.
func (h *serverStatsHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
func (h *serverMetricsHandler) TagConn(ctx context.Context, _ *stats.ConnTagInfo) context.Context {
return ctx
}

// HandleConn exists to satisfy stats.Handler.
func (h *serverStatsHandler) HandleConn(context.Context, stats.ConnStats) {}
func (h *serverMetricsHandler) HandleConn(context.Context, stats.ConnStats) {}

// TagRPC implements per RPC context management for metrics.
func (h *serverStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
func (h *serverMetricsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
method := info.FullMethodName
if h.options.MetricsOptions.MethodAttributeFilter != nil {
if !h.options.MetricsOptions.MethodAttributeFilter(method) {
Expand All @@ -197,20 +197,18 @@ func (h *serverStatsHandler) TagRPC(ctx context.Context, info *stats.RPCTagInfo)
}
}
ri := getRPCInfo(ctx)
var ai *attemptInfo
if ri == nil {
ai = &attemptInfo{}
} else {
ai = ri.ai
ri = &rpcInfo{ai: &attemptInfo{}}
}
ai := ri.ai
ai.startTime = time.Now()
ai.method = removeLeadingSlash(method)

return setRPCInfo(ctx, &rpcInfo{ai: ai})
}

// HandleRPC handles per RPC stats implementation.
func (h *serverStatsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
func (h *serverMetricsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
ri := getRPCInfo(ctx)
if ri == nil {
logger.Error("ctx passed into server side stats handler metrics event handling has no server call data present")
Expand All @@ -219,7 +217,7 @@ func (h *serverStatsHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
h.processRPCData(ctx, rs, ri.ai)
}

func (h *serverStatsHandler) processRPCData(ctx context.Context, s stats.RPCStats, ai *attemptInfo) {
func (h *serverMetricsHandler) processRPCData(ctx context.Context, s stats.RPCStats, ai *attemptInfo) {
switch st := s.(type) {
case *stats.InHeader:
if ai.pluginOptionLabels == nil && h.options.MetricsOptions.pluginOption != nil {
Expand All @@ -243,7 +241,7 @@ func (h *serverStatsHandler) processRPCData(ctx context.Context, s stats.RPCStat
}
}

func (h *serverStatsHandler) processRPCEnd(ctx context.Context, ai *attemptInfo, e *stats.End) {
func (h *serverMetricsHandler) processRPCEnd(ctx context.Context, ai *attemptInfo, e *stats.End) {
latency := float64(time.Since(ai.startTime)) / float64(time.Second)
st := "OK"
if e.Error != nil {
Expand Down
10 changes: 1 addition & 9 deletions stats/opentelemetry/server_tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,17 @@

func (h *serverTracingHandler) initializeTraces() {
if h.options.TraceOptions.TracerProvider == nil {
log.Printf("TracerProvider is not provided in server TraceOptions")
return
}

Check warning on line 38 in stats/opentelemetry/server_tracing.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/server_tracing.go#L36-L38

Added lines #L36 - L38 were not covered by tests
}

func (h *serverTracingHandler) unaryInterceptor(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
return handler(ctx, req)
}

func (h *serverTracingHandler) streamInterceptor(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
return handler(srv, ss)
}

// TagRPC implements per RPC attempt context management for traces.
func (h *serverTracingHandler) TagRPC(ctx context.Context, _ *stats.RPCTagInfo) context.Context {
ri := getRPCInfo(ctx)
var ai *attemptInfo
if ri == nil {
if ri.ai == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

same here. We can't assume ri to be not nil here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

ai = &attemptInfo{}

Check warning on line 46 in stats/opentelemetry/server_tracing.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/server_tracing.go#L46

Added line #L46 was not covered by tests
} else {
ai = ri.ai
}
Expand Down Expand Up @@ -83,9 +75,9 @@
func (h *serverTracingHandler) HandleRPC(ctx context.Context, rs stats.RPCStats) {
ri := getRPCInfo(ctx)
if ri == nil {
logger.Error("ctx passed into server side tracing handler trace event handling has no server call data present")
return
}

Check warning on line 80 in stats/opentelemetry/server_tracing.go

View check run for this annotation

Codecov / codecov/patch

stats/opentelemetry/server_tracing.go#L78-L80

Added lines #L78 - L80 were not covered by tests
populateSpan(rs, ri.ai)
}

Expand Down
Loading