skip to content

How does gNMI encode a path such as /interfaces/interface[name=eth0]/state/counters, and what do prefixes, keys and wildcards do in a subscription?

level: middleimportance: nice to knowfreq 8%

answer

  1. a list, not a string
  2. name plus a key map
  3. keys travel as strings
  4. prefix plus path
  5. star and three dots

basics

~20 s

gNMI encodes a path as a list of PathElem messages: a node name plus an optional map of list keys, sent as strings. A prefix is prepended to each path; omitted keys, * and ... are wildcards, and subscriptions include descendants.

solid answer

~50 s

A gNMI `Path` is not a slash string. It is an ordered list of **`PathElem`** messages from the root, each with a `name` and, for a list entry, a `key` map of key names to values, always as **strings** whatever the schema type. So `/interfaces/interface[name=eth0]/state/counters` is four elements, the second carrying `name` = `eth0`. The root `/` is an empty element list. A **`prefix`** in a `SubscriptionList` or `Notification` is joined to each path, so a device can send `counters/in-octets` under a prefix naming the interface. A path element without keys matches every list entry, as does a key value of `*`; an element named `*` is a one-level wildcard and `...` matches any depth. Subscription paths are recursive, and a valid path that does not exist yet keeps a `STREAM` subscription waiting rather than failing.

go deeper

for a junior

Recall that a gNMI path is a list of named elements, with list keys attached to the element they select, rather than a numeric identifier.

for a middle

Explain PathElem names and key maps, keys as strings, how prefix and path combine, and what omitted keys, * and ... match.

for a senior

Show collector-side judgment: use prefixes to cut message size, wildcard subscriptions that cover interfaces created later, and handle UNIMPLEMENTED versus not-yet-existing paths differently.

for a principal

Consider path design across different YANG models: how origin and model choice affect a collector that must normalise data from 2,000 devices.

## Why a structured path **gNMI** is an OpenConfig specification (version 0.10.0), not an RFC, with a companion document, the **gNMI path conventions**, that fixes how paths are written. Data is a tree, usually described by YANG models, and every value a device streams travels with the path that identifies it. A collector receiving updates from 2,000 devices must parse those paths quickly and without ambiguity, which is why gNMI does not use one slash-separated string. ## The Path and PathElem messages A **`Path`** has three fields: - **`elem`** - an ordered list of **`PathElem`** messages, from the root to the most specific node. - **`origin`** - optional; states which schema the path belongs to. - **`target`** - optional; a name for the device the data belongs to. It may appear only in a **prefix**, and if a client sets it in a request the target must reflect it in the matching responses. Each **`PathElem`** has a **`name`** (a UTF-8 string) and an optional **`key`** map from key name to key value. Key values are **strings regardless of their type in the schema**, so an integer key such as a VLAN number travels as text and the collector converts it. The path `/interfaces/interface[name=eth0]/state/counters` becomes: | Position | name | key | |---|---|---| | 1 | `interfaces` | - | | 2 | `interface` | `name` = `eth0` | | 3 | `state` | - | | 4 | `counters` | - | Two edge cases come up in interviews: 1. The **root** `/` is an `elem` list of **length zero**, which is not the same as one element with an empty name. 2. A list with several keys carries **one map entry per key** in the same `PathElem`. The bracketed string form is only for people; the conventions describe joining prefix and path elements with `/` and a leading `/` to make a readable path. ## Prefixes A **`prefix`** shortens repeated paths. In a `SubscriptionList` it applies to every subscribed path; in a `Notification` it applies to every update and delete. The absolute path is always **prefix elements followed by path elements**, and paths in messages must be absolute once joined. A device streaming two counters for `eth0` can send a prefix of `/interfaces/interface[name=eth0]/state/counters` and updates whose paths are just `in-octets` and `out-octets`. ## Wildcards and recursion in subscriptions - **Omitting the keys** of a list element matches **every entry**: `/interfaces/interface/state/oper-status` covers all interfaces. A key value of `*` means the same. - A `PathElem` whose **name is `*`** is a **single-level** wildcard. - A `PathElem` whose **name is `...`** is a **multi-level** wildcard, matching any depth. - Subscribe and Get paths are **recursive**: subscribing to `.../state` covers every leaf under it, including those in `state/counters`. ## Paths that do not exist yet A subscribed path need not exist when the subscription is made. If it is valid in the schemas the target supports, the target must not close the RPC; it watches for the path and sends updates if it appears. A `STREAM` subscription to an interface not yet configured therefore succeeds and starts reporting once the interface exists. A path the target does not implement is answered with `UNIMPLEMENTED`, and a syntactically invalid one with `INVALID_ARGUMENT`. ## Contrast with SNMP SNMP names a value with a numeric object identifier and puts the table row index into the OID suffix, so the manager needs the MIB to read it. A gNMI path carries readable node names and named keys, tied to a YANG model. That is part of why model-driven telemetry is easier to consume across devices that implement the same model.

  • Why does gNMI send a list key as a string even when the YANG key is an integer?
    The gNMI path conventions represent every key value as a string regardless of its type in the schema, and the `key` field is a map of string to string. One representation keeps path parsing uniform for every model; the collector converts the value using the schema when it needs the typed value.
  • What is the target field of a gNMI Path for, and where may it appear?
    `target` names the device the data belongs to. It may be set only in a `prefix`. If a client sets it in the prefix of a `SubscribeRequest`, `GetRequest` or `SetRequest`, the target must reflect it in the responses' prefix, which lets a collector label every update in a stream; if the client leaves it unset, responses must not carry it.

saying these in an interview costs you the question

  • gNMI sends each path as one slash-separated string.
  • Subscribing to /interfaces/interface[name=eth0]/state returns only leaves directly under state.
  • A list key keeps its YANG type on the wire, so an integer key is sent as an integer.
  • Subscribing to a valid path that does not exist yet makes the target close the RPC.
  • The root path is a single PathElem whose name is empty.