Hello @kyungeunni, partly yes. But after uploading source maps, i created another Error
After uploading source maps, i created another Error programmatically. Even though the sourcemaps were loaded, the wasn`t applied to new error.
If i understand correctly, you want to take a look at code, that i use to download sourcemaps to server, there bundle_filepath is used.
The version of apm is 7.17.6
This is my config file:
// const DEFAULT_VUE_APP_VERSION = require('../package.json').version;
const DEFAULT_VUE_APP_VERSION = require('../../package.json').version;
const DEFAULT_VUE_APP_HOST_URL = 'http://localhost:8080';
const DEFAULT_KIBANA_API_KEY = APIKEY
const DEFAULT_KIBANA_URL = URL
const DEFAULT_VUE_APM_SERVICE_NAME = 'crm-front';
const kibanaUrl = process.env.KIBANA_URL || DEFAULT_KIBANA_URL;
const kibanaApiKey = process.env.KIBANA_API_KEY || DEFAULT_KIBANA_API_KEY;
const publicUrl = PUBLIC_URL || DEFAULT_VUE_APP_HOST_URL;
const serviceVersion = process.env.VUE_APP_VERSION || DEFAULT_VUE_APP_VERSION;
const serviceName = process.env.VUE_APP_APM_SERVICE_NAME || DEFAULT_VUE_APM_SERVICE_NAME;
const config = {
kibanaUrl,
kibanaApiKey,
publicUrl,
service: {
version: serviceVersion,
name: serviceName,
},
};
module.exports = config;
This is my code for uploading sourcemaps:
const path = require('path');
const fs = require('fs');
const FormData = require('form-data');
const axiosInstance = require('./axiosInstance');
const config = require('./config');
const DIST_PATH = 'dist/js';
const EXIT_CODE = 1;
const STRINGIFY_SPACER = 2;
if (!fs.existsSync(path.resolve(__dirname, `../../${DIST_PATH}`))) {
console.log(`${DIST_PATH} does not exists`);
process.exit(EXIT_CODE);
}
/**
* Generate form data with a sourcemap file to upload.
* @param {string} folder
* @param {string} sourcemap
* @param {string} bundle
* @returns {object}
*/
const generateFormData = ({ folder, sourcemap, bundle }) => {
const fileName = path.resolve(__dirname, `../../${DIST_PATH}/${sourcemap}`);
const formData = new FormData();
formData.append('service_name', config.service.name);
formData.append('service_version', config.service.version);
formData.append('bundle_filepath', `${config.publicUrl}/js/${bundle}`);
formData.append('sourcemap', fs.readFileSync(fileName));
return formData;
};
/**
* Send a request to upload a sourcemap file.
* @param {object} data
* @returns {Promise<AxiosResponse<any>>}
*/
const sendUploadRequest = (data) => {
return axiosInstance.post(`api/apm/sourcemaps`, data, {
headers: {
...data.getHeaders(),
},
});
};
/**
* @param {string} bundle
* @param {object} response
*/
const onSuccess = (bundle, response) => {
console.log(`Successfully uploaded sourcemap for ${bundle}`);
console.log(JSON.stringify({
id: response.data.id,
created: response.data.created,
relative_url: response.data.relative_url,
identifier: response.data.identifier,
}, null, STRINGIFY_SPACER));
};
/**
* @param {string} bundle
* @param {object} error
*/
const onError = (bundle, error) => {
console.log(`Failed to upload sourcemap for ${bundle}:`);
if (error.response) {
console.log(`[${error.response.status}]\n`, error.response.data);
} else {
console.error(error);
}
throw error;
};
/**
* Upload .map files from a folder
* @param {string} folder
*/
const uploadFromFolder = (folder) => {
return fs.readdir(path.resolve(__dirname, `../../${DIST_PATH}`), (err, files) => {
if (err) {
throw err;
}
files.filter(fileName => fileName.endsWith('.js')).map((fileName) => {
const mapFile = files.find(currentFileName => currentFileName === `${fileName}.map`);
if (!mapFile || !mapFile.length) {
console.log(fileName, 'fileName aborted upload');
return null;
}
return {
bundle: fileName,
sourcemap: mapFile,
};
}).filter(file => !!file).reduce((promise, { bundle, sourcemap }) => promise.then(() => {
const formData = generateFormData({
folder,
bundle,
sourcemap,
});
return sendUploadRequest(formData)
.then(onSuccess.bind(null, bundle))
.catch(onError.bind(null, bundle));
}), Promise.resolve());
});
};
['client', 'server'].reduce((promise, folder) => {
return promise.then(() => uploadFromFolder(folder));
}, Promise.resolve()).catch(() => {
process.exit(EXIT_CODE);
});