Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 74 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/synthetics-sdk-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"error-stack-parser": "2.1.4",
"google-auth-library": "9.0.0",
"ts-proto": "1.148.1",
"winston": "3.10.0"
"winston": "3.10.0",
"axios": "1.6.7"
},
"author": "Google Inc.",
"license": "Apache-2.0"
Expand Down
48 changes: 48 additions & 0 deletions packages/synthetics-sdk-api/src/cloud_region_resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import axios, { AxiosResponse } from 'axios';

/**
*
* @public
*
* Retrieves the region in which the current Google Cloud Function (v2) is
* executing by querying the metadata server.
*/
export async function getExecutionRegion(): Promise<string | null> {
const metadataServerUrl =
'http://metadata.google.internal/computeMetadata/v1/instance/region';
const headers = { 'Metadata-Flavor': 'Google' };

try {
const response: AxiosResponse = await axios.get(metadataServerUrl, {
headers,
});

// Extract region from the response (e.g., 'us-east1')
const regions = response.data.split('/');
const region = regions[regions.length - 1];

return region;
} catch (error) {
if (axios.isAxiosError(error)) {
console.error('Error fetching region from metadata server:', error);
} else {
console.error('Unexpected error:', error);
}
}

return null;
}
1 change: 1 addition & 0 deletions packages/synthetics-sdk-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export {
instantiateAutoInstrumentation,
getInstrumentedLogger,
} from './auto_instrumentation';
export { getExecutionRegion } from './cloud_region_resolver';
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import axios from 'axios';
import * as sinon from 'sinon';
import { expect } from 'chai';
import { getExecutionRegion } from '../../src/cloud_region_resolver';

describe('getExecutionRegion', () => {
let axiosGetStub: sinon.SinonStub;

beforeEach(() => {
axiosGetStub = sinon.stub(axios, 'get');
});

afterEach(() => {
axiosGetStub.restore();
});

it('should retrieve the region from the metadata server', async () => {
axiosGetStub.resolves({ data: 'projects/123456789/regions/us-east1' });

const region = await getExecutionRegion();
expect(region).to.equal('us-east1');
expect(axiosGetStub.calledOnce).to.be.true;
});

it('should handle errors from the metadata server', async () => {
axiosGetStub.rejects(new Error('Metadata server error'));

const region = await getExecutionRegion();
expect(region).to.be.null;
});
});