UNPKG

3.74 kBJavaScriptView Raw
1// @flow
2
3// NOTE: dont use Module here, this class is used by Module
4import os from 'os'
5
6import type AWSClient from './AWSClient'
7import type { StatNameMode } from './InfluxDBClient'
8
9import { pad } from './stringUtils'
10import InfluxDBClient from './InfluxDBClient'
11import S3Logger from './S3Logger'
12
13const ORDERED_LOG_LEVELS = ['debug', 'info', 'warn', 'error']
14const HOSTNAME = os.hostname()
15
16type SystemConfig = {|
17 env: string,
18 influxDBs?: ?Array<{
19 statNameMode: StatNameMode,
20 host: string,
21 dbName: string,
22 flushIntervalMS?: ?number,
23 }>,
24 lifesignIntervalMS?: ?number,
25 logLevel?: 'debug' | 'info' | 'warn' | 'error',
26 aws: {
27 accountId: string,
28 defaultRegion: string,
29 profile?: ?string,
30 },
31 system: string,
32 component: string,
33 ver: string,
34 s3Logging?: ?{
35 noHTTPS?: boolean,
36 host: string,
37 dataFieldsToPromote?: ?{ [id: string]: string },
38 dataFieldsToRemove?: ?Array<string>,
39 },
40 trackVitalsIntervalMS?: ?number,
41 // trackingSchema?: ?number,
42|}
43
44
45export default class System {
46
47 static activeLogLevels: Array<string>
48 static runId: string
49 static influxDBClients: Array<InfluxDBClient>
50
51 static _s3logger: ?S3Logger
52 static _config: SystemConfig
53 static _awsClient: AWSClient
54
55
56 static async setConfig(systemConfig: SystemConfig) {
57 System._config = {
58
59 // det default values
60 logLevel: 'info',
61 system: 'none',
62
63 ...systemConfig,
64
65 }
66 System.activeLogLevels = ORDERED_LOG_LEVELS.slice(ORDERED_LOG_LEVELS.indexOf(System._config.logLevel))
67 System.runId = `${pad(Math.round((Date.now() - new Date((new Date()).getFullYear(), 0, 1).getTime()) / 1000), 8)}-${pad(Math.floor(Math.random() * 10000), 4)}` // seconds since start of year
68
69 // init InfluxDBClient
70 System.influxDBClients = (System._config.influxDBs || []).map(idbConfig => new InfluxDBClient(
71 idbConfig.host,
72 idbConfig.dbName,
73 idbConfig.statNameMode,
74 idbConfig.flushIntervalMS,
75 {
76 system: System._config.system,
77 component: System._config.component,
78 hostname: HOSTNAME,
79 env: System._config.env,
80 })
81 )
82
83 System._s3logger = new S3Logger({
84 noHTTPS: (System._config.s3Logging || {}).noHTTPS,
85 host: (System._config.s3Logging || {}).host,
86 dataFieldsToPromote: (System._config.s3Logging || {}).dataFieldsToPromote,
87 dataFieldsToRemove: (System._config.s3Logging || {}).dataFieldsToRemove,
88 systemMeta: {
89 hostname: HOSTNAME,
90 system: System._config.system,
91 component: System._config.component,
92 env: System._config.env,
93 run_id: System.runId,
94 ver: System._config.ver,
95 },
96 })
97
98 // track 'start'
99 await Promise.all(System.influxDBClients.map(idbClient => idbClient.trackMetrics([{
100 statName: 'appStart',
101 dims: {
102 module: 'System',
103 },
104 value: 1,
105 }])))
106
107 }
108
109 static getConfig(): SystemConfig {
110 if (!System._config) {
111 throw new Error('System config accessed before set')
112 }
113 return System._config
114 }
115
116
117 static setAWSClient(awsClient: AWSClient) {
118 System._awsClient = awsClient
119 }
120
121 static queueLogRecord(logObj: Object): void {
122 if (System._s3logger)
123 System._s3logger.queueLogRecord(logObj)
124 }
125
126 static async flush(): Promise<void> {
127 await Promise.all([
128
129 (async () => {
130 if (System._s3logger)
131 await System._s3logger.flushLogs()
132 })(),
133
134 ...System.influxDBClients.map(idbClient => (async () => {
135 await idbClient.trackMetrics([{
136 statName: 'appEnd',
137 dims: { module: 'System' },
138 value: 1,
139 }])
140
141 await idbClient.flushMetrics()
142 })()),
143
144 ])
145 }
146}