Skip to content

Commit

Permalink
grpc: add ability to compile with or without tracing (#6954)
Browse files Browse the repository at this point in the history
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 <chrisko@google.com>
  • Loading branch information
hugelgupf authored Feb 2, 2024
1 parent 84b85ba commit 03e76b3
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 12 deletions.
10 changes: 4 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import (
"sync/atomic"
"time"

"golang.org/x/net/trace"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/encoding"
Expand Down Expand Up @@ -131,7 +129,7 @@ type Server struct {
drain bool
cv *sync.Cond // signaled when connections close for GracefulStop
services map[string]*serviceInfo // service name -> service info
events trace.EventLog
events traceEventLog

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

if s.opts.numServerWorkers > 0 {
Expand Down Expand Up @@ -1734,8 +1732,8 @@ func (s *Server) handleStream(t transport.ServerTransport, stream *transport.Str
ctx = contextWithServer(ctx, s)
var ti *traceInfo
if EnableTracing {
tr := trace.New("grpc.Recv."+methodFamily(stream.Method()), stream.Method())
ctx = trace.NewContext(ctx, tr)
tr := newTrace("grpc.Recv."+methodFamily(stream.Method()), stream.Method())
ctx = newTraceContext(ctx, tr)
ti = &traceInfo{
tr: tr,
firstLine: firstLine{
Expand Down
5 changes: 2 additions & 3 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"sync"
"time"

"golang.org/x/net/trace"
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/encoding"
Expand Down Expand Up @@ -431,7 +430,7 @@ func (cs *clientStream) newAttemptLocked(isTransparent bool) (*csAttempt, error)
var trInfo *traceInfo
if EnableTracing {
trInfo = &traceInfo{
tr: trace.New("grpc.Sent."+methodFamily(method), method),
tr: newTrace("grpc.Sent."+methodFamily(method), method),
firstLine: firstLine{
client: true,
},
Expand All @@ -440,7 +439,7 @@ func (cs *clientStream) newAttemptLocked(isTransparent bool) (*csAttempt, error)
trInfo.firstLine.deadline = time.Until(deadline)
}
trInfo.tr.LazyLog(&trInfo.firstLine, false)
ctx = trace.NewContext(ctx, trInfo.tr)
ctx = newTraceContext(ctx, trInfo.tr)
}

if cs.cc.parsedTarget.URL.Scheme == internal.GRPCResolverSchemeExtraMetadata {
Expand Down
26 changes: 23 additions & 3 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
"strings"
"sync"
"time"

"golang.org/x/net/trace"
)

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

// traceEventLog mirrors golang.org/x/net/trace.EventLog.
//
// It exists in order to avoid importing x/net/trace on grpcnotrace builds.
type traceEventLog interface {
Printf(format string, a ...any)
Errorf(format string, a ...any)
Finish()
}

// traceLog mirrors golang.org/x/net/trace.Trace.
//
// It exists in order to avoid importing x/net/trace on grpcnotrace builds.
type traceLog interface {
LazyLog(x fmt.Stringer, sensitive bool)
LazyPrintf(format string, a ...any)
SetError()
SetRecycler(f func(any))
SetTraceInfo(traceID, spanID uint64)
SetMaxEvents(m int)
Finish()
}

// traceInfo contains tracing information for an RPC.
type traceInfo struct {
tr trace.Trace
tr traceLog
firstLine firstLine
}

Expand Down
52 changes: 52 additions & 0 deletions trace_notrace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//go:build grpcnotrace

/*
*
* Copyright 2024 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package grpc

// grpcnotrace can be used to avoid importing golang.org/x/net/trace, which in
// turn enables binaries using gRPC-Go for dead code elimination, which can
// yield 10-15% improvements in binary size when tracing is not needed.

import (
"context"
"fmt"
)

type notrace struct{}

func (notrace) LazyLog(x fmt.Stringer, sensitive bool) {}
func (notrace) LazyPrintf(format string, a ...any) {}
func (notrace) SetError() {}
func (notrace) SetRecycler(f func(any)) {}
func (notrace) SetTraceInfo(traceID, spanID uint64) {}
func (notrace) SetMaxEvents(m int) {}
func (notrace) Finish() {}

func newTrace(family, title string) traceLog {
return notrace{}
}

func newTraceContext(ctx context.Context, tr traceLog) context.Context {
return ctx
}

func newTraceEventLog(family, title string) traceEventLog {
return nil
}
39 changes: 39 additions & 0 deletions trace_withtrace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//go:build !grpcnotrace

/*
*
* Copyright 2024 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package grpc

import (
"context"

t "golang.org/x/net/trace"
)

func newTrace(family, title string) traceLog {
return t.New(family, title)
}

func newTraceContext(ctx context.Context, tr traceLog) context.Context {
return t.NewContext(ctx, tr)
}

func newTraceEventLog(family, title string) traceEventLog {
return t.NewEventLog(family, title)
}

0 comments on commit 03e76b3

Please sign in to comment.