Simplified metrics middleware and backend
This commit is contained in:
parent
6a2333be64
commit
e90c196598
@ -187,7 +187,7 @@ module.exports = class ApiRouter {
|
||||
this.mapRouter = new MapRouter({ collaborators });
|
||||
this.templateRouter = new TemplateRouter({ collaborators });
|
||||
this.metadataBackend = metadataBackend;
|
||||
this.pubSubMetricsBackend = PubSubMetricsBackend.build();
|
||||
this.pubSubMetricsBackend = new PubSubMetricsBackend(this.serverOptions.pubSubMetrics);
|
||||
}
|
||||
|
||||
route (app, routes) {
|
||||
@ -220,7 +220,10 @@ module.exports = class ApiRouter {
|
||||
apiRouter.use(sendResponse());
|
||||
apiRouter.use(syntaxError());
|
||||
apiRouter.use(errorMiddleware());
|
||||
apiRouter.use(pubSubMetrics(this.pubSubMetricsBackend));
|
||||
apiRouter.use(pubSubMetrics({
|
||||
enabled: this.serverOptions.pubSubMetrics.enabled,
|
||||
metricsBackend: this.pubSubMetricsBackend
|
||||
}));
|
||||
|
||||
paths.forEach(path => app.use(path, apiRouter));
|
||||
});
|
||||
|
@ -3,8 +3,8 @@
|
||||
const EVENT_VERSION = '1';
|
||||
const MAX_LENGTH = 100;
|
||||
|
||||
function pubSubMetrics (pubSubMetricsBackend) {
|
||||
if (!pubSubMetricsBackend.isEnabled()) {
|
||||
function pubSubMetrics ({ enabled, metricsBackend }) {
|
||||
if (!enabled) {
|
||||
return function pubSubMetricsDisabledMiddleware (req, res, next) {
|
||||
next();
|
||||
};
|
||||
@ -15,7 +15,7 @@ function pubSubMetrics (pubSubMetricsBackend) {
|
||||
const { event, attributes } = getEventData(req, res);
|
||||
|
||||
if (event) {
|
||||
pubSubMetricsBackend.sendEvent(event, attributes);
|
||||
metricsBackend.send(event, attributes);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -3,37 +3,18 @@
|
||||
const { PubSub } = require('@google-cloud/pubsub');
|
||||
|
||||
module.exports = class PubSubMetricsBackend {
|
||||
static build () {
|
||||
if (!global.environment.pubSubMetrics || !global.environment.pubSubMetrics.enabled) {
|
||||
return new PubSubMetricsBackend(undefined, false);
|
||||
}
|
||||
|
||||
const { project_id: projectId, credentials: keyFilename, topic } = global.environment.pubSubMetrics;
|
||||
const pubsub = new PubSub({ projectId, keyFilename });
|
||||
|
||||
return new PubSubMetricsBackend(pubsub, topic, true);
|
||||
constructor (options = {}) {
|
||||
const { project_id: projectId, credentials: keyFilename, topic } = options;
|
||||
this._pubsub = new PubSub({ projectId, keyFilename });
|
||||
this._topicName = topic;
|
||||
}
|
||||
|
||||
constructor (pubSub, topic, enabled) {
|
||||
this.pubsub = pubSub;
|
||||
this.topic = topic;
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
isEnabled () {
|
||||
return this.enabled;
|
||||
}
|
||||
|
||||
sendEvent (event, attributes) {
|
||||
if (!this.enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
send (event, attributes) {
|
||||
const data = Buffer.from(event);
|
||||
|
||||
this.pubsub.topic(this.topic).publish(data, attributes)
|
||||
this._pubsub.topic(this._topicName).publish(data, attributes)
|
||||
.then(() => {
|
||||
console.log(`PubSubTracker: event '${event}' published to '${this.topic}'`);
|
||||
console.log(`PubSubTracker: event '${event}' published to '${this._topicName}'`);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(`ERROR: pubsub middleware failed to publish event '${event}': ${error.message}`);
|
||||
|
@ -134,5 +134,6 @@ module.exports = {
|
||||
fastly: global.environment.fastly || {},
|
||||
cache_enabled: global.environment.cache_enabled,
|
||||
log_format: global.environment.log_format,
|
||||
useProfiler: global.environment.useProfiler
|
||||
useProfiler: global.environment.useProfiler,
|
||||
pubSubMetrics: Object.assign({ enabled: false }, global.environment.pubSubMetrics)
|
||||
};
|
||||
|
@ -21,6 +21,7 @@ module.exports = function createServer (serverOptions) {
|
||||
app.disable('etag');
|
||||
app.set('json replacer', jsonReplacer());
|
||||
|
||||
// FIXME: do not pass 'global.environment' as 'serverOptions' should keep defaults from 'global.environment'
|
||||
const apiRouter = new ApiRouter({ serverOptions, environmentOptions: global.environment });
|
||||
|
||||
apiRouter.route(app, serverOptions.routes.api);
|
||||
|
Loading…
Reference in New Issue
Block a user