Skip to content

feat: add traffic-label plugin#13342

Merged
AlinsRan merged 10 commits into
apache:masterfrom
AlinsRan:feat/traffic-label-plugin
May 12, 2026
Merged

feat: add traffic-label plugin#13342
AlinsRan merged 10 commits into
apache:masterfrom
AlinsRan:feat/traffic-label-plugin

Conversation

@AlinsRan
Copy link
Copy Markdown
Contributor

@AlinsRan AlinsRan commented May 9, 2026

Summary

This PR introduces the traffic-label plugin, which assigns configurable labels to requests based on weighted random distribution rules.

Description

The traffic-label plugin selects a label from a configured set of weighted labels and writes it to a request header or APISIX variable. Multiple rules can be configured, each with an optional match condition using resty.expr expressions. The first matching rule is applied; if no rule matches, the plugin is a no-op.

Use cases:

  • A/B testing by tagging requests and routing based on the label
  • Canary deployments with weighted traffic splitting at the label level
  • Request classification for observability and analytics

Key features

  • Weighted random label selection per rule
  • Optional resty.expr-based match conditions
  • Writes selected label to a request header or APISIX variable
  • First-match semantics across multiple rules

Example

The following example assigns the label gray to 10% of requests and stable to 90%, writing the result to the X-Traffic-Label header:

curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  -H "X-API-KEY: $admin_key" -X PUT -d '{
  "uri": "/api/*",
  "plugins": {
    "traffic-label": {
      "rules": [
        {
          "weighted_labels": [
            { "label": "gray",   "weight": 10 },
            { "label": "stable", "weight": 90 }
          ],
          "header": "X-Traffic-Label"
        }
      ]
    }
  },
  "upstream": {
    "type": "roundrobin",
    "nodes": { "127.0.0.1:1980": 1 }
  }
}'

Downstream services can then read X-Traffic-Label to apply canary logic.

Files changed

  • apisix/plugins/traffic-label.lua — plugin implementation
  • t/plugin/traffic-label.t — basic test cases
  • t/plugin/traffic-label2.t — extended test cases
  • docs/en/latest/plugins/traffic-label.md — English documentation
  • docs/zh/latest/plugins/traffic-label.md — Chinese documentation
  • apisix/cli/config.lua — register plugin in default list
  • docs/en/latest/config.json / docs/zh/latest/config.json — add to sidebar

The traffic-label plugin assigns configurable labels to requests based
on weighted random distribution rules. It supports matching conditions
using resty.expr expressions and writes the selected label to a
specified request header or APISIX variable.

Each rule defines a list of weighted labels and an optional match
condition. When multiple rules are configured, the first matching rule
is applied. If no rule matches, the plugin is a no-op.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@dosubot dosubot Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. enhancement New feature or request labels May 9, 2026
AlinsRan and others added 9 commits May 9, 2026 09:12
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Change priority from 995 to 967 to avoid conflict with gzip (995)
- Move plugin registration to between gzip and traffic-split in config.lua
- Add traffic-label entry to t/admin/plugins.t in correct priority position

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Rename APISIX CRD tab to APISIX Ingress Controller
- Simplify admin_key note block

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@AlinsRan AlinsRan merged commit 7539142 into apache:master May 12, 2026
25 of 26 checks passed
@AlinsRan AlinsRan deleted the feat/traffic-label-plugin branch May 12, 2026 03:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants