Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
feat(ec2): capture ec2 metadata (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
idonava authored Nov 16, 2020
1 parent bc9ac7f commit c961ac9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/containers/ec2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const axios = require('axios');
const utils = require('../utils');
const eventIterface = require('../event');

let currentEC2Labels = null;

const URL = 'http://169.254.169.254/latest/meta-data/';
const attributeToGet = ['instance-id', 'instance-type', 'local-ipv4', 'public-hostname', 'public-ipv4'];
const EPSAGON_EC2_REQUEST_TIMEOUT = process.env.EPSAGON_EC2_REQUEST_TIMEOUT || 3000;


/**
* Load EC2 metadata and store it
* @returns {Promise} when resolved will contain the EC2 metadata
*/
module.exports.loadEC2Metadata = function loadEC2Metadata() {
if (currentEC2Labels) return Promise.resolve(currentEC2Labels);


const promises = [];
utils.debugLog('Loading EC2 metadata');
attributeToGet.forEach((attribute) => {
const source = axios.CancelToken.source();
setTimeout(() => {
source.cancel();
}, EPSAGON_EC2_REQUEST_TIMEOUT);
promises.push(axios.get(URL + attribute, {
timeout: EPSAGON_EC2_REQUEST_TIMEOUT,
cancelToken: source.token,
}).then((response) => {
utils.debugLog(`Received response for ${attribute}`);
if (response.status === 200) {
const attributeKey = attribute.replace('-', '_');
const attributeData = response.data;
if (!currentEC2Labels) currentEC2Labels = {};
currentEC2Labels[`aws.ec2.${attributeKey}`] = attributeData;
utils.debugLog(`${attributeKey} stored with: ${attributeData}`);
}
return attribute;
})
.catch(() => {
utils.debugLog(`Could not load EC2 metadata for ${attribute}`);
}));
});

return Promise.all(promises);
};
/**
* If the current process is running in EC2 cloud,
* it will add metadata to trace
*
* @param {Object} runner runner object to add the metadata
*/
module.exports.addEC2Metadata = function addEC2Metadata(runner) {
if (!runner || !currentEC2Labels) return;
eventIterface.addToMetadata(runner, Object.assign({}, currentEC2Labels));
};
2 changes: 2 additions & 0 deletions src/patcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const winstonPatcher = require('./events/winston.js');
const amqplibPatcher = require('./events/amqplib.js');
const amqpPatcher = require('./events/amqp.js');
const ldapPatcher = require('./events/ldap.js');
const fs = require('./events/fs.js');


/**
Expand Down Expand Up @@ -71,5 +72,6 @@ if (!config.getConfig().isEpsagonPatchDisabled) {
amqplibPatcher,
amqpPatcher,
ldapPatcher,
fs,
].forEach(patch);
}
3 changes: 3 additions & 0 deletions src/tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const consts = require('./consts.js');
const ecs = require('./containers/ecs.js');
const k8s = require('./containers/k8s.js');
const azure = require('./containers/azure.js');
const ec2 = require('./containers/ec2.js');
const winstonCloudwatch = require('./events/winston_cloudwatch');
const { isStrongId } = require('./helpers/events');

Expand Down Expand Up @@ -133,6 +134,7 @@ module.exports.initTrace = function initTrace(
azure.loadAzureMetadata((azureAdditionalConfig) => {
config.setConfig(Object.assign(azureAdditionalConfig, configData));
});
ec2.loadEC2Metadata().catch(err => utils.debugLog(err));
}
} catch (err) {
utils.debugLog('Could not extract container env data');
Expand Down Expand Up @@ -325,6 +327,7 @@ function sendCurrentTrace(traceSender) {
ecs.addECSMetadata(tracerObj.currRunner);
k8s.addK8sMetadata(tracerObj.currRunner);
azure.addAzureMetadata(tracerObj.currRunner);
ec2.addEC2Metadata(tracerObj.currRunner);

// Check if got error events
if (sendOnlyErrors) {
Expand Down

0 comments on commit c961ac9

Please sign in to comment.