Implement the following plan: Consolidate buildQueries / Scope Transformation Logic…
- User
Implement the following plan:
Consolidate buildQueries / Scope Transformation Logic
Context
pkg/metricstore/query.go(InternalMetricStore) andinternal/metricstoreclient/cc-metric-store-queries.go(CCMetricStore) both implement theMetricDataRepositoryinterface. They contain nearly identical scope-transformation logic for building metric queries, but the code has diverged:- metricstoreclient has the cleaner design: a standalone
buildScopeQueries()function handling all transformations - metricstore has the same logic inlined as a long if-chain inside
buildQueries()and duplicated inbuildNodeQueries() - metricstore is missing the
MemoryDomain → Sockettransformation that metricstoreclient has - Both packages duplicate scope string constants,
intToStringSlice, andsanitizeStats
Goal: Extract shared scope-transformation logic, synchronize behavior, and eliminate duplication.
Approach
Extract
BuildScopeQueriesas a shared exported function inpkg/metricstore/(since metricstoreclient already imports it). Return a package-independent intermediate type (ScopeQueryResult) that each package converts to its ownAPIQuery— this avoids coupling the two differentAPIQuerytypes (they differ inResolutiontype:int64vsint, and internal hasScaleFactor).Steps
1. Create
pkg/metricstore/scopequery.goNew file containing:
ScopeQueryResultstruct — intermediate type with fields:Metric,Hostname,Aggregate,Type *string,TypeIds []string,Scope schema.MetricScope(no Resolution — callers set it)BuildScopeQueries()— exported function ported frommetricstoreclient/buildScopeQueries()(lines 257-517), returning([]ScopeQueryResult, error). Returns empty slice for expected exceptions, error for unhandled cases- Scope string constants —
HWThreadString,CoreString,MemoryDomainString,SocketString, (exported, move from both packages)
- metricstoreclient has the cleaner design: a standalone
Assistant
I'll start by reading the key source files to understand the current code structure.
Assistant
Now let me read the key sections of the metricstore query.go file.
Assistant
Now I have a full understanding of the code. Let me create task tracking and start implementing.
Assistant
Now update
pkg/metricstore/query.go— replace the inline if-chains in bothbuildQueriesandbuildNodeQueries, remove duplicated helpers, and updatesanitizeStatscalls.Assistant
Now replace the
sanitizeStatscalls in LoadData (takes*APIMetricData). I need to change them to useSanitizeStatswith individual field pointers.Assistant
Now replace the entire
buildQueriesfunction body (the scope transformation part) with a call toBuildScopeQueries:Assistant
Good, those errors are from
buildNodeQuerieswhich still uses the old constants. Now replacebuildNodeQueries's inline if-chain similarly. Let me also update thesanitizeStatscalls inLoadStats,LoadScopedStats,LoadNodeData, andLoadNodeListData.Assistant
Now let me replace the sanitizeStats calls in LoadScopedStats (takes
*APIMetricData):Assistant
Now replace the
sanitizeStatsinLoadNodeData:Assistant
Now replace
sanitizeStatsinLoadNodeListData:Assistant
Now replace the entire
buildNodeQueriesinline if-chain withBuildScopeQueries:Assistant
Now let me also undo the nolint comment I added — that was in LoadStats where it checks NaN inline (not using sanitizeStats). Let me check:
Assistant
Let me verify the file compiles:
Assistant
Clean build. Now update metricstoreclient.
Assistant
Now update
internal/metricstoreclient/cc-metric-store-queries.go— replacebuildScopeQuerieswithBuildScopeQueries, remove local helpers:Assistant
Remove local scope string constants:
Assistant
Now replace the call to local
buildScopeQueriesinbuildQueriesandbuildNodeQueries:Assistant
Now do the same for
buildNodeQueries:Assistant
Good — those errors are from the old
buildScopeQueriesfunction that still exists. Now delete it andintToStringSlice:Assistant
The diagnostics may be stale. Let me now update
sanitizeStatsincc-metric-store.go:Assistant
Now I need to update all
metricstore.references toms.andsanitizeStatscalls. Let me check what usesmetricstore.in this file:Assistant
I need to update
metricstore.toms.:Assistant
Now update the
sanitizeStatscalls to use the sharedms.SanitizeStats:Assistant
Now delete the local
sanitizeStatsandhasNaNStatsfunctions:Assistant
The replace_all also hit the function definition. Let me read the current state:
Assistant
Now also need to update the import in
cc-metric-store-queries.go— thecclogimport may no longer be needed since we removedbuildScopeQueries. Let me check:Assistant
Still used. Let me also check if
metricstoreis still referenced there or if I need_ "fmt":Assistant
Clean build. Now check the full build:
Assistant
Now let me check what topology methods are available so I can build proper test data:
Assistant
Now I can create the test file:
Assistant
I need to check if
schema.NaNexists:Assistant
Good,
schema.NaNexists. Now run the tests:Assistant
All tests pass. Let me run all the scopequery tests:
Assistant
All passing. Now run the full test suite:
Assistant
All tests pass. Let me mark the task complete:
Assistant
All done. Here's a summary of what was implemented:
New file:
pkg/metricstore/scopequery.goScopeQueryResult— package-independent intermediate type (no Resolution field)BuildScopeQueries()— shared scope transformation logic ported from metricstoreclient, covering all 16+ cases including the MemoryDomain → Socket transformation that was missing from metricstoreIntToStringSlice()— exported, using the optimized buffer-reuse versionSanitizeStats()— exported, zeros ALL stats if ANY is NaN (consistent behavior)- Exported scope string constants (
HWThreadString,CoreString, etc.)
New file:
pkg/metricstore/scopequery_test.go- Table-driven tests for all scope transformations (same-scope, aggregation, accelerator, memory domain)