Skip to content

Commit

Permalink
Plugin and sample app are both working
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlogic committed Apr 18, 2024
1 parent 6219dd2 commit f3ad4e0
Show file tree
Hide file tree
Showing 13 changed files with 16,399 additions and 442 deletions.
6 changes: 1 addition & 5 deletions express/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { mergeConfig } from 'judoscale-node-core'
import { MetricsStore } from 'judoscale-node-core'
import { Reporter } from 'judoscale-node-core'
import requestMetrics from './lib/request-metrics'
import WebMetricsCollector from './lib/web-metrics-collector'
import { mergeConfig, Reporter, MetricsStore, requestMetrics, WebMetricsCollector } from 'judoscale-node-core'
import Adapter from './lib/adapter'

export default (config) => {
Expand Down
2 changes: 1 addition & 1 deletion fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "judoscale-fastify",
"version": "1.0.0",
"description": "Fastify plugin for the Judoscale autoscaler",
"main": "plugin.js",
"main": "src/plugin.js",
"scripts": {
"test": "jest",
"lint": "npx standard"
Expand Down
22 changes: 22 additions & 0 deletions fastify/src/adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { defaultConfig } = require('judoscale-node-core')

class Adapter {
constructor(collectors) {
this.collectors = collectors
this.identifier = 'judoscale-fastify'
this.adapter_version = defaultConfig.version
this.language_version = process.version
}

asJson() {
const data = {}
data[this.identifier] = {
adapter_version: this.adapter_version,
language_version: this.language_version,
}

return data
}
}

module.exports = Adapter
12 changes: 8 additions & 4 deletions fastify/src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const fp = require('fastify-plugin')
const { MetricsStore } = require('judoscale-node-core')
const { mergeConfig, Reporter, MetricsStore, requestMetrics, WebMetricsCollector } = require('judoscale-node-core')
const Adapter = require('./adapter')

async function judoscaleFastify(fastify, options) {
// Allow injection for testing
const metricsStore = options.metricsStore || new MetricsStore()
const finalConfig = mergeConfig(options)
const collectors = [new WebMetricsCollector(metricsStore)]
const reporter = new Reporter()

reporter.start(finalConfig, metricsStore, collectors, Adapter)

fastify.addHook('onRequest', async (request, _reply) => {
try {
const startTime = Date.now()
const requestStart = parseInt(request.headers['x-request-start']) || startTime
const queueTime = startTime - requestStart
const queueTime = requestMetrics.queueTimeFromHeaders(request.headers, Date.now())

metricsStore.push('qt', queueTime)

Expand Down
15 changes: 14 additions & 1 deletion node-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,24 @@ import Api from './lib/api'
import defaultConfigFunction from './lib/default-config'
import mergeConfig from './lib/merge-config'
import logger from './lib/logger'
import requestMetrics from './lib/request-metrics'
import Metric from './lib/metric'
import MetricsStore from './lib/metrics-store'
import Report from './lib/report'
import Reporter from './lib/reporter'
import WebMetricsCollector from './lib/web-metrics-collector'

const defaultConfig = defaultConfigFunction()

export { Api, mergeConfig, logger, Metric, MetricsStore, Report, Reporter, defaultConfig }
export {
Api,
mergeConfig,
logger,
requestMetrics,
Metric,
MetricsStore,
Report,
Reporter,
WebMetricsCollector,
defaultConfig,
}
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions sample_apps/express_web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ bin/dev
This will run not only the app but also Judoscale proxy so you can check the metrics being collected by it.
http://localhost:5004
Note that the sample app uses the NPM registry version of judoscale-express, but the local version of judoscale-node-core. If you want to use the local version of judoscale-express, change the `resolutions` in `package.json`.
There doesn't seem to be a way to use _both_ local versions of judoscale-express and judoscale-node-core at the same time. When you use the local version of judoscale-express, the transitive dependency judoscale-node-core dependency will always be the register version, even if you include the local override in `resolutions`.
Expand Down
37 changes: 37 additions & 0 deletions sample_apps/fastify_web/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Judoscale Demo (Fastify)

A tiny Fastify app for testing Judoscale.

## Local Run

Ensure the Heroku CLI is installed.

```shell
heroku -v
```

Install dependencies. Note that we're using `yarn` instead of `npm` so we can use "resolutions". More on this below.

```shell
npm install
```

Run the app:

```shell
bin/dev
```

This will run both the app and a proxy server that adds the X-Request-Start header for simulating request queue time.

http://localhost:5004

## Local development

To reference the local version of `judoscale-fastify` instead of the NPM version:

```
npm link judoscale-fastify
```

This will create a symlink for `node_modules/judoscale-fastify` pointing to the local file system.
10 changes: 9 additions & 1 deletion sample_apps/fastify_web/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import Fastify from 'fastify'
import judoscale from 'judoscale-fastify'

const fastify = Fastify({
logger: true,
logger: {
level: process.env.LOG_LEVEL || 'debug',
},
})

fastify.register(judoscale, {
log_level: process.env.LOG_LEVEL || 'debug',
api_base_url: process.env.JUDOSCALE_URL || 'https://judoscale-node-sample.requestcatcher.com',
})

fastify.get('/', function (request, reply) {
Expand Down
Loading

0 comments on commit f3ad4e0

Please sign in to comment.