Skip to content

Commit

Permalink
Consolidate route logging
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkingit committed Mar 18, 2021
1 parent 1ff716f commit 47857c9
Show file tree
Hide file tree
Showing 15 changed files with 19,108 additions and 367 deletions.
12 changes: 6 additions & 6 deletions app/apollo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const buildCommonApolloContext = async ({ models, req, res, connection, logger }
context = { apiKey: apiKey, req: upgradeReq, req_id: upgradeReq ? upgradeReq.id : undefined, userToken, recoveryHintsMap, orgId, ...context };
} else if (req) {
context = { req, req_id: req.id, recoveryHintsMap, ...context };
}
}
return context;
};

Expand Down Expand Up @@ -165,15 +165,15 @@ const createApolloServer = () => {

logger.trace({ req_id, connectionParams, context }, 'subscriptions:onConnect');
const me = await models.User.getMeFromConnectionParams( connectionParams, {req_id, models, logger, ...context},);

logger.debug({ me }, 'subscriptions:onConnect upgradeReq getMe');
if (me === undefined) {
throw Error(
'Can not find the session for this subscription request.',
);
}
// add original upgrade request to the context

// add original upgrade request to the context
return { me, upgradeReq: webSocket.upgradeReq, logger, orgKey, orgId };
},
onDisconnect: (webSocket, context) => {
Expand All @@ -200,7 +200,7 @@ const apollo = async (options = {}) => {
try {
const db = await connectDb(options.mongo_url);
const app = options.app ? options.app : createDefaultApp();
router.use(ebl(getBunyanConfig('apollo')));
app.use(ebl(getBunyanConfig('razeedash-api/apollo')));
if (initModule.playgroundAuth && process.env.GRAPHQL_ENABLE_PLAYGROUND === 'true') {
logger.info('Enabled playground route with authorization enforcement.');
app.get(GRAPHQL_PATH, initModule.playgroundAuth);
Expand Down Expand Up @@ -239,7 +239,7 @@ const apollo = async (options = {}) => {
port = options.graphql_port;
}
httpServer.listen({ port });
}
}
return { db, server, httpServer, stop};
} catch (err) {
logger.error(err, 'Apollo api error');
Expand Down
4 changes: 0 additions & 4 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ const app = express();
const http = require('http');
const compression = require('compression');
const body_parser = require('body-parser');
const ebl = require('express-bunyan-logger');
const addRequestId = require('express-request-id')();
const {router, initialize} = require('./routes/index.js');
const log = require('./log').log;
const getBunyanConfig = require('./utils/bunyan.js').getBunyanConfig;
const port = 3333;

const swaggerUi = require('swagger-ui-express');
Expand All @@ -39,8 +37,6 @@ const i18nextMiddleware = require('i18next-http-middleware');
const i18nextBackend = require('i18next-fs-backend');
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));

router.use(ebl(getBunyanConfig('razeedash-api')));

app.set('trust proxy', true);
app.use(addRequestId);
app.use(compression());
Expand Down
32 changes: 16 additions & 16 deletions app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ router.get('/v1/health', (req, res)=>{
});

router.use('/kube', Kube);
router.use(ebl(getBunyanConfig('/api/v2/')));
router.use(ebl(getBunyanConfig('razeedash-api/api')));

