← Back to Pull requests

Add dimension-aware query helpers to TestMetrics #231

Merged
krishnapermi merged 2 files (202 lines) on Jul 24, 2026 10:56 UTC
View on GitHub
krishnapermi krishnapermi commented on Jul 03, 2026 17:05 UTC
Author

Closes apple/swift-metrics#230

Motivation

Testing code that emits metrics with dimensions is more boilerplate than it should be. The existing expectCounter(_:_:) API requires an exact label+dimensions match, so looking up all counters for a given label across any dimension set means manually filtering metrics.counters:

// throws even though "http_requests" counters exist with different dimensions
_ = try metrics.expectCounter("http_requests")

// what you have to write today
let total = metrics.counters
    .filter { $0.label == "http_requests" }
    .map(\.totalValue)
    .reduce(0, +)

I noticed this while writing tests for a service that tags every metric with HTTP method and status dimensions — expectCounter was completely unusable across dimension variants, forcing me to hand-roll every lookup.

Changes

Adds two overloads per instrument type (Counter, Meter, Recorder, Timer):

  • counters(label:) — returns every counter with the given label, regardless of dimensions

  • counters(label:dimensions:) — returns every counter with the given label whose dimension set is a superset of the specified pairs (counters with extra dimensions are still matched)
  • Same pattern for meters, recorders, and timers.

    Example

    // all "http_requests" counters across any dimension set
    let all = metrics.counters(label: "http_requests")
    let total = all.map(\.totalValue).reduce(0, +)
    
    // only GET requests, regardless of status code
    let gets = metrics.counters(label: "http_requests", dimensions: [("method", "GET")])
    

    Diff: 202 lines (+151 / -51) across 2 files Reviewer: None requested Review duration: 29870m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.48) × (1 − Review Confidence (0.44)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 202 lines diff across 2 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: 10%
    44%
    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
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.318
    No Reviewer Assigned no_reviewer_assigned
    0.120
    Real PR Conversation Timeline
    1 comments
    "Review active on #231"
    general weight: 0.35
    Reviewers
    None requested
    Assignees
    krishnapermi krishnapermi
    Labels
    risk: low confidence: 44.2% 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.2% (MEDIUM) · Higher = larger blast radius
    48.2%
    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: 44.2% · Lower = rushed or shallow review
    44.2%
    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: 26.9% · Safe to merge
    26.9%