forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] [Code] initial code telemetry integration (elastic#45467) (elas…
…tic#46161) * [Code] initial code telemetry integration (elastic#45467) * [Code] initial code telemetry backend integration * frontend metric code * Add lang server and repositories * adjustments * fix test * update test
- Loading branch information
Showing
8 changed files
with
205 additions
and
8 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
*/ | ||
|
||
export const APP_TITLE = 'Code (Beta)'; | ||
export const APP_USAGE_TYPE = 'code'; |
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
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
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
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,14 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { | ||
createUiStatsReporter, | ||
METRIC_TYPE, | ||
} from '../../../../../../src/legacy/core_plugins/ui_metric/public'; | ||
import { APP_USAGE_TYPE } from '../../common/constants'; | ||
|
||
export const trackUiAction = createUiStatsReporter(APP_USAGE_TYPE); | ||
export { METRIC_TYPE }; |
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
127 changes: 127 additions & 0 deletions
127
x-pack/legacy/plugins/code/server/usage_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,127 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import sinon from 'sinon'; | ||
|
||
import { LanguageServerStatus } from '../common/language_server'; | ||
import { RepositoryReservedField } from './indexer/schema'; | ||
import { EsClient } from './lib/esqueue'; | ||
import { LspService } from './lsp/lsp_service'; | ||
import { emptyAsyncFunc } from './test_utils'; | ||
import * as usageCollector from './usage_collector'; | ||
|
||
const esClient = { | ||
search: emptyAsyncFunc, | ||
}; | ||
|
||
const lspService = { | ||
languageServerStatus: emptyAsyncFunc, | ||
}; | ||
|
||
const createSearchSpy = (): sinon.SinonSpy => { | ||
return sinon.fake.returns( | ||
Promise.resolve({ | ||
hits: { | ||
hits: [ | ||
{ | ||
_source: { | ||
[RepositoryReservedField]: { | ||
uri: 'github.com/elastic/code1', | ||
}, | ||
}, | ||
}, | ||
{ | ||
_source: { | ||
[RepositoryReservedField]: { | ||
uri: 'github.com/elastic/code2', | ||
}, | ||
}, | ||
}, | ||
], | ||
}, | ||
}) | ||
); | ||
}; | ||
|
||
describe('Code Usage Collector', () => { | ||
let makeUsageCollectorStub: any; | ||
let registerStub: any; | ||
let serverStub: any; | ||
let callClusterStub: any; | ||
let languageServerStatusStub: any; | ||
let searchStub: any; | ||
|
||
beforeEach(() => { | ||
makeUsageCollectorStub = sinon.spy(); | ||
registerStub = sinon.stub(); | ||
serverStub = { | ||
usage: { | ||
collectorSet: { makeUsageCollector: makeUsageCollectorStub, register: registerStub }, | ||
register: {}, | ||
}, | ||
}; | ||
callClusterStub = sinon.stub(); | ||
|
||
searchStub = createSearchSpy(); | ||
esClient.search = searchStub; | ||
|
||
languageServerStatusStub = sinon.stub(); | ||
languageServerStatusStub.withArgs('TypeScript').returns(LanguageServerStatus.READY); | ||
languageServerStatusStub.withArgs('Java').returns(LanguageServerStatus.READY); | ||
languageServerStatusStub.withArgs('Ctags').returns(LanguageServerStatus.READY); | ||
languageServerStatusStub.withArgs('Go').returns(LanguageServerStatus.NOT_INSTALLED); | ||
lspService.languageServerStatus = languageServerStatusStub; | ||
}); | ||
|
||
describe('initCodeUsageCollector', () => { | ||
it('should call collectorSet.register', () => { | ||
usageCollector.initCodeUsageCollector( | ||
serverStub, | ||
esClient as EsClient, | ||
(lspService as any) as LspService | ||
); | ||
expect(registerStub.calledOnce).toBeTruthy(); | ||
}); | ||
|
||
it('should call makeUsageCollector with type = code', () => { | ||
usageCollector.initCodeUsageCollector( | ||
serverStub, | ||
esClient as EsClient, | ||
(lspService as any) as LspService | ||
); | ||
expect(makeUsageCollectorStub.calledOnce).toBeTruthy(); | ||
expect(makeUsageCollectorStub.getCall(0).args[0].type).toBe('code'); | ||
}); | ||
|
||
it('should return correct stats', async () => { | ||
usageCollector.initCodeUsageCollector( | ||
serverStub, | ||
esClient as EsClient, | ||
(lspService as any) as LspService | ||
); | ||
const codeStats = await makeUsageCollectorStub.getCall(0).args[0].fetch(callClusterStub); | ||
expect(callClusterStub.notCalled).toBeTruthy(); | ||
expect(codeStats).toEqual({ | ||
enabled: 1, | ||
repositories: 2, | ||
langserver: [ | ||
{ | ||
enabled: 1, | ||
key: 'TypeScript', | ||
}, | ||
{ | ||
enabled: 1, | ||
key: 'Java', | ||
}, | ||
{ | ||
enabled: 0, | ||
key: 'Go', | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
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,52 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { ServerFacade } from '../'; | ||
import { APP_USAGE_TYPE } from '../common/constants'; | ||
import { LanguageServerStatus } from '../common/language_server'; | ||
import { CodeUsageMetrics } from '../model/usage_telemetry_metrics'; | ||
import { EsClient } from './lib/esqueue'; | ||
import { RepositoryObjectClient } from './search'; | ||
import { LspService } from './lsp/lsp_service'; | ||
import { LanguageServers, LanguageServerDefinition } from './lsp/language_servers'; | ||
|
||
export async function fetchCodeUsageMetrics(client: EsClient, lspService: LspService) { | ||
const repositoryObjectClient: RepositoryObjectClient = new RepositoryObjectClient(client); | ||
const allRepos = await repositoryObjectClient.getAllRepositories(); | ||
const langServerEnabled = async (name: string) => { | ||
const status = await lspService.languageServerStatus(name); | ||
return status !== LanguageServerStatus.NOT_INSTALLED ? 1 : 0; | ||
}; | ||
|
||
const langServersEnabled = await Promise.all( | ||
LanguageServers.map(async (langServer: LanguageServerDefinition) => { | ||
return { | ||
key: langServer.name, | ||
enabled: await langServerEnabled(langServer.name), | ||
}; | ||
}) | ||
); | ||
|
||
return { | ||
[CodeUsageMetrics.ENABLED]: 1, | ||
[CodeUsageMetrics.REPOSITORIES]: allRepos.length, | ||
[CodeUsageMetrics.LANGUAGE_SERVERS]: langServersEnabled, | ||
}; | ||
} | ||
|
||
export function initCodeUsageCollector( | ||
server: ServerFacade, | ||
client: EsClient, | ||
lspService: LspService | ||
) { | ||
const codeUsageCollector = server.usage.collectorSet.makeUsageCollector({ | ||
type: APP_USAGE_TYPE, | ||
isReady: () => true, | ||
fetch: async () => fetchCodeUsageMetrics(client, lspService), | ||
}); | ||
|
||
server.usage.collectorSet.register(codeUsageCollector); | ||
} |