Skip to content

Commit 03e76b3

Browse files
authored
grpc: add ability to compile with or without tracing (#6954)
golang.org/x/net/trace is the only dependency of gRPC Go that depends -- through html/template and text/template -- on reflect's MethodByName. Binaries with MethodByName are not eligible for Go dead code elimination. As of protobuf v1.32.0 combined with Go 1.22, Go protobufs support compilation with dead code elimination. This commit allows users of gRPC Go to also make use of dead code elimination for smaller binary sizes. Signed-off-by: Chris Koch <[email protected]>
1 parent 84b85ba commit 03e76b3

File tree

5 files changed

+120
-12
lines changed

5 files changed

+120
-12
lines changed

Diff for: server.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ import (
3333
"sync/atomic"
3434
"time"
3535

36-
"golang.org/x/net/trace"
37-
3836
"google.golang.org/grpc/codes"
3937
"google.golang.org/grpc/credentials"
4038
"google.golang.org/grpc/encoding"
@@ -131,7 +129,7 @@ type Server struct {
131129
drain bool
132130
cv *sync.Cond // signaled when connections close for GracefulStop
133131
services map[string]*serviceInfo // service name -> service info
134-
events trace.EventLog
132+
events traceEventLog
135133

136134
quit *grpcsync.Event
137135
done *grpcsync.Event
@@ -670,7 +668,7 @@ func NewServer(opt ...ServerOption) *Server {
670668
s.cv = sync.NewCond(&s.mu)
671669
if EnableTracing {
672670
_, file, line, _ := runtime.Caller(1)
673-
s.events = trace.NewEventLog("grpc.Server", fmt.Sprintf("%s:%d", file, line))
671+
s.events = newTraceEventLog("grpc.Server", fmt.Sprintf("%s:%d", file, line))
674672
}
675673

676674
if s.opts.numServerWorkers > 0 {
@@ -1734,8 +1732,8 @@ func (s *Server) handleStream(t transport.ServerTransport, stream *transport.Str
17341732
ctx = contextWithServer(ctx, s)
17351733
var ti *traceInfo
17361734
if EnableTracing {
1737-
tr := trace.New("grpc.Recv."+methodFamily(stream.Method()), stream.Method())
1738-
ctx = trace.NewContext(ctx, tr)
1735+
tr := newTrace("grpc.Recv."+methodFamily(stream.Method()), stream.Method())
1736+
ctx = newTraceContext(ctx, tr)
17391737
ti = &traceInfo{
17401738
tr: tr,
17411739
firstLine: firstLine{

Diff for: stream.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"sync"
2828
"time"
2929

30-
"golang.org/x/net/trace"
3130
"google.golang.org/grpc/balancer"
3231
"google.golang.org/grpc/codes"
3332
"google.golang.org/grpc/encoding"
@@ -431,7 +430,7 @@ func (cs *clientStream) newAttemptLocked(isTransparent bool) (*csAttempt, error)
431430
var trInfo *traceInfo
432431
if EnableTracing {
433432
trInfo = &traceInfo{
434-
tr: trace.New("grpc.Sent."+methodFamily(method), method),
433+
tr: newTrace("grpc.Sent."+methodFamily(method), method),
435434
firstLine: firstLine{
436435
client: true,
437436
},
@@ -440,7 +439,7 @@ func (cs *clientStream) newAttemptLocked(isTransparent bool) (*csAttempt, error)
440439
trInfo.firstLine.deadline = time.Until(deadline)
441440
}
442441
trInfo.tr.LazyLog(&trInfo.firstLine, false)
443-
ctx = trace.NewContext(ctx, trInfo.tr)
442+
ctx = newTraceContext(ctx, trInfo.tr)
444443
}
445444

446445
if cs.cc.parsedTarget.URL.Scheme == internal.GRPCResolverSchemeExtraMetadata {

Diff for: trace.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import (
2626
"strings"
2727
"sync"
2828
"time"
29-
30-
"golang.org/x/net/trace"
3129
)
3230

3331
// EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
@@ -44,9 +42,31 @@ func methodFamily(m string) string {
4442
return m
4543
}
4644

45+
// traceEventLog mirrors golang.org/x/net/trace.EventLog.
46+
//
47+
// It exists in order to avoid importing x/net/trace on grpcnotrace builds.
48+
type traceEventLog interface {
49+
Printf(format string, a ...any)
50+
Errorf(format string, a ...any)
51+
Finish()
52+
}
53+
54+
// traceLog mirrors golang.org/x/net/trace.Trace.
55+
//
56+
// It exists in order to avoid importing x/net/trace on grpcnotrace builds.
57+
type traceLog interface {
58+
LazyLog(x fmt.Stringer, sensitive bool)
59+
LazyPrintf(format string, a ...any)
60+
SetError()
61+
SetRecycler(f func(any))
62+
SetTraceInfo(traceID, spanID uint64)
63+
SetMaxEvents(m int)
64+
Finish()
65+
}
66+
4767
// traceInfo contains tracing information for an RPC.
4868
type traceInfo struct {
49-
tr trace.Trace
69+
tr traceLog
5070
firstLine firstLine
5171
}
5272

Diff for: trace_notrace.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//go:build grpcnotrace
2+
3+
/*
4+
*
5+
* Copyright 2024 gRPC authors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
package grpc
22+
23+
// grpcnotrace can be used to avoid importing golang.org/x/net/trace, which in
24+
// turn enables binaries using gRPC-Go for dead code elimination, which can
25+
// yield 10-15% improvements in binary size when tracing is not needed.
26+
27+
import (
28+
"context"
29+
"fmt"
30+
)
31+
32+
type notrace struct{}
33+
34+
func (notrace) LazyLog(x fmt.Stringer, sensitive bool) {}
35+
func (notrace) LazyPrintf(format string, a ...any) {}
36+
func (notrace) SetError() {}
37+
func (notrace) SetRecycler(f func(any)) {}
38+
func (notrace) SetTraceInfo(traceID, spanID uint64) {}
39+
func (notrace) SetMaxEvents(m int) {}
40+
func (notrace) Finish() {}
41+
42+
func newTrace(family, title string) traceLog {
43+
return notrace{}
44+
}
45+
46+
func newTraceContext(ctx context.Context, tr traceLog) context.Context {
47+
return ctx
48+
}
49+
50+
func newTraceEventLog(family, title string) traceEventLog {
51+
return nil
52+
}

Diff for: trace_withtrace.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//go:build !grpcnotrace
2+
3+
/*
4+
*
5+
* Copyright 2024 gRPC authors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
package grpc
22+
23+
import (
24+
"context"
25+
26+
t "golang.org/x/net/trace"
27+
)
28+
29+
func newTrace(family, title string) traceLog {
30+
return t.New(family, title)
31+
}
32+
33+
func newTraceContext(ctx context.Context, tr traceLog) context.Context {
34+
return t.NewContext(ctx, tr)
35+
}
36+
37+
func newTraceEventLog(family, title string) traceEventLog {
38+
return t.NewEventLog(family, title)
39+
}

0 commit comments

Comments
 (0)