← Back to Pull requests

feat(otelgrpc): reduce allocations in TagRPC #10007

Open
unknown opened this pull request with 385 lines diff across 4 files
View on GitHub
unknown unknown commented on Oct 23, 2026 13:05 UTC
Author

Motivation

TagRPC is invoked on every gRPC request lifecycle across both client and server stats handlers. In high-throughput services, unnecessary heap allocations during context tagging add GC pressure and latency overhead.

Previously, internal.ParseFullMethod returned a slice with cap=1, which forced a slice reallocation immediately when appending semconv.RPCSystemNameGRPC to attrs. Additionally, both client and server handlers unconditionally created duplicate attribute slices (spanAttributes and metricAttrs), even when no custom span attributes or metric attributes were configured.

Summary

  • Pre-allocated attrs in internal.ParseFullMethod with len=1, cap=2 so appending semconv.RPCSystemNameGRPC fits without reallocation.

  • Avoided intermediate slice allocation and duplication for spanAttributes and metricAttrs in both serverHandler.TagRPC and clientHandler.TagRPC when no custom attributes are supplied.

  • Preserved complete immutability and thread safety; slices are conditionally allocated with exact capacity only when extra attributes are present.
  • Benchmarks

    Benchmark command

    ``bash id="p6t9w1"
    go test -run=^$ -bench='TagRPC' -benchmem -count=10

    
    <details>
    <summary>Benchmark results (base vs. head)</summary>
    
    
    text id="r7k2mv"
    goos: linux
    goarch: amd64
    go version: go1.26.5 linux/amd64
    pkg: go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc
    cpu: Intel(R) Core(TM) i5-10300H CPU @ 2.50GHz

    │ base_tagrpc_10.txt │ new_tagrpc_10.txt │
    │ sec/op │ sec/op vs base │
    ServerHandler_TagRPCNoOp-8 510.9n ± 1% 400.2n ± 1% -21.66% (p=0.000 n=10)
    ClientHandler_TagRPCNoOp-8 510.1n ± 1% 395.1n ± 1% -22.56% (p=0.000 n=10)
    geomean 510.5n 397.6n -22.11%

    │ base_tagrpc_10.txt │ new_tagrpc_10.txt │
    │ B/op │ B/op vs base │
    ServerHandler_TagRPCNoOp-8 648.0 ± 0% 456.0 ± 0% -29.63% (p=0.000 n=10)
    ClientHandler_TagRPCNoOp-8 648.0 ± 0% 456.0 ± 0% -29.63% (p=0.000 n=10)
    geomean 648.0 456.0 -29.63%

    │ base_tagrpc_10.txt │ new_tagrpc_10.txt │
    │ allocs/op │ allocs/op vs base │
    ServerHandler_TagRPCNoOp-8 11.000 ± 0% 9.000 ± 0% -18.18% (p=0.000 n=10)
    ClientHandler_TagRPCNoOp-8 12.00 ± 0% 10.00 ± 0% -16.67% (p=0.000 n=10)
    geomean 11.49 9.487 -17.43%
    ``

    </details>

    Results

  • Latency: Reduced by approximately 22.1% overall (geomean 510.5 ns/op to 397.6 ns/op).

  • Memory usage: Reduced by approximately 29.6% (648 B/op to 456 B/op), saving 192 bytes per operation.

  • Allocations: Eliminated 2 heap allocations per RPC execution across both server and client handlers.
  • Diff: 385 lines (+288 / -97) across 4 files Reviewer: MrAlias Review duration: 2895m
    All Vouch review confidence checks passed
    Residual risk 0.26 = Change Risk (0.48) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 385 lines diff across 4 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.356
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #10007"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48% (MEDIUM) · Higher = larger blast radius
    48%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 25.6% · Safe to merge
    25.6%