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

electron: allow accessing the metrics endpoint for performance analysis #13380

Merged
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
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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should either be http.IncomingMessage
or the type in allowWsUpgrade should be aligned to avoid the double import from "http".
(import * as http from 'http' and import { IncomingMessage } from 'http'; )

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
Loading