Skip to content

Commit

Permalink
feat: add metrics to api response
Browse files Browse the repository at this point in the history
  • Loading branch information
willgarrett64 committed Jul 13, 2022
1 parent 3500a3d commit 4d9bfaf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
29 changes: 29 additions & 0 deletions src/network-bot/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (C) 2022 Edge Network Technologies Limited
// Use of this source code is governed by a GNU GPL-style license
// that can be found in the LICENSE.md file. All rights reserved.

// import * as metrics from './metrics'
// import cors from 'cors'
import express, { ErrorRequestHandler, RequestHandler } from 'express'

export class API {
private app: express.Express
private network: any

constructor(network: any) {
this.app = express()
this.network = network
}

async initialize (): Promise<void> {
const app = this.app
// app.use(cors())
app.use(express.json())

app.get('/metrics', async (req, res) => {
res.send(await this.network.metricsRegistry.registry.metrics())
})

app.listen('5000')
}
}
9 changes: 4 additions & 5 deletions src/network-bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import { API } from './api'
import Discord from 'discord.js'
import { GlobalConfig } from '../config'
// import { Metrics } from './metrics'
import { Metrics } from './metrics'
import superagent from 'superagent'
import { Log, LogLevelFromString, StdioAdaptor } from'@edge/log'

export class NetworkBot {
private api: any
private metrics: any
// private metricsRegistry: Metrics
private metricsRegistry: Metrics

private log: Log
private client: Discord.Client
Expand All @@ -23,7 +23,7 @@ export class NetworkBot {
this.log = new Log([new StdioAdaptor()], 'network-bot', LogLevelFromString(GlobalConfig.logLevel))

this.api = new API(this)
// this.metricsRegistry = new Metrics(this)
this.metricsRegistry = new Metrics(this)
this.metrics = { offline: 0, online: 0}

this.client = new Discord.Client({ intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_MEMBERS', 'GUILD_PRESENCES']})
Expand Down Expand Up @@ -65,7 +65,6 @@ export class NetworkBot {

async updateActivity(): Promise<void> {
try {
console.log('test')
const response = await superagent.get('https://stargate.edge.network/sessions/open')

if (response.body) {
Expand Down Expand Up @@ -116,7 +115,7 @@ export class NetworkBot {
start(): void {
if (!GlobalConfig.networkBotEnabled) return
this.api.initialize()
// this.metricsRegistry.initialize()
this.metricsRegistry.initialize()
this.log.info('Logging in...')
this.client.login(GlobalConfig.networkBotToken)
}
Expand Down

0 comments on commit 4d9bfaf

Please sign in to comment.