{"id":904,"date":"2020-03-31T18:08:19","date_gmt":"2020-04-01T01:08:19","guid":{"rendered":"http:\/\/blog.nillsf.com\/?p=904"},"modified":"2020-03-31T18:09:03","modified_gmt":"2020-04-01T01:09:03","slug":"custom-metrics-in-azure-monitor","status":"publish","type":"post","link":"https:\/\/nillsf.com\/index.php\/2020\/03\/31\/custom-metrics-in-azure-monitor\/","title":{"rendered":"Custom metrics in Azure monitor"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I had a conversation today about custom metrics in Azure. I have worked with customers in the past that have streamed custom metrics into Azure Log Analytics to process them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This was a case where a customer had done more  research than me, and had discovered that it&#8217;s possible to <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/azure-monitor\/platform\/metrics-custom-overview\">send custom metrics into Azure Monitor<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since this was completely foreign to me, I decided to give a spin and try it out.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up a VM to stream custom metrics<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To start of the process, I&#8217;ll create a new VM, and assign it a managed identity. I&#8217;ll use this managed identity to provide the RBAC role required to stream custom metrics. Using managed identity, I can use the instance metadata service to get a token that I&#8217;ll use to send the data. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"808\" height=\"1024\" src=\"\/wp-content\/uploads\/2020\/03\/image-44-808x1024.png\" alt=\"\" class=\"wp-image-905\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-44-808x1024.png 808w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-44-237x300.png 237w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-44-768x973.png 768w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-44.png 862w\" sizes=\"auto, (max-width: 808px) 100vw, 808px\" \/><figcaption>Enabling a managed identity<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Once the VM is created (which only took me 23 seconds!!!), we&#8217;ll assign the  <strong>Monitoring Metrics Publisher<\/strong> role to that VM in the IAM blade. <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"471\" height=\"922\" src=\"\/wp-content\/uploads\/2020\/03\/image-45.png\" alt=\"\" class=\"wp-image-906\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-45.png 471w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-45-153x300.png 153w\" sizes=\"auto, (max-width: 471px) 100vw, 471px\" \/><figcaption>Adding the IAM role assignment.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">With that done, we should be able to log into to the VM and publish custom metrics.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sending custom metrics<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To send custom metrics, we&#8217;ll need to send the metrics to:  <code>https:\/\/&lt;azureregion>.monitoring.azure.com\/&lt;AzureResourceID>\/metrics<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll need to be authenticated to access that endpoint, so we&#8217;ll tackle that as well. Let&#8217;s first get the region information and the resource ID. To get those, we&#8217;ll use the Instance Metadata Service. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>IMDS=$(curl -H Metadata:true \"http:\/\/169.254.169.254\/metadata\/instance?api-version=2019-06-01\")\nLOCATION=$(echo $IMDS | jq .compute.location | sed -e 's\/^\"\/\/' -e 's\/\"$\/\/')\nRESOURCEID=$(echo $IMDS | jq .compute.resourceId | sed -e 's\/^\"\/\/' -e 's\/\"$\/\/')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we&#8217;ll get the authentication token to get from our managed identity. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>TOKEN=$(curl 'http:\/\/169.254.169.254\/metadata\/identity\/oauth2\/token?api-version=2018-02-01&amp;resource=https%3A%2F%2Fmonitoring.azure.com%2F' -H Metadata:true -s | jq .access_token | sed -e 's\/^\"\/\/' -e 's\/\"$\/\/')<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And with that, we should be able to send data to the custom metric. Let&#8217;s first construct a custom metric that we&#8217;ll want to send. For this, we&#8217;ll create a json file with a sample metric. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>DATE=$(date -u +\"%Y-%m-%dT%H:%M:%S\")\necho '{ \n    \"time\": \"'$DATE'\", \n    \"data\": { \n        \"baseData\": { \n            \"metric\": \"NillsCustomMetric\", \n            \"namespace\": \"CustomMetrics\", \n            \"series\": &#91; \n              { \n                \"min\": 3, \n                \"max\": 20, \n                \"sum\": 28, \n                \"count\": 3 \n              } \n            ] \n        } \n    } \n}' > custom.json<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> Now that we have our custom metric as a JSON file, we can upload it to the custom metric API. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -X POST https:\/\/$LOCATION.monitoring.azure.com$RESOURCEID\/metrics -H \"Content-Type: application\/json\" -H \"Authorization: Bearer $TOKEN\" -d @custom.json<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And then, after giving it a minute, the custom metric shows up:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"862\" height=\"810\" src=\"\/wp-content\/uploads\/2020\/03\/image-46.png\" alt=\"\" class=\"wp-image-907\" srcset=\"https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-46.png 862w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-46-300x282.png 300w, https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-46-768x722.png 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><figcaption>Custom metric showing up<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And this wraps up what I wanted to demonstrate. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">As I mentioned in the intro, I wasn&#8217;t aware about custom metrics in Azure monitor. I had streamed custom metrics into Log Analytics before, but this seems like a suitable approach as well for metric data. It comes with the Azure dashboarding built-in, and allows you to setup custom alert rules as well based on those metrics. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a conversation today about custom metrics in Azure. I have worked with customers in the past that have streamed custom metrics into Azure Log Analytics to process them. This was a case where a customer had done more research than me, and had discovered that it&#8217;s possible to send custom metrics into Azure [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":907,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,3,4],"tags":[100,99,76],"class_list":["post-904","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-azure","category-devops","category-management","tag-azure-monitor","tag-custom-metric","tag-monitoring"],"jetpack_featured_media_url":"https:\/\/nillsfblog.blob.core.windows.net\/media\/2020\/03\/image-46.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/comments?post=904"}],"version-history":[{"count":1,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/904\/revisions"}],"predecessor-version":[{"id":908,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/posts\/904\/revisions\/908"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/media\/907"}],"wp:attachment":[{"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/media?parent=904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/categories?post=904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nillsf.com\/index.php\/wp-json\/wp\/v2\/tags?post=904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}