<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Luke</title>
    <description>The latest articles on DEV Community by Luke (@lroberts).</description>
    <link>https://dev.to/lroberts</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F145079%2F73b4ae86-8f3a-47b6-9392-ebbca6a633b8.jpg</url>
      <title>DEV Community: Luke</title>
      <link>https://dev.to/lroberts</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lroberts"/>
    <language>en</language>
    <item>
      <title>Master CloudWatch with Insights</title>
      <dc:creator>Luke</dc:creator>
      <pubDate>Mon, 13 Jul 2020 10:58:51 +0000</pubDate>
      <link>https://dev.to/lroberts/master-cloudwatch-with-insights-coc</link>
      <guid>https://dev.to/lroberts/master-cloudwatch-with-insights-coc</guid>
      <description>&lt;p&gt;Depending on your past experience with CloudWatch, you might have no experience with it at all, you might have used it once or twice when Lambda automatically threw your logs into it, or, like me, this might be the usual view you get of it:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lNyuO-Eo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8xhx4lqakzmjvhsj78nk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lNyuO-Eo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8xhx4lqakzmjvhsj78nk.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hundreds of lines of logs and absolute agony to search for anything specific due to the simplistic search function. On top of this, what happens when you have a large number of services, each outputting their logs to a separate log group? Unfortunately Cloudwatch doesn’t allow you to view multiple log groups at once, making it impractical for large-scale applications. This is where a platform such as Insights comes in.&lt;/p&gt;

&lt;p&gt;CloudWatch Log Insights is a fully managed service to assist you in searching and visualizing logs that can be piped into CloudWatch from a number of sources including AWS services, EC2 instances, and even external services, all through queries in Insights' new ad-hoc language.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting to it
&lt;/h1&gt;

&lt;p&gt;To get started with Insights, open up the &lt;a href="https://console.aws.amazon.com/cloudwatch/home#logs-insights"&gt;Insights page&lt;/a&gt; and select the log group you want to run a query on through the dropdown at the top:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iwD_Rcsu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ko6o2lec7mekhbp46ovr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iwD_Rcsu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ko6o2lec7mekhbp46ovr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Unlike when accessing logs directly through their log group, Insights also allows you to select multiple log groups to run queries on, great for searching across environments or over multiple functions or services!&lt;/p&gt;

&lt;p&gt;You might also want to select a timeframe if you don't want to stick with the default of showing logs within the last hour. Using the custom dropdown you can enter either an absolute date range, or a relative time. This can be super convenient whether you're debugging an error you've just triggered, or investigating logs around a reported error. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tGpCEm4x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0nbfsv4bleb6rei6i3un.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tGpCEm4x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0nbfsv4bleb6rei6i3un.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's start out with a simple query, and then break it down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fields @timestamp, @message | sort @timestamp desc | limit 25
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;fields @timestamp, @message&lt;/code&gt;: fetch only the &lt;code&gt;@timestamp&lt;/code&gt; and &lt;code&gt;@message&lt;/code&gt; fields&lt;/p&gt;

&lt;p&gt;&lt;code&gt;|&lt;/code&gt;: apply a further command to the result set&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sort @timestamp desc&lt;/code&gt;: sort by the &lt;code&gt;@timestamp&lt;/code&gt; field, descending&lt;/p&gt;

&lt;p&gt;&lt;code&gt;limit 25&lt;/code&gt;: once 25 results have been fetched, stop searching and return those rows.&lt;/p&gt;

&lt;p&gt;So in essence, this query returns the timestamps and messages for the 25 most recent rows in your log group.&lt;/p&gt;

&lt;p&gt;Moving onto using the kinds of commands that should really start to get you excited about Insights, statistics!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;filter @message like /error/ 
| stats count(*) as errorCount by bin(1h)
| sort errorCount desc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we're using the &lt;code&gt;stats&lt;/code&gt; command which allows us to pass statistics functions such as &lt;code&gt;max&lt;/code&gt;, &lt;code&gt;sum&lt;/code&gt;, &lt;code&gt;avg&lt;/code&gt;, or in our case, &lt;code&gt;count&lt;/code&gt;, meaning each datapoint will be the number of rows containing the string &lt;code&gt;error&lt;/code&gt;, grouped by the hour. This query might show you something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ATnCcpdB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9raa4fcj75d5dkkbdfyc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ATnCcpdB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9raa4fcj75d5dkkbdfyc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or if you open up the Visualization tab, you can get a much more useful representation of the data, which can be displayed as a line, bar, or stacked area chart:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b_kYHLnH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gyf1hl05y9hzdqqwyy38.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b_kYHLnH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/gyf1hl05y9hzdqqwyy38.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another powerful command you can use is &lt;code&gt;parse&lt;/code&gt;, this allows you to &lt;em&gt;parse&lt;/em&gt; fields in your logs and translate their contents into columns manipulable within your command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;filter @message like /errorType/
| parse @message '"errorType":"*"' as @errorType
| stats count(@errorType) by bin(1h), @errorType
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This command fetches only rows containing the phrase &lt;code&gt;errorType&lt;/code&gt;, then parses that field out of the JSON body within the log, mapping it to the &lt;code&gt;@errorType&lt;/code&gt; variable, which we can then perform further commands on, such as by counting the number of each type of error, grouped by the hour.&lt;/p&gt;

&lt;p&gt;CloudWatch Insights provides a fairly simple solution, saving you from the effort of setting up and managing your own logging solution, while still being powerful and offering most of the core features you'd expect from a logging platform. There are, of course, some drawbacks of Insights, such as the limited visualization capabilities compared to other logging solutions such as an ELK stack. As well as this you have to face up to the requirement of learning a new query language, compared to the more convenient search capabilities provided by some logging solutions.&lt;/p&gt;

&lt;p&gt;Hopefully you've now enough information about the capabilities of Cloudwatch Log Insights to navigate your way around the tool. You're now monitoring your application logs much more efficiently, but there’s still a ways to go before you’ve covered everything, and there are plenty &lt;a href="https://coralogix.com/log-analytics-blog/devops-monitoring/"&gt;other sections of your applications&lt;/a&gt; that need to be watched!&lt;/p&gt;




&lt;p&gt;Luke writes about AWS and Log Analytics for &lt;a href="https://coralogix.com/"&gt;Coralogix&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>cloudwatch</category>
      <category>insights</category>
      <category>visualization</category>
      <category>logging</category>
    </item>
  </channel>
</rss>
