2020-02-20 18:48:32 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { PubSub } = require('@google-cloud/pubsub');
|
|
|
|
|
2020-04-29 16:40:45 +08:00
|
|
|
module.exports = class MetricsBackend {
|
2020-04-27 18:46:27 +08:00
|
|
|
constructor (options = {}) {
|
|
|
|
const { project_id: projectId, credentials: keyFilename, topic } = options;
|
2020-04-27 19:27:05 +08:00
|
|
|
|
2020-04-29 16:40:45 +08:00
|
|
|
this._metricsClient = new PubSub({ projectId, keyFilename });
|
2020-04-27 18:46:27 +08:00
|
|
|
this._topicName = topic;
|
2020-02-20 18:48:32 +08:00
|
|
|
}
|
|
|
|
|
2020-04-27 18:46:27 +08:00
|
|
|
send (event, attributes) {
|
2020-02-20 18:48:32 +08:00
|
|
|
const data = Buffer.from(event);
|
2020-04-29 16:40:45 +08:00
|
|
|
return this._metricsClient.topic(this._topicName).publish(data, attributes);
|
2020-02-20 18:48:32 +08:00
|
|
|
}
|
2020-04-27 18:13:54 +08:00
|
|
|
};
|