Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): added prometheus metrics to messaging server #546

Merged
merged 3 commits into from
Oct 19, 2022

Conversation

michaelmass
Copy link
Contributor

This adds prometheus metrics to the messaging server. It exposes an endpoint on port 9090 for Prometheus.

There are 2 custom metrics added messages_total and conversations_total for the total of messages and conversations created by the current messaging server.

Copy link
Member

@EFF EFF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@samuelmasse samuelmasse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

Be careful to not break design principles though. The express object isn't given to services anywhere because it's not the services job to interact with the API.

Generally in this codebase :

  • Api : makes something queryable
  • Stream : sends data to some place
  • Service : any kind of processing of data from the DB or from events
  • Api and Stream never interact with each other
  • Api and Stream depend on Service, never other way around

The express object is made available in the constructor of the base Api object (it's the root parameter). You can take that to extract the code in you service that depends on having the express router and make a new MetricsApi class. If it's not possible to separate the two cleanly then it's preferable to put everything in the API layer regardless.

Comment on lines 64 to 70
this.logger.debug(`${clc.blackBright(`[${id}]`)} ${clc.cyan('metrics')} messages incremented`)
messagesCount.inc()
}

private async handleConversationCreated({ conversation: { id } }: ConversationCreatedEvent) {
this.logger.debug(`${clc.blackBright(`[${id}]`)} ${clc.cyan('metrics')} conversations incremented`)
conversationsCount.inc()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might impact performance. If would put this before :

if (yn(process.env.LOGGING_ENABLED)) {

The debugger in messaging is very dumb, doing logger.debug doesn't stop it from printing messages even if logging is disabled

Comment on lines 1 to 3
import { Entry, start } from '@botpress/messaging-framework'
import './rewire'
import { Api } from './api'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not putting import './rewire' at the top is likely going to break import from @botpress/messaging-framework when using yarn dev

import * as promex from '@bpinternal/promex'
import { defaultNormalizers } from '@promster/express'
import clc from 'cli-color'
import type { Express } from 'express'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never seen import type before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prevents the package from being bundled since it's only a typescript concept. import type gives you only the access to the types of a package.

@@ -0,0 +1 @@
export * from './metrics'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "re-export from index.ts" pattern is not used in the codebase so it's a bit inconsistent to use it here

@michaelmass michaelmass merged commit bf7fd94 into master Oct 19, 2022
@michaelmass michaelmass deleted the mm-add-prometheus-metrics branch October 19, 2022 14:15
Comment on lines +1 to +10
import type { Express } from 'express'
import { App } from './app'

export class Interceptor {
constructor(private app: App, private express: Express) {}

async setup() {
this.app.metrics.init(this.express)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really what I meant but whatever. I think it's time I unsubscribe from this repo lol I'll stop bothering you with nitpick comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants