@@ -42,32 +42,51 @@ export function getClient(apiKey: string): httpm.HttpClient {
4242 } )
4343}
4444
45+ function isTimeoutError ( error : Error ) : boolean {
46+ return error . message . includes ( 'timeout' ) || error . message . includes ( 'Timeout' )
47+ }
48+
4549async function postMetricsIfAny (
4650 http : httpm . HttpClient ,
4751 apiURL : string ,
4852 metrics : { series : Array < Record < string , unknown > > } ,
49- endpoint : string
53+ endpoint : string ,
54+ ignoreTimeouts : boolean
5055) : Promise < void > {
5156 // POST data
5257 if ( metrics . series . length ) {
53- core . debug ( `About to send ${ metrics . series . length } metrics` )
54- const res : httpm . HttpClientResponse = await http . post (
55- `${ apiURL } /api/${ endpoint } ` ,
56- JSON . stringify ( metrics )
57- )
58-
59- if ( res . message . statusCode === undefined || res . message . statusCode >= 400 ) {
60- throw new Error (
61- `HTTP request failed: ${ res . message . statusMessage } ${ res . message . statusCode } `
58+ try {
59+ core . debug ( `About to send ${ metrics . series . length } metrics` )
60+ const res : httpm . HttpClientResponse = await http . post (
61+ `${ apiURL } /api/${ endpoint } ` ,
62+ JSON . stringify ( metrics )
6263 )
64+
65+ if (
66+ res . message . statusCode === undefined ||
67+ res . message . statusCode >= 400
68+ ) {
69+ throw new Error (
70+ `HTTP request failed: ${ res . message . statusMessage } ${ res . message . statusCode } `
71+ )
72+ }
73+ } catch ( error ) {
74+ if ( ignoreTimeouts && isTimeoutError ( error ) ) {
75+ core . warning (
76+ `Timeout occurred while sending metrics, but continuing due to ignore-timeout setting`
77+ )
78+ return
79+ }
80+ throw error
6381 }
6482 }
6583}
6684
6785export async function sendMetrics (
6886 apiURL : string ,
6987 apiKey : string ,
70- metrics : Metric [ ]
88+ metrics : Metric [ ] ,
89+ ignoreTimeouts : boolean
7190) : Promise < void > {
7291 const http : httpm . HttpClient = getClient ( apiKey )
7392 // distributions use a different procotol.
@@ -89,27 +108,53 @@ export async function sendMetrics(
89108 } )
90109 }
91110
92- await postMetricsIfAny ( http , apiURL , otherMetrics , 'v1/series' )
93- await postMetricsIfAny ( http , apiURL , distributions , 'v1/distribution_points' )
111+ await postMetricsIfAny (
112+ http ,
113+ apiURL ,
114+ otherMetrics ,
115+ 'v1/series' ,
116+ ignoreTimeouts
117+ )
118+ await postMetricsIfAny (
119+ http ,
120+ apiURL ,
121+ distributions ,
122+ 'v1/distribution_points' ,
123+ ignoreTimeouts
124+ )
94125}
95126
96127export async function sendEvents (
97128 apiURL : string ,
98129 apiKey : string ,
99- events : Event [ ]
130+ events : Event [ ] ,
131+ ignoreTimeouts : boolean
100132) : Promise < void > {
101133 const http : httpm . HttpClient = getClient ( apiKey )
102134 let errors = 0
103135
104136 core . debug ( `About to send ${ events . length } events` )
105137 for ( const ev of events ) {
106- const res : httpm . HttpClientResponse = await http . post (
107- `${ apiURL } /api/v1/events` ,
108- JSON . stringify ( ev )
109- )
110- if ( res . message . statusCode === undefined || res . message . statusCode >= 400 ) {
111- errors ++
112- core . error ( `HTTP request failed: ${ res . message . statusMessage } ` )
138+ try {
139+ const res : httpm . HttpClientResponse = await http . post (
140+ `${ apiURL } /api/v1/events` ,
141+ JSON . stringify ( ev )
142+ )
143+ if (
144+ res . message . statusCode === undefined ||
145+ res . message . statusCode >= 400
146+ ) {
147+ errors ++
148+ core . error ( `HTTP request failed: ${ res . message . statusMessage } ` )
149+ }
150+ } catch ( error ) {
151+ if ( ignoreTimeouts && isTimeoutError ( error ) ) {
152+ core . warning (
153+ `Timeout occurred while sending event, but continuing due to ignore-timeout setting`
154+ )
155+ continue
156+ }
157+ throw error
113158 }
114159 }
115160
@@ -121,20 +166,34 @@ export async function sendEvents(
121166export async function sendServiceChecks (
122167 apiURL : string ,
123168 apiKey : string ,
124- serviceChecks : ServiceCheck [ ]
169+ serviceChecks : ServiceCheck [ ] ,
170+ ignoreTimeouts : boolean
125171) : Promise < void > {
126172 const http : httpm . HttpClient = getClient ( apiKey )
127173 let errors = 0
128174
129175 core . debug ( `About to send ${ serviceChecks . length } service checks` )
130176 for ( const sc of serviceChecks ) {
131- const res : httpm . HttpClientResponse = await http . post (
132- `${ apiURL } /api/v1/check_run` ,
133- JSON . stringify ( sc )
134- )
135- if ( res . message . statusCode === undefined || res . message . statusCode >= 400 ) {
136- errors ++
137- core . error ( `HTTP request failed: ${ res . message . statusMessage } ` )
177+ try {
178+ const res : httpm . HttpClientResponse = await http . post (
179+ `${ apiURL } /api/v1/check_run` ,
180+ JSON . stringify ( sc )
181+ )
182+ if (
183+ res . message . statusCode === undefined ||
184+ res . message . statusCode >= 400
185+ ) {
186+ errors ++
187+ core . error ( `HTTP request failed: ${ res . message . statusMessage } ` )
188+ }
189+ } catch ( error ) {
190+ if ( ignoreTimeouts && isTimeoutError ( error ) ) {
191+ core . warning (
192+ `Timeout occurred while sending service check, but continuing due to ignore-timeout setting`
193+ )
194+ continue
195+ }
196+ throw error
138197 }
139198 }
140199
@@ -148,21 +207,35 @@ export async function sendServiceChecks(
148207export async function sendLogs (
149208 logApiURL : string ,
150209 apiKey : string ,
151- logs : Log [ ]
210+ logs : Log [ ] ,
211+ ignoreTimeouts : boolean
152212) : Promise < void > {
153213 const http : httpm . HttpClient = getClient ( apiKey )
154214 let errors = 0
155215
156216 core . debug ( `About to send ${ logs . length } logs` )
157217 for ( const log of logs ) {
158- const res : httpm . HttpClientResponse = await http . post (
159- `${ logApiURL } /v1/input` ,
160- JSON . stringify ( log )
161- )
162- if ( res . message . statusCode === undefined || res . message . statusCode >= 400 ) {
163- errors ++
164- core . error ( `HTTP request failed: ${ res . message . statusMessage } ` )
165- throw new Error ( `Failed sending ${ errors } out of ${ logs . length } events` )
218+ try {
219+ const res : httpm . HttpClientResponse = await http . post (
220+ `${ logApiURL } /v1/input` ,
221+ JSON . stringify ( log )
222+ )
223+ if (
224+ res . message . statusCode === undefined ||
225+ res . message . statusCode >= 400
226+ ) {
227+ errors ++
228+ core . error ( `HTTP request failed: ${ res . message . statusMessage } ` )
229+ throw new Error ( `Failed sending ${ errors } out of ${ logs . length } events` )
230+ }
231+ } catch ( error ) {
232+ if ( ignoreTimeouts && isTimeoutError ( error ) ) {
233+ core . warning (
234+ `Timeout occurred while sending logs, but continuing due to ignore-timeout setting`
235+ )
236+ continue
237+ }
238+ throw error
166239 }
167240 }
168241
0 commit comments