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

Commit

Permalink
feat(telemetry): added server init & app routes telemetry views (and …
Browse files Browse the repository at this point in the history
…events)
  • Loading branch information
didimitrie committed Sep 20, 2019
1 parent fba29e8 commit f8286b1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 33 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
37 changes: 37 additions & 0 deletions app/telemetry/appTelemetry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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( {
app_key: '6b79ee267ff23c4b99108591c5b33f0ba8ed5e4b',
url: 'https://telemetry.speckle.works',
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 {
logger.info( 'Failed to initialise route based telemetry.' )
}
} )
} catch {
logger.info( 'Failed to initialise route based telemetry.' )
}
}
23 changes: 14 additions & 9 deletions telemetry.js → app/telemetry/initTelemetry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ const countly = require( 'countly-sdk-nodejs' )
const machineIdSync = require( 'node-machine-id' ).machineIdSync
const { exec } = require( 'child_process' )

const logger = require( './config/logger' )
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, stderr ) => {
tagVersion = stdout
logger.info( `` )
tagVersion = stdout.split( '-' )[ 0 ]
logger.info( `Version: ${tagVersion}` )
logger.info( `` )

countly.init( {
app_key: '6b79ee267ff23c4b99108591c5b33f0ba8ed5e4b',
Expand All @@ -27,19 +31,20 @@ module.exports = ( ) => {
'username': myMachineId
} )

countly.begin_session( false )

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

countly.track_view( `server-deployment/${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` )

countly.end_session( )
} )
} catch ( err ) {
logger.error( err )
Expand Down
28 changes: 4 additions & 24 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,7 @@ if ( cluster.isMaster ) {
redisClient.flushdb( )
} )

require( './telemetry' )()

// Countly.init( {
// app_key: '6b79ee267ff23c4b99108591c5b33f0ba8ed5e4b',
// device_id: 'test',
// url: 'https://telemetry.speckle.works',
// debug: true
// } )

// Countly.begin_session( false )

// Countly.add_event( {
// "key": "server-start",
// "segmentation": {
// "serverName": process.env.SERVER_NAME,
// "machineId": 'bender rocks!'
// }
// } )

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

/////////////////////////////////////////////////////////////////////////
/// CHILD processes /////.
Expand Down Expand Up @@ -132,6 +113,9 @@ if ( cluster.isMaster ) {

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 @@ -162,10 +146,6 @@ if ( cluster.isMaster ) {
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 f8286b1

Please sign in to comment.