Let to the caller to choose how to handle the call to a method
This commit is contained in:
parent
e90c196598
commit
1bbde4f5e3
@ -3,7 +3,7 @@
|
||||
const EVENT_VERSION = '1';
|
||||
const MAX_LENGTH = 100;
|
||||
|
||||
function pubSubMetrics ({ enabled, metricsBackend }) {
|
||||
function pubSubMetrics ({ enabled, metricsBackend, logger }) {
|
||||
if (!enabled) {
|
||||
return function pubSubMetricsDisabledMiddleware (req, res, next) {
|
||||
next();
|
||||
@ -15,7 +15,9 @@ function pubSubMetrics ({ enabled, metricsBackend }) {
|
||||
const { event, attributes } = getEventData(req, res);
|
||||
|
||||
if (event) {
|
||||
metricsBackend.send(event, attributes);
|
||||
metricsBackend.send(event, attributes)
|
||||
.then(() => logger.debug(`PubSubTracker: event '${event}' published succesfully`))
|
||||
.catch((error) => logger.error(`ERROR: pubsub middleware failed to publish event '${event}': ${error.message}`));
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -5,19 +5,13 @@ const { PubSub } = require('@google-cloud/pubsub');
|
||||
module.exports = class PubSubMetricsBackend {
|
||||
constructor (options = {}) {
|
||||
const { project_id: projectId, credentials: keyFilename, topic } = options;
|
||||
|
||||
this._pubsub = new PubSub({ projectId, keyFilename });
|
||||
this._topicName = topic;
|
||||
}
|
||||
|
||||
send (event, attributes) {
|
||||
const data = Buffer.from(event);
|
||||
|
||||
this._pubsub.topic(this._topicName).publish(data, attributes)
|
||||
.then(() => {
|
||||
console.log(`PubSubTracker: event '${event}' published to '${this._topicName}'`);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(`ERROR: pubsub middleware failed to publish event '${event}': ${error.message}`);
|
||||
});
|
||||
return this._pubsub.topic(this._topicName).publish(data, attributes);
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user