onclick to view logs of a trace id
当前为
// ==UserScript==
// @name datadog-traceId
// @namespace http://tampermonkey.net/
// @version 0.1.2
// @description onclick to view logs of a trace id
// @author You
// @match https://app.datadoghq.com/logs*
// @grant unsafeWindow
// @run-at document-end
// ==/UserScript==
function buildAttrbutes() {
const rows = document.querySelectorAll('.log_raw-json tr');
const attrs = {};
for (let i = 0; i < rows.length; i += 1) {
const row = rows[i];
const cells = row.innerText.split(/\s+/ig);
if (cells.length === 2) {
attrs[cells[0]] = {
row: row,
value: cells[1]
};
}
}
return attrs;
}
function clearExistButton() {
const btn = document.querySelector('#btnOpenTraceIdView');
if (btn) btn.remove();
}
function findTraceId() {
const attributes = buildAttrbutes();
const traceIdObj = attributes['traceId'];
const timestampObj = attributes['@timestamp'];
if (!traceIdObj || !traceIdObj.row || !timestampObj) {
return;
}
const timestamp = timestampObj.value;
const traceId = traceIdObj.value;
const traceIdRow = traceIdObj.row;
if (traceIdRow.lastTraceId != traceId) {
clearExistButton();
const button = document.createElement('button');
const divBox = document.querySelector('.ui_layout_expandable-block__content');
const boxPreClass = divBox.childNodes[0];
button.innerText = 'Open Logs of traceId';
button.id = 'btnOpenTraceIdView';
button.onclick = () => {
const startMilliSec = new Date(timestamp).getTime() - 10 * 60 * 1000;
const destMilliSec = new Date(timestamp).getTime() + 10 * 60 * 1000;
const url = `https://app.datadoghq.com/logs?cols=core_host%2Ccore_service&from_ts=${startMilliSec}&index=main&live=false&messageDisplay=inline&query=%40traceId%3A${traceId}&stream_sort=desc&to_ts=${destMilliSec}`
unsafeWindow.open(url, '_blank');
};
button.style.padding = '5px';
button.style.marginBottom = '3px';
button.style.borderColor = '#ccc';
button.style.cursor = 'pointer';
divBox.insertBefore(button, boxPreClass);
rowOfTraceId.lastTraceId = traceId;
}
}
(function () {
'use strict';
setInterval(() => {
findTraceId();
}, 1000);
})();