cubelog: emit the Region/Cluster defaults that Trace already computes - #1473
cubelog: emit the Region/Cluster defaults that Trace already computes#1473dwin-gharibi wants to merge 1 commit into
Conversation
Signed-off-by: Dwin Gharibi <dwin.gharibi@email.kntu.ac.ir>
|
|
||
| if enableLogMetric { | ||
| fields := makeLogFieldsFromTrace(trace) | ||
| fields["CostTime"] = cost |
There was a problem hiding this comment.
Minor (non-blocking): region and tmcluster are now only consumed inside the enableLogMetric branch, but the fallback computation above still runs on every Trace() call — including the common case where metric logging is disabled (the package default at init). Since these values are read only here, moving the region/tmcluster computation inside this block would keep identical behavior and skip the wasted work when the feature is off. Not a correctness issue; the base code had the same unconditional computation, so this is a cleanup opportunity the PR naturally creates.
Review: cubelog: emit the Region/Cluster defaults that Trace already computesAI-generated review — not a human approval. SummarySmall, correct, and well-tested change. I traced the value flow to confirm correctness:
The Findings
TestsThe new
Minor, non-blocking notes:
RiskThe PR's "Risk / rollout" section is accurate and appropriately flagged: trace lines from callers that omit Region/Cluster will now carry the configured values. This is the intended behavior change, and existing tests in the repo that consume VerdictApprove. The change is minimal, correctly scoped to |
Closes #1472.
Motivation
Trace()computedregion,tmclusterandversionfallbacks from the configured globals and thennever used them —
staticcheckreports all three asSA4006. BecausemakeLogFieldsFromTraceonly setsRegion/Clusterwhen the trace struct carries them, any callerthat omits them produced a trace line with no Region and no Cluster, even when the logger had defaults
configured.
Versionwas already defaulted insidemakeLogFieldsFromTrace(entry.go:213-217), so theversionlocal was pure duplication.
What this changes
cubelog/metric.goinTrace():region/tmclusterare now applied to the emitted fields.see below.
versionlocal is removed, sincemakeLogFieldsFromTracealready defaults it.No comment changes.
The omit-when-empty detail
My first attempt set the fields unconditionally, which broke
TestTraceEnds(
logger_test.go:92-93). That test asserts that when neither the trace nor the globals carry acluster, the output must not contain
Cluster— an unconditional assignment made the key presentwith an empty value.
The guarded version keeps that contract: absent stays absent, configured defaults now come through.
Both halves of
TestTraceEndspass.Testing
New:
cubelog/metric_trace_test.goTestTraceEmitsConfiguredRegionAndClusterDefaults— with globals configured and the trace fieldsempty, the emitted JSON carries the defaults.
TestTracePrefersExplicitRegionAndCluster— an explicit value on the trace still wins over theglobal.
Both capture the real trace line via
SetTraceOutput+EnableLogMetricand parse it as JSON, and bothrestore the globals and the writer afterwards so they do not leak into other tests in the package.
Red/green verified — with
metric.goreverted to master:and with the fix:
CI gates checked locally:
gofmt -l .— clean (fmt-check).go build ./...— clean.staticcheck -checks 'SA4006' ./— the threemetric.gofindings are gone.Note the package directory is
cubelog/but the Go package isCubeLog; the new test file matches.Risk / rollout
Trace lines from callers that omit Region/Cluster will start carrying the configured values. Anything
that parses those lines and currently treats the fields as absent will now see them populated — which
is the intended behaviour, but it is a visible change in log output.
Related, not fixed here
cubelog/metric.go:77-180— therequestTrace/indexKey/realKey/slice2Strblock isunreachable (never instantiated). It looks like the other half of a partially-removed metric
aggregation path, and
Trace()computing inputs for it is probably why these locals existed. Trackedseparately.