MCPcopy
hub / github.com/prometheus/client_js / useGateway

Function useGateway

lib/pushgateway.js:48–118  ·  view source on GitHub ↗
(method, job, groupings)

Source from the content-addressed store, hash-verified

46 }
47}
48async function useGateway(method, job, groupings) {
49 // `URL` first added in v6.13.0
50 // eslint-disable-next-line n/no-deprecated-api
51 const gatewayUrlParsed = url.parse(this.gatewayUrl);
52 const gatewayUrlPath =
53 gatewayUrlParsed.pathname && gatewayUrlParsed.pathname !== '/'
54 ? gatewayUrlParsed.pathname
55 : '';
56 const jobPath = job
57 ? `/job/${encodeURIComponent(job)}${generateGroupings(groupings)}`
58 : '';
59 const path = `${gatewayUrlPath}/metrics${jobPath}`;
60
61 // eslint-disable-next-line n/no-deprecated-api
62 const target = url.resolve(this.gatewayUrl, path);
63 // eslint-disable-next-line n/no-deprecated-api
64 const requestParams = url.parse(target);
65 const httpModule = isHttps(requestParams.href) ? https : http;
66 const options = Object.assign(requestParams, this.requestOptions, {
67 method,
68 });
69
70 return new Promise((resolve, reject) => {
71 if (method === 'DELETE' && options.headers) {
72 delete options.headers['Content-Encoding'];
73 }
74 const req = httpModule.request(options, resp => {
75 let body = '';
76 resp.setEncoding('utf8');
77 resp.on('data', chunk => {
78 body += chunk;
79 });
80 resp.on('end', () => {
81 if (resp.statusCode >= 400) {
82 reject(
83 new Error(`push failed with status ${resp.statusCode}, ${body}`),
84 );
85 } else {
86 resolve({ resp, body });
87 }
88 });
89 });
90 req.on('error', err => {
91 reject(err);
92 });
93
94 req.on('timeout', () => {
95 req.destroy(new Error('Pushgateway request timed out'));
96 });
97
98 if (method !== 'DELETE') {
99 this.registry
100 .metrics()
101 .then(metrics => {
102 if (
103 options.headers &&
104 options.headers['Content-Encoding'] === 'gzip'
105 ) {

Callers

nothing calls this directly

Calls 3

generateGroupingsFunction · 0.85
isHttpsFunction · 0.85
metricsMethod · 0.80

Tested by

no test coverage detected