Skip to content

Commit 44a3bde

Browse files
authored
fix(telemetry): SAM CLI should inherit Toolkit telemetry setting #2094
Problem: - SAM CLI is an implicit dependency of AWS Toolkit, but if the customer disables Toolkit telemetry, that choice isn't applied to SAM CLI's own telemetry. - SAM CLI telemetry messages are very noisy in the Toolkit logs (at "debug" log-level). - Note: when the Toolkit log-level is "debug", we invoke SAM CLI with `sam --debug`. Solution: - When the customer has disabled AWS Toolkit telemetry, reflect that in SAM CLI invocations by setting an [env var](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-telemetry.html): ``` SAM_CLI_TELEMETRY=0 ``` - Reduces CI logs by 400 lines. - before: ``` 2023-09-29 22:09:58 [VERBOSE]: stderr: 2023-09-29 22:09:58,967 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics 2023-09-29 22:09:58 [VERBOSE]: stderr: 2023-09-29 22:09:58,973 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics 2023-09-29 22:09:58 [VERBOSE]: stderr: 2023-09-29 22:09:58,973 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': 'c676ac68-9e8e-4bdb-b7a3-69488d5b31c3', 'installationId': 'fc43f435-e8ba-4f9c-a905-a5f64bbcb6a2', 'sessionId': 'ca07ba9c-c856-4a17-bad1-05d06ec0447a', 'executionEnvironment': 'AWSCodeBuild', 'ci': True, 'pyversion': '3.10.12', 'samcliVersion': '1.94.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'metricSpecificAttributes': {'projectType': 'CFN', 'gitOrigin': None, 'projectName': 'b404669a6e496d0c9cc117229b17f443fb375168d14b426dc94025e393f40113', 'initialCommit': None}, 'duration': 26295, 'exitReason': 'success', 'exitCode': 0}}]} 2023-09-29 22:09:58,973 | Unable to find Click Context for getting session_id. 2023-09-29 22:09:58 [VERBOSE]: stderr: 2023-09-29 22:09:58,975 | Sending Telemetry: {'metrics': [{'events': {'requestId': '7203adeb-c6ed-49ab-8d0d-b729f56debf4', 'installationId': 'fc43f435-e8ba-4f9c-a905-a5f64bbcb6a2', 'sessionId': 'ca07ba9c-c856-4a17-bad1-05d06ec0447a', 'executionEnvironment': 'AWSCodeBuild', 'ci': True, 'pyversion': '3.10.12', 'samcliVersion': '1.94.0', 'metricSpecificAttributes': {'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': '6e01f234a91a4f49abb1c12d3a030e99', 'time_stamp': '2023-09-29 22:09:32.653', 'exception_name': None}, {'event_name': 'BuildWorkflowUsed', 'event_value': 'nodejs-npm', 'thread_id': 'e761a23adae449c4a8eda09fa655de47', 'time_stamp': '2023-09-29 22:09:32.786', 'exception_name': None}, {'event_name': 'BuildFunctionRuntime', 'event_value': 'nodejs16.x', 'thread_id': 'ec4c0a9911494ddf9608db7041d1edf6', 'time_stamp': '2023-09-29 22:09:32.786', 'exception_name': None}, {'event_name': 'BuildWorkflowUsed', 'event_value': 'nodejs-npm', 'thread_id': '66a77cf36f734613b8571e32548a054f', 'time_stamp': '2023-09-29 22:09:32.792', 'exception_name': None}]}}}]} 2023-09-29 22:09:59 [VERBOSE]: stderr: 2023-09-29 22:09:59,174 | Telemetry response: 200 2023-09-29 22:09:59 [VERBOSE]: stderr: 2023-09-29 22:09:59,177 | Telemetry response: 200 ``` - after: ``` 2023-09-30 16:24:53,795 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics 2023-09-30 16:24:53 [VERBOSE]: stderr: 2023-09-30 16:24:53,795 | Unable to find Click Context for getting session_id. ```
1 parent 528a4a6 commit 44a3bde

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/shared/sam/cli/samCliInvokerUtils.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { getLogger } from '../../logger'
88
import { getUserAgent } from '../../telemetry/util'
99
import { ChildProcessResult, ChildProcessOptions } from '../../utilities/childProcess'
1010
import { ErrorInformation, ToolkitError } from '../../errors'
11+
import globals from '../../extensionGlobals'
12+
import { isAutomation } from '../../vscode/env'
1113

1214
/** Generic SAM CLI invocation error. */
1315
export class SamCliError extends ToolkitError.named('SamCliError') {
@@ -125,10 +127,14 @@ function matchSamError(text: string): string {
125127
}
126128

127129
export async function addTelemetryEnvVar(options: SpawnOptions | undefined): Promise<SpawnOptions> {
130+
const telemetryEnabled = globals.telemetry.telemetryEnabled && !isAutomation()
131+
// If AWS Toolkit telemetry was disabled, implicit dependencies (such as SAM CLI) should inherit that choice.
132+
const samEnv = telemetryEnabled ? {} : { SAM_CLI_TELEMETRY: '0' }
128133
return {
129134
...options,
130135
env: {
131136
AWS_TOOLING_USER_AGENT: await getUserAgent({ includeClientId: false }),
137+
...samEnv,
132138
...options?.env,
133139
},
134140
}

src/test/shared/sam/cli/samCliInvokerUtils.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ describe('addTelemetryEnvVar', async function () {
8585
cwd: '/foo',
8686
env: {
8787
AWS_TOOLING_USER_AGENT: result.env?.['AWS_TOOLING_USER_AGENT'],
88+
SAM_CLI_TELEMETRY: '0',
8889
AWS_REGION: 'us-east-1',
8990
},
9091
})

0 commit comments

Comments
 (0)