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(metrics): added collectMetricsExpressMiddleware and deprecated defaultMetricsMiddleware #54

Merged
merged 20 commits into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
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
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [5.1.13](https://github.com/MapColonies/telemetry/compare/v5.1.12...v5.1.13) (2023-08-01)

### [5.1.12](https://github.com/MapColonies/telemetry/compare/v5.1.11...v5.1.12) (2023-08-01)

### [5.1.11](https://github.com/MapColonies/telemetry/compare/v5.1.10...v5.1.11) (2023-08-01)

### [5.1.10](https://github.com/MapColonies/telemetry/compare/v5.1.9...v5.1.10) (2023-08-01)

### [5.1.9](https://github.com/MapColonies/telemetry/compare/v5.1.8...v5.1.9) (2023-07-31)

### [5.1.8](https://github.com/MapColonies/telemetry/compare/v5.1.7...v5.1.8) (2023-07-31)

### [5.1.7](https://github.com/MapColonies/telemetry/compare/v5.1.5...v5.1.7) (2023-07-31)

### [5.1.5](https://github.com/MapColonies/telemetry/compare/v5.1.4...v5.1.5) (2023-07-31)

### [5.1.4](https://github.com/MapColonies/telemetry/compare/v5.1.3...v5.1.4) (2023-07-31)

### [5.1.3](https://github.com/MapColonies/telemetry/compare/v5.1.2...v5.1.3) (2023-07-31)

### [5.1.2](https://github.com/MapColonies/telemetry/compare/v5.1.1...v5.1.2) (2023-07-31)

### [5.1.1](https://github.com/MapColonies/telemetry/compare/v5.1.0...v5.1.1) (2023-07-31)

## [5.1.0](https://github.com/MapColonies/telemetry/compare/v5.0.0...v5.1.0) (2023-07-31)

syncush marked this conversation as resolved.
Show resolved Hide resolved

### Features

* added defaultLabels ([ca565a5](https://github.com/MapColonies/telemetry/commit/ca565a5ba1a83667e3cbd2623e0a4a174347f3e1))
* collectMetricsExpressMiddleware ([8f8001f](https://github.com/MapColonies/telemetry/commit/8f8001fa4cb46dfc5e87667115859b3a78f5c3a1))
* **metrics:** added collectMetricsExpressMiddleware and deprecated defaultMetricsMiddleware ([5604cc3](https://github.com/MapColonies/telemetry/commit/5604cc3350076c0659e72b3251fdda1d184670be))

## [5.0.0](https://github.com/MapColonies/telemetry/compare/v4.2.0...v5.0.0) (2023-07-07)


Expand Down
44 changes: 42 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@map-colonies/telemetry",
"version": "5.0.0",
"version": "5.1.13",
Copy link
Collaborator

Choose a reason for hiding this comment

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

?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

"description": "Package for everything opentelemetry related in map colonies",
"main": "./dist/index.js",
"scripts": {
Expand Down Expand Up @@ -47,6 +47,7 @@
"@opentelemetry/sdk-trace-base": "^1.14.0",
"@opentelemetry/sdk-trace-node": "^1.14.0",
"env-var": "^7.1.1",
"express-prom-bundle": "^6.6.0",
"prom-client": "^14.2.0"
},
"devDependencies": {
Expand Down
3 changes: 1 addition & 2 deletions src/metrics/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { Metrics } from './metrics';
export { defaultMetricsMiddleware } from './middleware/defaultMetrics';
export { metricsMiddleware } from './middleware/metrics';
export { defaultMetricsMiddleware, collectMetricsExpressMiddleware } from './middleware/metrics';
15 changes: 0 additions & 15 deletions src/metrics/middleware/defaultMetrics.ts

This file was deleted.

102 changes: 101 additions & 1 deletion src/metrics/middleware/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { Registry, collectDefaultMetrics } from 'prom-client';
import { hostname as osHostname } from 'os';
import { Registry, collectDefaultMetrics, Gauge, register } from 'prom-client';
import * as promBundle from 'express-prom-bundle';
import * as express from 'express';
import { loadPackageInfo } from '../../common/packageInfoLoader';

const deconstructSemver = (semverString: string): { major: string; minor: string; patch: string; prerelease: string; build: string } | null => {
const match = /^(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([\w-]+))?(?:\+([\w-]+))?/i.exec(semverString);
if (!match) {
return null;
}
return {
major: match[1],
minor: match[2],
patch: match[3],
prerelease: match[4],
build: match[5],
};
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

move to a common util area

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done


interface Opts {
registry: Registry;
collectNodeMetrics: boolean;
collectServiceVersion: boolean;
prefix: string;
labels: Record<string, string>;
}

export function metricsMiddleware(
registry: Registry,
Expand All @@ -19,3 +44,78 @@ export function metricsMiddleware(
}
};
}

/**
* @deprecated since version v5.1, please use collectMetricsExpressMiddleware
*/
export function defaultMetricsMiddleware(prefix?: string, labels?: Record<string, string>): express.RequestHandler {
const register = new Registry();
collectDefaultMetrics({ prefix, register, labels });
return async (req: express.Request, res: express.Response, next: express.NextFunction): Promise<void> => {
try {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
} catch (error) {
return next(error);
}
};
}

export function collectMetricsExpressMiddleware(options: Partial<Opts>): promBundle.Middleware {
const pacakgeInfo = loadPackageInfo();
const defaultOpts: Opts = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe upgrade typescript version and try the satisfy keyword

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

prefix: '',
labels: {},
collectNodeMetrics: true,
collectServiceVersion: true,
registry: new Registry(),
};
const meregedOptions = { ...defaultOpts, ...options };
/* eslint-disable @typescript-eslint/naming-convention */
const mergedLabels = {
hostname: osHostname(),
service_name: pacakgeInfo.name,
...(options.labels ? options.labels : {}),
};
register.setDefaultLabels(mergedLabels);
if (meregedOptions.collectNodeMetrics) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

merged

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

collectDefaultMetrics({ prefix: meregedOptions.prefix, labels: mergedLabels, register: meregedOptions.registry });
}

if (meregedOptions.collectServiceVersion) {
const gaugeLabels = ['service_version_major', 'service_version_minor', 'service_version_patch', 'service_version_prerelease'];
const semver = deconstructSemver(pacakgeInfo.version);
if (!semver) {
throw new Error('package.json includes version not according to semver spec');
}
const { major, minor, patch, prerelease } = semver;
const serviceVersionGauge = new Gauge({
name: `${meregedOptions.prefix ? `${meregedOptions.prefix}_` : ''}service_version`,
help: 'Service Version Specified in package.json file',
labelNames: gaugeLabels,
registers: [meregedOptions.registry],
});
/* eslint-disable @typescript-eslint/naming-convention */
serviceVersionGauge.set(
{
...mergedLabels,
service_version_major: major,
service_version_minor: minor,
service_version_patch: patch,
service_version_prerelease: prerelease,
},
1
);
}

const promBundleConfig: promBundle.Opts = {
promRegistry: meregedOptions.registry,
autoregister: true,
includeUp: true,
customLabels: mergedLabels,
includeMethod: true,
includeStatusCode: true,
includePath: true,
};
return promBundle(promBundleConfig);
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "ES2022",
"allowSyntheticDefaultImports": true,
"strict": true,
"module": "commonjs",
Expand Down