2020-02-20 18:48:32 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { PubSub } = require('@google-cloud/pubsub');
|
|
|
|
|
2020-04-27 17:59:36 +08:00
|
|
|
module.exports = class PubSubMetricsBackend {
|
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-27 18:46:27 +08:00
|
|
|
this._pubsub = new PubSub({ projectId, keyFilename });
|
|
|
|
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-27 19:27:05 +08:00
|
|
|
return this._pubsub.topic(this._topicName).publish(data, attributes);
|
2020-02-20 18:48:32 +08:00
|
|
|
}
|
2020-04-27 18:13:54 +08:00
|
|
|
};
|