<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>loki on Jack Henschel's Blog</title><link>https://blog.cubieserver.de/tags/loki/</link><description>Recent content in loki on Jack Henschel's Blog</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Sat, 21 Oct 2023 00:00:00 +0000</lastBuildDate><atom:link href="https://blog.cubieserver.de/tags/loki/index.xml" rel="self" type="application/rss+xml"/><item><title>Shipping logs to Grafana Cloud with Vector</title><link>https://blog.cubieserver.de/2023/shipping-logs-to-grafana-cloud-with-vector/</link><pubDate>Sat, 21 Oct 2023 00:00:00 +0000</pubDate><guid>https://blog.cubieserver.de/2023/shipping-logs-to-grafana-cloud-with-vector/</guid><description>&lt;p>&lt;a href="https://vector.dev/">Vector&lt;/a> is a new, lightweight, resource-efficient and flexible log collector, aggregator and forwarder.
I have &lt;a href="https://blog.cubieserver.de/tags/vector/">written about Vector before&lt;/a> and after using for a couple of different projects, I&amp;rsquo;m really fond of the characteristics of the tool, its documentation and ease of use.&lt;/p>
&lt;p>&lt;em>Ease of use&lt;/em> also applies to Grafana Cloud: instead of having to set up Prometheus, Loki, Grafana et al. yourself (and keep them up to date, monitor resource usage, &amp;hellip;), all these services are available online - Grafana Cloud even includes a generous free tier!
While there is various documentation on how to set up the &lt;a href="https://grafana.com/oss/agent/">Grafana Agent&lt;/a> for shipping logs to their managed Loki service, I couldn&amp;rsquo;t find much for third-party tools.
As you will see in this post, the setup is not difficult, it just took me a couple of attempts to find the correct settings, thus I want to share them here.&lt;/p>
&lt;h2 id="create-access-policy">
&lt;a href="#create-access-policy">#
&lt;/a>&amp;nbsp;Create Access Policy
&lt;/h2>
&lt;p>An &lt;em>Access Policy&lt;/em> is a rule that allows (or denies) certain actions in your Grafana Cloud environment.
From the Grafana Cloud Portal, switch to the &amp;ldquo;Security&amp;rdquo; &amp;gt; &amp;ldquo;Access Policies&amp;rdquo; tab and create a new one named &amp;ldquo;Push Logs&amp;rdquo; with the following settings:&lt;/p>
&lt;ul>
&lt;li>Realms: &lt;em>name of your stack&lt;/em>&lt;/li>
&lt;li>Scopes: &lt;code>Logs:Write&lt;/code>&lt;/li>
&lt;/ul>
&lt;p>&lt;img src="./access-policy.png" alt="">&lt;/p>
&lt;p>Then add a new token for this access policy: give the token a descriptive name (such as &lt;code>homelab-vector-log-forwarding&lt;/code>) so you can later identify where this token is used and for what purpose.&lt;/p>
&lt;p>&lt;img src="./create-new-token.png" alt="">&lt;/p>
&lt;p>Make sure you copy the &lt;code>token&lt;/code> that gets shown after clicking on &amp;ldquo;Create&amp;rdquo;.&lt;/p>
&lt;p>The final result should look roughly like this:&lt;/p>
&lt;p>&lt;img src="./overview-access-policy.png" alt="">&lt;/p>
&lt;h2 id="configure-vector">
&lt;a href="#configure-vector">#
&lt;/a>&amp;nbsp;Configure vector
&lt;/h2>
&lt;p>Vector comes with a built-in &amp;ldquo;sink&amp;rdquo; (destination) for sending logs to a Loki instance.
While the &lt;a href="https://vector.dev/docs/reference/configuration/sinks/loki/">documentation&lt;/a> outlines all possible configuration settings, it took me quite a while to find the right combination of settings that allowed sending logs to Grafana Cloud.
Sending to a managed Loki service also requires a few more configuration settings compared to a locally hosted, unauthenticated instance (see for example &lt;a href="https://ilhicas.com/2023/01/29/Using-vector-to-centralize-logs-in-loki.html">here&lt;/a>).&lt;/p>
&lt;p>The configuration below (tested with Vector 0.32) first sets up a dummy source that produces random logs (read more about it &lt;a href="https://vector.dev/docs/reference/configuration/sources/demo_logs/">here&lt;/a>), which get consumed by the loki sink.&lt;/p>
&lt;div class="highlight">&lt;div style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">
&lt;table style="border-spacing:0;padding:0;margin:0;border:0;width:auto;overflow:auto;display:block;">&lt;tr>&lt;td style="vertical-align:top;padding:0;margin:0;border:0;">
&lt;pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 1
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 2
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 3
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 4
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 5
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 6
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 7
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 8
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f"> 9
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">10
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">11
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">12
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">13
&lt;/span>&lt;span style="margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#7f7f7f">14
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td style="vertical-align:top;padding:0;margin:0;border:0;;width:100%">
&lt;pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-toml" data-lang="toml">[&lt;span style="color:#a6e22e">sources&lt;/span>.&lt;span style="color:#a6e22e">demo&lt;/span>]
&lt;span style="color:#a6e22e">type&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;demo_logs&amp;#34;&lt;/span>
&lt;span style="color:#a6e22e">format&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;apache_common&amp;#34;&lt;/span>
[&lt;span style="color:#a6e22e">sinks&lt;/span>.&lt;span style="color:#a6e22e">loki&lt;/span>]
&lt;span style="color:#a6e22e">type&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;loki&amp;#34;&lt;/span>
&lt;span style="color:#a6e22e">inputs&lt;/span> = [&lt;span style="color:#e6db74">&amp;#34;demo&amp;#34;&lt;/span>]
&lt;span style="color:#a6e22e">endpoint&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;&amp;lt;GRAFANA_CLOUD_URL&amp;gt;&amp;#34;&lt;/span>
&lt;span style="color:#a6e22e">out_of_order_action&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;accept&amp;#34;&lt;/span>
&lt;span style="color:#a6e22e">compression&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;snappy&amp;#34;&lt;/span>
&lt;span style="color:#a6e22e">encoding&lt;/span>.&lt;span style="color:#a6e22e">codec&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;json&amp;#34;&lt;/span>
&lt;span style="color:#a6e22e">auth&lt;/span>.&lt;span style="color:#a6e22e">strategy&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;basic&amp;#34;&lt;/span>
&lt;span style="color:#a6e22e">auth&lt;/span>.&lt;span style="color:#a6e22e">user&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;&amp;lt;GRAFANA_CLOUD_USER_ID&amp;gt;&amp;#34;&lt;/span>
&lt;span style="color:#a6e22e">auth&lt;/span>.&lt;span style="color:#a6e22e">password&lt;/span> = &lt;span style="color:#e6db74">&amp;#34;&amp;lt;GRAFANA_CLOUD_TOKEN&amp;gt;&amp;#34;&lt;/span> &lt;span style="color:#75715e"># token &amp;#34;homelab-vector-log-forwarding&amp;#34;&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>The &lt;code>auth.password&lt;/code> parameter should be set to the token generated in the previous step.
It&amp;rsquo;s a good practice to note down the name of the token as well.&lt;/p>
&lt;p>The URL and user ID can be found in the Grafana Cloud Portal after clicking on &amp;ldquo;Loki&amp;rdquo; &amp;gt; &amp;ldquo;Details&amp;rdquo;:&lt;/p>
&lt;p>&lt;img src="./grafana-cloud-portal.png" alt="">
&lt;img src="./loki-settings.png" alt="">&lt;/p>
&lt;p>It is not necessary to insert any special fields or add labels to the logs for Grafana Cloud&amp;rsquo;s Loki.
Somewhat surprisingly, also the &lt;code>tenant_id&lt;/code> does not need to be specified.&lt;/p>
&lt;p>It&amp;rsquo;s recommended to the &lt;code>snappy&lt;/code> compression with &lt;code>JSON&lt;/code> formatting, as this will send logs in the efficient Protocol Buffers (&lt;em>&amp;ldquo;protobuf&amp;rdquo;&lt;/em>) binary format (instead of a text representation).&lt;/p>
&lt;p>If any of the configuration parameters is incorrect, Vector will complain about it during startup thanks to its health checks.&lt;/p>
&lt;p>Happy logging!&lt;/p></description></item></channel></rss>