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 Mar 12, 2024
1 parent 9ff0ced commit 5017e39
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/metrics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
{
"frontend": "lib/browser/metrics-frontend-module",
"backend": "lib/node/metrics-backend-module"
},
{
"backendElectron": "lib/electron-node/electron-metrics-backend-module"
}
],
"keywords": [
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});
37 changes: 37 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,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);
}
}
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 5017e39

Please sign in to comment.