Skip to content

Commit

Permalink
grpc: add ability to compile with or without tracing
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 committed Jan 31, 2024
1 parent a3f5ed6 commit e1690e9
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 12 deletions.
10 changes: 4 additions & 6 deletions server.go
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))

Check warning on line 671 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L671

Added line #L671 was not covered by tests
}

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)

Check warning on line 1736 in server.go

View check run for this annotation

Codecov / codecov/patch

server.go#L1735-L1736

Added lines #L1735 - L1736 were not covered by tests
ti = &traceInfo{
tr: tr,
firstLine: firstLine{
Expand Down
5 changes: 2 additions & 3 deletions stream.go
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),

Check warning on line 433 in stream.go

View check run for this annotation

Codecov / codecov/patch

stream.go#L433

Added line #L433 was not covered by tests
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)

Check warning on line 442 in stream.go

View check run for this annotation

Codecov / codecov/patch

stream.go#L442

Added line #L442 was not covered by tests
}

if cs.cc.parsedTarget.URL.Scheme == internal.GRPCResolverSchemeExtraMetadata {
Expand Down
26 changes: 23 additions & 3 deletions trace.go
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 ...interface{})
Errorf(format string, a ...interface{})
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 ...interface{})
SetError()
SetRecycler(f func(interface{}))
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
@@ -0,0 +1,52 @@
//go:build grpcnotrace

/*
*
* Copyright 2015 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 ...interface{}) {}
func (notrace) SetError() {}
func (notrace) SetRecycler(f func(interface{})) {}
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
@@ -0,0 +1,39 @@
//go:build !grpcnotrace

/*
*
* Copyright 2015 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)

Check warning on line 30 in trace_withtrace.go

View check run for this annotation

Codecov / codecov/patch

trace_withtrace.go#L29-L30

Added lines #L29 - L30 were not covered by tests
}

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

Check warning on line 34 in trace_withtrace.go

View check run for this annotation

Codecov / codecov/patch

trace_withtrace.go#L33-L34

Added lines #L33 - L34 were not covered by tests
}

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

Check warning on line 38 in trace_withtrace.go

View check run for this annotation

Codecov / codecov/patch

trace_withtrace.go#L37-L38

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

0 comments on commit e1690e9

Please sign in to comment.