Skip to content

Commit

Permalink
[telemetry] Adds cloud provider metadata. (elastic#95131)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers authored Apr 12, 2021
1 parent 5879d1f commit c9cd4a0
Show file tree
Hide file tree
Showing 33 changed files with 1,348 additions and 1,003 deletions.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { cloudDetectorMock } from './detector/cloud_detector.mock';

const mock = cloudDetectorMock.create();

export const cloudDetailsMock = mock.getCloudDetails;
export const detectCloudServiceMock = mock.detectCloudService;

jest.doMock('./detector', () => ({
CloudDetector: jest.fn().mockImplementation(() => mock),
}));
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { cloudDetailsMock, detectCloudServiceMock } from './cloud_provider_collector.test.mocks';
import { loggingSystemMock } from '../../../../../core/server/mocks';
import {
Collector,
createUsageCollectionSetupMock,
createCollectorFetchContextMock,
} from '../../../../usage_collection/server/usage_collection.mock';

import { registerCloudProviderUsageCollector } from './cloud_provider_collector';

describe('registerCloudProviderUsageCollector', () => {
let collector: Collector<unknown>;
const logger = loggingSystemMock.createLogger();

const usageCollectionMock = createUsageCollectionSetupMock();
usageCollectionMock.makeUsageCollector.mockImplementation((config) => {
collector = new Collector(logger, config);
return createUsageCollectionSetupMock().makeUsageCollector(config);
});

const mockedFetchContext = createCollectorFetchContextMock();

beforeEach(() => {
cloudDetailsMock.mockClear();
detectCloudServiceMock.mockClear();
registerCloudProviderUsageCollector(usageCollectionMock);
});

test('registered collector is set', () => {
expect(collector).not.toBeUndefined();
});

test('isReady() => false when cloud details are not available', () => {
cloudDetailsMock.mockReturnValueOnce(undefined);
expect(collector.isReady()).toBe(false);
});

test('isReady() => true when cloud details are available', () => {
cloudDetailsMock.mockReturnValueOnce({ foo: true });
expect(collector.isReady()).toBe(true);
});

test('initiates CloudDetector.detectCloudDetails when called', () => {
expect(detectCloudServiceMock).toHaveBeenCalledTimes(1);
});

describe('fetch()', () => {
test('returns undefined when no details are available', async () => {
cloudDetailsMock.mockReturnValueOnce(undefined);
await expect(collector.fetch(mockedFetchContext)).resolves.toBeUndefined();
});

test('returns cloud details when defined', async () => {
const mockDetails = {
name: 'aws',
vm_type: 't2.micro',
region: 'us-west-2',
zone: 'us-west-2a',
};

cloudDetailsMock.mockReturnValueOnce(mockDetails);
await expect(collector.fetch(mockedFetchContext)).resolves.toEqual(mockDetails);
});

test('should not fail if invoked when not ready', async () => {
cloudDetailsMock.mockReturnValueOnce(undefined);
await expect(collector.fetch(mockedFetchContext)).resolves.toBe(undefined);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { CloudDetector } from './detector';

interface Usage {
name: string;
vm_type?: string;
region?: string;
zone?: string;
}

export function registerCloudProviderUsageCollector(usageCollection: UsageCollectionSetup) {
const cloudDetector = new CloudDetector();
// determine the cloud service in the background
cloudDetector.detectCloudService();

const collector = usageCollection.makeUsageCollector<Usage | undefined>({
type: 'cloud_provider',
isReady: () => Boolean(cloudDetector.getCloudDetails()),
async fetch() {
const details = cloudDetector.getCloudDetails();
if (!details) {
return;
}

return {
name: details.name,
vm_type: details.vm_type,
region: details.region,
zone: details.zone,
};
},
schema: {
name: {
type: 'keyword',
_meta: {
description: 'The name of the cloud provider',
},
},
vm_type: {
type: 'keyword',
_meta: {
description: 'The VM instance type',
},
},
region: {
type: 'keyword',
_meta: {
description: 'The cloud provider region',
},
},
zone: {
type: 'keyword',
_meta: {
description: 'The availability zone within the region',
},
},
},
});

usageCollection.registerCollector(collector);
}
Loading

0 comments on commit c9cd4a0

Please sign in to comment.