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: remove http-server dependency and http related functionality #232

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ test:
ci: | build test

update-interfaces-next:
npm install @well-known-components/interfaces@next
yarn add @well-known-components/interfaces@next

update-interfaces-latest:
npm install @well-known-components/interfaces@latest
yarn add @well-known-components/interfaces@latest

.PHONY: build test
.PHONY: build test
13 changes: 0 additions & 13 deletions etc/metrics.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import { IBaseComponent } from '@well-known-components/interfaces';
import { IConfigComponent } from '@well-known-components/interfaces';
import { IHttpServerComponent } from '@well-known-components/interfaces';
import { IMetricsComponent } from '@well-known-components/interfaces';
import { Registry } from 'prom-client';

Expand All @@ -26,18 +25,6 @@ export function createTestMetricsComponent<K extends string>(metricsDefinition:
registry: Registry;
};

// Warning: (ae-forgotten-export) The symbol "HttpMetrics" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export function getDefaultHttpMetrics(): IMetricsComponent.MetricsRecordDefinition<HttpMetrics>;

// @public
export function instrumentHttpServerWithMetrics<K extends string>(components: {
metrics: IMetricsComponent<K | HttpMetrics>;
server: IHttpServerComponent<any>;
config: IConfigComponent;
}): Promise<void>;

// @public
export function validateMetricsDeclaration<T extends string>(metricsDefinition: IMetricsComponent.MetricsRecordDefinition<T>): IMetricsComponent.MetricsRecordDefinition<T>;

