Skip to content

Commit

Permalink
fixed some issues with the log system
Browse files Browse the repository at this point in the history
- Fixed some issues where the logs were not being streamed
  • Loading branch information
cjlapao committed Jan 14, 2025
1 parent f9b4add commit 3beb159
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 24 deletions.
19 changes: 15 additions & 4 deletions src/services/logChannelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@ export function closeLogChannelById(id: string): void {
export function openChannelById(
provider: DevOpsRemoteHostProvider | DevOpsCatalogHostProvider,
outputChannel: vscode.OutputChannel,
url: string
url: string,
id = ""
): Promise<WebSocket | undefined> {
return new Promise(async (resolve, reject) => {
const channelId = `${provider.ID}%%logs`;
let channelId = `${provider.ID}%%logs`;
if (id) {
channelId = `${id}%%logs`;
url = `${url}/api/v1/orchestrator/hosts/${id}/logs/stream`;
} else {
url = `${url}/api/v1/logs/stream`;
}

if (!channelId) {
vscode.window.showErrorMessage("Channel ID is required!");
return resolve(undefined);
}

const channelIdSocket = getLogChannelById(provider.ID);
const channelIdSocket = getLogChannelById(channelId);
if (channelIdSocket) {
vscode.window.showErrorMessage("Channel ID is already in use!");
return resolve(undefined);
Expand All @@ -62,7 +69,10 @@ export function openChannelById(
outputChannel.show(true); // Show the output channel
};
ws.onmessage = (event: WebSocket.MessageEvent) => {
const line = event.data.toString();
let line = event.data.toString();
if (!line.endsWith("\n")) {
line = line.concat("\n");
}
outputChannel.append(line); // Stream log data
};

Expand All @@ -72,6 +82,7 @@ export function openChannelById(

ws.onclose = () => {
outputChannel.appendLine("Connection to the service logs closed.");
closeLogChannelById(channelId);
};

logChannels.push({id: channelId, channel: outputChannel, socket: ws});
Expand Down
38 changes: 26 additions & 12 deletions src/tree/commands/devopsManagementProvider/startHostLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {ShowErrorMessage} from "../../../helpers/error";
import {LogService} from "../../../services/logService";
import {DevOpsService} from "../../../services/devopsService";
import {getLogChannelById, openChannelById} from "../../../services/logChannelService";
import {rm} from "fs";
import {getId} from "../../common/devops_common";
import {url} from "inspector";

const registerDevOpsManagementStartLogsCommand = (
context: vscode.ExtensionContext,
Expand All @@ -26,6 +29,7 @@ const registerDevOpsManagementStartLogsCommand = (
if (item) {
const config = Provider.getConfiguration();
const providerId = item.id.split("%%")[0];

let localProvider: DevOpsRemoteHostProvider | DevOpsCatalogHostProvider | undefined = undefined;
if (item.className === "DevOpsRemoteHostProvider") {
localProvider = config.findRemoteHostProviderById(providerId);
Expand All @@ -38,15 +42,25 @@ const registerDevOpsManagementStartLogsCommand = (
return;
}

if (!localProvider.rawHost) {
ShowErrorMessage("Remote Provider", `Provider ${localProvider.name} not found`);
let orchestratorHostId = "";
let channelId = `${localProvider.ID}%%logs`;
if (item.type == "provider.remote_host.host.logs") {
const id = item.id.split("%%")[2];
const rmProvider = localProvider as DevOpsRemoteHostProvider;
rmProvider.hosts?.find(rmHost => {
if (rmHost.id === id) {
orchestratorHostId = rmHost.id;
channelId = `${rmHost.id}%%logs`;
}
});
}

const channelId = `${providerId}%%logs`;
const host = localProvider.host ?? "";
const port = localProvider.port ?? -1;
const schema = localProvider.scheme ?? "";

if (!channelId) {
vscode.window.showErrorMessage("Channel ID is required!");
return;
if (!host) {
ShowErrorMessage("Remote Provider", `Provider ${localProvider.name} not found`);
}

const channelIdSocket = getLogChannelById(channelId);
Expand All @@ -58,15 +72,15 @@ const registerDevOpsManagementStartLogsCommand = (
LogService.sendTelemetryEvent(TelemetryEventIds.VirtualMachineAction, `Get Remote Host ${item.name} logs`);
const outputChannel = vscode.window.createOutputChannel(`Remote Host: ${localProvider.name} Logs`);
let protocol = "ws";
if (localProvider.scheme === "https") {
if (schema === "https") {
protocol = "wss";
}
let websocketAddress = `${protocol}://${localProvider.host}`;
if (localProvider.port) {
websocketAddress += `:${localProvider.port}`;
let websocketAddress = `${protocol}://${host}`;
if (port > 0) {
websocketAddress += `:${port}`;
}
websocketAddress += `/api/logs/stream`;
openChannelById(localProvider, outputChannel, websocketAddress).catch(error => {

openChannelById(localProvider, outputChannel, websocketAddress, orchestratorHostId).catch(error => {
ShowErrorMessage("Remote Provider", error);
});

Expand Down
38 changes: 32 additions & 6 deletions src/tree/commands/devopsManagementProvider/stopHostLogs.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import * as vscode from "vscode";
import {Provider} from "../../../ioc/provider";
import {config, Provider} from "../../../ioc/provider";
import {CommandsFlags, TelemetryEventIds} from "../../../constants/flags";
import {DevOpsRemoteProviderManagementCommand, VirtualMachineCommand} from "../BaseCommand";
import {TELEMETRY_DOCKER} from "../../../telemetry/operations";
import {DevOpsRemoteProviderManagementCommand} from "../BaseCommand";
import {TELEMETRY_DEVOPS_CATALOG, TELEMETRY_DEVOPS_REMOTE, TELEMETRY_DOCKER} from "../../../telemetry/operations";
import {DevOpsRemoteHostsProvider} from "../../devopsRemoteHostProvider/devOpsRemoteHostProvider";
import {DevOpsCatalogProvider} from "../../devopsCatalogProvider/devopsCatalogProvider";
import {closeLogChannelById, getLogChannelById, openChannelById} from "../../../services/logChannelService";
import {closeLogChannelById, getLogChannelById} from "../../../services/logChannelService";
import {LogService} from "../../../services/logService";
import {ShowErrorMessage} from "../../../helpers/error";
import {DevOpsCatalogHostProvider} from "../../../models/devops/catalogHostProvider";
import {DevOpsRemoteHostProvider} from "../../../models/devops/remoteHostProvider";

const registerDevOpsManagementStopLogsCommand = (
context: vscode.ExtensionContext,
Expand All @@ -15,13 +18,36 @@ const registerDevOpsManagementStopLogsCommand = (
context.subscriptions.push(
vscode.commands.registerCommand(CommandsFlags.remoteProviderCloseLogs, async (item: any) => {
const telemetry = Provider.telemetry();
const providerName =
provider instanceof DevOpsCatalogProvider ? TELEMETRY_DEVOPS_CATALOG : TELEMETRY_DEVOPS_REMOTE;

telemetry.sendOperationEvent(TELEMETRY_DOCKER, "STOP_REMOTE_PROVIDER_LOGS_COMMAND_CLICK");
if (item) {
LogService.sendTelemetryEvent(TelemetryEventIds.VirtualMachineAction, `Stop Remote Host ${item.name} logs`);

const providerId = item.id.split("%%")[0];
const channelId = `${providerId}%%logs`;
let localProvider: DevOpsRemoteHostProvider | DevOpsCatalogHostProvider | undefined = undefined;
if (item.className === "DevOpsRemoteHostProvider") {
localProvider = config.findRemoteHostProviderById(providerId);
}
if (item.className === "DevOpsCatalogHostProvider") {
localProvider = config.findCatalogProviderByIOrName(providerId);
}
if (!localProvider) {
ShowErrorMessage(providerName, `Provider ${item.name} not found`);
return;
}

LogService.sendTelemetryEvent(TelemetryEventIds.VirtualMachineAction, `Stop Remote Host ${item.name} logs`);
let channelId = `${localProvider.ID}%%logs`;
if (item.type == "provider.remote_host.host.logs") {
const id = item.id.split("%%")[2];
const rmProvider = localProvider as DevOpsRemoteHostProvider;
rmProvider.hosts?.find(rmHost => {
if (rmHost.id === id) {
channelId = `${rmHost.id}%%logs`;
}
});
}

if (!channelId) {
vscode.window.showErrorMessage("Channel ID is required!");
Expand Down
2 changes: 1 addition & 1 deletion src/tree/devopsRemoteHostProvider/orchestrator_hosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function drawOrchestratorHostsHostItems(
);
}
if (compareVersions(host.devops_version, "0.9.12") > 0) {
const socketId = `${provider.ID}%%logs`;
const socketId = `${host.id}%%logs`;
const logsSocket = getLogChannelById(socketId);
let contextType = "devops.remote.orchestrator.devops_service.logs";
if (logsSocket) {
Expand Down
2 changes: 1 addition & 1 deletion src/tree/devopsRemoteHostProvider/remote_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export function drawRemoteHostItems(
`${id}%%logs`,
elementId,
"Logs",
"provider.remote_host.host.logs",
"provider.remote_host.logs",
"Logs",
"",
"DevOpsRemoteHostProvider",
Expand Down
1 change: 1 addition & 0 deletions src/tree/treeItems/devOpsTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class DevOpsTreeItem extends vscode.TreeItem {
| "provider.remote_host.orchestrator"
| "provider.remote_host.host"
| "provider.remote_host"
| "provider.remote_host.logs"
| "provider.remote_host.host.info"
| "provider.remote_host.host.logs"
| "provider.remote_host.host.hardware"
Expand Down

0 comments on commit 3beb159

Please sign in to comment.