Science and technology

Level up your shell historical past with Loki and fzf

Loki is an Apache 2.Zero-licensed open supply log-aggregation framework designed by Grafana Labs and constructed with great assist from a rising group. It can also be the venture I work on every single day. In this text, slightly than simply speaking about how Loki works, I’ll present a hands-on introduction to fixing actual issues with it.

The downside: a sturdy centralized shell historical past

I like my shell historical past and have at all times been a fanatical CTRL+R consumer. About a 12 months in the past, my terminal life modified without end when my peer Dieter Plaetinck launched me to the command-line fuzzy finder fzf.

Suddenly, looking out by means of instructions went from this:

To this:

While fzf considerably improved my high quality of life, there have been nonetheless some items lacking round my shell historical past:

  • Losing shell historical past when terminals shut abruptly, computer systems crash, computer systems die, complete disk encryption keys are forgotten
  • Having entry to my shell historical past from all my computer systems on all my computer systems

I consider my shell historical past as documentation: it is an vital story I do not need to lose. Combining Loki with my shell historical past helps resolve these issues and extra.

About Loki

Loki takes the intuitive label mannequin that the open supply Prometheus venture makes use of for metrics and expands it into the world of log aggregation. This allows builders and operators to seamlessly pivot between their metrics and logs utilizing the identical set of labels. Even for those who’re not utilizing Prometheus, there are nonetheless loads of causes Loki is likely to be an excellent match to your log-storage wants:

  • Low overhead: Loki doesn’t do full-text log indexing; it solely creates an index of the labels you place in your logs. Keeping a small index considerably reduces Loki’s working necessities. I am operating my loki-shell venture, which makes use of Loki to retailer shell historical past, on a Raspberry Pi utilizing just a bit over 50MB of reminiscence.
  • Low value: The log content material is compressed and saved in object shops like Amazon S3, Google Cloud Storage, Azure Blob, and even immediately on a filesystem. The purpose is to make use of storage that’s cheap and sturdy.
  • Flexibility: Loki is accessible in a single binary that may be downloaded and run immediately or as a Docker picture to run in any container atmosphere. A Helm chart is accessible to get began shortly in Kubernetes. If you demand quite a bit out of your logging instruments, check out the production setup operating at Grafana Labs. It makes use of open supply Jsonnet and Tanka to deploy the identical Loki picture as discrete constructing blocks to allow huge horizontal scaling, excessive availability, replication, separate scaling of learn and write paths, extremely parallelizable querying, and extra.

In abstract, Loki’s method is to maintain a small index of metadata about your logs (labels) and retailer the unindexed and compressed log content material in cheap object shops to make working simpler and cheaper. The software is constructed to run as a single course of and simply evolve right into a extremely accessible distributed system. You can acquire excessive question efficiency on bigger logging workloads by means of parallelization and sharding of queries—a bit like MapReduce to your logs.

In addition, this performance is accessible for anybody to make use of at no cost. As with its Grafana open observability platform, Grafana Labs is dedicated to creating Loki a totally featured, totally open log-aggregation software program anybody can use.

Get began

I am operating Loki on a Raspberry Pi on my dwelling community and storing my shell historical past offsite in an S3 bucket.

When I hit CTRL+R, Loki’s LogCLI command-line interface makes a number of batching requests which can be streamed into fzf. Here is an instance—the highest half reveals the Loki server logs on the Pi.

Ready to present it a strive? The following information will make it easier to arrange and run Loki to be built-in together with your shell historical past. Since this tutorial goals to maintain issues easy, this setup will run Loki domestically in your laptop and retailer all of the recordsdata on the filesystem.

You can discover all of this, plus details about the best way to arrange a extra elaborate set up, within the loki-shell GitHub repository.

Note that this tutorial is not going to change any present behaviors round your historical past, so your present shell historical past command and historical past settings can be untouched. Instead, this duplicates the command historical past to Loki with $PROMPT_COMMAND in Bash and precmd in Zsh. On the CTRL+R facet of issues, it overloads the perform that fzf makes use of to entry the CTRL+R command. Trying that is protected, and for those who determine you do not prefer it, simply observe the uninstall steps within the GitHub repo to take away all traces. Your shell historical past can be untouched.

Step 1: Install fzf

There are a number of methods to put in fzf, however I favor the Git method:

git clone --depth 1 ~/.fzf
~/.fzf/set up

Say sure to all of the query prompts.

If you have already got fzf put in, ensure you have the important thing bindings enabled (i.e., be certain that whenever you sort CTRL+R, fzf pops up). You can rerun the fzf set up to allow key bindings if vital.

Step 2: Install loki-shell

Like fzf, loki-shell additionally has a Git repo and set up script:

git clone --depth 1 ~/.loki-shell
~/.loki-shell/set up

First, the script creates the ~/.loki-shell listing the place all recordsdata can be stored (together with Loki information). Next, it would obtain binaries for Promtail, LogCLI, and Loki.

Then it would ask:

Do you need to set up Loki? ([y]/n)

If you have already got a centralized Loki operating for loki-shell, you can reply n; nevertheless, for this tutorial, reply y or press Enter.

There are two choices accessible for operating Loki domestically: as a Docker picture or as a single binary (with assist for including a systemd service). I like to recommend utilizing Docker if it is accessible, as I feel it simplifies operations a bit, however each work simply wonderful.

Running with Docker

To run Loki as a Docker picture:

