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: Export last check and last update to prometheus #188

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ jobs:
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'yarn'
- run: yarn install --frozen-lockfile --ignore-scripts
- run: yarn fmt:check
- run: yarn lint
- run: yarn fmt:ci
- run: yarn build
- run: yarn test --coverage
4 changes: 2 additions & 2 deletions .github/workflows/release_changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Build changelog
id: github_release
uses: metcalfc/changelog-generator@v0.4.4
uses: metcalfc/changelog-generator@v4.3.1
with:
myToken: ${{ secrets.GITHUB_TOKEN }}
- name: Create release
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v14.19
16
82 changes: 82 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"correctness": {
"noUnsafeOptionalChaining": "off",
"useExhaustiveDependencies": "off",
"noUnusedImports": "warn",
"useJsxKeyInIterable": "off"
},
"complexity": {
"noBannedTypes": "off",
"noUselessConstructor": "off",
"useOptionalChain": "warn",
"noStaticOnlyClass": "off",
"noForEach": "off"
},
"style": {
"noNonNullAssertion": "off",
"noInferrableTypes": "off",
"noUnusedTemplateLiteral": "off",
"useSingleVarDeclarator": "off",
"noUselessElse": "off",
"useNodejsImportProtocol": "off"
},
"suspicious": {
"noExplicitAny": "off",
"noExtraNonNullAssertion": "off",
"noRedeclare": "off",
"noPrototypeBuiltins": "off",
"noConfusingVoidType": "off",
"noArrayIndexKey": "off",
"noThenProperty": "off",
"noExportsInTest": "off"
},
"performance": {
"noAccumulatingSpread": "off",
"noDelete": "off"
}
},
"ignore": [
"node_modules",
"docker",
"setupJest.js",
"dist",
"build",
".docusaurus",
"CHANGELOG.md",
"./package.json",
"tsconfig.json"
]
},
"organizeImports": {
"enabled": false
},
"formatter": {
"indentStyle": "space",
"ignore": [
"node_modules",
"dist",
"build",
"./package.json",
"tsconfig.json"
],
"indentWidth": 4
},
"javascript": {
"formatter": {
"semicolons": "always",
"quoteStyle": "single",
"jsxQuoteStyle": "single",
"indentWidth": 4
}
},
"json": {
"formatter": {
"indentWidth": 2
}
}
}
70 changes: 24 additions & 46 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"scripts": {
"build": "tsc --pretty",
"example": "npm run build && node example",
"lint": "eslint ./src",
"lint": "biome lint ./src --write",
"test": "jest",
"updateSnapshots": "jest -u",
"prepare": "npm run build",
"dev": "ENABLE_OAS=true ts-node-dev src/start.ts",
"fmt": "prettier src --write --loglevel warn",
"fmt:check": "prettier src --check"
"fmt": "biome format ./src --write",
"fmt:ci": "biome ci ./src"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -41,59 +41,37 @@
"@unleash/express-openapi": "^0.3.0",
"compression": "^1.7.4",
"cors": "^2.8.5",
"express": "^4.18.2",
"json-schema-to-ts": "^2.3.0",
"openapi-types": "^11.0.0",
"qs": "^6.9.7",
"express": "^4.19.2",
"json-schema-to-ts": "^3.1.0",
"openapi-types": "^12.1.3",
"prom-client": "^15.1.3",
"qs": "^6.12.3",
"type-is": "^1.6.18",
"unleash-client": "^6.1.1"
},
"devDependencies": {
"@apidevtools/swagger-parser": "10.1.0",
"@babel/core": "^7.24.4",
"@types/compression": "^1.7.2",
"@types/cors": "^2.8.12",
"@babel/core": "^7.25.2",
"@biomejs/biome": "^1.8.3",
"@types/compression": "^1.7.5",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.13",
"@types/jest": "^27.5.0",
"@types/jest": "^29.5.12",
"@types/node": "18.7.14",
"@types/supertest": "^2.0.12",
"@types/type-is": "^1.6.3",
"@typescript-eslint/eslint-plugin": "^5.22.0",
"@typescript-eslint/parser": "^5.22.0",
"babel-jest": "^28.0.3",
"eslint": "^8.14.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"jest": "^28.0.3",
"prettier": "^2.6.2",
"semver": "^7.3.7",
"supertest": "^6.2.3",
"ts-jest": "^28.0.1",
"ts-node-dev": "^1.1.8",
"typescript": "^4.6.4"
},
"prettier": {
"proseWrap": "never",
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "all",
"overrides": [
{
"files": "*.{json,yaml,yml,md}",
"options": {
"tabWidth": 2
}
}
]
"@types/supertest": "^6.0.2",
"@types/type-is": "^1.6.6",
"babel-jest": "^28.1.3",
"jest": "^29.7.0",
"semver": "^7.6.3",
"supertest": "^7.0.0",
"ts-jest": "^29.2.4",
"ts-node-dev": "^2.0.0",
"typescript": "^5.5.4"
},
"resolutions": {
"wrap-ansi": "7.0.0",
"string-width": "4.1.0",
"qs": "^6.9.7",
"json5": "^2.2.2",
"cookiejar": "^2.1.4"
}
},
"packageManager": "yarn@1.22.22+sha1.ac34549e6aa8e7ead463a7407e1c7390f61a6610"
}
7 changes: 4 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import compression from 'compression';
import express, { Application } from 'express';
import express, { type Application } from 'express';
import cors from 'cors';
import { IClient } from './client';
import { createProxyConfig, IProxyOption } from './config';
import type { IClient } from './client';
import { createProxyConfig, type IProxyOption } from './config';

