Skip to content

feat(metric): add APICallMutex wait duration and TryLock skip metrics - #76

Merged
whywaita merged 3 commits into
masterfrom
feat/metrics-mutex-wait
Mar 10, 2026
Merged

whywaita merged 3 commits into
masterfrom
feat/metrics-mutex-wait

Conversation

@whywaita

@whywaita whywaita commented Mar 6, 2026

Copy link
Copy Markdown
Owner

Summary

  • Add metrics to observe APICallMutex contention: lock wait duration histogram and TryLock skip counter
  • Instrument all 3 Lock() call sites and the TryLock() site in scrape_lxd.go
  • Use the existing observer pattern to avoid circular dependency from lxdclient package

Changes

  • server/pkg/metric/lxd_api.go: Add LXDAPIMutexWaitDuration (histogram) and LXDAPIMutexSkippedTotal (counter) metrics, plus ObserveMutexWait helper
  • server/pkg/lxdclient/metrics_hook.go: Add MutexWaitObserver type and SetMutexWaitObserver/observeMutexWait functions
  • server/main.go: Register the two new metrics with Prometheus
  • server/pkg/api/pool.go: Measure mutex wait in allocateInstance
  • server/pkg/api/server_host.go: Measure mutex wait in isExistInstanceWithTimeout
  • server/pkg/lxdclient/resource.go: Measure mutex wait in GetResourceFromLXD
  • server/pkg/metric/scrape_lxd.go: Increment skip counter on TryLock failure
  • server/pkg/metric/lxd_api_test.go: Tests for ObserveMutexWait and LXDAPIMutexSkippedTotal

Metrics

shoes_lxd_multi_lxd_api_mutex_wait_duration_seconds{host, caller, instance}
shoes_lxd_multi_lxd_api_mutex_skipped_total{host}

Test Plan

  • go test ./pkg/metric/... passes
  • go vet ./... passes
  • go build ./... passes

whywaita added 3 commits March 6, 2026 18:28
Add metrics to observe mutex contention on LXD API calls:
- mutex_wait_duration_seconds histogram for Lock() wait time
- mutex_skipped_total counter for TryLock() skips

prompt: Implement the following plan: APICallMutex のロック待ち時間メトリクス追加
----
Added LXDAPIMutexWaitDuration histogram and LXDAPIMutexSkippedTotal counter metrics.
Instrumented all Lock() call sites (allocateInstance, isExistInstanceWithTimeout, GetResourceFromLXD)
and the TryLock site in scrape_lxd.go. Used observer pattern to avoid circular dependency
from lxdclient package.
WithContext was called before APICallMutex.Lock(), causing concurrent
goroutines to overwrite each other's context on the shared client.
Additionally, context was not reset on error paths, leaving the client
with a canceled context for all subsequent calls.

Changes:
- Move WithContext after Mutex acquisition in all call sites
- Add defer reset to context.Background() on all paths
- Add Mutex to setLXDHostResourceCache (was missing)
- Remove xsemaphore from GetResourceFromLXDWithClient (callers now
  hold the Mutex and set context)
- Add regression tests for Mutex-based concurrency control
fix(lxdclient): fix context race condition in ProtocolLXD WithContext
@whywaita
whywaita merged commit 7aa0102 into master Mar 10, 2026
9 checks passed
whywaita added a commit that referenced this pull request Mar 10, 2026
…trics

Revert the metrics-only changes from #76 (commit d9f0c80):
- Remove LXDAPIMutexWaitDuration histogram and LXDAPIMutexSkippedTotal counter
- Remove MutexWaitObserver from lxdclient package
- Remove instrumentation from Lock()/TryLock() call sites
whywaita added a commit that referenced this pull request Mar 10, 2026
revert(metric): remove APICallMutex mutex wait metrics from #76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant