Skip to content

Commit

Permalink
Allow the trace server URL to be configured
Browse files Browse the repository at this point in the history
Fixes #79

The trace server URL can be set when starting the server, using the
TRACE_SERVER_URL environment variable. So running

TRACE_SERVER_URL=http://my.trace.server:port/tsp/api yarn start:browser
will start the browser app with the trace server pointing to the URL
variable.

Signed-off-by: Geneviève Bastien <gbastien@versatic.net>
  • Loading branch information
tahini committed Nov 6, 2020
1 parent 2ff935b commit 74e1c45
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 10 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@ or

And use the Electron application.

### Change the trace server URL

By default, the trace server is expected to be on responding at `http://localhost:8080/tsp/api`. If it is not the case, the URL can be changed using the `TRACE_SERVER_URL` environment variable when running the app.

For example, to start the browser example app with a specific URL, one can run

```bash
$ TRACE_SERVER_URL=https://my.trace.server:port/tsp/api yarn start:browser
```

## Package the Example Theia Trace Viewer Application

It's possible to package the repo's example application with `electron-builder`. After running `yarn` in the repo root, do:
Expand Down
6 changes: 5 additions & 1 deletion viewer-prototype/src/browser/trace-server-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,20 @@ import { StatusBar, StatusBarAlignment } from '@theia/core/lib/browser/status-ba
import { Disposable, DisposableCollection } from '@theia/core/lib//common';
import { ConnectionStatusService, ConnectionStatus, AbstractConnectionStatusService } from '@theia/core/lib/browser/connection-status-service';
import { TspClient } from 'tsp-typescript-client/lib/protocol/tsp-client';
import { TspClientProvider } from './tsp-client-provider';

