@@ -15,12 +15,15 @@ import {
1515 SessionId ,
1616 telemetryClientIdEnvKey ,
1717 TelemetryConfig ,
18+ validateMetricEvent ,
1819} from '../../../shared/telemetry/util'
1920import { extensionVersion } from '../../../shared/vscode/env'
2021import { FakeMemento } from '../../fakeExtensionContext'
2122import { GlobalState } from '../../../shared/globalState'
2223import { randomUUID } from 'crypto'
2324import { isUuid } from '../../../shared/crypto'
25+ import { MetricDatum } from '../../../shared/telemetry/clienttelemetry'
26+ import { assertLogsContain } from '../../globalSetup.test'
2427
2528describe ( 'TelemetryConfig' , function ( ) {
2629 const settingKey = 'aws.telemetry'
@@ -281,3 +284,110 @@ describe('getUserAgent', function () {
281284 assert . strictEqual ( beforeClient , platformPair ( ) )
282285 } )
283286} )
287+
288+ describe ( 'validateMetricEvent' , function ( ) {
289+ it ( 'does not validate exempt metrics' , function ( ) {
290+ const metricEvent : MetricDatum = {
291+ MetricName : 'exempt_metric' ,
292+ Value : 1 ,
293+ Unit : 'None' ,
294+ Metadata : [ { Key : 'result' , Value : 'Succeeded' } ] ,
295+ } as MetricDatum
296+
297+ validateMetricEvent ( metricEvent , true , ( _ ) => true )
298+ assert . throws ( ( ) => assertLogsContain ( 'invalid Metric' , false , 'warn' ) )
299+ } )
300+
301+ it ( 'passes validation for metrics with proper result property' , function ( ) {
302+ const metricEvent : MetricDatum = {
303+ MetricName : 'valid_metric' ,
304+ Value : 1 ,
305+ Unit : 'None' ,
306+ Metadata : [ { Key : 'result' , Value : 'Succeeded' } ] ,
307+ } as MetricDatum
308+
309+ validateMetricEvent ( metricEvent , true , ( _ ) => false )
310+ assert . throws ( ( ) => assertLogsContain ( 'invalid Metric' , false , 'warn' ) )
311+ } )
312+
313+ it ( 'passes validation for metrics with Failed result and reason property' , function ( ) {
314+ const metricEvent : MetricDatum = {
315+ MetricName : 'valid_failed_metric' ,
316+ Value : 1 ,
317+ Unit : 'None' ,
318+ Metadata : [
319+ { Key : 'result' , Value : 'Failed' } ,
320+ { Key : 'reason' , Value : 'Something went wrong' } ,
321+ ] ,
322+ } as MetricDatum
323+
324+ validateMetricEvent ( metricEvent , true , ( _ ) => false )
325+ } )
326+
327+ it ( 'fails validation for metrics missing result property when fatal=true' , function ( ) {
328+ const metricEvent : MetricDatum = {
329+ MetricName : 'invalid_metric_no_result' ,
330+ Value : 1 ,
331+ Unit : 'None' ,
332+ Metadata : [ { Key : 'someOtherProperty' , Value : 'value' } ] ,
333+ } as MetricDatum
334+
335+ assert . throws (
336+ ( ) => validateMetricEvent ( metricEvent , true , ( _ ) => false ) ,
337+ / e m i t t e d w i t h o u t t h e ` r e s u l t ` p r o p e r t y /
338+ )
339+ } )
340+
341+ it ( 'logs warning for metrics missing result property when fatal=false' , function ( ) {
342+ const metricEvent : MetricDatum = {
343+ MetricName : 'invalid_metric_no_result' ,
344+ Value : 1 ,
345+ Unit : 'None' ,
346+ Metadata : [ { Key : 'someOtherProperty' , Value : 'value' } ] ,
347+ } as MetricDatum
348+
349+ validateMetricEvent ( metricEvent , false , ( _ ) => false )
350+ assertLogsContain ( 'invalid Metric' , false , 'warn' )
351+ } )
352+
353+ it ( 'fails validation for metrics with Failed result but missing reason property when fatal=true' , function ( ) {
354+ const metricEvent : MetricDatum = {
355+ MetricName : 'invalid_metric_failed_no_reason' ,
356+ Value : 1 ,
357+ Unit : 'None' ,
358+ Metadata : [ { Key : 'result' , Value : 'Failed' } ] ,
359+ } as MetricDatum
360+
361+ assert . throws (
362+ ( ) => validateMetricEvent ( metricEvent , true ) ,
363+ / e m i t t e d w i t h r e s u l t = F a i l e d b u t w i t h o u t t h e ` r e a s o n ` p r o p e r t y /
364+ )
365+ } )
366+
367+ it ( 'logs warning for metrics with Failed result but missing reason property when fatal=false' , function ( ) {
368+ const metricEvent : MetricDatum = {
369+ MetricName : 'invalid_metric_failed_no_reason' ,
370+ Value : 1 ,
371+ Unit : 'None' ,
372+ Metadata : [ { Key : 'result' , Value : 'Failed' } ] ,
373+ } as MetricDatum
374+
375+ validateMetricEvent ( metricEvent , false )
376+ assertLogsContain ( 'invalid Metric' , false , 'warn' )
377+ } )
378+
379+ it ( 'does not fail validation for metrics with missing fields with fatal=true' , function ( ) {
380+ const metricEvent : MetricDatum = {
381+ MetricName : 'invalid_metric_missing_fields' ,
382+ Value : 1 ,
383+ Unit : 'None' ,
384+ Metadata : [
385+ { Key : 'result' , Value : 'Succeeded' } ,
386+ { Key : 'missingFields' , Value : 'field1,field2' } ,
387+ ] ,
388+ } as MetricDatum
389+
390+ validateMetricEvent ( metricEvent , false )
391+ assertLogsContain ( 'invalid Metric' , false , 'warn' )
392+ } )
393+ } )
0 commit comments