NiFiCpuUsage

Cribl LogStream Global Variables: A Practical Introduction

Last edited: March 12, 2020

Global Variables were one of the most important features introduced with Cribl LogStream 2.1. In this short post we’ll take a look at what they are, how they work and how to use them to build more effective pipelines.

If you’re new to Cribl LogStream it may make sense to take our sandbox for a drive before reading further.

LogStream Global Variables are re-usable and composable expressions for the purposes of storing constants that can be referenced later or storing value expressions that can be invoked with optional arguments. They are managed via Knowledge > Global Variables and can be any of the following types: number, string, boolean, object, array, expression. They are accessed via C.vars. and can be called anywhere JS expressions are supported in the product, including obviously functions in any pipeline. They’re also typeahead enabled for convenient and contextual help.

global_variables-1024x265

How to use them

The typical use-case is code reuse which simultaneously helps improve expression manageability and intention articulation. For instance, if you find yourself wanting to add current time in epoch format in many pipelines then it’s more convenient to just define a single Global Variable and call it whenever you need it, as opposed to typing the original expression every single time. LogStream ships with an expression variable named: unixtime. Go to Knowledge > Global Variables and expand unixtime:

gv-definition-1024x380

Adding a field with Eval, is as easy as calling C.vars.unixtime():

gv-pipeline-1024x309

Another example with a longer expression

Suppose we wanted to convert a field holding a byte size into a human-readable size. E.g., 2048 -> 2KB. We can do that with a simple value expression in Eval. But it’s much easier to have a re-usable definition via Global Variables and call it whenever we need it by passing the field of interest as an argument.

gv-definition-2-1024x529

 

In this example, we’re creating a new field called filesizeHuman by passing filesize to the Global Variable as such: C.vars.convertBytes(filesize):

gv-pipeline2-1024x348

Notice how long the expression is, typing it every single time would be tedious and prone to typos or mistakes. Using a Global Variable we now have a single definition that is easy to control, manage and update.

One more example – adding internal fields to events

Say we wanted to decorate each event with the inputId of the Source they came from and the LogStream hostname of the Worker that processed it. We can define a Global Variable expression with the following: {'cribl_host': C.os.hostname(), 'cribl_inputId': __inputId} and then assign it to a field in an Eval function.

gv-definition4-1024x387

The expression evaluates a simple object with the hostname and inputId as below:

gv-pipeline4-1024x379

One last example, this time with an array

Suppose we wanted to drop all events from any pipeline in the system if they match a certain log level – for example, all DEBUG events. Suppose further that later we have a new requirement to drop all TRACE level events, too. As above, we can first add a Drop function with a Filter expression set to level=='DEBUG' and later go back to each and every pipeline and modify it to   level=='DEBUG' || level=='TRACE'. Or, we can use a Global Variable as a Filter expression, for instance:

gv-definition3-1024x360

C.vars.dropLogLevel is defined as an array and the includes() method can be used. For good measure, level can be converted to lowercase before the check.

gv-pipeline3-e1583712929399-1024x252

Using Global Variable decouples definition from the application and allows us to control the expression from a single place instead of modifying all the pipelines where it’s applied at.

New to Cribl LogStream? Take our Sandbox for a drive!

If you have any questions or feedback join our community Slack– we’d love to help you out. If you’re looking to join a fast-paced, innovative team drop us a line at hello@cribl.io– we’re hiring!

Cribl, the Data Engine for IT and Security, empowers organizations to transform their data strategy. Customers use Cribl’s suite of products to collect, process, route, and analyze all IT and security data, delivering the flexibility, choice, and control required to adapt to their ever-changing needs.

We offer free training, certifications, and a free tier across our products. Our community Slack features Cribl engineers, partners, and customers who can answer your questions as you get started and continue to build and evolve. We also offer a variety of hands-on Sandboxes for those interested in how companies globally leverage our products for their data challenges.

More from the blog

get started

Choose how to get started

See

Cribl

See demos by use case, by yourself or with one of our team.

Try

Cribl

Get hands-on with a Sandbox or guided Cloud Trial.

Free

Cribl

Process up to 1TB/day, no license required.