[Uptime] [Synthetics Service] log errors when fetching service locations (#124239)

* log errors when fetching service locations

* update tests

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Dominique Clarke 2022-02-02 07:42:30 -05:00 committed by GitHub
parent 2781a855e4
commit 6b9e127654
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 9 deletions

View file

@ -6,7 +6,7 @@
*/
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import type { SavedObjectsClientContract, IScopedClusterClient } from 'src/core/server';
import type { SavedObjectsClientContract, IScopedClusterClient, Logger } from 'src/core/server';
import { ObservabilityPluginSetup } from '../../../../../observability/server';
import {
EncryptedSavedObjectsPluginSetup,
@ -51,6 +51,7 @@ export interface UptimeServerSetup {
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
syntheticsService: SyntheticsService;
kibanaVersion: string;
logger: Logger;
}
export interface UptimeCorePluginsSetup {

View file

@ -6,6 +6,7 @@
*/
import axios from 'axios';
import { getServiceLocations } from './get_service_locations';
jest.mock('axios');
const mockedAxios = axios as jest.Mocked<typeof axios>;
@ -27,7 +28,15 @@ describe('getServiceLocations', function () {
});
it('should return parsed locations', async () => {
const locations = await getServiceLocations({
manifestUrl: 'http://local.dev',
config: {
service: {
manifestUrl: 'http://local.dev',
},
},
// @ts-ignore
logger: {
error: jest.fn(),
},
});
expect(locations).toEqual({

View file

@ -11,16 +11,19 @@ import {
ServiceLocations,
ServiceLocationsApiResponse,
} from '../../../common/runtime_types';
import { UptimeServerSetup } from '../adapters/framework';
export async function getServiceLocations({ manifestUrl }: { manifestUrl?: string }) {
export async function getServiceLocations(server: UptimeServerSetup) {
const locations: ServiceLocations = [];
if (!manifestUrl) {
if (!server.config.service!.manifestUrl!) {
return { locations };
}
try {
const { data } = await axios.get<{ locations: Record<string, ManifestLocation> }>(manifestUrl);
const { data } = await axios.get<{ locations: Record<string, ManifestLocation> }>(
server.config.service!.manifestUrl!
);
Object.entries(data.locations).forEach(([locationId, location]) => {
locations.push({
@ -33,6 +36,7 @@ export async function getServiceLocations({ manifestUrl }: { manifestUrl?: strin
return { locations } as ServiceLocationsApiResponse;
} catch (e) {
server.logger.error(e);
return {
locations: [],
} as ServiceLocationsApiResponse;

View file

@ -104,9 +104,7 @@ export class SyntheticsService {
async run() {
const { state } = taskInstance;
const { manifestUrl } = service.config;
getServiceLocations({ manifestUrl }).then((result) => {
getServiceLocations(service.server).then((result) => {
service.locations = result.locations;
service.apiClient.locations = result.locations;
});

View file

@ -72,6 +72,7 @@ export class Plugin implements PluginType {
router: core.http.createRouter(),
cloud: plugins.cloud,
kibanaVersion: this.initContext.env.packageInfo.version,
logger: this.logger,
} as UptimeServerSetup;
if (this.server?.config?.service?.enabled) {

View file

@ -18,6 +18,6 @@ export const getServiceLocationsRoute: UMRestApiRouteFactory = () => ({
return { locations: server.syntheticsService.locations };
}
return getServiceLocations({ manifestUrl: server.config.service!.manifestUrl! });
return getServiceLocations(server);
},
});