LoginSignup
1
1

More than 1 year has passed since last update.

Datadog APIをGASから叩く

Posted at

GASのUrlFetchAppでDatadog APIを叩く

ダッシュボードAPIの例

ダッシュボードのJSONを取得する

main.gs
let option = {
  'method' : 'GET',
  'contentType' : 'application/json',
  'headers' : {
    'DD-API-KEY' : APIキー,
    'DD-APPLICATION-KEY' : アプリケーションキー
  }
};

let response = UrlFetchApp.fetch('https://api.datadoghq.com/api/v1/dashboard/' + 'ダッシュボードID', options);
let json = response.getContentText();

// ダッシュボード内に日本語などのマルチバイト文字がある場合はunicodeエスケープを戻すこと
json = json.replace(/\\u([a-fA-F0-9]{4})/g, function(x,y){
  return String.fromCharCode(parseInt(y,16));
}

// 配列で扱う場合はパースしてね
json = JSON.parse(json);

ダッシュボードを更新する

main.gs
let option = {
  'method' : 'PUT',
  'contentType' : 'application/json',
  'headers' : {
    'DD-API-KEY' : APIキー,
    'DD-APPLICATION-KEY' : アプリケーションキー
  },
  'payload' : JSON文字列
};

UrlFetchApp.fetch('https://api.datadoghq.com/api/v1/dashboard/' + 'ダッシュボードID', options);

ダッシュボード一覧を取得してスプレッドシートに出力する

main.gs
let option = {
  'method' : 'GET',
  'contentType' : 'application/json',
  'headers' : {
    'DD-API-KEY' : APIキー,
    'DD-APPLICATION-KEY' : アプリケーションキー
  }
};

let response = UrlFetchApp.fetch('https://api.datadoghq.com/api/v1/dashboard/' + 'ダッシュボードID', options);
let json = JSON.parse(response.getContentText());

// 必要な情報だけ2次配列として格納する
let dashboard_list = [];
for(let i=0; i < json.length; i++){
  let value1 = json[i].id;
  let value2 = json[i].title;
  let value3 = json[i].description;
  let value4 = json[i].author_handle;

  dashboard_list.push([value1,value2,value3,value4]);
}

// スプレッドシートに書き込み
let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
let sheet = spreadsheet.getActiveSheet();
let row = dashboard_list.length;
let col = dashboard_list[0].length;

sheet.getRange(任意のセル行, 任意のセル列, row, col).setValues(dashboard_list);

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1