[y] to run Loki in Docker, [n] to run Loki as a binary ([y]/n) y
Error: No such object: loki-shell
Error response from daemon: No such container: loki-shell
Error: No such container: loki-shell

If that is the primary time you are operating the set up, you’ll be able to disregard the error messages. This script will cease and exchange a operating Loki container if the model doesn’t match, which lets you rerun this script to improve Loki.

That’s it! Loki is now operating as a Docker container.

Data from Loki can be saved in ~/.loki-shell/information.

The picture runs with --restart=unless-stopped, so it would restart at reboot however will keep stopped for those who run docker cease loki-shell.

(If you are utilizing Docker, you’ll be able to skip all the way down to Shell integration.)

Running as binary

There are some ways to run a binary on a Linux system. This script can set up a systemd service. If you do not have systemd, you’ll be able to nonetheless use the binary set up:

[y] to run Loki in Docker, [n] to run Loki as a binary ([y]/n) n

Run Loki with systemd? ([y]/n) n

This is so far as this script can take you
You might want to setup an auto-start for Loki
It might be run with this command: /dwelling/username/.loki-shell/bin/loki -config.file=/dwelling/username/.loki-shell/config/loki-binary-config.yaml

The script will spit out the command it’s good to use to run Loki, and you may be by yourself to arrange an init script or one other methodology of auto-starting it.

You can run the command immediately, if you’d like, and run Loki out of your present shell.

If you do have systemd, you may have the choice of letting the script set up the systemd service or exhibiting you the instructions to run it your self:

Run Loki with systemd? ([y]/n) y

Installing the systemd service requires root permissions.
[y] to run these instructions with sudo [n] to print out the instructions and you may run them your self. ([y]/n) n
sudo cp /dwelling/ed/.loki-shell/config/loki-shell.service /and so forth/systemd/system/loki-shell.service
sudo systemctl daemon-reload
sudo systemctl allow loki-shell
sudo systemctl begin loki-shell
Copy these instructions and run them when the script finishes. (press enter to proceed)

Shell integration

Regardless of the way you put in Loki, you must now see a immediate:

Enter the URL to your Loki server or press enter for default (http://localhost:4100)

If you had arrange a centralized Loki, you’ll enter that URL right here. However, this demo simply makes use of the default, so you’ll be able to press Enter.

A variety of textual content will spit out explaining all of the entries added to your ~.bashrc or ~.zshrc (or each).

That’s it!

Finished. Restart your shell or reload config file.
   supply ~/.bashrc  # bash
   supply ~/.zshrc   # zsh

Step three: Try it out!

Start utilizing your shell, and use CTRL+R to see your instructions.

Open a number of terminal home windows, sort a command in a single and CTRL+R in one other, and you will see your instructions accessible instantly.

Also, discover that whenever you change between terminals and enter instructions, they’re accessible instantly with CTRL+R, however the Up arrow’s operation is just not affected between terminals. (This might not be true if in case you have Oh My Zsh put in, because it robotically appends all instructions to the historical past.)

Use CTRL+R a number of occasions to toggle between sorting by time and by relevance.

Note that this configuration will present solely the present hosts’ question historical past, even if you’re sending shell information from a number of hosts to Loki. I feel by default this makes probably the most sense. There is quite a bit you’ll be able to tweak if you’d like this conduct to alter; see the loki-shell repo to study extra.

It additionally put in an alias referred to as hist:

alias hist="$HOME/.loki-shell/bin/logcli --addr=$LOKI_URL"

LogCLI can be utilized to question and search your historical past immediately in Loki, together with permitting you to go looking different hosts. Check out the getting started guide for LogCLI to study extra about querying.

Loki’s log question language (LogQL) gives metric queries that let you do some fascinating issues; for instance, I can see what number of occasions I issued the kc command (my alias for kubectl) within the final 30 days:

Install Grafana and mess around together with your shell historical past:

docker run -d -p 3000:3000 --name=grafana grafana/grafana

Open an online browser at http://localhost:3000 and log in utilizing the default admin/admin username and password.

On the left, navigate to Configuration -> Datasources, click on the Add Datasource button, and choose Loki.

For the URL, you must be capable of use http://localhost:4100 (nevertheless, on my WSL2 machine, I had to make use of the pc’s precise IP handle).

Click Save and Test. You ought to see Data supply related and labels discovered.

Click on the Explore icon on the left, be certain that the Loki information supply is chosen, and check out a question:


If you may have extra hosts sending shell instructions, you’ll be able to restrict the outcomes to a sure host utilizing the hostname label:

job="shell", hostname="myhost".

You may also search for particular instructions with filter expressions:

job="shell" |= "docker"

Or you can begin exploring the world of metrics from logs to see how usually you might be utilizing your shell:


Want to reconstruct a timeline from an incident? You can filter by a selected command and see when it ran.

To see what else you are able to do and study extra about Loki’s question language, take a look at the LogQL guide.

Final ideas

For extra concepts, troubleshooting, and updates, observe the GitHub repo. This remains to be a piece in progress, so please report any points there.

To study extra about Loki, take a look at the documentation, blog posts, and GitHub repo, or strive it in Grafana Cloud.

A particular because of my colleague Jack Baldry for planting the seed for this concept. I had the Loki data to make this occur, but when it weren’t for his suggestion, I do not suppose I ever would have made it right here.

Most Popular features the latest multimedia technologies, from live video streaming to audio packages to searchable archives of news features and background information. The site is updated continuously throughout the day.

Copyright © 2017 Breaking Express, Green Media Corporation

To Top