deploy: current vibn theia state
Made-with: Cursor
This commit is contained in:
10
packages/metrics/.eslintrc.js
Normal file
10
packages/metrics/.eslintrc.js
Normal file
@@ -0,0 +1,10 @@
|
||||
/** @type {import('eslint').Linter.Config} */
|
||||
module.exports = {
|
||||
extends: [
|
||||
'../../configs/build.eslintrc.json'
|
||||
],
|
||||
parserOptions: {
|
||||
tsconfigRootDir: __dirname,
|
||||
project: 'tsconfig.json'
|
||||
}
|
||||
};
|
||||
31
packages/metrics/README.md
Normal file
31
packages/metrics/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
<div align='center'>
|
||||
|
||||
<br />
|
||||
|
||||
<img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
|
||||
|
||||
<h2>ECLIPSE THEIA - METRICS EXTENSION</h2>
|
||||
|
||||
<hr />
|
||||
|
||||
</div>
|
||||
|
||||
## Description
|
||||
|
||||
The `@theia/metrics` extension provides metrics using the [Prometheus](https://prometheus.io/) API.
|
||||
|
||||
## Additional Information
|
||||
|
||||
- [API documentation for `@theia/metrics`](https://eclipse-theia.github.io/theia/docs/next/modules/_theia_metrics.html)
|
||||
- [Theia - GitHub](https://github.com/eclipse-theia/theia)
|
||||
- [Theia - Website](https://theia-ide.org/)
|
||||
|
||||
## License
|
||||
|
||||
- [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
|
||||
- [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
|
||||
|
||||
## Trademark
|
||||
|
||||
"Theia" is a trademark of the Eclipse Foundation
|
||||
<https://www.eclipse.org/theia>
|
||||
53
packages/metrics/package.json
Normal file
53
packages/metrics/package.json
Normal file
@@ -0,0 +1,53 @@
|
||||
{
|
||||
"name": "@theia/metrics",
|
||||
"version": "1.68.0",
|
||||
"description": "Theia - Metrics Extension",
|
||||
"dependencies": {
|
||||
"@theia/core": "1.68.0",
|
||||
"prom-client": "^10.2.0",
|
||||
"tslib": "^2.6.2"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"theiaExtensions": [
|
||||
{
|
||||
"frontend": "lib/browser/metrics-frontend-module",
|
||||
"backend": "lib/node/metrics-backend-module"
|
||||
},
|
||||
{
|
||||
"backendElectron": "lib/electron-node/electron-metrics-backend-module"
|
||||
}
|
||||
],
|
||||
"keywords": [
|
||||
"theia-extension"
|
||||
],
|
||||
"license": "EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/eclipse-theia/theia.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/eclipse-theia/theia/issues"
|
||||
},
|
||||
"homepage": "https://github.com/eclipse-theia/theia",
|
||||
"files": [
|
||||
"lib",
|
||||
"src"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "theiaext build",
|
||||
"clean": "theiaext clean",
|
||||
"compile": "theiaext compile",
|
||||
"lint": "theiaext lint",
|
||||
"test": "theiaext test",
|
||||
"watch": "theiaext watch"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@theia/ext-scripts": "1.68.0"
|
||||
},
|
||||
"nyc": {
|
||||
"extends": "../../configs/nyc.json"
|
||||
},
|
||||
"gitHead": "21358137e41342742707f660b8e222f940a27652"
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2023 STMicroelectronics and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
import { inject, injectable } from '@theia/core/shared/inversify';
|
||||
import { FrontendApplicationContribution } from '@theia/core/lib/browser';
|
||||
import { ILogger, LogLevel, MeasurementResult, Stopwatch } from '@theia/core';
|
||||
import { UUID } from '@theia/core/shared/@lumino/coreutils';
|
||||
import { MeasurementNotificationService } from '../common';
|
||||
|
||||
@injectable()
|
||||
export class MetricsFrontendApplicationContribution implements FrontendApplicationContribution {
|
||||
@inject(Stopwatch)
|
||||
protected stopwatch: Stopwatch;
|
||||
|
||||
@inject(MeasurementNotificationService)
|
||||
protected notificationService: MeasurementNotificationService;
|
||||
|
||||
@inject(ILogger)
|
||||
protected logger: ILogger;
|
||||
|
||||
readonly id = UUID.uuid4();
|
||||
|
||||
initialize(): void {
|
||||
this.doInitialize();
|
||||
}
|
||||
|
||||
protected async doInitialize(): Promise<void> {
|
||||
const logLevel = await this.logger.getLogLevel();
|
||||
if (logLevel !== LogLevel.DEBUG) {
|
||||
return;
|
||||
}
|
||||
this.stopwatch.storedMeasurements.forEach(result => this.notify(result));
|
||||
this.stopwatch.onDidAddMeasurementResult(result => this.notify(result));
|
||||
}
|
||||
|
||||
protected notify(result: MeasurementResult): void {
|
||||
this.notificationService.onFrontendMeasurement(this.id, result);
|
||||
}
|
||||
}
|
||||
28
packages/metrics/src/browser/metrics-frontend-module.ts
Normal file
28
packages/metrics/src/browser/metrics-frontend-module.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2023 STMicroelectronics and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { ContainerModule } from '@theia/core/shared/inversify';
|
||||
import { MetricsFrontendApplicationContribution } from './metrics-frontend-application-contribution';
|
||||
import { MeasurementNotificationService, measurementNotificationServicePath } from '../common';
|
||||
import { FrontendApplicationContribution, WebSocketConnectionProvider } from '@theia/core/lib/browser';
|
||||
|
||||
export default new ContainerModule(bind => {
|
||||
bind(FrontendApplicationContribution).to(MetricsFrontendApplicationContribution).inSingletonScope();
|
||||
bind(MeasurementNotificationService).toDynamicValue(ctx => {
|
||||
const connection = ctx.container.get(WebSocketConnectionProvider);
|
||||
return connection.createProxy<MeasurementNotificationService>(measurementNotificationServicePath);
|
||||
}).inSingletonScope();
|
||||
});
|
||||
17
packages/metrics/src/common/index.ts
Normal file
17
packages/metrics/src/common/index.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2023 STMicroelectronics and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
export * from './measurement-notification-service';
|
||||
@@ -0,0 +1,29 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2023 STMicroelectronics and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { MeasurementResult } from '@theia/core';
|
||||
|
||||
export const measurementNotificationServicePath = '/services/measurement-notification';
|
||||
|
||||
export const MeasurementNotificationService = Symbol('MeasurementNotificationService');
|
||||
export interface MeasurementNotificationService {
|
||||
/**
|
||||
* Notify the backend when a fronted stopwatch provides a new measurement.
|
||||
* @param frontendId The unique id associated with the frontend that sends the notification
|
||||
* @param result The new measurement result
|
||||
*/
|
||||
onFrontendMeasurement(frontendId: string, result: MeasurementResult): void;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2023 STMicroelectronics and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { ContainerModule } from '@theia/core/shared/inversify';
|
||||
import { MetricsElectronTokenValidator } from './electron-token-validator';
|
||||
import { ElectronTokenValidator } from '@theia/core/lib/electron-node/token/electron-token-validator';
|
||||
|
||||
export default new ContainerModule((bind, unbind, isBound, rebind) => {
|
||||
bind(MetricsElectronTokenValidator).toSelf().inSingletonScope();
|
||||
rebind(ElectronTokenValidator).to(MetricsElectronTokenValidator);
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2023 STMicroelectronics and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { injectable, postConstruct } from '@theia/core/shared/inversify';
|
||||
import { ElectronTokenValidator } from '@theia/core/lib/electron-node/token/electron-token-validator';
|
||||
import { IncomingMessage } from 'http';
|
||||
import { MetricsBackendApplicationContribution } from '../node/metrics-backend-application-contribution';
|
||||
import { MaybePromise } from '@theia/core';
|
||||
|
||||
@injectable()
|
||||
export class MetricsElectronTokenValidator extends ElectronTokenValidator {
|
||||
@postConstruct()
|
||||
protected override init(): void {
|
||||
super.init();
|
||||
}
|
||||
|
||||
override allowWsUpgrade(request: IncomingMessage): MaybePromise<boolean> {
|
||||
return this.allowRequest(request);
|
||||
}
|
||||
|
||||
override allowRequest(request: IncomingMessage): boolean {
|
||||
return request.url === MetricsBackendApplicationContribution.ENDPOINT || super.allowRequest(request);
|
||||
}
|
||||
}
|
||||
48
packages/metrics/src/node/extensions-metrics-contribution.ts
Normal file
48
packages/metrics/src/node/extensions-metrics-contribution.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2018 Ericsson and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
import { injectable } from '@theia/core/shared/inversify';
|
||||
import { MetricsContribution } from './metrics-contribution';
|
||||
import { PROMETHEUS_REGEXP, toPrometheusValidName } from './prometheus';
|
||||
import { backendGlobal } from '@theia/core/lib/node';
|
||||
|
||||
const metricsName = 'theia_extension_version';
|
||||
|
||||
@injectable()
|
||||
export class ExtensionMetricsContribution implements MetricsContribution {
|
||||
private metrics: string = '';
|
||||
|
||||
getMetrics(): string {
|
||||
return this.metrics;
|
||||
}
|
||||
|
||||
startCollecting(): void {
|
||||
let latestMetrics = '';
|
||||
const installedExtensions = backendGlobal.extensionInfo;
|
||||
latestMetrics += `# HELP ${metricsName} Theia extension version info.\n`;
|
||||
latestMetrics += `# TYPE ${metricsName} gauge\n`;
|
||||
installedExtensions.forEach(extensionInfo => {
|
||||
let extensionName = extensionInfo.name;
|
||||
if (!PROMETHEUS_REGEXP.test(extensionName)) {
|
||||
extensionName = toPrometheusValidName(extensionName);
|
||||
}
|
||||
|
||||
const metricsValue = metricsName + `{extension="${extensionName}",version="${extensionInfo.version}"} 1`;
|
||||
latestMetrics += metricsValue + '\n';
|
||||
});
|
||||
|
||||
this.metrics = latestMetrics;
|
||||
}
|
||||
}
|
||||
21
packages/metrics/src/node/index.ts
Normal file
21
packages/metrics/src/node/index.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2018 Ericsson and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
export * from './metrics-backend-application-contribution';
|
||||
export * from './metrics-contribution';
|
||||
export * from './node-metrics-contribution';
|
||||
export * from './extensions-metrics-contribution';
|
||||
export * from './prometheus';
|
||||
@@ -0,0 +1,75 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2023 STMicroelectronics and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
import { inject, injectable, } from '@theia/core/shared/inversify';
|
||||
import { MetricsContribution } from './metrics-contribution';
|
||||
import { LogLevel, MeasurementResult, Stopwatch } from '@theia/core';
|
||||
import { MeasurementNotificationService } from '../common';
|
||||
import { LogLevelCliContribution } from '@theia/core/lib/node/logger-cli-contribution';
|
||||
|
||||
const backendId = 'backend';
|
||||
const metricsName = 'theia_measurements';
|
||||
|
||||
@injectable()
|
||||
export class MeasurementMetricsBackendContribution implements MetricsContribution, MeasurementNotificationService {
|
||||
@inject(Stopwatch)
|
||||
protected backendStopwatch: Stopwatch;
|
||||
|
||||
@inject(LogLevelCliContribution)
|
||||
protected logLevelCli: LogLevelCliContribution;
|
||||
|
||||
protected metrics = '';
|
||||
protected frontendCounters = new Map<string, string>();
|
||||
|
||||
startCollecting(): void {
|
||||
if (this.logLevelCli.defaultLogLevel !== LogLevel.DEBUG) {
|
||||
return;
|
||||
}
|
||||
this.metrics += `# HELP ${metricsName} Theia stopwatch measurement results.\n`;
|
||||
this.metrics += `# TYPE ${metricsName} gauge\n`;
|
||||
this.backendStopwatch.storedMeasurements.forEach(result => this.onBackendMeasurement(result));
|
||||
this.backendStopwatch.onDidAddMeasurementResult(result => this.onBackendMeasurement(result));
|
||||
}
|
||||
|
||||
getMetrics(): string {
|
||||
return this.metrics;
|
||||
}
|
||||
|
||||
protected appendMetricsValue(id: string, result: MeasurementResult): void {
|
||||
const { name, elapsed, startTime, owner } = result;
|
||||
const labels: string = `id="${id}", name="${name}", startTime="${startTime}", owner="${owner}"`;
|
||||
const metricsValue = `${metricsName}{${labels}} ${elapsed}`;
|
||||
this.metrics += (metricsValue + '\n');
|
||||
}
|
||||
|
||||
protected onBackendMeasurement(result: MeasurementResult): void {
|
||||
this.appendMetricsValue(backendId, result);
|
||||
}
|
||||
|
||||
protected createFrontendCounterId(frontendId: string): string {
|
||||
const counterId = `frontend-${this.frontendCounters.size + 1}`;
|
||||
this.frontendCounters.set(frontendId, counterId);
|
||||
return counterId;
|
||||
}
|
||||
|
||||
protected toCounterId(frontendId: string): string {
|
||||
return this.frontendCounters.get(frontendId) ?? this.createFrontendCounterId(frontendId);
|
||||
}
|
||||
|
||||
onFrontendMeasurement(frontendId: string, result: MeasurementResult): void {
|
||||
this.appendMetricsValue(this.toCounterId(frontendId), result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2017-2018 Ericsson and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { injectable, inject, named } from '@theia/core/shared/inversify';
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import * as express from '@theia/core/shared/express';
|
||||
import { ContributionProvider } from '@theia/core/lib/common';
|
||||
import { BackendApplicationContribution } from '@theia/core/lib/node';
|
||||
import { MetricsContribution } from './metrics-contribution';
|
||||
|
||||
@injectable()
|
||||
export class MetricsBackendApplicationContribution implements BackendApplicationContribution {
|
||||
static ENDPOINT = '/metrics';
|
||||
constructor(
|
||||
@inject(ContributionProvider) @named(MetricsContribution)
|
||||
protected readonly metricsProviders: ContributionProvider<MetricsContribution>
|
||||
) {
|
||||
}
|
||||
|
||||
configure(app: express.Application): void {
|
||||
app.get(MetricsBackendApplicationContribution.ENDPOINT, (req, res) => {
|
||||
const lastMetrics = this.fetchMetricsFromProviders();
|
||||
res.send(lastMetrics);
|
||||
});
|
||||
}
|
||||
|
||||
onStart(server: http.Server | https.Server): void {
|
||||
this.metricsProviders.getContributions().forEach(contribution => {
|
||||
contribution.startCollecting();
|
||||
});
|
||||
}
|
||||
|
||||
fetchMetricsFromProviders(): string {
|
||||
return this.metricsProviders.getContributions().reduce((total, contribution) =>
|
||||
total += contribution.getMetrics() + '\n', '');
|
||||
}
|
||||
}
|
||||
40
packages/metrics/src/node/metrics-backend-module.ts
Normal file
40
packages/metrics/src/node/metrics-backend-module.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2017-2018 Ericsson and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import { ContainerModule } from '@theia/core/shared/inversify';
|
||||
import { BackendApplicationContribution } from '@theia/core/lib/node';
|
||||
import { ConnectionHandler, RpcConnectionHandler, bindContributionProvider } from '@theia/core/lib/common';
|
||||
import { MetricsContribution } from './metrics-contribution';
|
||||
import { NodeMetricsContribution } from './node-metrics-contribution';
|
||||
import { ExtensionMetricsContribution } from './extensions-metrics-contribution';
|
||||
import { MetricsBackendApplicationContribution } from './metrics-backend-application-contribution';
|
||||
import { measurementNotificationServicePath } from '../common';
|
||||
import { MeasurementMetricsBackendContribution } from './measurement-metrics-contribution';
|
||||
|
||||
export default new ContainerModule(bind => {
|
||||
bindContributionProvider(bind, MetricsContribution);
|
||||
bind(MetricsContribution).to(NodeMetricsContribution).inSingletonScope();
|
||||
bind(MetricsContribution).to(ExtensionMetricsContribution).inSingletonScope();
|
||||
|
||||
bind(MeasurementMetricsBackendContribution).toSelf().inSingletonScope();
|
||||
bind(MetricsContribution).toService(MeasurementMetricsBackendContribution);
|
||||
bind(ConnectionHandler).toDynamicValue(ctx =>
|
||||
new RpcConnectionHandler(measurementNotificationServicePath,
|
||||
() => ctx.container.get(MeasurementMetricsBackendContribution)));
|
||||
|
||||
bind(BackendApplicationContribution).to(MetricsBackendApplicationContribution).inSingletonScope();
|
||||
|
||||
});
|
||||
20
packages/metrics/src/node/metrics-contribution.ts
Normal file
20
packages/metrics/src/node/metrics-contribution.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2018 Ericsson and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
export const MetricsContribution = Symbol('MetricsContribution');
|
||||
export interface MetricsContribution {
|
||||
startCollecting(): void;
|
||||
getMetrics(): string;
|
||||
}
|
||||
33
packages/metrics/src/node/node-metrics-contribution.ts
Normal file
33
packages/metrics/src/node/node-metrics-contribution.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2018 Ericsson and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import * as prom from 'prom-client';
|
||||
import { injectable } from '@theia/core/shared/inversify';
|
||||
import { MetricsContribution } from './metrics-contribution';
|
||||
|
||||
@injectable()
|
||||
export class NodeMetricsContribution implements MetricsContribution {
|
||||
getMetrics(): string {
|
||||
return prom.register.metrics().toString();
|
||||
}
|
||||
|
||||
startCollecting(): void {
|
||||
const collectDefaultMetrics = prom.collectDefaultMetrics;
|
||||
|
||||
// Probe every 5th second.
|
||||
collectDefaultMetrics({ timeout: 5000 });
|
||||
}
|
||||
}
|
||||
49
packages/metrics/src/node/prometheus.spec.ts
Normal file
49
packages/metrics/src/node/prometheus.spec.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2018 Ericsson and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
import * as chai from 'chai';
|
||||
import { PROMETHEUS_REGEXP, toPrometheusValidName } from './prometheus';
|
||||
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('Prometheus helper module', () => {
|
||||
/* According to https://prometheus.io/docs/concepts/data_model/ */
|
||||
|
||||
const validName = 'theia_extension3242-:';
|
||||
const invalidTheiaName = '@theia/something';
|
||||
const validTheiaName = 'theia_something';
|
||||
const invalidName2 = '@theia/?$%^ ';
|
||||
|
||||
it('Should correctly validate a metric name', () => {
|
||||
expect(PROMETHEUS_REGEXP.test(validName)).to.be.true;
|
||||
|
||||
expect(PROMETHEUS_REGEXP.test(invalidTheiaName)).to.be.false;
|
||||
|
||||
expect(PROMETHEUS_REGEXP.test(invalidName2)).to.be.false;
|
||||
|
||||
});
|
||||
|
||||
it('Should correctly return a valid name from an otherwise invalid prometheus string', () => {
|
||||
expect(PROMETHEUS_REGEXP.test(invalidTheiaName)).to.be.false;
|
||||
|
||||
const newName = toPrometheusValidName(invalidTheiaName);
|
||||
expect(PROMETHEUS_REGEXP.test(newName)).to.be.true;
|
||||
expect(newName).to.be.equal(validTheiaName);
|
||||
|
||||
const newName2 = toPrometheusValidName(invalidName2);
|
||||
expect(PROMETHEUS_REGEXP.test(newName2)).to.be.true;
|
||||
});
|
||||
});
|
||||
25
packages/metrics/src/node/prometheus.ts
Normal file
25
packages/metrics/src/node/prometheus.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
// *****************************************************************************
|
||||
// Copyright (C) 2018 Ericsson and others.
|
||||
//
|
||||
// This program and the accompanying materials are made available under the
|
||||
// terms of the Eclipse Public License v. 2.0 which is available at
|
||||
// http://www.eclipse.org/legal/epl-2.0.
|
||||
//
|
||||
// This Source Code may also be made available under the following Secondary
|
||||
// Licenses when the conditions for such availability set forth in the Eclipse
|
||||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
||||
// with the GNU Classpath Exception which is available at
|
||||
// https://www.gnu.org/software/classpath/license.html.
|
||||
//
|
||||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
||||
// *****************************************************************************
|
||||
|
||||
export function toPrometheusValidName(name: string): string {
|
||||
/* Make sure that start of name is valid and respect [a-zA-Z_:] */
|
||||
const validFirstCharString = name.replace(/(^[^a-zA-Z_:]+)/gi, '');
|
||||
/* Make sure that rest of the name respect [a-zA-Z0-9_:]* */
|
||||
const validPrometheusName = validFirstCharString.replace(/([^a-zA-Z0-9_:])/gi, '_');
|
||||
return validPrometheusName;
|
||||
}
|
||||
|
||||
export const PROMETHEUS_REGEXP = /^[a-zA-Z_:][a-zA-Z0-9_:]*/;
|
||||
16
packages/metrics/tsconfig.json
Normal file
16
packages/metrics/tsconfig.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"extends": "../../configs/base.tsconfig",
|
||||
"compilerOptions": {
|
||||
"composite": true,
|
||||
"rootDir": "src",
|
||||
"outDir": "lib"
|
||||
},
|
||||
"include": [
|
||||
"src"
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"path": "../core"
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user