-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Implement basic CI metric reporting #64263
Merged
spalger
merged 8 commits into
elastic:master
from
spalger:implement/ci-stats-build-creation
Apr 27, 2020
+2,715
−2,215
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a226114
Report very simple CI metrics to ci-stats service
spalger 2c8d9e1
[Jenkinsfile] fix indentation
spalger 3600013
add basic docs to CiStatsReporter
spalger 770f65d
Merge branch 'master' into implement/ci-stats-build-creation
elasticmachine 21ab56d
Merge branch 'master' of github.com:elastic/kibana into implement/ci-…
spalger 2249021
Merge branch 'master' of github.com:elastic/kibana into implement/ci-…
spalger 8e0ed59
add labels to sh calls
spalger f99c6c3
add missing comma
spalger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,23 @@ | ||
# Kibana CI Stats reporter | ||
|
||
We're working on building a new service, the Kibana CI Stats service, which will collect information about CI runs and metrics produced while testing Kibana, and then provide tools for reporting on those metrics in specific PRs and overall as a way to spot trends. | ||
|
||
### `CiStatsReporter` | ||
|
||
This class integrates with the `ciStats.trackBuild {}` Jenkins Pipeline function, consuming the `KIBANA_CI_STATS_CONFIG` variable produced by that wrapper, and then allowing test code to report stats to the service. | ||
|
||
To create an instance of the reporter, import the class and call `CiStatsReporter.fromEnv(log)` (passing it a tooling log). | ||
|
||
#### `CiStatsReporter#metric(name: string, subName: string, value: number)` | ||
|
||
Use this method to record metrics in the Kibana CI Stats service. | ||
|
||
Example: | ||
|
||
```ts | ||
import { CiStatsReporter, ToolingLog } from '@kbn/dev-utils'; | ||
|
||
const log = new ToolingLog(...); | ||
const reporter = CiStatsReporter.fromEnv(log) | ||
reporter.metric('Build speed', specificBuildName, timeToRunBuild) | ||
``` |
153 changes: 153 additions & 0 deletions
153
packages/kbn-dev-utils/src/ci_stats_reporter/ci_stats_reporter.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,153 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { inspect } from 'util'; | ||
|
||
import Axios from 'axios'; | ||
|
||
import { ToolingLog } from '../tooling_log'; | ||
|
||
interface Config { | ||
apiUrl: string; | ||
apiToken: string; | ||
buildId: string; | ||
} | ||
|
||
function parseConfig(log: ToolingLog) { | ||
const configJson = process.env.KIBANA_CI_STATS_CONFIG; | ||
if (!configJson) { | ||
log.debug('KIBANA_CI_STATS_CONFIG environment variable not found, disabling CiStatsReporter'); | ||
return; | ||
} | ||
|
||
let config: unknown; | ||
try { | ||
config = JSON.parse(configJson); | ||
} catch (_) { | ||
// handled below | ||
} | ||
|
||
if (typeof config === 'object' && config !== null) { | ||
return validateConfig(log, config as { [k in keyof Config]: unknown }); | ||
} | ||
|
||
log.warning('KIBANA_CI_STATS_CONFIG is invalid, stats will not be reported'); | ||
return; | ||
} | ||
|
||
function validateConfig(log: ToolingLog, config: { [k in keyof Config]: unknown }) { | ||
const validApiUrl = typeof config.apiUrl === 'string' && config.apiUrl.length !== 0; | ||
if (!validApiUrl) { | ||
log.warning('KIBANA_CI_STATS_CONFIG is missing a valid api url, stats will not be reported'); | ||
return; | ||
} | ||
|
||
const validApiToken = typeof config.apiToken === 'string' && config.apiToken.length !== 0; | ||
if (!validApiToken) { | ||
log.warning('KIBANA_CI_STATS_CONFIG is missing a valid api token, stats will not be reported'); | ||
return; | ||
} | ||
|
||
const validId = typeof config.buildId === 'string' && config.buildId.length !== 0; | ||
if (!validId) { | ||
log.warning('KIBANA_CI_STATS_CONFIG is missing a valid build id, stats will not be reported'); | ||
return; | ||
} | ||
|
||
return config as Config; | ||
} | ||
|
||
export class CiStatsReporter { | ||
static fromEnv(log: ToolingLog) { | ||
return new CiStatsReporter(parseConfig(log), log); | ||
} | ||
|
||
constructor(private config: Config | undefined, private log: ToolingLog) {} | ||
|
||
isEnabled() { | ||
return !!this.config; | ||
} | ||
|
||
async metric(name: string, subName: string, value: number) { | ||
if (!this.config) { | ||
return; | ||
} | ||
|
||
let attempt = 0; | ||
const maxAttempts = 5; | ||
|
||
while (true) { | ||
attempt += 1; | ||
|
||
try { | ||
await Axios.request({ | ||
method: 'POST', | ||
url: '/metric', | ||
baseURL: this.config.apiUrl, | ||
params: { | ||
buildId: this.config.buildId, | ||
}, | ||
headers: { | ||
Authorization: `token ${this.config.apiToken}`, | ||
}, | ||
data: { | ||
name, | ||
subName, | ||
value, | ||
}, | ||
}); | ||
|
||
return; | ||
} catch (error) { | ||
if (!error?.request) { | ||
// not an axios error, must be a usage error that we should notify user about | ||
throw error; | ||
} | ||
|
||
if (error?.response && error.response.status !== 502) { | ||
// error response from service was received so warn the user and move on | ||
this.log.warning( | ||
`error recording metric [status=${error.response.status}] [resp=${inspect( | ||
error.response.data | ||
)}] [${name}/${subName}=${value}]` | ||
); | ||
return; | ||
} | ||
|
||
if (attempt === maxAttempts) { | ||
this.log.warning( | ||
`failed to reach kibana-ci-stats service too many times, unable to record metric [${name}/${subName}=${value}]` | ||
); | ||
return; | ||
} | ||
|
||
// we failed to reach the backend and we have remaining attempts, lets retry after a short delay | ||
const reason = error?.response?.status | ||
? `${error.response.status} response` | ||
: 'no response'; | ||
|
||
this.log.warning( | ||
`failed to reach kibana-ci-stats service [reason=${reason}], retrying in ${attempt} seconds` | ||
); | ||
|
||
await new Promise(resolve => setTimeout(resolve, attempt * 1000)); | ||
} | ||
} | ||
} | ||
} |
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,20 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export * from './ci_stats_reporter'; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small thing, should this be
!this.isEnabled()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use that, but it wouldn't teach TypeScript that
this.config
should be considered non-null below.