Skip to content
This repository has been archived by the owner on Aug 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #162 from speckleworks/Dimitrie/dev/telemetry
Browse files Browse the repository at this point in the history
Dimitrie/dev/telemetry
  • Loading branch information
didimitrie committed Sep 20, 2019
2 parents 58ec080 + 426955f commit 00d42f7
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 14 deletions.
8 changes: 8 additions & 0 deletions .env-base
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,11 @@ REDIRECT_URLS="http://localhost:5050,https://localhost:8080,https://app.speckle.
# "localhost" redirect urls will always work with or without TLS.
ALLOW_INSECURE_REDIRECTS=false

#
# Telemetry
# see: https://discourse.speckle.works/t/community-consultation-time-telemetry/410
# This helps us keep track anonymously of the number of speckle servers deployed.
# Disabling this will make Speckle sad :(
#
TELEMETRY=true

1 change: 1 addition & 0 deletions app/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const passport = require( 'passport' )
const adminCheck = require( './middleware/AdminCheck' )

module.exports = function ( app, express, urlRoot, plugins ) {

var r = new express.Router( )

// strict auth will return a 401 if no authorization header is present. pass means req.user exists
Expand Down
42 changes: 42 additions & 0 deletions app/telemetry/appTelemetry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const countly = require( 'countly-sdk-nodejs' )
const myMachineId = require( 'node-machine-id' ).machineIdSync( )

const logger = require( '../../config/logger' )

// This module lets us know which routes are hit on a server, thus aggregating
// the "hot" endpoints.

module.exports = ( app ) => {

if ( process.env.TELEMETRY === 'false' )
return

try {
countly.init( {
// eslint-disable-next-line camelcase
app_key: '6b79ee267ff23c4b99108591c5b33f0ba8ed5e4b',
url: 'https://telemetry.speckle.works',
// eslint-disable-next-line camelcase
device_id: myMachineId,
debug: false
} )

countly.user_details( {
'username': myMachineId
} )

app.use( ( req, res, next ) => {

next( ) // let's not block things, in case telemetry server is down.

try {
countly.track_view( `${req.method} "${req.route.path}"` )
} catch ( err ) {
logger.info( 'Failed to initialise route based telemetry.' )
}

} )
} catch ( err ) {
logger.info( 'Failed to initialise route based telemetry.' )
}
}
55 changes: 55 additions & 0 deletions app/telemetry/initTelemetry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const countly = require( 'countly-sdk-nodejs' )
const machineIdSync = require( 'node-machine-id' ).machineIdSync
const { exec } = require( 'child_process' )

const logger = require( '../../config/logger' )

// This module lets us know the version of the running server, and how many times
// it's initialised.
module.exports = ( ) => {

if ( process.env.TELEMETRY === 'false' )
return

let myMachineId = machineIdSync( )
let tagVersion = 'unknown'

try {
exec( 'git describe --tags', ( err, stdout ) => {
tagVersion = stdout.split( '-' )[ 0 ]
logger.info( `Version: ${tagVersion}` )

countly.init( {
// eslint-disable-next-line camelcase
app_key: '6b79ee267ff23c4b99108591c5b33f0ba8ed5e4b',
url: 'https://telemetry.speckle.works',
// eslint-disable-next-line camelcase
device_id: myMachineId,
// eslint-disable-next-line camelcase
app_version: tagVersion,
debug: false
} )

countly.user_details( {
'username': myMachineId
} )

countly.add_event( {
"key": "server-deployment",
"segmentation": {
"machineId": myMachineId,
"version": tagVersion
}
} )

// one view to track version
countly.track_view( `SERVER DEPLOYMENT versioned at ${tagVersion}` )

// one view to track generic usage, regardless of version
countly.track_view( `SERVER DEPLOYMENT generic` )

} )
} catch ( err ) {
logger.error( err )
}
}
26 changes: 18 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"chalk": "^1.1.3",
"connect-redis": "^3.4.2",
"cors": "^2.8.1",
"countly-sdk-nodejs": "^19.8.0",
"crypto-random-string": "^3.0.1",
"dotenv": "^6.2.0",
"express": "^4.17.1",
Expand All @@ -30,6 +31,7 @@
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.15",
"mongoose": "^5.6.11",
"node-machine-id": "^1.1.12",
"nodemailer": "^6.3.0",
"passport": "^0.4.0",
"passport-anonymous": "^1.0.1",
Expand Down
13 changes: 7 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ if ( cluster.isMaster ) {
█ The Open Source Data Platform for AEC.
` +
chalk.red( `
chalk.red( `
█ Server running at: ${process.env.CANONICAL_URL}
` )
)
)

logger.level = 'debug'

Expand Down Expand Up @@ -68,6 +68,8 @@ chalk.red( `
redisClient.flushdb( )
} )

require( './app/telemetry/initTelemetry' )( )

/////////////////////////////////////////////////////////////////////////
/// CHILD processes /////.
/////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -111,6 +113,9 @@ chalk.red( `

app.use( passport.initialize( ) )

// Telemetry
require( './app/telemetry/appTelemetry' )( app )

if ( process.env.INDENT_RESPONSES === 'true' ) { app.set( 'json spaces', 2 ) }
if ( process.env.EXPOSE_EMAILS === 'true' ) { app.enable( 'expose emails' ) }

Expand Down Expand Up @@ -141,10 +146,6 @@ chalk.red( `
require( './app/api/index' )( app, express, '/api', plugins )
require( './app/api/index' )( app, express, '/api/v1', plugins )


// init email transport
// require( './app/email/index' ) // soon

// init default register/login routes
require( './app/auth/index' )( app )

Expand Down

0 comments on commit 00d42f7

Please sign in to comment.