@@ -13,16 +13,20 @@ export interface BaseTelemetryClient {
13
13
14
14
export interface ITelemetryAppender {
15
15
logEvent ( eventName : string , data ?: AppenderData ) : void ;
16
- logEventDangerously ( eventName : string , data ?: AppenderData ) : void ;
17
16
logException ( exception : Error , data ?: AppenderData ) : void ;
18
- logExceptionDangerously ( exception : Error , data ?: AppenderData ) : void ;
19
17
flush ( ) : void | Promise < void > ;
20
18
instantiateAppender ( ) : void ;
21
19
}
22
20
21
+ enum InstantiationStatus {
22
+ NOT_INSTANTIATED ,
23
+ INSTANTIATING ,
24
+ INSTANTIATED ,
25
+ }
26
+
23
27
export class BaseTelemetryAppender implements ITelemetryAppender {
24
28
// Whether or not the client has been instantiated
25
- private _isInstantiated = false ;
29
+ private _instantiationStatus : InstantiationStatus = InstantiationStatus . NOT_INSTANTIATED ;
26
30
private _telemetryClient : BaseTelemetryClient | undefined ;
27
31
28
32
// Queues used to store events until the appender is ready
@@ -43,61 +47,29 @@ export class BaseTelemetryAppender implements ITelemetryAppender {
43
47
44
48
/**
45
49
* Sends the event to the passed in telemetry client
50
+ * The appender does no telemetry level checks as those are done by the reporter.
46
51
* @param eventName The name of the event to log
47
52
* @param data The data contanied in the event
48
53
*/
49
54
logEvent ( eventName : string , data ?: AppenderData ) : void {
50
55
if ( ! this . _telemetryClient ) {
51
- if ( ! this . _isInstantiated && getTelemetryLevel ( ) === TelemetryLevel . ON ) {
56
+ if ( this . _instantiationStatus !== InstantiationStatus . INSTANTIATED ) {
52
57
this . _eventQueue . push ( { eventName, data } ) ;
53
58
}
54
59
return ;
55
60
}
56
61
this . _telemetryClient . logEvent ( eventName , data ) ;
57
62
}
58
63
59
- /**
60
- * **DANGEROUS**: Logs an event regardless of telemetry level.
61
- * This should only be used in controlled environments i.e. CI pipelines
62
- * @param eventName The named of the event to log
63
- * @param data The data contained in the event
64
- */
65
- logEventDangerously ( eventName : string , data ?: AppenderData ) : void {
66
- if ( ! this . _telemetryClient ) {
67
- return ;
68
- }
69
- if ( ! this . _isInstantiated ) {
70
- this . _eventQueue . push ( { eventName, data } ) ;
71
- } else {
72
- this . _telemetryClient . logEvent ( eventName , data ) ;
73
- }
74
- }
75
-
76
- /**
77
- * **DANGEROUS**: Logs an exception regardless of telemetry level.
78
- * This should only be used in controlled environments i.e. CI pipelines
79
- * @param exception The exception to log
80
- * @param data The data associated with the exception
81
- */
82
- logExceptionDangerously ( exception : Error , data ?: AppenderData ) : void {
83
- if ( ! this . _telemetryClient ) {
84
- return ;
85
- }
86
- if ( ! this . _isInstantiated ) {
87
- this . _exceptionQueue . push ( { exception, data } ) ;
88
- } else {
89
- this . _telemetryClient . logException ( exception , data ) ;
90
- }
91
- }
92
-
93
64
/**
94
65
* Sends an exception to the passed in telemetry client
66
+ * The appender does no telemetry level checks as those are done by the reporter.
95
67
* @param exception The exception to collect
96
68
* @param data Data associated with the exception
97
69
*/
98
70
logException ( exception : Error , data ?: AppenderData ) : void {
99
71
if ( ! this . _telemetryClient ) {
100
- if ( ! this . _isInstantiated && getTelemetryLevel ( ) !== TelemetryLevel . OFF ) {
72
+ if ( this . _instantiationStatus !== InstantiationStatus . INSTANTIATED ) {
101
73
this . _exceptionQueue . push ( { exception, data } ) ;
102
74
}
103
75
return ;
@@ -130,16 +102,20 @@ export class BaseTelemetryAppender implements ITelemetryAppender {
130
102
* Instantiates the telemetry client to make the appender "active"
131
103
*/
132
104
instantiateAppender ( ) : void {
133
- if ( this . _isInstantiated ) {
105
+ if ( this . _instantiationStatus !== InstantiationStatus . NOT_INSTANTIATED ) {
134
106
return ;
135
107
}
108
+ this . _instantiationStatus = InstantiationStatus . INSTANTIATING ;
136
109
// Call the client factory to get the client and then let it know it's instatntiated
137
110
this . _clientFactory ( this . _key ) . then ( client => {
138
111
this . _telemetryClient = client ;
139
- this . _isInstantiated = true ;
112
+ this . _instantiationStatus = InstantiationStatus . INSTANTIATED ;
140
113
this . _flushQueues ( ) ;
141
114
} ) . catch ( err => {
142
115
console . error ( err ) ;
116
+ // If it failed to instntiate, then we don't want to try again.
117
+ // So we mark it as instantiated. See #94
118
+ this . _instantiationStatus = InstantiationStatus . INSTANTIATED ;
143
119
} ) ;
144
120
}
145
121
}
0 commit comments