Expand Down
6 changes: 2 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module.exports = {
globals: {
'ts-jest': {
tsconfig: 'test/tsconfig.json'
}
transform: {
"^.+\\.(ts|tsx)$": ["ts-jest", {tsconfig: "test/tsconfig.json"}]
},
moduleFileExtensions: ['ts', 'js'],
transform: {
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"description": "metrics component",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {},
"scripts": {
"build": "tsc -p tsconfig.json",
"test": "jest --forceExit --detectOpenHandles --coverage --verbose"
},
"repository": {
"type": "git",
"url": "git+https://github.com/well-known-components/metrics.git"
Expand All @@ -17,19 +20,22 @@
},
"prettier": {
"printWidth": 120,
"semi": false
"semi": false,
"singleQuote": true,
"trailingComma": "none",
"tabWidth": 2
},
"homepage": "https://github.com/well-known-components/metrics#readme",
"devDependencies": {
"@microsoft/api-extractor": "^7.18.5",
"@well-known-components/env-config-provider": "^1.1.0",
"@well-known-components/http-server": "^2.0.0",
"@well-known-components/interfaces": "^1.1.1",
"@well-known-components/interfaces": "^1.4.3",
"@well-known-components/logger": "^3.1.2",
"@well-known-components/test-helpers": "^1.5.1",
"typescript": "^5.1.3"
},
"dependencies": {
"@well-known-components/http-server": "^2.0.1-20240313124908.commit-80c33da",
"prom-client": "^14.1.0"
},
"files": [
Expand Down
113 changes: 0 additions & 113 deletions src/http.ts

This file was deleted.

45 changes: 8 additions & 37 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import {
IBaseComponent,
IConfigComponent,
IHttpServerComponent,
IMetricsComponent,
} from "@well-known-components/interfaces"
import { IBaseComponent, IConfigComponent, IMetricsComponent } from '@well-known-components/interfaces'

import { collectDefaultMetrics } from "prom-client"
import { HttpMetrics, instrumentHttpServerWithPromClientRegistry, getDefaultHttpMetrics } from "./http"
import { createTestMetricsComponent } from "./base"
import { collectDefaultMetrics } from 'prom-client'
import { createTestMetricsComponent } from './base'

export { createTestMetricsComponent, getDefaultHttpMetrics }
export { createTestMetricsComponent }

/**
* Metrics configuration prefix.
* @public
*/
export const CONFIG_PREFIX = "WKC_METRICS" as const
export const CONFIG_PREFIX = 'WKC_METRICS' as const

/**
* @internal
*/
export function _configKey(key: Uppercase<string>): string {
return `${CONFIG_PREFIX}_${key.toUpperCase().replace(/^(_*)/, "")}`
return `${CONFIG_PREFIX}_${key.toUpperCase().replace(/^(_*)/, '')}`
}

/**
Expand All @@ -40,36 +34,13 @@ export async function createMetricsComponent<K extends string, V extends object
...basePort,
// IBaseComponent
start: async () => {
if ((await config.getString(_configKey("COLLECT_DEFAULT"))) != "false") {
if ((await config.getString(_configKey('COLLECT_DEFAULT'))) != 'false') {
collectDefaultMetrics({ register: basePort.registry })
}
},
}
}
}

/**
* Instruments an HTTP server with a IMetricsComponent created by this library
*
* @public
*/
export async function instrumentHttpServerWithMetrics<K extends string>(components: {
metrics: IMetricsComponent<K | HttpMetrics>
server: IHttpServerComponent<any>
config: IConfigComponent
}) {
const metricsPath = (await components.config.getString(_configKey("PUBLIC_PATH"))) || "/metrics"
const bearerToken = await components.config.getString(_configKey("BEARER_TOKEN"))
const rotateMetrics = await components.config.getString(_configKey("RESET_AT_NIGHT")) == 'true'

instrumentHttpServerWithPromClientRegistry<K>({
server: components.server,
metrics: components.metrics,
metricsPath,
bearerToken,
resetEveryNight: rotateMetrics
})
}

/**
* This function only validates the types.
* In the future it may perform real runtime assertions.
Expand Down
31 changes: 16 additions & 15 deletions test/harness/defaultMetrics.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import { IMetricsComponent } from "@well-known-components/interfaces"
import { validateMetricsDeclaration, getDefaultHttpMetrics } from "../../src"
import { metricDeclarations as logMetricDeclarations } from "@well-known-components/logger"
import { IMetricsComponent } from '@well-known-components/interfaces'
import { validateMetricsDeclaration } from '../../src'
import { metricDeclarations as logMetricDeclarations } from '@well-known-components/logger'
import { getDefaultHttpMetrics } from '@well-known-components/http-server'

export const metricDeclarations = {
...logMetricDeclarations,
...getDefaultHttpMetrics(),
test_counter: {
type: IMetricsComponent.CounterType,
help: "Count calls to ping",
labelNames: ["pathname"],
help: 'Count calls to ping',
labelNames: ['pathname']
},
test_gauge: {
type: IMetricsComponent.GaugeType,
help: "Gauges calls to ping (?)",
labelNames: ["pathname"],
help: 'Gauges calls to ping (?)',
labelNames: ['pathname']
},
test_histogram: {
type: IMetricsComponent.HistogramType,
help: "Histograms calls to ping (?)",
labelNames: ["pathname"],
help: 'Histograms calls to ping (?)',
labelNames: ['pathname']
},
test_summary: {
type: IMetricsComponent.SummaryType,
help: "Summaries calls to ping (?)",
labelNames: ["pathname"],
help: 'Summaries calls to ping (?)',
labelNames: ['pathname']
},
user_counter: {
type: IMetricsComponent.CounterType,
help: "Count calls to /user/:userId",
labelNames: ["userId"],
},
...getDefaultHttpMetrics()
help: 'Count calls to /user/:userId',
labelNames: ['userId']
}
}

// type assertions
Expand Down
34 changes: 17 additions & 17 deletions test/harness/test-e2e-express-server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { createConfigComponent } from "@well-known-components/env-config-provider"
import { createServerComponent, IFetchComponent } from "@well-known-components/http-server"
import { createLogComponent } from "@well-known-components/logger"
import { createRunner } from "@well-known-components/test-helpers"
import nodeFetch from "node-fetch"
import { createMetricsComponent, instrumentHttpServerWithMetrics } from "../../src"
import { metricDeclarations } from "./defaultMetrics"
import { mockedRouter } from "./mockedServer"
import { TestComponents } from "./test-helpers"
import { createConfigComponent } from '@well-known-components/env-config-provider'
import { createServerComponent, instrumentHttpServerWithPromClientRegistry } from '@well-known-components/http-server'
import { createLogComponent } from '@well-known-components/logger'
import { createRunner } from '@well-known-components/test-helpers'
import nodeFetch from 'node-fetch'
import { createMetricsComponent } from '../../src'
import { metricDeclarations } from './defaultMetrics'
import { mockedRouter } from './mockedServer'
import { TestComponents } from './test-helpers'
import { IFetchComponent } from '@well-known-components/interfaces'

let currentPort = 19000

Expand All @@ -17,15 +18,14 @@ export const describeE2E = createRunner({
components.server.setContext({ components })
await startComponents()
},
initComponents,
initComponents
})

async function initComponents(): Promise<TestComponents> {

const config = createConfigComponent(
{
HTTP_SERVER_PORT: (currentPort + 1).toString(),
HTTP_SERVER_HOST: "0.0.0.0",
HTTP_SERVER_HOST: '0.0.0.0'
},
process.env
)
Expand All @@ -34,18 +34,18 @@ async function initComponents(): Promise<TestComponents> {
const logs = await createLogComponent({ metrics })

const protocolHostAndProtocol = `http://${await config.requireString(
"HTTP_SERVER_HOST"
)}:${await config.requireNumber("HTTP_SERVER_PORT")}`
'HTTP_SERVER_HOST'
)}:${await config.requireNumber('HTTP_SERVER_PORT')}`

const server = await createServerComponent<any>({ logs, config }, {})

const fetch: IFetchComponent = {
async fetch(url, initRequest?) {
return nodeFetch(protocolHostAndProtocol + url, { ...initRequest })
},
return nodeFetch(protocolHostAndProtocol + url, initRequest ? initRequest : {})
}
}

await instrumentHttpServerWithMetrics({ metrics, server, config })
await instrumentHttpServerWithPromClientRegistry({ metrics, server, config, registry: metrics.registry! })

return { logs, config, server, fetch, metrics }
}
Loading
Loading