-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathlogStreamRequest.ts
More file actions
133 lines (116 loc) · 6.45 KB
/
logStreamRequest.ts
File metadata and controls
133 lines (116 loc) · 6.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { type ContainerAppsAPIClient } from '@azure/arm-appcontainers';
import { type ServiceClient } from '@azure/core-client';
import { createHttpHeaders, createPipelineRequest } from '@azure/core-rest-pipeline';
import { createGenericClient } from "@microsoft/vscode-azext-azureutils";
import { callWithTelemetryAndErrorHandling, createSubscriptionContext, nonNullValue, parseError } from "@microsoft/vscode-azext-utils";
import * as vscode from 'vscode';
import { ext } from '../../extensionVariables';
import { createContainerAppsAPIClient } from '../../utils/azureClients';
import { localize } from '../../utils/localize';
import { type IStreamLogsContext } from './IStreamLogsContext';
export interface ILogStream extends vscode.Disposable {
isConnected: boolean;
outputChannel: vscode.OutputChannel;
data: {
containerApp?: string;
revision?: string;
replica?: string;
container?: string;
}
}
const logStreams: Map<string, ILogStream> = new Map<string, ILogStream>();
export function getActiveLogStreams(context: IStreamLogsContext): Map<string, ILogStream> {
const activeStreams = new Map<string, ILogStream>();
for (const [key, value] of logStreams) {
if (value.data.containerApp === context.containerApp.name && value.data.revision === context.revision?.name && value.isConnected) {
activeStreams.set(key, value);
}
}
if (activeStreams.size === 0) {
throw new Error(localize('noActiveStreams', 'There are no active log streams.'));
}
return activeStreams;
}
function getLogStreamId(context: IStreamLogsContext) {
return `${context.container?.containerId}${context.container?.logStreamEndpoint}`;
}
export async function logStreamRequest(context: IStreamLogsContext): Promise<ILogStream> {
const client: ContainerAppsAPIClient = await createContainerAppsAPIClient([context, createSubscriptionContext(context.subscription)]);
const token = await client.containerApps.getAuthToken(context.resourceGroupName, context.containerApp.name);
const endpoint = nonNullValue(context.container?.logStreamEndpoint);
const logStreamId = getLogStreamId(context);
const logStream: ILogStream | undefined = logStreams.get(logStreamId);
if (logStream && logStream.isConnected) {
logStream.outputChannel.show();
void context.ui.showWarningMessage(localize('logStreamAlreadyActive', 'The log-streaming service for "{0}" is already active.', context.replica?.name));
return logStream;
} else {
const outputChannel: vscode.OutputChannel = logStream ? logStream.outputChannel : vscode.window.createOutputChannel(localize('logStreamLabel', '{0} ({1})', context.replica?.name, context.container?.name));
ext.context.subscriptions.push(outputChannel);
outputChannel.show();
outputChannel.appendLine(localize('connectingToLogStream', 'Connecting to log stream...'));
return await new Promise((onLogStreamCreated: (ls: ILogStream) => void): void => {
void callWithTelemetryAndErrorHandling('containerApps.streamingLogs', async (_context) => {
const abortController: AbortController = new AbortController();
const genericClient: ServiceClient = await createGenericClient(context, undefined);
const headers = createHttpHeaders({
authorization: `Bearer ${token.token}`
});
const logsResponse = await genericClient.sendRequest(createPipelineRequest({
method: "GET",
url: endpoint,
abortSignal: abortController.signal,
headers,
streamResponseStatusCodes: new Set<number>([200])
}));
await new Promise<void>((onLogStreamEnded: () => void, reject: (err: Error) => void): void => {
const newLogStream: ILogStream = {
dispose: (): void => {
logsResponse.readableStreamBody?.removeAllListeners();
abortController.abort();
outputChannel.show();
outputChannel.appendLine(localize('logStreamDisconnected', 'Disconnected from log-streaming service.'));
newLogStream.isConnected = false;
void onLogStreamEnded();
},
isConnected: true,
outputChannel: outputChannel,
data: {
revision: context.revision?.name,
replica: context.replica?.name,
container: context.container?.name,
containerApp: context.containerApp.name,
}
};
logsResponse.readableStreamBody?.on('data', (chunk: Buffer | string) => {
outputChannel.append(chunk.toString());
}).on('error', (err: Error) => {
newLogStream.isConnected = false;
outputChannel.show();
outputChannel.appendLine(localize('logStreamError', 'Error connecting to log-streaming service:'));
outputChannel.appendLine(parseError(err).message);
reject(err);
}).on('complete', () => {
newLogStream.dispose();
});
logStreams.set(logStreamId, newLogStream);
onLogStreamCreated(newLogStream);
});
});
});
}
}
export async function disconnectLogStreaming(context: IStreamLogsContext): Promise<void> {
const allStreams = context.logStreamToStop ? [context.logStreamToStop] : getActiveLogStreams(context);
for (const streams of allStreams.values()) {
if (streams && streams.isConnected) {
streams.dispose();
} else {
await context.ui.showWarningMessage(localize('alreadyDisconnected', 'The log-streaming service is already disconnected.'));
}
}
}