Azure 日志 api JavaScript 样本
Azure log api JavaScript sample
我想登录到 Azure Log Analytics 工作区。我可以看到几个如何记录 here 的例子。没有时间研究转换给定的样本 我想知道是否有人使用 JavaScript 开发了一个他们愿意分享的类似样本?
根据我的测试,如果要调用HTTP Data Collector API,请参考以下步骤。详情请参考document.
az login
az account set --subscription <the subscription you use>
az monitor log-analytics workspace get-shared-keys --resource-group <> --workspace-name <>
- 代码
var crypto = require("crypto")
var request = require('request');
async function main() {
var json = [
{
slot_ID: 12345,
ID: "5cdad72f-c848-4df0-8aaa-ffe033e75d57",
availability_Value: 100,
performance_Value: 6.954,
measurement_Name: "last_one_hour",
duration: 3600,
warning_Threshold: 0,
critical_Threshold: 0,
IsActive: "true",
},
{
slot_ID: 67890,
ID: "b6bee458-fb65-492e-996d-61c4d7fbb942",
availability_Value: 100,
performance_Value: 3.379,
measurement_Name: "last_one_hour",
duration: 3600,
warning_Threshold: 0,
critical_Threshold: 0,
IsActive: "false",
}
];
var body = JSON.stringify(json);
var contentLength =Buffer.byteLength(body,'utf8');
var key = ''
var workspaceId = '';
var apiVersion = '2016-04-01';
var GMTTime = new Date().toUTCString()
var StringToSign = 'POST' + '\n'
+ contentLength+'\n'
+ 'application/json' + '\n'
+'x-ms-date:'+ GMTTime + '\n'
+ '/api/logs'
var Sig = crypto.createHmac('sha256', Buffer.from(key, 'base64')).update(StringToSign, 'utf-8').digest('base64');
var authorization = 'SharedKey ' + workspaceId + ':' + Sig;
var headers = {
"Content-Type":"application/json",
"Authorization": authorization,
"Log-Type": 'demoexample',
"x-ms-date": GMTTime,
"time-generated-field" : new Date().toISOString
};
var url = 'https://' + workspaceId + '.ods.opinsights.azure.com/api/logs?api-version=' + apiVersion;
request.post({ url: url, headers: headers, body: body }, function (error, response){
if (error){
console.log(error)
}else{
console.log(response.statusCode + " " + response.statusMessage)
}
})
}
main()
我想登录到 Azure Log Analytics 工作区。我可以看到几个如何记录 here 的例子。没有时间研究转换给定的样本 我想知道是否有人使用 JavaScript 开发了一个他们愿意分享的类似样本?
根据我的测试,如果要调用HTTP Data Collector API,请参考以下步骤。详情请参考document.
az login
az account set --subscription <the subscription you use>
az monitor log-analytics workspace get-shared-keys --resource-group <> --workspace-name <>
- 代码
var crypto = require("crypto")
var request = require('request');
async function main() {
var json = [
{
slot_ID: 12345,
ID: "5cdad72f-c848-4df0-8aaa-ffe033e75d57",
availability_Value: 100,
performance_Value: 6.954,
measurement_Name: "last_one_hour",
duration: 3600,
warning_Threshold: 0,
critical_Threshold: 0,
IsActive: "true",
},
{
slot_ID: 67890,
ID: "b6bee458-fb65-492e-996d-61c4d7fbb942",
availability_Value: 100,
performance_Value: 3.379,
measurement_Name: "last_one_hour",
duration: 3600,
warning_Threshold: 0,
critical_Threshold: 0,
IsActive: "false",
}
];
var body = JSON.stringify(json);
var contentLength =Buffer.byteLength(body,'utf8');
var key = ''
var workspaceId = '';
var apiVersion = '2016-04-01';
var GMTTime = new Date().toUTCString()
var StringToSign = 'POST' + '\n'
+ contentLength+'\n'
+ 'application/json' + '\n'
+'x-ms-date:'+ GMTTime + '\n'
+ '/api/logs'
var Sig = crypto.createHmac('sha256', Buffer.from(key, 'base64')).update(StringToSign, 'utf-8').digest('base64');
var authorization = 'SharedKey ' + workspaceId + ':' + Sig;
var headers = {
"Content-Type":"application/json",
"Authorization": authorization,
"Log-Type": 'demoexample',
"x-ms-date": GMTTime,
"time-generated-field" : new Date().toISOString
};
var url = 'https://' + workspaceId + '.ods.opinsights.azure.com/api/logs?api-version=' + apiVersion;
request.post({ url: url, headers: headers, body: body }, function (error, response){
if (error){
console.log(error)
}else{
console.log(response.statusCode + " " + response.statusMessage)
}
})
}
main()