Skip to content
This repository has been archived by the owner on Mar 7, 2018. It is now read-only.

Commit

Permalink
Add event tracing to all resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
c-w committed Jul 6, 2017
1 parent 8ab3e2e commit 6f34ceb
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 39 deletions.
26 changes: 26 additions & 0 deletions src/clients/appinsights/AppInsightsClient.js
Expand Up @@ -51,7 +51,33 @@ function trackDependency(promiseFunc, dependencyName, callName) {
return dependencyTracker;
}

function trackEvent(promiseFunc, eventName) {
if (!client) return promiseFunc;

function eventTracker(...args) {
return new Promise((resolve, reject) => {
const start = new Date();
promiseFunc(...args)
.then(returnValue => {
const duration = new Date() - start;
const success = true;
client.trackEvent(eventName, { duration: duration, success: success });
resolve(returnValue);
})
.catch(err => {
const duration = new Date() - start;
const success = false;
client.trackEvent(eventName, { duration: duration, success: success });
reject(err);
});
});
}

return eventTracker;
}

module.exports = {
trackDependency: trackDependency,
trackEvent: trackEvent,
setup: setup
};
9 changes: 5 additions & 4 deletions src/resolvers-cassandra/Edges/mutations.js
@@ -1,6 +1,7 @@
'use strict';

const withRunTime = require('../shared').withRunTime;
const trackEvent = require('../../clients/appinsights/AppInsightsClient').trackEvent;

