Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate route logging #827

Merged
merged 5 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions app/apollo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const recoveryHintsMap = require('./resolvers/recoveryHintsMap');
const { models, connectDb } = require('./models');
const bunyanConfig = getBunyanConfig('apollo');
const bunyanConfig = getBunyanConfig('razeedash-api/apollo');
const logger = bunyan.createLogger(bunyanConfig);
const promClient = require('prom-client');
const createMetricsPlugin = require('apollo-metrics');
Expand Down 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
2 changes: 1 addition & 1 deletion app/apollo/models/user.default.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const { getBunyanConfig } = require('../../utils/bunyan');
const _ = require('lodash');

const logger = bunyan.createLogger(
getBunyanConfig('apollo/models/user.default.schema'),
getBunyanConfig('razeedash-api/apollo/models/user.default.schema'),
);

const UserDefaultSchema = new mongoose.Schema({
Expand Down
2 changes: 1 addition & 1 deletion app/apollo/models/user.local.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { getBunyanConfig } = require('../../utils/bunyan');
const SECRET = require('./const').SECRET;

const logger = bunyan.createLogger(
getBunyanConfig('apollo/models/user.local.schema'),
getBunyanConfig('razeedash-api/apollo/models/user.local.schema'),
);

const UserLocalSchema = new mongoose.Schema({
Expand Down
2 changes: 1 addition & 1 deletion app/apollo/models/user.passport.local.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { getBunyanConfig } = require('../../utils/bunyan');
const SECRET = require('./const').SECRET;

const logger = bunyan.createLogger(
getBunyanConfig('apollo/models/user.passport.local.schema'),
getBunyanConfig('razeedash-api/apollo/models/user.passport.local.schema'),
);

const UserPassportLocalSchema = new mongoose.Schema({
Expand Down
20 changes: 10 additions & 10 deletions app/apollo/subscription/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const { APOLLO_STREAM_SHARDING } = require('../models/const');
const { RazeeQueryError } = require('../resolvers/common');
const { getBunyanConfig } = require('../../utils/bunyan');

const logger = bunyan.createLogger(getBunyanConfig('apollo/subscription'));
const logger = bunyan.createLogger(getBunyanConfig('razeedash-api/apollo/subscription'));

const EVENTS = {
RESOURCE: {
Expand All @@ -43,14 +43,14 @@ function getStreamingTopic(prefix, org_id) {
if (org_id) {
const last2 = org_id.slice(-2);
return `${prefix}_${last2}`;
}
}
return `${prefix}_00`;
}
return prefix;
}

class PubSubImpl {

constructor(params) {
this.initRetries = 0;
this.lastPubSubMessage = null;
Expand All @@ -64,7 +64,7 @@ class PubSubImpl {
);
this.init();
}

async init() {
const url = new URL(this.redisUrl);

Expand Down Expand Up @@ -118,7 +118,7 @@ class PubSubImpl {
this.pubSub.redisSubscriber.subscribe(topic);
setInterval( () => {
message = {
time: Date.now(),
time: Date.now(),
message : 'liveness check'
};
this.pubSub.redisPublisher.publish(topic, JSON.stringify(message));
Expand All @@ -133,11 +133,11 @@ class PubSubImpl {
logger.info({ data, topic }, 'Publishing channel subscription update');
await this.pubSub.publish(topic, { subscriptionUpdated: { data }, });
} catch (error) {
logger.error(error, 'Channel subscription publish error'); throw new RazeeQueryError(context.req.t('Failed to Publish subscription notification to clusters, please retry.'), context);
logger.error(error, 'Channel subscription publish error'); throw new RazeeQueryError(context.req.t('Failed to Publish subscription notification to clusters, please retry.'), context);
}
} else {
logger.warn( { data, topic }, 'Failed to Publish subscription update, since pubsub is not ready.');
throw new RazeeQueryError(context.req.t('Failed to Publish subscription notification to clusters, pubsub is not ready yet, please retry.'), context);
throw new RazeeQueryError(context.req.t('Failed to Publish subscription notification to clusters, pubsub is not ready yet, please retry.'), context);
}
return data;
}
Expand All @@ -156,11 +156,11 @@ class PubSubImpl {
});
} catch (error) {
logger.error(error, 'Resource publish error');
throw new RazeeQueryError(context.req.t('Failed to Publish resource notification, please reload the page.'), context);
throw new RazeeQueryError(context.req.t('Failed to Publish resource notification, please reload the page.'), context);
}
} else {
logger.warn( { resource, topic }, 'Failed to Publish resource update, since pubsub is not ready.');
throw new RazeeQueryError(context.req.t('Failed to Publish resource notification, pubsub is not ready yet, please retry later.'), context);
throw new RazeeQueryError(context.req.t('Failed to Publish resource notification, pubsub is not ready yet, please retry later.'), context);
}
return resource;
}
Expand All @@ -179,7 +179,7 @@ var GraphqlPubSub = (function() {
if (singleton && singleton.enabled) {
singleton.pubSub.close();
singleton = undefined;
}
}
}
};
})();
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
34 changes: 17 additions & 17 deletions app/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const asyncHandler = require('express-async-handler');

const getBunyanConfig = require('../utils/bunyan.js').getBunyanConfig;
const bunyan = require('bunyan');
const logger = bunyan.createLogger(getBunyanConfig('/'));
const logger = bunyan.createLogger(getBunyanConfig('razeedash-api'));
const ebl = require('express-bunyan-logger');

const MongoClientClass = require('../mongo/mongoClient.js');
Expand Down 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
4 changes: 2 additions & 2 deletions app/routes/kube/kube.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const router = express.Router();
const { GraphqlPubSub } = require('../../apollo/subscription');
const pubSub = GraphqlPubSub.getInstance();
const { getBunyanConfig } = require('../../utils/bunyan');
const logger = bunyan.createLogger(getBunyanConfig('kube/liveness'));
const logger = bunyan.createLogger(getBunyanConfig('razeedash-api/kube/liveness'));
const timeInterval = 300000; //5 mintues

// /kube/liveness
Expand All @@ -40,7 +40,7 @@ const kube = router.get('/liveness', asyncHandler(async(req, res) => {
logger.error('Razeedash Api is down due to Redis pubsub connection issue, please check logs.');
return res.sendStatus(503);
}

if (pubSub.lastPubSubMessage !== null && Date.now()- pubSub.lastPubSubMessage.time > timeInterval) {
// check if the most recent message received is within ${timeInterval/60000} minitue
logger.error(`Razeedash Api is down, haven't received any published messages within ${timeInterval/60000} minitue, please check logs.`);
Expand Down
Loading