Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[telemetry] Adds cloud provider metadata. #95131

Merged
merged 15 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from 14 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

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();
lukeelmers marked this conversation as resolved.
Show resolved Hide resolved

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