@@ -26,9 +26,12 @@ export enum TotaltimeType {
2626 None = 'none' ,
2727}
2828
29+ export type LogLevel = 'none' | 'debug' | 'info' | 'error' ;
30+
2931interface Props {
3032 metricsSink : MetricsSink ;
3133 totalTimeType : TotaltimeType ;
34+ logLevel ?: LogLevel ;
3235}
3336
3437interface RecordOptions {
@@ -42,12 +45,57 @@ interface RecordWithBehaviorOptions {
4245 behavior : MetricsBehavior ;
4346}
4447
48+ const NONE_LEVEL = 0 ;
49+ const DEBUG_LEVEL = 1 ;
50+ const INFO_LEVEL = 2 ;
51+ const ERROR_LEVEL = 3 ;
52+
53+ class Logger {
54+ private readonly level : number ;
55+ constructor ( level : LogLevel ) {
56+ switch ( level ) {
57+ case 'none' :
58+ this . level = NONE_LEVEL ;
59+ break ;
60+ case 'debug' :
61+ this . level = DEBUG_LEVEL ;
62+ break ;
63+ case 'info' :
64+ this . level = INFO_LEVEL ;
65+ break ;
66+ case 'error' :
67+ this . level = ERROR_LEVEL ;
68+ break ;
69+ }
70+ }
71+
72+ debug ( message : string ) : void {
73+ if ( this . level >= DEBUG_LEVEL ) {
74+ console . debug ( message ) ;
75+ }
76+ }
77+
78+ info ( message : string ) : void {
79+ if ( this . level >= INFO_LEVEL ) {
80+ console . info ( message ) ;
81+ }
82+ }
83+
84+ error ( message : string ) : void {
85+ if ( this . level >= ERROR_LEVEL ) {
86+ console . error ( message ) ;
87+ }
88+ }
89+ }
90+
4591export class MetricsFactory {
4692 protected readonly metricsSink : MetricsSink ;
4793 private readonly totalTimeType : TotaltimeType ;
94+ private readonly logger : Logger ;
4895 constructor ( props : Props ) {
4996 this . metricsSink = props . metricsSink ;
5097 this . totalTimeType = props . totalTimeType ;
98+ this . logger = new Logger ( props . logLevel ?? 'none' ) ;
5199 }
52100
53101 /**
@@ -109,26 +157,33 @@ export class MetricsFactory {
109157 */
110158 private async emit ( metrics : _Metrics ) {
111159 this . finalizeMetrics ( metrics ) ;
160+ this . logger . debug ( 'metrics finalized' ) ;
161+ console . log ( 'metrics' , metrics ) ;
112162 await this . metricsSink . emit ( metrics ) ;
113163 }
114164
115165 private finalizeMetrics ( metrics : _Metrics ) {
166+ this . logger . debug ( 'finalizing metrics' ) ;
116167 if ( metrics . timestampMillis < 1 ) {
117168 metrics . timestampMillis = Date . now ( ) ;
118169 }
119170 if ( metrics . metricsBehavior === MetricsBehavior . NO_TOTALTIME ) {
171+ this . logger . debug ( 'no total time being recorded' ) ;
120172 return ;
121173 }
122174
123175 const duration = metrics . getDurationMillis ( ) ;
124176 switch ( this . totalTimeType ) {
125177 case TotaltimeType . DistributionMilliseconds :
178+ this . logger . debug ( `distribution milliseconds, duration: ${ duration } ` ) ;
126179 metrics . distribution ( 'totaltime' , duration ) ;
127180 break ;
128181 case TotaltimeType . MeasurementMilliseconds :
182+ this . logger . debug ( `measurement milliseconds, duration: ${ duration } ` ) ;
129183 metrics . measure ( 'totaltime' , duration ) ;
130184 break ;
131185 case TotaltimeType . None :
186+ this . logger . debug ( `totaltime.none, duration: ${ duration } ` ) ;
132187 break ;
133188 }
134189 }
0 commit comments