Skip to content

Commit

Permalink
electron: allow accessing the metrics endpoint for performance analysis
Browse files Browse the repository at this point in the history
By default, when running Theia in Electron, all endpoints are protected
by the ElectronTokenValidator.
This patch allows accessing the '/metrics' endpoint without a token,
which enables us to collect metrics for performance analysis.

For this, ElectronTokenValidator is extended to allow access to the
metrics endpoint. All other endpoints are still protected.

Contributed on behalf of STMicroelectronics

Signed-off-by: Olaf Lessenich <[email protected]>
  • Loading branch information
xai committed Feb 12, 2024
1 parent 4b8ec2d commit f32aa41
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"theiaExtensions": [
{
"frontend": "lib/browser/metrics-frontend-module",
"backendElectron": "lib/electron-node/electron-metrics-backend-module",
"backend": "lib/node/metrics-backend-module"
}
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// *****************************************************************************
// 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';
import { BackendApplicationContribution } from '@theia/core/lib/node';
import { ExtensionMetricsContribution, MetricsBackendApplicationContribution, MetricsContribution, NodeMetricsContribution } from '../node';
import { ConnectionHandler, RpcConnectionHandler, bindContributionProvider } from '@theia/core';
import { measurementNotificationServicePath } from '../common';
import { MeasurementMetricsBackendContribution } from '../node/measurement-metrics-contribution';


export default new ContainerModule((bind, unbind, isBound, rebind) => {
bind(MetricsElectronTokenValidator).toSelf().inSingletonScope();
rebind(ElectronTokenValidator).to(MetricsElectronTokenValidator);

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();
});
38 changes: 38 additions & 0 deletions packages/metrics/src/electron-node/electron-token-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// *****************************************************************************
// 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 * as http from 'http';
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: http.IncomingMessage): MaybePromise<boolean> {
return this.allowRequest(request);
}

override allowRequest(request: IncomingMessage): boolean {
return request.url === MetricsBackendApplicationContribution.ENDPOINT || super.allowRequest(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ 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('/metrics', (req, res) => {
app.get(MetricsBackendApplicationContribution.ENDPOINT, (req, res) => {
const lastMetrics = this.fetchMetricsFromProviders();
res.send(lastMetrics);
});
Expand Down

0 comments on commit f32aa41

Please sign in to comment.