- 2 minutes to read
Formula - ContextKey
The contextKey Formula function returns the key names (not the values) of the Message Context properties of the Log Event whose key matches a regular expression.
Syntax
contextKey(regex('Key pattern'))
Parameters
| Parameter | Required | Description |
|---|---|---|
regex('Key pattern') |
Yes | A regex that is matched against the context key names. |
Returns
Every matching key name, one value per key. Duplicates are collapsed (see Formula language rules). When no key matches, nothing is returned. To get the values of the matching context keys instead, use context with the same regex(...) argument.
Examples
Example 1: List the order keys in the context
Input
| Key | Value |
|---|---|
| Order#123 | OK |
| Order#456 | OK |
| Order#789 | Fail |
Formula
contextKey(regex('Order#.*'))
Result
Order#123
Order#456
Order#789

Each key that matches Order#.* is returned as its own unique match. The screenshot writes ContextKey; function names are case-insensitive.
Notes
contextKeyreturns the whole key name whenever the pattern matches part of it. The pattern matches anywhere in the key name unless you anchor it: use^Order[#]to match only keys that start withOrder#.- In Formula regular expressions
#starts a comment, so'Order#.*'in the example really runs as'Order'. It works here because every key starts withOrder; write[#]or\#when the#matters. See Formula language rules. - Combine with count to get the number of matching keys, for example
count(contextKey(regex('Order#.*'))).