in packages/opentelemetry-node/lib/detector-gcp.js [110:146]
function metadataRequest(path) {
const baseUrl = 'http://metadata.google.internal:80';
const options = {
method: 'GET',
headers: {'Metadata-Flavor': 'Google'},
signal: AbortSignal.timeout(1000),
};
return fetch(`${baseUrl}/computeMetadata/v1/${path}`, options)
.then((res) => {
// Validate status
if (!(res.status >= 200 && res.status < 300)) {
throw new Error(
`Invalid response from metadata service: invalid status code: ${res.status} text: ${res.statusText}`
);
}
// Validation from gcp-metadata
// https://github.com/googleapis/gcp-metadata/blob/d8a868e5f487dcc3dd4bfd2d59d8c331fcf2895b/src/index.ts#L177
const headerVal = res.headers.get('metadata-flavor');
if (res.headers.get('Metadata-Flavor') !== 'Google') {
throw new Error(
`Invalid response from metadata service: incorrect 'Metadata-Flavor' header. Expected 'Google', got ${
headerVal || 'no header'
}`
);
}
return res.text();
})
.then((txt) => {
// ref: https://github.com/googleapis/gcp-metadata/blob/d8a868e5f487dcc3dd4bfd2d59d8c331fcf2895b/src/index.ts#L184
try {
return jsonBigint.parse(txt);
} catch {
// nothing
}
return txt;
});
}