router.use(asyncHandler(async (req, res, next) => {
const db = req.app.get('db');
Expand All @@ -67,7 +67,7 @@ router.use(asyncHandler(async (req, res, next) => {
}));

// the orgs routes should be above the razee-org-key checks since the user
// won't have a razee-org-key when creating an org for the first time.
// won't have a razee-org-key when creating an org for the first time.
router.use('/v2/orgs', Orgs);

router.use((req, res, next) => {
Expand All @@ -92,17 +92,17 @@ async function initialize(){
const options = {
'collection-indexes': {
externalApplications: [
{
{
keys: { org_id: 1 },
options: { name: 'org_id', }
},
],
deployments: [
{
{
keys: { org_id: 1 },
options: { name: 'org_id', }
},
{
{
keys: { org_id: 1, 'containers.image': 1 },
options: { name: 'org_id.containers.image', }
}
Expand All @@ -127,17 +127,17 @@ async function initialize(){
},
],
orgs: [
{
{
keys: { orgKeys: 1 },
options: { name: 'orgKeys', }
}
],
clusters: [
{
{
keys: { org_id: 1 },
options: { name: 'org_id', }
},
{
{
keys: { org_id: 1, 'registration.location' : 1 },
options: { name: 'org_id.registration_text', }
},
Expand Down Expand Up @@ -171,23 +171,23 @@ async function initialize(){
}
],
resources: [
{
{
keys: { org_id: 1 },
options: { name: 'org_id', }
},
{
{
keys: { org_id: 1, deleted: 1 },
options: { name: 'org_id.deleted', }
},
{
{
keys: { org_id: 1, cluster_id: 1, selfLink: 1 },
options: { name: 'org_id.cluster_id.selfLink', }
},
{
{
keys: { org_id: 1, cluster_id: 1, selfLink: 1, 'searchableData.subscription_id': 1, deleted: 1 },
options: { name: 'org_id.cluster_id.subid.deleted', }
},
{
{
keys: { org_id: 1, cluster_id: 1, 'searchableData.kind': 1, 'searchableData.children': 1, deleted: 1 },
options: { name: 'org_id.cluster_id.subid.kind.children', }
},
Expand All @@ -200,7 +200,7 @@ async function initialize(){
options: { name: 'cluster_id_text_selfLink_text_searchableData.searchableExpression_text', }
}
],
messages:[
messages:[
{
keys: { org_id: 1, cluster_id: 1 },
options: { name: 'org_id.cluster_id', }
Expand All @@ -211,11 +211,11 @@ async function initialize(){
}
],
user_log: [
{
{
keys: { userid: 1 },
options: { name: 'userid', }
},
{
{
keys: { action: 1 },
options: { name: 'action', }
},
Expand Down
7 changes: 1 addition & 6 deletions app/routes/install/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@
const express = require('express');
const router = express.Router();
const asyncHandler = require('express-async-handler');
const ebl = require('express-bunyan-logger');
const Mustache = require('mustache');
const readFile = require('fs-readfile-promise');
const axios = require('axios');
const getBunyanConfig = require('../../utils/bunyan.js').getBunyanConfig;
const { CLUSTER_REG_STATES } = require('../../apollo/models/const');

router.use(ebl(getBunyanConfig('/api/install')));


router.get('/razeedeploy-job', asyncHandler(async (req, res, next) => {
let args = req.query.args ? req.query.args : [];
let args_array = Array.isArray(args) ? args : [args];
Expand All @@ -42,7 +37,7 @@ router.get('/razeedeploy-job', asyncHandler(async (req, res, next) => {
// populate registration data into --razeedash-cluster-metadata64
const Clusters = req.db.collection('clusters');
const preUpdatedCluster = await Clusters.findOneAndUpdate(
{org_id: req.org._id, cluster_id: req.query.clusterId, reg_state: CLUSTER_REG_STATES.REGISTERING},
{org_id: req.org._id, cluster_id: req.query.clusterId, reg_state: CLUSTER_REG_STATES.REGISTERING},
{$set: {reg_state: CLUSTER_REG_STATES.PENDING}});
if (preUpdatedCluster && preUpdatedCluster.value) {
req.log.debug(`preUpdatedCluster = ${JSON.stringify(preUpdatedCluster)}`);
Expand Down
16 changes: 6 additions & 10 deletions app/routes/v1/channels.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const express = require('express');
const router = express.Router();
const asyncHandler = require('express-async-handler');
const ebl = require('express-bunyan-logger');
const getBunyanConfig = require('../../utils/bunyan.js').getBunyanConfig;
const mongoConf = require('../../conf.js').conf;
const MongoClientClass = require('../../mongo/mongoClient.js');
const MongoClient = new MongoClientClass(mongoConf);
Expand All @@ -15,8 +13,6 @@ const algorithm = 'aes-256-cbc';

const getOrg = require('../../utils/orgs.js').getOrg;

router.use(ebl(getBunyanConfig('razee-api/v1Channels')));

router.use(asyncHandler(async (req, res, next) => {
req.db = await MongoClient.getClient();
next();
Expand All @@ -38,7 +34,7 @@ router.get('/:channelName/:versionId', getOrg, asyncHandler(async(req, res, next
res.status(404).send({status: 'error', message: `channel "${channelName}" not found for this org`});
return;
}

var deployableVersion = await DeployableVersions.findOne({ org_id: orgId, channel_id: deployable.uuid, uuid: versionId });
if(!deployableVersion){
res.status(404).send({status: 'error', message: `versionId "${versionId}" not found`});
Expand All @@ -49,7 +45,7 @@ router.get('/:channelName/:versionId', getOrg, asyncHandler(async(req, res, next
if (conf.s3.endpoint) {
try {
const s3Client = new S3ClientClass(conf);
const link = url.parse(deployableVersion.content);
const link = url.parse(deployableVersion.content);
const iv = Buffer.from(deployableVersion.iv, 'base64');
const paths = link.path.split('/');
const bucket = paths[1];
Expand All @@ -59,7 +55,7 @@ router.get('/:channelName/:versionId', getOrg, asyncHandler(async(req, res, next
const s3stream = s3Client.getObject(bucket, resourceName).createReadStream();
s3stream.on('error', function(error) {
req.log.error(error);
return res.status(403).json({ status: 'error', message: error.message});
return res.status(403).json({ status: 'error', message: error.message});
});
s3stream.pipe(decipher).pipe(res);
s3stream.on('httpError', (error) => {
Expand All @@ -71,10 +67,10 @@ router.get('/:channelName/:versionId', getOrg, asyncHandler(async(req, res, next
}
});
} catch (error) {
return res.status(403).json({ status: 'error', message: error.message});
return res.status(403).json({ status: 'error', message: error.message});
}
} else {
return res.status(403).json({ status: 'error', message: 'An endpoint must be configured for the S3 client'});
return res.status(403).json({ status: 'error', message: 'An endpoint must be configured for the S3 client'});
}
} else {
// in this case the resource was stored directly in mongo rather than in COS
Expand All @@ -83,7 +79,7 @@ router.get('/:channelName/:versionId', getOrg, asyncHandler(async(req, res, next
res.set('Content-Type', deployableVersion.type);
res.status(200).send(data);
} catch (error) {
return res.status(500).json({ status: 'error', message: error });
return res.status(500).json({ status: 'error', message: error });
}
}
}));
Expand Down
20 changes: 8 additions & 12 deletions app/routes/v2/clusters.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const { v4: uuid } = require('uuid');
const express = require('express');
const router = express.Router();
const asyncHandler = require('express-async-handler');
const ebl = require('express-bunyan-logger');
const objectHash = require('object-hash');
const _ = require('lodash');
const moment = require('moment');
Expand All @@ -29,7 +28,6 @@ var fs = require('fs');
const mongoSanitize = require('express-mongo-sanitize');

const verifyAdminOrgKey = require('../../utils/orgs.js').verifyAdminOrgKey;
const getBunyanConfig = require('../../utils/bunyan.js').getBunyanConfig;
const getCluster = require('../../utils/cluster.js').getCluster;
const deleteResource = require('../../utils/resources.js').deleteResource;
const buildSearchableDataForResource = require('../../utils/cluster.js').buildSearchableDataForResource;
Expand Down Expand Up @@ -71,7 +69,7 @@ const addUpdateCluster = async (req, res, next) => {
res.status(205).send('Please resync');
}
else {
await Clusters.updateOne({ org_id: req.org._id, cluster_id: req.params.cluster_id },
await Clusters.updateOne({ org_id: req.org._id, cluster_id: req.params.cluster_id },
{ $set: { metadata, reg_state, updated: new Date() } });
res.status(200).send('Thanks for the update');
}
Expand Down Expand Up @@ -240,10 +238,10 @@ const updateClusterResources = async (req, res, next) => {
req.log.info({ 'milliseconds': Date.now() - childStart, 'operation': 'updateClusterResources:Resources.updateMany', 'data': childSearchKey }, 'satcon-performance');
}
}
const rrSearchKey = {
org_id: req.org._id,
const rrSearchKey = {
org_id: req.org._id,
cluster_id: req.params.cluster_id,
'searchableData.kind': 'RemoteResource',
'searchableData.kind': 'RemoteResource',
'searchableData.children': selfLink,
deleted: false
};
Expand All @@ -253,7 +251,7 @@ const updateClusterResources = async (req, res, next) => {
if(remoteResource) {
searchableDataObj['subscription_id'] = remoteResource.searchableData['annotations["deploy_razee_io_clustersubscription"]'];
searchableDataObj['searchableExpression'] = searchableDataObj['searchableExpression'] + ':' + searchableDataObj['subscription_id'];
}
}
const searchableDataHash = buildSearchableDataObjHash(searchableDataObj);

start = Date.now();
Expand Down Expand Up @@ -323,7 +321,7 @@ const updateClusterResources = async (req, res, next) => {
// publish notification to graphql
if (result) {
let resourceId = null;
let resourceCreated = Date.now;
let resourceCreated = Date.now;
if (result.upsertedId) {
resourceId = result.upsertedId._id;
} else if (currentResource) {
Expand All @@ -333,7 +331,7 @@ const updateClusterResources = async (req, res, next) => {
if (resourceId) {
pubSub.resourceChangedFunc(
{_id: resourceId, data: dataStr, created: resourceCreated,
deleted: false, org_id: req.org._id, cluster_id: req.params.cluster_id, selfLink: selfLink,
deleted: false, org_id: req.org._id, cluster_id: req.params.cluster_id, selfLink: selfLink,
hash: resourceHash, searchableData: searchableDataObj, searchableDataHash: searchableDataHash});
}
}
Expand Down Expand Up @@ -484,12 +482,10 @@ const deleteCluster = async (req, res, next) => {
next();
} catch (error) {
req.log.error(error.message);
return res.status(500).json({ status: 'error', message: error.message });
return res.status(500).json({ status: 'error', message: error.message });
}
};

router.use(ebl(getBunyanConfig('razeedash-api/clusters')));

// /api/v2/clusters/:cluster_id
router.post('/:cluster_id', mongoSanitize({ replaceWith: '_' }), asyncHandler(addUpdateCluster));

Expand Down
Loading

0 comments on commit 47857c9

Please sign in to comment.