traffic-label
Labels requests for later plugins by match rules.
traffic-labelPriority 967What it does
traffic-label writes headers into the requests a route takes, chosen by rules, so the backend and the plugins that run after it can tell kinds of traffic apart. Each of the Rules pairs Conditions, read like a route's own, with Actions whose Headers to set are written into the request.
The header it writes replaces a header of the same name the client sent. The label is a request header and nothing else: it is not one of the Labels of a consumer, and acl does not read it.
How a request gets its label
The rules are read from the top for each request.
- A rule whose Conditions the request meets takes it. A rule with no conditions at all takes every request that reaches it.
- Of the matched rule's Actions, one is taken per request, chosen by the Weight of each.
- An action that writes headers ends the walk, and the rules below are not read for that request.
- An action that carries only a Weight hands its share to the rules below, and a request no rule labels reaches the backend unlabelled.
A last rule with no conditions catches every request the rules above it let through, as external does in the example.
When to use it
- The backend of the orders API treats calls from the internal network apart from calls from the internet, and reads which is which from one header.
- A new version of a backend should see a share of the traffic first, marked by a header that a later step reads, with the weights of two actions.
- Requests that carry a release header,
canaryfor example, should be marked for the backend, and every other request marked as ordinary. - A header that tells the backend how a request was classified must come from the gateway, never from the client.
Not for letting a consumer through or refusing it by a team or tier set on the consumer — see acl (Allows or denies consumers by the labels they carry), sending a share of the requests to another upstream — see traffic-split (Splits traffic between upstreams by weight and match rules) or headers the backend always receives on a route, whatever the request — see proxy-rewrite (Rewrites the request URI, method, host and headers before proxying).
Set it up
Mark requests from the internal network with a header the backend can read
The last rule has no conditions, so every other request is marked external and a value the client sent itself is overwritten; behind a load balancer, add real-ip so that the first rule reads the caller's own address.
Fill Rules as in the JSON below.
{
"rules": [
{
"match": [
[
"remote_addr",
"ipmatch",
"10.0.0.0/8"
]
],
"actions": [
{
"set_headers": {
"X-Traffic-Source": "internal"
}
}
]
},
{
"actions": [
{
"set_headers": {
"X-Traffic-Source": "external"
}
}
]
}
]
}
Things to know
- Rules — The first matching rule ends the walk only when its action writes headers. An action with a weight alone lets the rules below label the request.
- Rules — A request from
10.20.30.40meets the rule on10.0.0.0/8and reaches the backend withX-Traffic-Sourceset tointernal. A request from203.0.113.9falls to the last rule and is markedexternal. - Headers to set — A header the client sent under the same name is overwritten: a client from
203.0.113.9that sendsX-Traffic-Sourceasinternalreaches the backend markedexternal. - Conditions — A condition on
remote_addrreads the address the request arrives from. Behind a load balancer, add real-ip to the same route, as the example does, so it reads each client's own address. - Headers to set — A value is one text or number: a header given a list of values is refused when you save.
- Headers to set — A header name with a colon in it is refused when you save.
- Headers to set — A value whose only variable is unknown is sent as nothing, and the header is dropped.
- Weight — The turns are counted inside each worker process, so the shares only approach the weights over many requests.
- AdviceEnd the rules with one that has no conditions, as the example does, so every request carries a value the backend can trust.
Settings
The fields as the console shows them, section by section.
rulesarray of objectsRequiredrules[].matcharrayRead like a route's conditions. A rule with no conditions at all takes every request.
rules[].actionsarray of objectsRequiredOne action of the matched rule is taken per request, chosen by the weights. An action carrying only a weight hands its share to the rules below, or leaves it unlabelled where none follows.
rules[].actions[].set_headersobjectHeaders written into the request; a $name or ${name} anywhere in a value takes that request variable, so $host sends the host asked for. A value whose only variable is unknown is sent as nothing, and the header is dropped. An object whose keys are a name without a colon, each with a string or number as its value.
rules[].actions[].weightintegerdefault 1The share of the matched rule's requests this action takes, from 1 up. The turns are counted inside each worker process, so the shares only approach the weights over many requests.