datadog-traceId

onclick to view logs of a trace id

נכון ליום 28-08-2019. ראה הגרסה האחרונה.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         datadog-traceId
// @namespace    http://tampermonkey.net/
// @version      0.1.4
// @description  onclick to view logs of a trace id
// @author       You
// @match        https://app.datadoghq.com/logs*
// @grant        unsafeWindow
// @run-at       document-end
// ==/UserScript==

const baseURL = 'https://app.datadoghq.com';

function buildURL(baseURL, params) {
    const paramArr = [];
    Object.keys(params).forEach((key) => {
        paramArr.push(`${key}=${encodeURIComponent(params[key])}`);
    });
    return `${baseURL}?${paramArr.join('&')}`;
}

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 = buildURL(`${baseURL}/logs`, {
                cols: 'core_host,core_service',
                from_ts: startMilliSec,
                index: 'main',
                live: 'false',
                messageDisplay: 'inline',
                query: `@traceId:${traceId}`,
                stream_sort: 'desc',
                to_ts: destMilliSec
            });
            // eslint-disable-next-line no-undef
            unsafeWindow.open(url, '_blank');
        };
        button.className = 'ui_form_button ui_form_button--md ui_form_button--default log_export-dropdown ui_dialogs_popover167 ui_dialogs_popover__handle';

        button.style.marginBottom = '3px';

        divBox.insertBefore(button, boxPreClass);

        traceIdRow.lastTraceId = traceId;
    }
}

(function () {
    'use strict';

    setInterval(() => {
        findTraceId();
    }, 1000);
})();