跳到主要內容

Google Cloud Monitor Customer Metrics

Google Cloud Monitor在結合StackDriver之後,提供了一個強大且彈性的Cloud Monitor API,並且允許客制監控,Google稱為Customer Metrics...
透過Cloud Monitor API可以以RESTful方式輸入Customer Metrics,然後在StackDriver內取用來作為圖表以及監控的資料來源...

在Customer Metrics在實作上分成Lightweight custom metric與Labeled custom metric兩種,分別在資料內容上有些許的差別:

Lightweight custom metric以數值+時間為主,資料內容大致如下:
$3,918.33 on March 20.
$4,107.68 on March 21.
$3,798.00 on March 22.

而Labeled custom metric以數值+時間+描述為主,資料內容大致如下:
$23,213.97 at the "Columbus" store on January 10.
$26,087.44 at the "Magellan" store on January 10.
$19,650.00 at the "Columbus" store on January 11.

因此只要能夠抽取需要監控的數值與當下的時間,就可以開始監控 :D
下面是針對Customer Metrics的Lightweight customer metric來做資料輸入的片段Node.js程式:

var request = require('request');
var auth = require('google-api-utility')
  , request = auth.request
  , util = require('util')
  , project = 'your-project-id';

auth.init({
  key_pem: '/path/to/your/pemkey/xxx.pem'
});

var url = {
  writeTimeSeries: 'https://www.googleapis.com/cloudmonitoring/v2beta2/projects/' + project + '/timeseries:write'
}

var data = {
 "timeseries": [
  {
   "timeseriesDesc": {
    "project": project,
    "metric": "custom.cloudmonitoring.googleapis.com/daily_sales"
   },
   "point": {
    "start": "2015-06-11T10:52:00.000Z",
    "end": "2015-06-11T10:52:00.000Z",
    "doubleValue": 2228.74
   }
  }
 ]
}

request({
  url: url.writeTimeSeries,
  json: data,
  method: 'POST'
}, function(e, r, d){
  if(e) console.log('[ERROR] ',e);
  console.log(d);
});


其中data部分是寫入的資料格式內容,我們定義這個metric名稱為daily_sales,並且在point中指定某個數值與發生時間區間...
以上面的程式再多寫幾筆資料後,我們就可以到StackDriver中拉出該Metric的報表(可以開個Dashboard並且在其中Add Chart...)


然後再出現的圖表中,就可以看到該Metric數值所呈現的圖表:


再進一步就是去設定一些Alert了... 在StackDriver中支援Email, SMS, PageDuty...等數種通知的方式,可以好好規劃一下哪些資訊需要Alert了唷!

留言

這個網誌中的熱門文章

透過Google Apps Script結合Google Form做即時郵件通知

體驗過Google Apps Script的功能後,也發現他結合GmailApps的模組 GmailApps的應用可以用在表單填寫完成後,做發信的通知 例如您開立了一個訂購的表單,為了要在第一時間通知商家有訂單進入 就可以直接呼叫Gmail做發信的通知,讓手持Smart Phone的我們可以很快的知道生意上門了! 下面規劃三個function,其中: onCommit():為form commit時候觸發的function,需要掛載於form commit trigger上 jsonArrToTable():目的將json array解析成為一個Table getLastRowTable():目的將整個table的回傳過濾為剩下第一筆(表頭,含有Form的欄位說明)與最後一筆(原則上就是剛剛送出的那一筆表單) 完整程式碼如下: function onCommit(){   var sheet = SpreadsheetApp.getActiveSheet();   var rows = sheet.getDataRange();   var numRows = rows.getNumRows();   var values = rows.getValues();   var content = getLastRowTable(values);   var htmlBody = "Hi Admin: <br/><br/>有訂單拉,檢查一下吧! <br/><br/>" + content + '<br/><br/>Send by Google Apps';   GmailApp.sendEmail(     " your-email-address@gmail.com ",      "Order Confirm Notice",      htmlBody,      {from: ' from-email-...

Share a chrome plugin for manage google cloud platform

好玩意兒報報.... 同事的新作,把Google Project List在Chrome Plugin中! 對一次管理多個專案的人來說,真得超方便的拉! 下載: https://chrome.google.com/webstore/detail/gdclauncher/bicgkglnnilldakpenngnblekooejnpg 使用說明: 1. Use browser url bar to quick search: Type "gdcl" in browser Press "TAB" to start search Type the project id key word then select the search result... 2. Using quick launch bar... You can search by keyword or click project name to go to the project or gae link to go to gae or go to billing page....

Cloud Monitor嚐鮮

GCP上,我們非常想要的一個功能,終於問世.... Cloud Monitor來了!