UNPKG

@beincom/node-profile

Version:

``` npm install @beincom/node-profile # yarn add @beincom/node-profile ```

89 lines 3.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PyroscopeApiExporter = void 0; const common_1 = require("@nestjs/common"); const node_url_1 = require("node:url"); const pprof_1 = require("@datadog/pprof"); const axios_1 = __importDefault(require("axios")); const form_data_1 = __importDefault(require("form-data")); const utils_1 = require("../utils"); class PyroscopeApiExporter { constructor(applicationName, authToken, serverAddress, config) { this.applicationName = applicationName; this.authToken = authToken; this.serverAddress = serverAddress; this.config = config; } async export(profileExport) { await this.uploadProfile(profileExport); } buildEndpointUrl(profileExport) { const endpointUrl = new node_url_1.URL(`${this.serverAddress}/ingest`); endpointUrl.searchParams.append('from', (0, utils_1.dateToUnixTimestamp)(profileExport.startedAt).toString()); endpointUrl.searchParams.append('name', this.applicationName); endpointUrl.searchParams.append('spyName', 'nodespy'); endpointUrl.searchParams.append('until', (0, utils_1.dateToUnixTimestamp)(profileExport.stoppedAt).toString()); if (profileExport.sampleRate !== undefined) { endpointUrl.searchParams.append('sampleRate', profileExport.sampleRate.toString()); } return endpointUrl; } buildRequestHeaders(formData) { const headers = formData.getHeaders(); if (this.authToken !== undefined) { headers['authorization'] = `Bearer ${this.authToken}`; } if (this.config.tenantID) { headers['X-Scope-OrgID'] = this.config.tenantID; } return headers; } async buildUploadProfileFormData(profile) { const processedProfile = (0, utils_1.processProfile)(profile); const profileBuffer = await (0, pprof_1.encode)(processedProfile); const formData = new form_data_1.default(); formData.append('profile', profileBuffer, { contentType: 'text/json', filename: 'profile', knownLength: profileBuffer.byteLength, }); return formData; } handleAxiosError(error) { if (error.response !== undefined) { common_1.Logger.log('Pyroscope received error while ingesting data to server'); common_1.Logger.log(error.response.data); } else if (error.request !== undefined) { common_1.Logger.log('Error when ingesting data to server:', error.message); } else { common_1.Logger.log('Error', error.message); } } async uploadProfile(profileExport) { const formData = await this.buildUploadProfileFormData(profileExport.profile); const auth = this.config.basicAuthUser && this.config.basicAuthPassword ? { username: this.config.basicAuthUser, password: this.config.basicAuthPassword, } : undefined; try { await (0, axios_1.default)(this.buildEndpointUrl(profileExport).toString(), { data: formData, headers: this.buildRequestHeaders(formData), method: 'POST', auth: auth, }); } catch (error) { this.handleAxiosError(error); } } } exports.PyroscopeApiExporter = PyroscopeApiExporter; //# sourceMappingURL=pyroscope-api-exporter.js.map