/**
* @param {{input: {site: string, edges: Array<{name: string}>}}} args
Expand Down Expand Up @@ -45,8 +46,8 @@ function removeLocations(args, res) { // eslint-disable-line no-unused-vars
}

module.exports = {
removeKeywords: withRunTime(removeKeywords),
addKeywords: withRunTime(addKeywords),
saveLocations: withRunTime(saveLocations),
removeLocations: withRunTime(removeLocations)
removeKeywords: trackEvent(withRunTime(removeKeywords), 'removeKeywords'),
addKeywords: trackEvent(withRunTime(addKeywords), 'addKeywords'),
saveLocations: trackEvent(withRunTime(saveLocations), 'saveLocations'),
removeLocations: trackEvent(withRunTime(removeLocations), 'removeLocations')
};
11 changes: 6 additions & 5 deletions src/resolvers-cassandra/Edges/queries.js
@@ -1,6 +1,7 @@
'use strict';

const withRunTime = require('../shared').withRunTime;
const trackEvent = require('../../clients/appinsights/AppInsightsClient').trackEvent;

/**
* @param {{site: string, query: string, fromDate: string, toDate: string, sourceFilter: string[]}} args
Expand Down Expand Up @@ -38,9 +39,9 @@ function topSources(args,res) { // eslint-disable-line no-unused-vars
}

module.exports = {
terms: withRunTime(terms),
locations: withRunTime(locations),
popularLocations: withRunTime(popularLocations),
timeSeries: timeSeries,
topSources: topSources
terms: trackEvent(withRunTime(terms), 'terms'),
locations: trackEvent(withRunTime(locations), 'locations'),
popularLocations: trackEvent(withRunTime(popularLocations), 'popularLocations'),
timeSeries: trackEvent(timeSeries, 'timeSeries'),
topSources: trackEvent(topSources, 'topSources')
};
5 changes: 3 additions & 2 deletions src/resolvers-cassandra/Facts/queries.js
@@ -1,6 +1,7 @@
'use strict';

const withRunTime = require('../shared').withRunTime;
const trackEvent = require('../../clients/appinsights/AppInsightsClient').trackEvent;

/**
* @param {{pageSize: number, skip: number, tagFilter: string[]}} args
Expand All @@ -17,6 +18,6 @@ function get(args, res) { // eslint-disable-line no-unused-vars
}

module.exports = {
list: withRunTime(list),
get: get
list: trackEvent(withRunTime(list), 'listFacts'),
get: trackEvent(get, 'getFact')
};
3 changes: 2 additions & 1 deletion src/resolvers-cassandra/Messages/mutations.js
@@ -1,6 +1,7 @@
'use strict';

const eventHubSender = require('../../clients/eventhub/EventHubSender');
const trackEvent = require('../../clients/appinsights/AppInsightsClient').trackEvent;

/**
* @param {{messages: Array<{RowKey: string, created_at: string, featureCollection: Array<{type: string, features: Array<{type: string, coordinates: number[]}>>, message: string, language: string, link: string, source: string, title: string}>}}} args
Expand All @@ -11,5 +12,5 @@ function publishEvents(args, res) { // eslint-disable-line no-unused-vars
}

module.exports = {
publishEvents: publishEvents
publishEvents: trackEvent(publishEvents, 'publishEvents')
};
13 changes: 7 additions & 6 deletions src/resolvers-cassandra/Messages/queries.js
Expand Up @@ -6,6 +6,7 @@ const cassandraConnector = require('../../clients/cassandra/CassandraConnector')
const featureServiceClient = require('../../clients/locations/FeatureServiceClient');
const withRunTime = require('../shared').withRunTime;
const makeMap = require('../../utils/collections').makeMap;
const trackEvent = require('../../clients/appinsights/AppInsightsClient').trackEvent;

/**
* @typedef {type: string, coordinates: number[][], properties: {edges: string[], messageid: string, createdtime: string, sentiment: number, title: string, originalSources: string[], sentence: string, language: string, source: string, properties: {retweetCount: number, fatalaties: number, userConnecionCount: number, actor1: string, actor2: string, actor1Type: string, actor2Type: string, incidentType: string, allyActor1: string, allyActor2: string, title: string, link: string, originalSources: string[]}, fullText: string}} Feature
Expand Down Expand Up @@ -241,10 +242,10 @@ function translateWords(args, res) { // eslint-disable-line no-unused-vars
}

module.exports = {
byLocation: withRunTime(byLocation),
byBbox: withRunTime(byBbox),
byEdges: withRunTime(byEdges),
event: event,
translate: translate,
translateWords: translateWords
byLocation: trackEvent(withRunTime(byLocation), 'messagesForLocation'),
byBbox: trackEvent(withRunTime(byBbox), 'messagesForBbox'),
byEdges: trackEvent(withRunTime(byEdges), 'messagesForEdges'),
event: trackEvent(event, 'messageForEven'),
translate: trackEvent(translate, 'translate'),
translateWords: trackEvent(translateWords, 'translateWords')
};
19 changes: 10 additions & 9 deletions src/resolvers-cassandra/Settings/mutations.js
Expand Up @@ -3,6 +3,7 @@
const Promise = require('promise');
const cassandraConnector = require('../../clients/cassandra/CassandraConnector');
const withRunTime = require('../shared').withRunTime;
const trackEvent = require('../../clients/appinsights/AppInsightsClient').trackEvent;

/**
* @param {{input: {targetBbox: number[], defaultZoomLevel: number, logo: string, title: string, name: string, defaultLocation: number[], storageConnectionString: string, featuresConnectionString: string, mapzenApiKey: string, fbToken: string, supportedLanguages: string[]}}} args
Expand Down Expand Up @@ -79,13 +80,13 @@ function removeBlacklist(args, res) { // eslint-disable-line no-unused-vars
}

module.exports = {
createOrReplaceSite: createOrReplaceSite,
modifyFacebookPages: withRunTime(modifyFacebookPages),
removeFacebookPages: withRunTime(removeFacebookPages),
modifyTrustedTwitterAccounts: withRunTime(modifyTrustedTwitterAccounts),
removeTrustedTwitterAccounts: withRunTime(removeTrustedTwitterAccounts),
modifyTwitterAccounts: withRunTime(modifyTwitterAccounts),
removeTwitterAccounts: withRunTime(removeTwitterAccounts),
modifyBlacklist: withRunTime(modifyBlacklist),
removeBlacklist: withRunTime(removeBlacklist)
createOrReplaceSite: trackEvent(createOrReplaceSite, 'createOrReplaceSite'),
modifyFacebookPages: trackEvent(withRunTime(modifyFacebookPages), 'modifyFacebookPages'),
removeFacebookPages: trackEvent(withRunTime(removeFacebookPages), 'removeFacebookPages'),
modifyTrustedTwitterAccounts: trackEvent(withRunTime(modifyTrustedTwitterAccounts), 'modifyTrustedTwitterAccounts'),
removeTrustedTwitterAccounts: trackEvent(withRunTime(removeTrustedTwitterAccounts), 'removeTrustedTwitterAccounts'),
modifyTwitterAccounts: trackEvent(withRunTime(modifyTwitterAccounts), 'modifyTwitterAccounts'),
removeTwitterAccounts: trackEvent(withRunTime(removeTwitterAccounts), 'removeTwitterAccounts'),
modifyBlacklist: trackEvent(withRunTime(modifyBlacklist), 'modifyBlacklist'),
removeBlacklist: trackEvent(withRunTime(removeBlacklist), 'removeBlacklist')
};
13 changes: 7 additions & 6 deletions src/resolvers-cassandra/Settings/queries.js
Expand Up @@ -4,6 +4,7 @@ const Promise = require('promise');
const facebookAnalyticsClient = require('../../clients/facebook/FacebookAnalyticsClient');
const cassandraConnector = require('../../clients/cassandra/CassandraConnector');
const withRunTime = require('../shared').withRunTime;
const trackEvent = require('../../clients/appinsights/AppInsightsClient').trackEvent;

function cassandraRowToSite(row) {
// Please note that the following properties in the SiteProperties are NOT in Cassandra's sitessetings:
Expand Down Expand Up @@ -160,10 +161,10 @@ function termBlacklist(args, res) { // eslint-disable-line no-unused-vars
}

module.exports = {
sites: withRunTime(sites),
twitterAccounts: withRunTime(twitterAccounts),
trustedTwitterAccounts: withRunTime(trustedTwitterAccounts),
facebookPages: withRunTime(facebookPages),
facebookAnalytics: facebookAnalytics,
termBlacklist: withRunTime(termBlacklist)
sites: trackEvent(withRunTime(sites), 'sites'),
twitterAccounts: trackEvent(withRunTime(twitterAccounts), 'twitterAccounts'),
trustedTwitterAccounts: trackEvent(withRunTime(trustedTwitterAccounts), 'trustedTwitterAccounts'),
facebookPages: trackEvent(withRunTime(facebookPages), 'facebookPages'),
facebookAnalytics: trackEvent(facebookAnalytics, 'facebookAnalytics'),
termBlacklist: trackEvent(withRunTime(termBlacklist), 'termBlacklist')
};
13 changes: 7 additions & 6 deletions src/resolvers-cassandra/Tiles/queries.js
Expand Up @@ -4,7 +4,8 @@ const Promise = require('promise');
const geotile = require('geotile');
const cassandraConnector = require('../../clients/cassandra/CassandraConnector');
const featureServiceClient = require('../../clients/locations/FeatureServiceClient');
const { withRunTime } = require('../shared');
const withRunTime = require('../shared').withRunTime;
const trackEvent = require('../../clients/appinsights/AppInsightsClient').trackEvent;
const { makeMap, makeSet } = require('../../utils/collections');

function makeDefaultFilters(args) {
Expand Down Expand Up @@ -222,9 +223,9 @@ function fetchEdgesByBBox(args, res) { // eslint-disable-line no-unused-vars
}

module.exports = {
fetchTilesByBBox: withRunTime(fetchTilesByBBox),
fetchTilesByLocations: withRunTime(fetchTilesByLocations),
fetchPlacesByBBox: withRunTime(fetchPlacesByBBox),
fetchEdgesByLocations: withRunTime(fetchEdgesByLocations),
fetchEdgesByBBox: withRunTime(fetchEdgesByBBox)
fetchTilesByBBox: trackEvent(withRunTime(fetchTilesByBBox), 'fetchTilesByBBox'),
fetchTilesByLocations: trackEvent(withRunTime(fetchTilesByLocations), 'fetchTilesByLocations'),
fetchPlacesByBBox: trackEvent(withRunTime(fetchPlacesByBBox), 'fetchPlacesByBBox'),
fetchEdgesByLocations: trackEvent(withRunTime(fetchEdgesByLocations), 'fetchEdgesByLocations'),
fetchEdgesByBBox: trackEvent(withRunTime(fetchEdgesByBBox, 'fetchEdgesByBBox'))
};

0 comments on commit 6f34ceb

Please sign in to comment.