Skip to content

Commit ca3ba22

Browse files
HweinstockkaranA-aws
authored andcommitted
refactor(ec2): pass log level to connect script (aws#6161)
## Problem We want to only log file paths when in debug mode, but the connect script has no way of determining this. ## Solution - pass log level to connect script. - use it to determine what to log. Example logs: ``` 2024/12/05 15:50:24 ============================================================================== 2024/12/05 15:50:24 LOG_LEVEL=3 2024/12/05 15:50:24 AWS_REGION=us-east-1 2024/12/05 15:50:24 SESSION_ID=... 2024/12/05 15:51:11 ============================================================================== 2024/12/05 15:51:11 LOG_LEVEL=1 2024/12/05 15:51:11 AWS_REGION=us-east-1 2024/12/05 15:51:11 SESSION_ID=... 2024/12/05 15:51:11 AWS_SSM_CLI=.../Amazon/sessionmanagerplugin/bin/session-manager-plugin 2024/12/05 15:51:11 LOG_FILE_LOCATION=/.../ec2.{instanceId}.log 2024/12/05 15:51:55 ============================================================================== 2024/12/05 15:51:55 LOG_LEVEL=2 2024/12/05 15:51:55 AWS_REGION=us-east-1 2024/12/05 15:51:55 SESSION_ID=default-uk9cv4h86y5rdbrzarlkj9opci 2024/12/05 15:51:56 AWS_SSM_CLI=../Amazon/sessionmanagerplugin/bin/session-manager-plugin 2024/12/05 15:51:56 LOG_FILE_LOCATION=/.../ec2.{instanceId}.log ``` --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 6aadaaf commit ca3ba22

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

packages/core/resources/ec2_connect

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Usage:
44
# When connecting to a dev environment
5-
# AWS_REGION=… AWS_SSM_CLI=… STREAM_URL=… TOKEN=… LOG_FILE_LOCATION==… ./ec2_connect
5+
# AWS_REGION=… AWS_SSM_CLI=… STREAM_URL=… TOKEN=… LOG_FILE_LOCATION==… DEBUG_LOG=… ./ec2_connect
66

77
set -e
88
set -u
@@ -44,13 +44,21 @@ _ec2() {
4444

4545
_main() {
4646
_log "=============================================================================="
47-
48-
_require AWS_SSM_CLI "${AWS_SSM_CLI:-}"
47+
_require DEBUG_LOG "${DEBUG_LOG:-}"
4948
_require AWS_REGION "${AWS_REGION:-}"
49+
50+
_require SESSION_ID "${SESSION_ID:-}"
5051
_require_nolog STREAM_URL "${STREAM_URL:-}"
5152
_require_nolog TOKEN "${TOKEN:-}"
52-
_require SESSION_ID "${SESSION_ID:-}"
53-
_require LOG_FILE_LOCATION "${LOG_FILE_LOCATION:-}"
53+
54+
# Only log file paths when debug level is enabled.
55+
if [ "${DEBUG_LOG:-}" -eq 1 ]; then
56+
_require AWS_SSM_CLI "${AWS_SSM_CLI:-}"
57+
_require LOG_FILE_LOCATION "${LOG_FILE_LOCATION:-}"
58+
else
59+
_require_nolog AWS_SSM_CLI "${AWS_SSM_CLI:-}"
60+
_require_nolog LOG_FILE_LOCATION "${LOG_FILE_LOCATION:-}"
61+
fi
5462

5563
_ec2 "$AWS_SSM_CLI" "$AWS_REGION" "$STREAM_URL" "$TOKEN" "$SESSION_ID"
5664
}

packages/core/src/awsService/ec2/model.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ export class Ec2Connecter implements vscode.Disposable {
242242
const ssmSession = await this.startSSMSession(selection.instanceId)
243243

244244
const vars = getEc2SsmEnv(selection, ssm, ssmSession)
245-
getLogger().info(`ec2: connect script logs at ${vars.LOG_FILE_LOCATION}`)
245+
getLogger().debug(`ec2: connect script logs at ${vars.LOG_FILE_LOCATION}`)
246+
246247
const envProvider = async () => {
247248
return { [sshAgentSocketVariable]: await startSshAgent(), ...vars }
248249
}

packages/core/src/awsService/ec2/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { copyToClipboard } from '../../shared/utilities/messages'
88
import { Ec2Selection } from './prompter'
99
import { sshLogFileLocation } from '../../shared/sshConfig'
1010
import { SSM } from 'aws-sdk'
11+
import { getLogger } from '../../shared/logger'
1112

1213
export function getIconCode(instance: SafeEc2Instance) {
1314
if (instance.LastSeenStatus === 'running') {
@@ -42,6 +43,7 @@ export function getEc2SsmEnv(
4243
STREAM_URL: session.StreamUrl,
4344
SESSION_ID: session.SessionId,
4445
TOKEN: session.TokenValue,
46+
DEBUG_LOG: getLogger().logLevelEnabled('debug') ? 1 : 0,
4547
},
4648
process.env
4749
)

packages/core/src/shared/logger/activation.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { resolvePath } from '../utilities/pathUtils'
1313
import fs from '../../shared/fs/fs'
1414
import { isWeb } from '../extensionGlobals'
1515
import { getUserAgent } from '../telemetry/util'
16-
import { isBeta } from '../vscode/env'
16+
import { isBeta, isDebugInstance } from '../vscode/env'
1717

1818
/**
1919
* Activate Logger functionality for the extension.
@@ -46,7 +46,10 @@ export async function activate(
4646
const newLogLevel = fromVscodeLogLevel(logLevel)
4747
mainLogger.setLogLevel(newLogLevel) // Also logs a message.
4848
})
49-
mainLogger.setLogLevel('debug') // HACK: set to "debug" when debugging the extension.
49+
50+
if (isDebugInstance()) {
51+
mainLogger.setLogLevel('debug') // HACK: set to "debug" when debugging the extension.
52+
}
5053

5154
setLogger(mainLogger)
5255

0 commit comments

Comments
 (0)