metrics-service spike
This commit is contained in:
parent
2945190dbb
commit
3c1780342d
3
spikes/metrics-service/graphql/.gitignore
vendored
Normal file
3
spikes/metrics-service/graphql/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/node_modules
|
||||||
|
/npm-debug.log
|
||||||
|
.idea
|
16
spikes/metrics-service/graphql/package.json
Normal file
16
spikes/metrics-service/graphql/package.json
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "graphql",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node src/index.js"
|
||||||
|
},
|
||||||
|
"main": "src/index.js",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"graphql": "^0.7.2",
|
||||||
|
"hapi": "^15.2.0",
|
||||||
|
"hapi-graphql": "^1.0.1",
|
||||||
|
"inert": "^4.0.2",
|
||||||
|
"require-dir": "^0.3.1"
|
||||||
|
}
|
||||||
|
}
|
29
spikes/metrics-service/graphql/src/index.js
Normal file
29
spikes/metrics-service/graphql/src/index.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
const requireDir = require('require-dir');
|
||||||
|
const plugins = require('./plugins');
|
||||||
|
const routes = requireDir('./routes');
|
||||||
|
const Hapi = require('hapi');
|
||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const server = new Hapi.Server();
|
||||||
|
|
||||||
|
server.connection({
|
||||||
|
host: 'localhost',
|
||||||
|
port: 8000
|
||||||
|
});
|
||||||
|
|
||||||
|
server.register(plugins, (err) => {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.keys(routes).forEach((name) => {
|
||||||
|
routes[name](server);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.start((err) => {
|
||||||
|
server.connections.forEach((conn) => {
|
||||||
|
console.log(`started at: ${conn.info.uri}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
15
spikes/metrics-service/graphql/src/plugins.js
Normal file
15
spikes/metrics-service/graphql/src/plugins.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
module.exports = [
|
||||||
|
require('inert'), {
|
||||||
|
register: require('hapi-graphql'),
|
||||||
|
options: {
|
||||||
|
query: {
|
||||||
|
pretty: true,
|
||||||
|
graphiql: true,
|
||||||
|
schema: require('./schema')
|
||||||
|
},
|
||||||
|
route: {
|
||||||
|
path: '/graphql'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
11
spikes/metrics-service/graphql/src/routes/home.js
Normal file
11
spikes/metrics-service/graphql/src/routes/home.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/',
|
||||||
|
handler: (request, reply) => {
|
||||||
|
reply.file(path.join(__dirname, 'index.html'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
28
spikes/metrics-service/graphql/src/routes/index.html
Normal file
28
spikes/metrics-service/graphql/src/routes/index.html
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script>
|
||||||
|
const client = fetch('http://localhost:8000/graphql', {
|
||||||
|
method: 'POST',
|
||||||
|
body: `
|
||||||
|
subscription {
|
||||||
|
events(container:"1") {
|
||||||
|
value,
|
||||||
|
when
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
});
|
||||||
|
|
||||||
|
client.then((client) => {
|
||||||
|
debugger;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
18
spikes/metrics-service/graphql/src/routes/version.js
Normal file
18
spikes/metrics-service/graphql/src/routes/version.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
const Pkg = require('../../package.json');
|
||||||
|
|
||||||
|
const internals = {
|
||||||
|
response: {
|
||||||
|
version: Pkg.version
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/ops/version',
|
||||||
|
config: {
|
||||||
|
description: 'Returns the version of the server',
|
||||||
|
handler: (request, reply) => reply(internals.response)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
61
spikes/metrics-service/graphql/src/schema.js
Normal file
61
spikes/metrics-service/graphql/src/schema.js
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
const graphql = require('graphql');
|
||||||
|
|
||||||
|
const {
|
||||||
|
GraphQLID,
|
||||||
|
GraphQLSchema,
|
||||||
|
GraphQLObjectType,
|
||||||
|
GraphQLString
|
||||||
|
} = graphql;
|
||||||
|
|
||||||
|
const EventType = new GraphQLObjectType({
|
||||||
|
name: 'EventType',
|
||||||
|
fields: {
|
||||||
|
value: {
|
||||||
|
type: GraphQLString
|
||||||
|
},
|
||||||
|
when: {
|
||||||
|
type: GraphQLString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const EventSubscription = {
|
||||||
|
type: EventType,
|
||||||
|
args: {
|
||||||
|
container: {
|
||||||
|
type: GraphQLID
|
||||||
|
}
|
||||||
|
},
|
||||||
|
start: function() {
|
||||||
|
console.log('start', arguments);
|
||||||
|
},
|
||||||
|
stop: function() {
|
||||||
|
console.log('stop', arguments);
|
||||||
|
},
|
||||||
|
resolve: function() {
|
||||||
|
console.log('resolve', arguments);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const subscription = new GraphQLObjectType({
|
||||||
|
name: 'RootSubscriptionType',
|
||||||
|
fields: {
|
||||||
|
events: EventSubscription
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = new GraphQLSchema({
|
||||||
|
query: new GraphQLObjectType({
|
||||||
|
name: 'Q',
|
||||||
|
fields: {
|
||||||
|
a: { type: GraphQLString },
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
mutation: new GraphQLObjectType({
|
||||||
|
name: 'M',
|
||||||
|
fields: {
|
||||||
|
c: { type: GraphQLString },
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
subscription
|
||||||
|
});
|
3
spikes/metrics-service/http1/.gitignore
vendored
Normal file
3
spikes/metrics-service/http1/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/node_modules
|
||||||
|
/npm-debug.log
|
||||||
|
.idea
|
14
spikes/metrics-service/http1/package.json
Normal file
14
spikes/metrics-service/http1/package.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"name": "http1",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node src/index.js"
|
||||||
|
},
|
||||||
|
"main": "src/index.js",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"hapi": "^15.2.0",
|
||||||
|
"inert": "^4.0.2",
|
||||||
|
"require-dir": "^0.3.1"
|
||||||
|
}
|
||||||
|
}
|
29
spikes/metrics-service/http1/src/index.js
Normal file
29
spikes/metrics-service/http1/src/index.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
const requireDir = require('require-dir');
|
||||||
|
const plugins = require('./plugins');
|
||||||
|
const routes = requireDir('./routes');
|
||||||
|
const Hapi = require('hapi');
|
||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const server = new Hapi.Server();
|
||||||
|
|
||||||
|
server.connection({
|
||||||
|
host: 'localhost',
|
||||||
|
port: 8000
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.keys(routes).forEach((name) => {
|
||||||
|
routes[name](server);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.register(plugins, (err) => {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.start((err) => {
|
||||||
|
server.connections.forEach((conn) => {
|
||||||
|
console.log(`started at: ${conn.info.uri}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
3
spikes/metrics-service/http1/src/plugins.js
Normal file
3
spikes/metrics-service/http1/src/plugins.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module.exports = [
|
||||||
|
require('inert')
|
||||||
|
];
|
11
spikes/metrics-service/http1/src/routes/home.js
Normal file
11
spikes/metrics-service/http1/src/routes/home.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/',
|
||||||
|
handler: (request, reply) => {
|
||||||
|
reply.file(path.join(__dirname, 'index.html'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
16
spikes/metrics-service/http1/src/routes/index.html
Normal file
16
spikes/metrics-service/http1/src/routes/index.html
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<script>
|
||||||
|
var source = new EventSource(`${document.location.origin}/stats`);
|
||||||
|
|
||||||
|
source.onmessage = function(e) {
|
||||||
|
document.body.innerHTML += "Message: " + e.data + '<br />';
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
21
spikes/metrics-service/http1/src/routes/metrics.js
Normal file
21
spikes/metrics-service/http1/src/routes/metrics.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
let messageId = 0;
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/stats',
|
||||||
|
handler: (request, reply) => {
|
||||||
|
request.raw.res.setHeader('Content-Type', 'text/event-stream');
|
||||||
|
|
||||||
|
const intervalId = setInterval(() => {
|
||||||
|
messageId += 1;
|
||||||
|
|
||||||
|
const str = JSON.stringify({
|
||||||
|
msg: messageId
|
||||||
|
});
|
||||||
|
|
||||||
|
request.raw.res.write(`data:${str}\n\n`);
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
18
spikes/metrics-service/http1/src/routes/version.js
Normal file
18
spikes/metrics-service/http1/src/routes/version.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
const Pkg = require('../../package.json');
|
||||||
|
|
||||||
|
const internals = {
|
||||||
|
response: {
|
||||||
|
version: Pkg.version
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/ops/version',
|
||||||
|
config: {
|
||||||
|
description: 'Returns the version of the server',
|
||||||
|
handler: (request, reply) => reply(internals.response)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
3
spikes/metrics-service/http2/.gitignore
vendored
Normal file
3
spikes/metrics-service/http2/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/node_modules
|
||||||
|
/npm-debug.log
|
||||||
|
.idea
|
33
spikes/metrics-service/http2/cert.pem
Normal file
33
spikes/metrics-service/http2/cert.pem
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
-----BEGIN CERTIFICATE-----
|
||||||
|
MIIFtTCCA52gAwIBAgIJAK4ScT3ylJVTMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
|
||||||
|
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
|
||||||
|
aWRnaXRzIFB0eSBMdGQwHhcNMTYxMTAzMTQyNDIyWhcNMTcxMTAzMTQyNDIyWjBF
|
||||||
|
MQswCQYDVQQGEwJBVTETMBEGA1U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b21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGSCCQCu
|
||||||
|
EnE98pSVUzAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBBQUAA4ICAQBAZ4Wvcnoi
|
||||||
|
dAZ3HMBODZTw9E6NZLKgl4aVQiktkCOBOwzJKxkzCrrrr+YQXYx4rHPi85FBevt/
|
||||||
|
NkSsm/Ux+am5iNLjMOW3kDIt1VYYQ6Wn4iPEPUeWzCeyHCOfbG3gOsjDWC1GmWaY
|
||||||
|
WIBi3Zh+7txvrzyhRdNwoS+pAZOzbljCisSDmt1an48tpojI/ZqLD+oGg0LEMuik
|
||||||
|
sBWl227KxyW9DEwLcK9K3I0zhR3NKqWYt47XYvVDq+CqiVI7+/RMwnX/gtIjjGGS
|
||||||
|
5gLdD7mS+Fjig0LtCasDqKYraNQfTAyW3afYITeU4QAKukpJXwwGil3SEP2PxG/y
|
||||||
|
GBz2x6hgQZHB+8VzeA4Zxu5SmLOetJ3yxHRi8NWbiaIn/J+wdRLMjXZ0jTvv86mE
|
||||||
|
VB9X9e0ltvCH/o/VrCOVWHuUXP0zNRawQqYB1qRZ7I7Z/Aes0TQTcQOD/RYUnsfK
|
||||||
|
Mk9XhEpp9IQerufd4e0wlUq6BBA2sN6mkgy57Zsix+LAjU15z0hDRE2xWWNmbakY
|
||||||
|
VBgUTQ8KObAMaSbzUwTildtbsDhtI6pYr8y096NjFiWqkon6R8dveZBfvv5vwoxc
|
||||||
|
h2HTw85eGHyBDcWUe7c7+D+Tp7feNVOeeWtvF7LSRnmTlzqoHdMQ5ns9DxaoUu1Y
|
||||||
|
kbIEE8WnLU0m72Vq5Jzvode8VP3tZpxyvw==
|
||||||
|
-----END CERTIFICATE-----
|
51
spikes/metrics-service/http2/key.pem
Normal file
51
spikes/metrics-service/http2/key.pem
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
-----BEGIN RSA PRIVATE KEY-----
|
||||||
|
MIIJKQIBA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W0n1JGHKihf5E3/KWkOwEIewPrff1wCjFJYDncbPbV7UqyOGC1qLv5tHSLBQ/
|
||||||
|
sr3e4pAcqHh1bJWjq7+VN41LwPsoQ8EZdHsoS4Nw0zDln0VTKk532b6yisXCvaye
|
||||||
|
zpoWnfPNS6JfL6M1zw+6vLZCEyBO+d8rVo9lCeO+FOI1N84dTZwmZD3K7QubMKco
|
||||||
|
QShjtmygjEpmw02yRF/85mh1ki6f5rklCeJGqgTj8CDQr6uMczC80n5ISXn87uYW
|
||||||
|
HAkErZpC4ZEqsq+ZexAZSJElATogEsfx7psS4UoYfvf52/X8/Idl+MEmY5h6
|
||||||
|
-----END RSA PRIVATE KEY-----
|
17
spikes/metrics-service/http2/package.json
Normal file
17
spikes/metrics-service/http2/package.json
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"name": "http2",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node src/index.js"
|
||||||
|
},
|
||||||
|
"main": "src/index.js",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"bunyan": "^1.8.4",
|
||||||
|
"hapi": "^15.2.0",
|
||||||
|
"http2": "^3.3.6",
|
||||||
|
"inert": "^4.0.2",
|
||||||
|
"require-dir": "^0.3.1",
|
||||||
|
"string-to-stream": "^1.1.0"
|
||||||
|
}
|
||||||
|
}
|
40
spikes/metrics-service/http2/src/index.js
Normal file
40
spikes/metrics-service/http2/src/index.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
const http2 = require('http2');
|
||||||
|
const requireDir = require('require-dir');
|
||||||
|
const plugins = require('./plugins');
|
||||||
|
const routes = requireDir('./routes');
|
||||||
|
const Hapi = require('hapi');
|
||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const server = new Hapi.Server();
|
||||||
|
|
||||||
|
server.connection({
|
||||||
|
listener: http2.createServer({
|
||||||
|
key: fs.readFileSync(path.join(__dirname, '../key.pem')),
|
||||||
|
cert: fs.readFileSync(path.join(__dirname, '../cert.pem')),
|
||||||
|
log: require('bunyan').createLogger({
|
||||||
|
name: 'server',
|
||||||
|
stream: process.stdout,
|
||||||
|
serializers: require('http2/lib/http').serializers
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
host: 'localhost',
|
||||||
|
port: 8000,
|
||||||
|
tls: true
|
||||||
|
});
|
||||||
|
|
||||||
|
Object.keys(routes).forEach((name) => {
|
||||||
|
routes[name](server);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.register(plugins, (err) => {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.start((err) => {
|
||||||
|
server.connections.forEach((conn) => {
|
||||||
|
console.log(`started at: ${conn.info.uri}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
3
spikes/metrics-service/http2/src/plugins.js
Normal file
3
spikes/metrics-service/http2/src/plugins.js
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module.exports = [
|
||||||
|
require('inert')
|
||||||
|
];
|
11
spikes/metrics-service/http2/src/routes/home.js
Normal file
11
spikes/metrics-service/http2/src/routes/home.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/',
|
||||||
|
handler: (request, reply) => {
|
||||||
|
reply.file(path.join(__dirname, 'index.html'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
25
spikes/metrics-service/http2/src/routes/index.html
Normal file
25
spikes/metrics-service/http2/src/routes/index.html
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<script>
|
||||||
|
var source = new EventSource(`${document.location.origin}/stats`);
|
||||||
|
|
||||||
|
source.onmessage = function(e) {
|
||||||
|
document.body.innerHTML += "SSE notification: " + e.data + '<br />';
|
||||||
|
|
||||||
|
// fetch resource via XHR... from cache!
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.open('GET', e.data);
|
||||||
|
xhr.onload = function() {
|
||||||
|
document.body.innerHTML += "Message: " + this.response + '<br />';
|
||||||
|
};
|
||||||
|
|
||||||
|
xhr.send();
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
</body>
|
||||||
|
</html>
|
38
spikes/metrics-service/http2/src/routes/metrics.js
Normal file
38
spikes/metrics-service/http2/src/routes/metrics.js
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
const str = require('string-to-stream');
|
||||||
|
|
||||||
|
let messageId = 0;
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/stats',
|
||||||
|
handler: (request, reply) => {
|
||||||
|
request.raw.res.setHeader('Content-Type', 'text/event-stream');
|
||||||
|
|
||||||
|
const intervalId = setInterval(() => {
|
||||||
|
const resourcePath = `/resource/${messageId}`;
|
||||||
|
|
||||||
|
if (!request.raw.res.push) {
|
||||||
|
clearInterval(intervalId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const stream = request.raw.res.push(resourcePath, {
|
||||||
|
status: 200,
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
str(JSON.stringify({
|
||||||
|
msg: messageId
|
||||||
|
})).pipe(stream);
|
||||||
|
|
||||||
|
request.raw.res.write(`data:${resourcePath}\n\n`);
|
||||||
|
|
||||||
|
messageId += 1;
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
18
spikes/metrics-service/http2/src/routes/version.js
Normal file
18
spikes/metrics-service/http2/src/routes/version.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
const Pkg = require('../../package.json');
|
||||||
|
|
||||||
|
const internals = {
|
||||||
|
response: {
|
||||||
|
version: Pkg.version
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/ops/version',
|
||||||
|
config: {
|
||||||
|
description: 'Returns the version of the server',
|
||||||
|
handler: (request, reply) => reply(internals.response)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
30
spikes/metrics-service/readme.md
Normal file
30
spikes/metrics-service/readme.md
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# metrics-service
|
||||||
|
|
||||||
|
> A service which produces random streams of data (firehose) of interpretable metric information for various subsystems that can or will be pulled out from a container. These are currently defined in [RFD-0037](https://github.com/joyent/rfd/blob/master/rfd/0037/README.md#default-metric-keys).
|
||||||
|
|
||||||
|
This spike's purpose was to find the best pattern to expose such metrics service with mock data.
|
||||||
|
|
||||||
|
## http1
|
||||||
|
|
||||||
|
+ very simple API to push data from the server to the client
|
||||||
|
+ plain HTTP
|
||||||
|
- requires a connection to each container - because it's not bidirectional, we can't subscribe to more containers on-demand
|
||||||
|
|
||||||
|
## http2
|
||||||
|
|
||||||
|
+ very simple API to push data from the server to the client
|
||||||
|
+ multiplexing
|
||||||
|
- push doesn't really help us, because it's only useful for static resources
|
||||||
|
|
||||||
|
## graphql
|
||||||
|
|
||||||
|
+ common interface between all data resources
|
||||||
|
+ filter received data
|
||||||
|
- api not finalised yet
|
||||||
|
- api not documented
|
||||||
|
|
||||||
|
## ws (with [nes](https://github.com/hapijs/nes))
|
||||||
|
|
||||||
|
+ strong integration with [Hapi](https://github.com/hapijs/hapi)
|
||||||
|
+ allow to subscribe and unsubscribe on-demand on a single connection
|
||||||
|
- some networks might require fallback to pooling
|
3
spikes/metrics-service/ws/.gitignore
vendored
Normal file
3
spikes/metrics-service/ws/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/node_modules
|
||||||
|
/npm-debug.log
|
||||||
|
.idea
|
16
spikes/metrics-service/ws/package.json
Normal file
16
spikes/metrics-service/ws/package.json
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "ws",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node src/index.js"
|
||||||
|
},
|
||||||
|
"main": "src/index.js",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"component-emitter": "^1.2.1",
|
||||||
|
"hapi": "^15.2.0",
|
||||||
|
"inert": "^4.0.2",
|
||||||
|
"nes": "^6.3.1",
|
||||||
|
"require-dir": "^0.3.1"
|
||||||
|
}
|
||||||
|
}
|
29
spikes/metrics-service/ws/src/index.js
Normal file
29
spikes/metrics-service/ws/src/index.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
const requireDir = require('require-dir');
|
||||||
|
const plugins = require('./plugins');
|
||||||
|
const routes = requireDir('./routes');
|
||||||
|
const Hapi = require('hapi');
|
||||||
|
const path = require('path');
|
||||||
|
const fs = require('fs');
|
||||||
|
|
||||||
|
const server = new Hapi.Server();
|
||||||
|
|
||||||
|
server.connection({
|
||||||
|
host: 'localhost',
|
||||||
|
port: 8000
|
||||||
|
});
|
||||||
|
|
||||||
|
server.register(plugins, (err) => {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.keys(routes).forEach((name) => {
|
||||||
|
routes[name](server);
|
||||||
|
});
|
||||||
|
|
||||||
|
server.start((err) => {
|
||||||
|
server.connections.forEach((conn) => {
|
||||||
|
console.log(`started at: ${conn.info.uri}`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
30
spikes/metrics-service/ws/src/metric.js
Normal file
30
spikes/metrics-service/ws/src/metric.js
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
const Emitter = require('component-emitter');
|
||||||
|
|
||||||
|
const cdm = {};
|
||||||
|
|
||||||
|
module.exports = (server) => ({
|
||||||
|
on: (id) => {
|
||||||
|
if (cdm[id]) {
|
||||||
|
cdm[id].sockets +=1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let messageId = 0;
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
server.publish(`/stats/${id}`, {
|
||||||
|
id: messageId += 1
|
||||||
|
});
|
||||||
|
}, 500);
|
||||||
|
|
||||||
|
cdm[id] = {
|
||||||
|
interval,
|
||||||
|
sockets: 1
|
||||||
|
};
|
||||||
|
},
|
||||||
|
off: (id) => {
|
||||||
|
if (!(cdm[id].sockets -= 1)) {
|
||||||
|
clearInterval(cdm[id].interval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
4
spikes/metrics-service/ws/src/plugins.js
Normal file
4
spikes/metrics-service/ws/src/plugins.js
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
module.exports = [
|
||||||
|
require('inert'),
|
||||||
|
require('nes')
|
||||||
|
];
|
11
spikes/metrics-service/ws/src/routes/home.js
Normal file
11
spikes/metrics-service/ws/src/routes/home.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/',
|
||||||
|
handler: (request, reply) => {
|
||||||
|
reply.file(path.join(__dirname, 'index.html'));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
33
spikes/metrics-service/ws/src/routes/index.html
Normal file
33
spikes/metrics-service/ws/src/routes/index.html
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<script src="https://unpkg.com/nes@6.3.1"></script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="root"></div>
|
||||||
|
<script>
|
||||||
|
const client = new window.nes.Client('ws://localhost:8000');
|
||||||
|
|
||||||
|
client.connect((err) => {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('connected');
|
||||||
|
|
||||||
|
client.subscribe('/stats/5', (update, flag) => {
|
||||||
|
console.log(update, flag);
|
||||||
|
}, (err) => {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('subscribed');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
18
spikes/metrics-service/ws/src/routes/metrics.js
Normal file
18
spikes/metrics-service/ws/src/routes/metrics.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
const Metric = require('../metric');
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
const metric = Metric(server);
|
||||||
|
|
||||||
|
server.subscription('/stats/{id}', {
|
||||||
|
onSubscribe: (socket, path, params, next) => {
|
||||||
|
console.log('onSubscribe');
|
||||||
|
metric.on(params.id);
|
||||||
|
next();
|
||||||
|
},
|
||||||
|
onUnsubscribe: (socket, path, params, next) => {
|
||||||
|
console.log('onUnsubscribe');
|
||||||
|
metric.off(params.id);
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
18
spikes/metrics-service/ws/src/routes/version.js
Normal file
18
spikes/metrics-service/ws/src/routes/version.js
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
const Pkg = require('../../package.json');
|
||||||
|
|
||||||
|
const internals = {
|
||||||
|
response: {
|
||||||
|
version: Pkg.version
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = (server) => {
|
||||||
|
server.route({
|
||||||
|
method: 'GET',
|
||||||
|
path: '/ops/version',
|
||||||
|
config: {
|
||||||
|
description: 'Returns the version of the server',
|
||||||
|
handler: (request, reply) => reply(internals.response)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user