-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[telemetry] Adds cloud provider metadata. (#95131)
- Loading branch information
1 parent
5879d1f
commit c9cd4a0
Showing
33 changed files
with
1,348 additions
and
1,003 deletions.
There are no files selected for viewing
8 changes: 5 additions & 3 deletions
8
src/plugins/kibana_usage_collection/server/__snapshots__/index.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
...ns/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.test.mocks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
})); |
78 changes: 78 additions & 0 deletions
78
src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
src/plugins/kibana_usage_collection/server/collectors/cloud/cloud_provider_collector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.