Skip to content

Fix routing key order in OTLP metrics v1 forwarder - #292

Merged
mrproliu merged 3 commits into
apache:mainfrom
surlymochan:fix-routing-key-metadata-order
Nov 6, 2026
Merged

mrproliu merged 3 commits into
apache:mainfrom
surlymochan:fix-routing-key-metadata-order

Conversation

@surlymochan

Copy link
Copy Markdown
Contributor

Problem

plugins/forwarder/grpc/otlpmetricsv1/forwarder.go's generateRoutingKey concatenated routing values in the arrival order of the resource attributes (outer loop over attributes, inner loop over metadataKeys), not in the configured metadataKeys order. The same service can therefore produce different keys across reports when the attribute order changes, e.g. ,X,Y vs ,Y,X.

Call chain / impact

Forwarder.Forward -> generateRoutingKey -> lb.WithLoadBalanceConfig(ctx, key, loadCachedPeer(key)) -> satelliteDynamicPicker.Pick, which computes hashCode(routeKey) % connCount where hashCode is crc32.ChecksumIEEE (plugins/client/grpc/lb/load_balancer.go). A different key string can therefore hash to a different backend, and the upstreamCache entry keyed by the old string no longer matches, so cache stickiness is lost.

Fix

Build the key in metadataKeys order: index the first value of each attribute key, then join values in configured order. Otherwise unchanged:

  • The first resource yielding a non-empty key still wins; resources without a routing key are still skipped.
  • Both error branches are preserved (no resources / no attributes / no routing key).
  • Typo-only fix in the trailing error message: anly -> only.
  • Out of scope (intentionally untouched): non-string attribute values still collapse to an empty string via GetStringValue(), as before.

Tests (RED -> GREEN)

New plugins/forwarder/grpc/otlpmetricsv1/forwarder_test.go covers: same attribute set in two arrival orders yields the same key in metadataKeys order; a resource without a routing key is skipped; attributes without any routing key still error.

Before the fix, the order test failed with:

routing key depends on attribute arrival order: ",my-job,my-service" vs ",my-service,my-job"

After the fix, local run of the affected package only:

go test ./plugins/forwarder/grpc/otlpmetricsv1/ -v

3 passed, 0 failed (TestGenerateRoutingKeyFollowsMetadataKeyOrder, TestGenerateRoutingKeySkipsResourceWithoutRoutingKey, TestGenerateRoutingKeyNoRoutingKey); go vet on the package is clean.

Note: this PR is on the SkyWalking PMC track and is not claimed toward any external PR-count goal.

generateRoutingKey previously concatenated values in the arrival
order of resource attributes, so the same service could produce
different keys (e.g. ",X,Y" vs ",Y,X") when the attribute order
changed between reports. Since the key feeds the load balancer
hash and the upstream cache, this could route the same service to
different backends and break cache stickiness.

Build the key in metadataKeys order instead: index the first value
of each attribute key, then join in configured order. Semantics are
otherwise unchanged: the first resource yielding a non-empty key
still wins, and both error branches are preserved. Also fix a typo
in the trailing error message (anly -> only).
@wu-sheng
wu-sheng requested a review from mrproliu November 1, 2026 01:30
@wu-sheng wu-sheng added this to the 1.4.0 milestone Nov 1, 2026
@wu-sheng wu-sheng added the bug Something isn't working label Nov 1, 2026
@wu-sheng

wu-sheng commented Nov 1, 2026

Copy link
Copy Markdown
Member

infra-e2e should need to update to follow Ubuntu latest changed.

@mrproliu mrproliu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Could you help to update the changed.md?

@surlymochan

Copy link
Copy Markdown
Contributor Author

Done — added a 1.4.0 Bug Fixes entry to CHANGES.md (commit 97c3d46). Thanks for the review!

@mrproliu
mrproliu merged commit 1249c6a into apache:main Nov 6, 2026
45 of 65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants