File size: 3,001 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// @flow
/**
 * Database setup is done here
 */
import fs from 'fs';
import path from 'path';
import inspect from 'rethinkdb-inspector';
import { statsd } from '../statsd';

const IS_PROD = !process.env.FORCE_DEV && process.env.NODE_ENV === 'production';

const CONNECTIONS = 20;
const DEFAULT_CONFIG = {
  // Connect to the test database when, well, testing
  db: !process.env.TEST_DB ? 'spectrum' : 'testing',
  max: CONNECTIONS, // Maximum number of connections, default is 1000
  buffer: CONNECTIONS, // Minimum number of connections open at any given moment, default is 50
  timeoutGb: 60 * 60 * 1000, // How long should an unused connection stick around, default is an hour, this is a minute
  timeout: 30, // The number of seconds for a connection to be opened, default 20
};

let ca;

try {
  ca = fs.readFileSync(path.join(process.cwd(), 'cacert'));
} catch (err) {}

if (!ca && IS_PROD)
  throw new Error(
    'Please provide the SSL certificate to connect to the production database in a file called `cacert` in the root directory.'
  );

const PRODUCTION_CONFIG = {
  servers: [
    {
      password: process.env.COMPOSE_RETHINKDB_PASSWORD,
      host: process.env.COMPOSE_RETHINKDB_URL,
      port: process.env.COMPOSE_RETHINKDB_PORT,
      ...(ca
        ? {
            ssl: {
              ca,
            },
          }
        : {}),
    },
    {
      password: process.env.COMPOSE_RETHINKDB_PASSWORD,
      host: process.env.BACKUP_RETHINKDB_URL,
      port: process.env.BACKUP_RETHINKDB_PORT,
      ...(ca
        ? {
            ssl: {
              ca,
            },
          }
        : {}),
    },
  ],
};

const config = IS_PROD
  ? {
      ...DEFAULT_CONFIG,
      ...PRODUCTION_CONFIG,
    }
  : {
      ...DEFAULT_CONFIG,
    };

let r = require('rethinkhaberdashery')(config);
const poolMaster = r.getPoolMaster();

poolMaster.on('queueing', size => {
  statsd.gauge('db.query_queue.size', size);
});

setInterval(() => {
  statsd.gauge('db.connections.count', poolMaster.getLength());
}, 5000);

// Exit the process on unhealthy db in test env
if (process.env.TEST_DB) {
  poolMaster.on('healthy', healthy => {
    if (!healthy) {
      process.exit(1);
    }
  });
}

const queries = [];
inspect(r, {
  onQueryComplete: (query, { size, time }) => {
    if (query.indexOf('.changes') > -1) return;
    statsd.increment('db.queries.count');
    statsd.histogram('db.queries.response_time', time);
    statsd.histogram('db.queries.response_size', size);
    // In development write out a file of the most expensive queries
    if (process.env.NODE_ENV === 'development' && process.env.TRACK_DB_PERF) {
      queries.push({ query, time, size });
      fs.writeFileSync(
        'queries-by-time.js',
        JSON.stringify(queries.sort((a, b) => b.time - a.time), null, 2)
      );
      fs.writeFileSync(
        'queries-by-response-size.js',
        JSON.stringify(queries.sort((a, b) => b.size - a.size), null, 2)
      );
    }
  },
});

module.exports = { db: r };