import UnleashProxy from './unleash-proxy';
import { OpenApiService } from './openapi/openapi-service';
Expand Down Expand Up @@ -48,6 +48,7 @@ export function createApp(
const corsOptions = config.cors;
app.use(cors(corsOptions));

// @ts-expect-error - Seems to be an incompatibility with express's own middleware here
app.use(compression());

app.use(
Expand Down
20 changes: 15 additions & 5 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import EventEmitter from 'events';
import { Context, Unleash, Variant } from 'unleash-client';
import { FeatureInterface } from 'unleash-client/lib/feature';
import Metrics from 'unleash-client/lib/metrics';
import { Context, Unleash, UnleashEvents, Variant } from "unleash-client";
import type { FeatureInterface } from 'unleash-client/lib/feature';
import type Metrics from 'unleash-client/lib/metrics';
import { getDefaultVariant } from 'unleash-client/lib/variant';
import { IProxyConfig } from './config';
import { Logger } from './logger';
import type { IProxyConfig } from './config';
import type { Logger } from './logger';
import { lastMetricsFetch, lastMetricsUpdate } from "./prometheus";

export type FeatureToggleStatus = {
name: string;
Expand Down Expand Up @@ -72,11 +73,20 @@ class Client extends EventEmitter implements IClient {

this.metrics.on('error', (msg) => this.logger.error(`metrics: ${msg}`));
this.unleash.on('error', (msg) => this.logger.error(msg));

this.unleash.on('ready', () => {
this.emit('ready');
this.ready = true;
this.metrics.start();
});
this.unleash.on(UnleashEvents.Unchanged, () => {
lastMetricsFetch.set(new Date().getTime());
});
this.unleash.on(UnleashEvents.Changed, () => {
const updated = new Date().getTime();
lastMetricsFetch.set(updated);
lastMetricsUpdate.set(updated);
});
}
Comment on lines +87 to 95
Copy link
Member Author

Choose a reason for hiding this comment

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

Other important behavior change, adds an event listener to Unchanged updating the fetch counter, and to Changed events updating both last fetch and last update.


setUnleashApiToken(unleashApiToken: string): void {
Expand Down
22 changes: 13 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { CorsOptions } from 'cors';
import { Application } from 'express';
import { ClientFeaturesResponse, Strategy, TagFilter } from 'unleash-client';
import { BootstrapOptions } from 'unleash-client/lib/repository/bootstrap-provider';
import { Logger, LogLevel, SimpleLogger } from './logger';
import type { CorsOptions } from 'cors';
import type { Application } from 'express';
import type {
ClientFeaturesResponse,
Strategy,
TagFilter,
} from 'unleash-client';
import type { BootstrapOptions } from 'unleash-client/lib/repository/bootstrap-provider';
import { type Logger, type LogLevel, SimpleLogger } from './logger';
import { generateInstanceId } from './util';
import { HttpOptions } from 'unleash-client/lib/http-options';
import { StorageProvider } from 'unleash-client/lib/repository/storage-provider';
import { ContextEnricher } from './enrich-context';
import type { HttpOptions } from 'unleash-client/lib/http-options';
import type { StorageProvider } from 'unleash-client/lib/repository/storage-provider';
import type { ContextEnricher } from './enrich-context';

export interface ServerSideSdkConfig {
tokens: string[];
Expand Down Expand Up @@ -310,7 +314,7 @@ export function createProxyConfig(option: IProxyOption): IProxyConfig {
process.env.UNLEASH_INSTANCE_ID ||
generateInstanceId();

let proxyBasePath = sanitizeBasePath(
const proxyBasePath = sanitizeBasePath(
option.proxyBasePath || process.env.PROXY_BASE_PATH,
);
return {
Expand Down
2 changes: 1 addition & 1 deletion src/content-type-checker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RequestHandler } from 'express';
import type { RequestHandler } from 'express';
import { hasBody, is } from 'type-is';

const DEFAULT_ACCEPTED_CONTENT_TYPE = 'application/json';
Expand Down
4 changes: 2 additions & 2 deletions src/context-middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextFunction, Request, Response } from 'express';
import type { NextFunction, Request, Response } from 'express';
import { createContext } from './create-context';
import { ContextEnricher, enrichContext } from './enrich-context';
import { type ContextEnricher, enrichContext } from './enrich-context';

const POST = 'POST';
const GET = 'GET';
Expand Down
10 changes: 5 additions & 5 deletions src/create-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IProxyConfig } from './config';
import type { IProxyConfig } from './config';
import Client from './client';
import { initialize, Unleash } from 'unleash-client';
import Metrics from 'unleash-client/lib/metrics';
Expand All @@ -23,7 +23,7 @@ export const createSingletonClient = (config: IProxyConfig): Client => {
customHeadersFunction,
bootstrap: config.bootstrap,
storageProvider: config.storageProvider,
...(!!config.httpOptions ? { httpOptions: config.httpOptions } : {}),
...(config.httpOptions ? { httpOptions: config.httpOptions } : {}),
});

const metrics = new Metrics({
Expand All @@ -35,7 +35,7 @@ export const createSingletonClient = (config: IProxyConfig): Client => {
metricsJitter: config.metricsJitter,
url: config.unleashUrl,
customHeadersFunction,
...(!!config.httpOptions ? { httpOptions: config.httpOptions } : {}),
...(config.httpOptions ? { httpOptions: config.httpOptions } : {}),
});

return new Client(config, unleash, metrics);
Expand All @@ -60,7 +60,7 @@ export const createNewClient = (config: IProxyConfig): Client => {
customHeadersFunction,
bootstrap: config.bootstrap,
storageProvider: config.storageProvider,
...(!!config.httpOptions ? { httpOptions: config.httpOptions } : {}),
...(config.httpOptions ? { httpOptions: config.httpOptions } : {}),
});

const metrics = new Metrics({
Expand All @@ -72,7 +72,7 @@ export const createNewClient = (config: IProxyConfig): Client => {
metricsJitter: config.metricsJitter,
url: config.unleashUrl,
customHeadersFunction,
...(!!config.httpOptions ? { httpOptions: config.httpOptions } : {}),
...(config.httpOptions ? { httpOptions: config.httpOptions } : {}),
});

return new Client(config, unleash, metrics);
Expand Down
4 changes: 2 additions & 2 deletions src/create-context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* eslint-disable prefer-object-spread */
import { Context } from 'unleash-client';
import type { Context } from 'unleash-client';

function tryParseDate(dateString: string | undefined): Date | undefined {
if (!dateString) {
return undefined;
}
const parsedDate = new Date(dateString);
if (!isNaN(parsedDate.getTime())) {
if (!Number.isNaN(parsedDate.getTime())) {
return parsedDate;
} else {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion src/enrich-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context } from 'unleash-client';
import type { Context } from 'unleash-client';

export type ContextEnricher = (context: Context) => Promise<Context>;

Expand Down
2 changes: 1 addition & 1 deletion src/openapi/common-responses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3 } from 'openapi-types';

export const format500ErrorMessage = (errorMessage: string): string =>
`Whoops! We dropped the ball on this one (an unexpected error occurred): ${errorMessage}`;
Expand Down
2 changes: 1 addition & 1 deletion src/openapi/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OpenAPIV3 } from 'openapi-types';
import type { OpenAPIV3 } from 'openapi-types';
import { apiRequestSchema } from './spec/api-request-schema';
import { featureSchema } from './spec/feature-schema';
import { featuresSchema } from './spec/features-schema';
Expand Down
Loading
Loading