mirror of https://github.com/grpc/grpc-go.git
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 <chrisko@google.com>
This commit is contained in:
parent
84b85babc0
commit
03e76b3d2a
10
server.go
10
server.go
|
@ -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"
|
||||
|
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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{
|
||||
|
|
|
@ -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"
|
||||
|
@ -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,
|
||||
},
|
||||
|
@ -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 {
|
||||
|
|
26
trace.go
26
trace.go
|
@ -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.
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue