Skip to content

Commit

Permalink
[#107] size check before proto buffer encoding for big exception meta…
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
dwkang committed Jan 9, 2025
1 parent 57d8110 commit efa6d37
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ func (span *span) addCauserCallStack(err error, eid int64) {
if !ok {
break
}
if !span.canAddErrorChain() {
break
}

e = c.Cause()
if t := span.findError(e); t == nil {
Expand Down
9 changes: 8 additions & 1 deletion grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,19 @@ func (agentGrpc *agentGrpc) sendSqlUidMetadataWithRetry(sqlUid []byte, sql strin
}

func (agentGrpc *agentGrpc) sendExceptionMetadata(ctx context.Context, in *pb.PExceptionMetaData) error {
// size check before proto buffer encoding to ensure grpc stability
if in.XXX_Size() >= grpcWriteBufferSize {
err := status.Errorf(codes.ResourceExhausted, "gRPC message exceeds maximum size: %d", grpcWriteBufferSize)
Log("grpc").Warnf("skip exception metadata - %v", err)
return err
}

ctx, cancel := context.WithTimeout(ctx, agentGrpcTimeOut)
defer cancel()

_, err := agentGrpc.metaClient.RequestExceptionMetaData(ctx, in)
if err != nil {
Log("grpc").Errorf("send sql metadata - %v", err)
Log("grpc").Errorf("send exception metadata - %v", err)
}

return err
Expand Down
8 changes: 7 additions & 1 deletion span.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
defaultEventDepth = 64
defaultEventSequence = 5000
defaultEventChunkSize = 20
maxErrorChainEntry = 10
)

var (
Expand Down Expand Up @@ -127,8 +128,9 @@ func (span *span) EndSpan() {

chunk := span.newEventChunk(true)
if chunk.enqueue() {
if len(span.errorChains) > 0 {
if span.errorChains != nil && len(span.errorChains) > 0 {
span.agent.enqueueExceptionMeta(span)
span.errorChains = nil
}
} else {
Log("span").Tracef("span channel - max capacity reached or closed")
Expand Down Expand Up @@ -443,6 +445,10 @@ func (span *span) config() *Config {
return span.agent.config
}

func (span *span) canAddErrorChain() bool {
return span.errorChains != nil && len(span.errorChains) < maxErrorChainEntry
}

type spanChunk struct {
span *span
eventChunk []*spanEvent
Expand Down
2 changes: 1 addition & 1 deletion span_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (se *spanEvent) SetError(e error, errorName ...string) {
se.errorString = e.Error()

cfg := se.config()
if cfg.errorTraceCallStack {
if cfg.errorTraceCallStack && se.parentSpan.canAddErrorChain() {
se.exceptionId = se.parentSpan.traceCallStack(e, cfg.errorCallStackDepth)
se.Annotations().AppendLong(AnnotationExceptionChainId, se.exceptionId)
}
Expand Down

0 comments on commit efa6d37

Please sign in to comment.