@injectable()
export class TraceServerConnectionStatusService extends AbstractConnectionStatusService {

private scheduledPing: number | undefined;
private tspClient: TspClient;

private constructor(
@inject(TspClient) private tspClient: TspClient
@inject(TspClientProvider) private tspClientProvider: TspClientProvider
) {
super();
this.tspClient = this.tspClientProvider.getTspClient();
this.tspClientProvider.addTspClientChangeListener(tspClient => this.tspClient = tspClient);
}

@postConstruct()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { inject, injectable } from 'inversify';
import { TraceViewerEnvironment } from '../common/trace-viewer-environment';
import { TraceServerUrlProvider, TRACE_SERVER_DEFAULT_URL } from '../common/trace-server-url-provider';
import { FrontendApplicationContribution, FrontendApplication } from '@theia/core/lib/browser';

@injectable()
export class TraceServerUrlProviderImpl implements TraceServerUrlProvider, FrontendApplicationContribution {

protected _traceServerUrl: string;
protected _listeners: ((url: string) => void)[];

constructor(
@inject(TraceViewerEnvironment) protected readonly traceViewerEnvironment: TraceViewerEnvironment
) {
this._traceServerUrl = TRACE_SERVER_DEFAULT_URL;
this._listeners = [];
}

async onStart(_app: FrontendApplication): Promise<void> {
this._traceServerUrl = await this.traceViewerEnvironment.getTraceServerUrl();
this.updateListeners();
}

async updateListeners(): Promise<void> {
this._listeners.forEach(listener => listener(this._traceServerUrl));
}

getTraceServerUrl(): string {
return this._traceServerUrl;
}

/**
* Add a listener for trace server url changes
* @param listener The listener function to be called when the url is
* changed
*/
addTraceServerUrlChangedListener(listener: (url: string) => void): void {
this._listeners.push(listener);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ContainerModule, Container } from 'inversify';
import { WidgetFactory, OpenHandler, FrontendApplicationContribution, bindViewContribution } from '@theia/core/lib/browser';
import { TraceViewerWidget, TraceViewerWidgetOptions } from './trace-viewer';
import { TraceViewerContribution } from './trace-viewer-contribution';
import { TraceViewerEnvironment } from '../../common/trace-viewer-environment';
import { TraceServerUrlProvider } from '../../common/trace-server-url-provider';
import { CommandContribution } from '@theia/core/lib/common';

import 'ag-grid-community/dist/styles/ag-grid.css';
Expand All @@ -16,17 +18,21 @@ import '../../../src/browser/style/status-bar.css';
// import 'semantic-ui-css/semantic.min.css';
import { TraceExplorerContribution } from '../trace-explorer/trace-explorer-contribution';
import { TRACE_EXPLORER_ID, TraceExplorerWidget } from '../trace-explorer/trace-explorer-widget';
import { TspClient } from 'tsp-typescript-client/lib/protocol/tsp-client';
import { TspClientProvider } from '../tsp-client-provider';
import { TraceManager } from '../../common/trace-manager';
import { ExperimentManager } from '../../common/experiment-manager';
import { TraceServerConnectionStatusService, TraceServerConnectionStatusContribution } from '../../browser/trace-server-status';
import { TraceServerUrlProviderImpl } from '../trace-server-url-provider-frontend-impl';
// import { TracePropertiesContribution } from '../trace-properties-view/trace-properties-view-contribution';
// import { TracePropertiesWidget, TRACE_PROPERTIES_ID } from '../trace-properties-view/trace-properties-view-widget';

export default new ContainerModule(bind => {
bind(TspClient).toDynamicValue(() => new TspClient('http://localhost:8080/tsp/api')).inSingletonScope();
bind(TraceManager).toSelf().inSingletonScope();
bind(ExperimentManager).toSelf().inSingletonScope();

bind(TraceViewerEnvironment).toSelf().inRequestScope();
bind(TraceServerUrlProviderImpl).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).toService(TraceServerUrlProviderImpl);
bind(TraceServerUrlProvider).toService(TraceServerUrlProviderImpl);
bind(TspClientProvider).toSelf().inSingletonScope();

bind(TraceViewerWidget).toSelf();
bind<WidgetFactory>(WidgetFactory).toDynamicValue(context => ({
Expand All @@ -52,6 +58,9 @@ export default new ContainerModule(bind => {
createWidget: () => context.container.get<TraceExplorerWidget>(TraceExplorerWidget)
}));

bind(TraceManager).toSelf().inSingletonScope();
bind(ExperimentManager).toSelf().inSingletonScope();

bind(TraceServerConnectionStatusService).toSelf().inSingletonScope();
bind(FrontendApplicationContribution).toService(TraceServerConnectionStatusService);
bind(TraceServerConnectionStatusContribution).toSelf().inSingletonScope();
Expand Down
6 changes: 5 additions & 1 deletion viewer-prototype/src/browser/trace-viewer/trace-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as React from 'react';
import { OutputDescriptor } from 'tsp-typescript-client/lib/models/output-descriptor';
import { Trace } from 'tsp-typescript-client/lib/models/trace';
import { TspClient } from 'tsp-typescript-client/lib/protocol/tsp-client';
import { TspClientProvider } from '../tsp-client-provider';
import { TraceManager } from '../../common/trace-manager';
import { ExperimentManager } from '../../common/experiment-manager';
import { OutputAddedSignalPayload, TraceExplorerWidget } from '../trace-explorer/trace-explorer-widget';
Expand All @@ -27,6 +28,7 @@ export class TraceViewerWidget extends ReactWidget {
protected readonly uri: Path;
private openedExperiment: Experiment | undefined;
private outputDescriptors: OutputDescriptor[] = [];
private tspClient: TspClient;

private resizeHandlers: (() => void)[] = [];
private readonly addResizeHandler = (h: () => void) => {
Expand All @@ -37,7 +39,7 @@ export class TraceViewerWidget extends ReactWidget {
@inject(TraceViewerWidgetOptions) protected readonly options: TraceViewerWidgetOptions,
@inject(TraceManager) private traceManager: TraceManager,
@inject(ExperimentManager) private experimentManager: ExperimentManager,
@inject(TspClient) private tspClient: TspClient,
@inject(TspClientProvider) private tspClientProvider: TspClientProvider,
@inject(StatusBar) private statusBar: StatusBar,
@inject(FileSystem) private readonly fileSystem: FileSystem,
) {
Expand All @@ -49,6 +51,8 @@ export class TraceViewerWidget extends ReactWidget {
this.addClass('theia-trace-open');
this.toDispose.push(TraceExplorerWidget.outputAddedSignal(output => this.onOutputAdded(output)));
this.initialize();
this.tspClient = this.tspClientProvider.getTspClient();
this.tspClientProvider.addTspClientChangeListener(tspClient => this.tspClient = tspClient);
}

async initialize(): Promise<void> {
Expand Down
34 changes: 34 additions & 0 deletions viewer-prototype/src/browser/tsp-client-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { injectable, inject } from 'inversify';
import { TraceServerUrlProvider } from '../common/trace-server-url-provider';
import { TspClient } from 'tsp-typescript-client/lib/protocol/tsp-client';

@injectable()
export class TspClientProvider {

private _tspClient: TspClient;
private _listeners: ((tspClient: TspClient) => void)[];

constructor(
@inject(TraceServerUrlProvider) private tspUrlProvider: TraceServerUrlProvider
) {
this._tspClient = new TspClient(this.tspUrlProvider.getTraceServerUrl());
this._listeners = [];
tspUrlProvider.addTraceServerUrlChangedListener(url => {
this._tspClient = new TspClient(url);
this._listeners.forEach(listener => listener(this._tspClient));
});
}

public getTspClient(): TspClient {
return this._tspClient;
}

/**
* Add a listener for trace server url changes
* @param listener The listener function to be called when the url is
* changed
*/
addTspClientChangeListener(listener: (tspClient: TspClient) => void): void {
this._listeners.push(listener);
}
}
10 changes: 8 additions & 2 deletions viewer-prototype/src/common/experiment-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Trace } from 'tsp-typescript-client/lib/models/trace';
import { Emitter } from '@theia/core';
import { TspClient } from 'tsp-typescript-client/lib/protocol/tsp-client';
import { TspClientProvider } from '../browser/tsp-client-provider';
import { Query } from 'tsp-typescript-client/lib/models/query/query';
import { injectable, inject } from 'inversify';
import { OutputDescriptor } from 'tsp-typescript-client/lib/models/output-descriptor';
Expand All @@ -19,9 +20,14 @@ export class ExperimentManager {

private fOpenExperiments: Map<string, Experiment> = new Map();

private tspClient: TspClient;

private constructor(
@inject(TspClient) private tspClient: TspClient
) { }
@inject(TspClientProvider) private tspClientProvider: TspClientProvider
) {
this.tspClient = this.tspClientProvider.getTspClient();
this.tspClientProvider.addTspClientChangeListener(tspClient => this.tspClient = tspClient);
}

/**
* Get an array of opened experiments
Expand Down
10 changes: 8 additions & 2 deletions viewer-prototype/src/common/trace-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Trace } from 'tsp-typescript-client/lib/models/trace';
import { Path, Emitter } from '@theia/core';
import { TspClient } from 'tsp-typescript-client/lib/protocol/tsp-client';
import { TspClientProvider } from '../browser/tsp-client-provider';
import { Query } from 'tsp-typescript-client/lib/models/query/query';
import { injectable, inject } from 'inversify';
import { OutputDescriptor } from 'tsp-typescript-client/lib/models/output-descriptor';
Expand All @@ -18,9 +19,14 @@ export class TraceManager {

private fOpenTraces: Map<string, Trace> = new Map();

private tspClient: TspClient;

private constructor(
@inject(TspClient) private tspClient: TspClient
) { }
@inject(TspClientProvider) private tspClientProvider: TspClientProvider
) {
this.tspClient = this.tspClientProvider.getTspClient();
this.tspClientProvider.addTspClientChangeListener(tspClient => this.tspClient = tspClient);
}

/**
* Get an array of opened traces
Expand Down
17 changes: 17 additions & 0 deletions viewer-prototype/src/common/trace-server-url-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const TraceServerUrlProvider = Symbol('TraceServerUrlProvider');
export const TRACE_SERVER_DEFAULT_URL = 'http://localhost:8080/tsp/api';
export interface TraceServerUrlProvider {

/**
* Get the default trace server URL from the server
*/
getTraceServerUrl(): Readonly<string>;

/**
* Add a listener for trace server url changes
* @param listener The listener function to be called when the url is
* changed
*/
addTraceServerUrlChangedListener(listener: (url: string) => void): void;

}
35 changes: 35 additions & 0 deletions viewer-prototype/src/common/trace-viewer-environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { injectable, inject } from 'inversify';
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables';
import { TRACE_SERVER_DEFAULT_URL } from './trace-server-url-provider';

@injectable()
export class TraceViewerEnvironment {

constructor(
@inject(EnvVariablesServer) protected readonly environments: EnvVariablesServer) {

}

protected _traceServerUrl: string | undefined;
async getTraceServerUrl(): Promise<string> {
if (!this._traceServerUrl) {
const traceServerUrl = await this.environments.getValue('TRACE_SERVER_URL');
this._traceServerUrl = traceServerUrl ? this.parseUrl(traceServerUrl.value || TRACE_SERVER_DEFAULT_URL) : TRACE_SERVER_DEFAULT_URL;
}
return this._traceServerUrl;
}

private parseUrl(url: string): string {
let lcUrl = url.toLowerCase();
// Add the http
if (!lcUrl.startsWith('http')) {
lcUrl = 'http://' + lcUrl;
}
// Make sure it does not end with a slash
if (lcUrl.endsWith('/')) {
lcUrl = lcUrl.substring(0, lcUrl.length - 1);
}
return lcUrl;
}

}

0 comments on commit 74e1c45

Please sign in to comment.