This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_usage_collector.js
164 lines (141 loc) · 5.32 KB
/
meta_usage_collector.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var url = require('url');
// collect usage meta data
module.exports = function(config) {
var client = config.client;
var serviceRegistryClient = config.serviceRegistryClient;
var tenantId = null;
var tenantIdTimer = null;
var _pendingGetTennatId = false;
return function(runtime) {
var getTenantId = function() {
if (_pendingGetTennatId || tenantId !== null) {
return;
}
_pendingGetTennatId = true;
serviceRegistryClient.get(config.serverUrl, function(err, result) {
_pendingGetTennatId = false;
if (err) {
console.error(err);
return;
}
if (result.tenantId) {
tenantId = result.tenantId;
clearInterval(tenantIdTimer);
}
});
};
tenantIdTimer = setInterval(getTenantId, 30000);
runtime.httpServer.cloud.use(function(handle) {
handle('response', function(env, next) {
if (tenantId === null) {
return next(env);
}
// Dont record 500 error codes
if (env.response.statusCode >= 500 && env.response.statusCode < 600) {
return next(env);
}
var parsed = url.parse(env.request.url, true);
// Record device query requests
if (parsed.pathname === '/' && parsed.query.ql) {
var servers = [];
if (parsed.query.server === '*') {
Object.keys(runtime.httpServer.peers).forEach(function(targetName) {
// Only record requests going to connected peers
if (runtime.httpServer.peers[targetName] && runtime.httpServer.peers[targetName].state === 2) {
servers.push(targetName);
}
});
} else {
servers = [parsed.query.server];
}
// Record device queries
servers.forEach(function(targetName) {
client.increment('linkusage.hub.http.count', { tenantId: tenantId, targetName: targetName, reqType: 'device-query' });
});
} else {
var match = /^\/servers\/(.+)$/.exec(env.request.url);
if (match) {
var targetName = decodeURIComponent(/^\/servers\/(.+)$/.exec(parsed.pathname)[1].split('/')[0]);
client.increment('linkusage.hub.http.count', { tenantId: tenantId, targetName: targetName, reqType: 'proxy'});
}
}
next(env);
});
});
var peers = [];
runtime.pubsub.subscribe('_peer/connect', function(ev, msg) {
if (peers.indexOf(msg.peer.name) < 0) {
peers.push(msg.peer.name);
getTenantId();
// Log number of spdy pings sent
var ping = msg.peer.agent.ping;
msg.peer.agent.ping = function() {
if (tenantId !== null) {
client.increment('linkusage.hub.spdypings.count', { tenantId: tenantId, targetName: msg.peer.name});
}
ping.apply(msg.peer.agent, arguments);
};
// Log number of subscribe/unsubscribe requests are proxied to hub
['subscribe', 'unsubscribe', 'transition'].forEach(function(func) {
var origFunc = msg.peer[func];
msg.peer[func] = function() {
if (tenantId !== null) {
client.increment('linkusage.hub.http.count', { tenantId: tenantId, targetName: msg.peer.name, reqType: func });
}
origFunc.apply(msg.peer, arguments);
}
});
// Record peer connect/error/end events
['connected', 'error', 'end'].forEach(function(event) {
msg.peer.on(event, function() {
if (tenantId !== null) {
client.increment('link.targets.hub.' + event, { tenantId: tenantId, targetName: msg.peer.name});
}
});
});
// Record all messages sent from hub
msg.peer.on('zetta-events', function(topic, data) {
if (!data.topic || tenantId === null) {
return;
}
var split = data.topic.split('/');
// linkusage.hub.messages
var sendData = {
tenantId: tenantId,
targetName: msg.peer.name,
topic: data.topic
};
// Device data events that conform to {type}/{id}/{stream}
if (split.length === 3) {
sendData.deviceType = split[0];
sendData.device = split[1];
sendData.stream = split[2];
}
var dataBytes = 0;
if (data.data) {
try {
dataBytes = JSON.stringify(data.data).length
} catch (err) {
return;
}
}
client.increment('linkusage.hub.messages.count', sendData);
client.count('linkusage.hub.messages.bytes', dataBytes, sendData);
});
}
});
};
};