Skip to content

Commit

Permalink
[ci-stats] support sending meta with metrics (#114198)
Browse files Browse the repository at this point in the history
* [ci-stats] support sending meta with metrics

* update kbn/pm dist

* improve comments stat

* update kbn/pm dist

Co-authored-by: spalger <[email protected]>
Co-authored-by: Kibana Machine <[email protected]>
  • Loading branch information
3 people authored Oct 12, 2021
1 parent 243c213 commit 7c087fe
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
51 changes: 44 additions & 7 deletions packages/kbn-dev-utils/src/ci_stats_reporter/ci_stats_reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,43 @@ import { parseConfig, Config } from './ci_stats_config';
const BASE_URL = 'https://ci-stats.kibana.dev';

export interface CiStatsMetric {
/** Top-level categorization for the metric, e.g. "page load bundle size" */
group: string;
/** Specific sub-set of the "group", e.g. "dashboard" */
id: string;
/** integer value recorded as the value of this metric */
value: number;
/** optional limit which will generate an error on PRs when the metric exceeds the limit */
limit?: number;
/**
* path, relative to the repo, where the config file contianing limits
* is kept. Linked from PR comments instructing contributors how to fix
* their PRs.
*/
limitConfigPath?: string;
/** Arbitrary key-value pairs which can be used for additional filtering/reporting */
meta?: CiStatsMetadata;
}

export interface CiStatsTimingMetadata {
export interface CiStatsMetadata {
/**
* Arbitrary key-value pairs which can be attached to CiStatsTiming and CiStatsMetric
* objects stored in the ci-stats service
*/
[key: string]: string | string[] | number | boolean | undefined;
}
export interface CiStatsTiming {
/** Top-level categorization for the timing, e.g. "scripts/foo", process type, etc. */
group: string;
/** Specific timing (witin the "group" being tracked) e.g. "total" */
id: string;
/** time in milliseconds which should be recorded */
ms: number;
meta?: CiStatsTimingMetadata;
/** hash of key-value pairs which will be stored with the timing for additional filtering and reporting */
meta?: CiStatsMetadata;
}

export interface ReqOptions {
interface ReqOptions {
auth: boolean;
path: string;
body: any;
Expand All @@ -54,17 +73,34 @@ export interface TimingsOptions {
/** value of data/uuid, automatically loaded if not specified */
kibanaUuid?: string | null;
}

export interface MetricsOptions {
/** Default metadata to add to each metric */
defaultMeta?: CiStatsMetadata;
}
export class CiStatsReporter {
/**
* Create a CiStatsReporter by inspecting the ENV for the necessary config
*/
static fromEnv(log: ToolingLog) {
return new CiStatsReporter(parseConfig(log), log);
}

constructor(private config: Config | undefined, private log: ToolingLog) {}

/**
* Determine if CI_STATS is explicitly disabled by the environment. To determine
* if the CiStatsReporter has enough information in the environment to send metrics
* for builds use #hasBuildConfig().
*/
isEnabled() {
return process.env.CI_STATS_DISABLED !== 'true';
}

/**
* Determines if the CiStatsReporter is disabled by the environment, or properly
* configured and able to send stats
*/
hasBuildConfig() {
return this.isEnabled() && !!this.config?.apiToken && !!this.config?.buildId;
}
Expand Down Expand Up @@ -103,7 +139,7 @@ export class CiStatsReporter {
const memUsage = process.memoryUsage();
const isElasticCommitter = email && email.endsWith('@elastic.co') ? true : false;

const defaultMetadata = {
const defaultMeta = {
kibanaUuid,
isElasticCommitter,
committerHash: email
Expand All @@ -127,16 +163,16 @@ export class CiStatsReporter {
totalMem: Os.totalmem(),
};

this.log.debug('CIStatsReporter committerHash: %s', defaultMetadata.committerHash);
this.log.debug('CIStatsReporter committerHash: %s', defaultMeta.committerHash);

return await this.req({
auth: !!buildId,
path: '/v1/timings',
body: {
buildId,
upstreamBranch,
defaultMeta,
timings,
defaultMetadata,
},
bodyDesc: timings.length === 1 ? `${timings.length} timing` : `${timings.length} timings`,
});
Expand All @@ -146,7 +182,7 @@ export class CiStatsReporter {
* Report metrics data to the ci-stats service. If running outside of CI this method
* does nothing as metrics can only be reported when associated with a specific CI build.
*/
async metrics(metrics: CiStatsMetric[]) {
async metrics(metrics: CiStatsMetric[], options?: MetricsOptions) {
if (!this.hasBuildConfig()) {
return;
}
Expand All @@ -162,6 +198,7 @@ export class CiStatsReporter {
path: '/v1/metrics',
body: {
buildId,
defaultMeta: options?.defaultMeta,
metrics,
},
bodyDesc: `metrics: ${metrics
Expand Down
25 changes: 20 additions & 5 deletions packages/kbn-pm/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9010,6 +9010,9 @@ var _ci_stats_config = __webpack_require__(218);
const BASE_URL = 'https://ci-stats.kibana.dev';

class CiStatsReporter {
/**
* Create a CiStatsReporter by inspecting the ENV for the necessary config
*/
static fromEnv(log) {
return new CiStatsReporter((0, _ci_stats_config.parseConfig)(log), log);
}
Expand All @@ -9018,10 +9021,21 @@ class CiStatsReporter {
this.config = config;
this.log = log;
}
/**
* Determine if CI_STATS is explicitly disabled by the environment. To determine
* if the CiStatsReporter has enough information in the environment to send metrics
* for builds use #hasBuildConfig().
*/


isEnabled() {
return process.env.CI_STATS_DISABLED !== 'true';
}
/**
* Determines if the CiStatsReporter is disabled by the environment, or properly
* configured and able to send stats
*/


hasBuildConfig() {
var _this$config, _this$config2;
Expand Down Expand Up @@ -9069,7 +9083,7 @@ class CiStatsReporter {

const memUsage = process.memoryUsage();
const isElasticCommitter = email && email.endsWith('@elastic.co') ? true : false;
const defaultMetadata = {
const defaultMeta = {
kibanaUuid,
isElasticCommitter,
committerHash: email ? _crypto.default.createHash('sha256').update(email).digest('hex').substring(0, 20) : undefined,
Expand All @@ -9090,15 +9104,15 @@ class CiStatsReporter {
osRelease: _os.default.release(),
totalMem: _os.default.totalmem()
};
this.log.debug('CIStatsReporter committerHash: %s', defaultMetadata.committerHash);
this.log.debug('CIStatsReporter committerHash: %s', defaultMeta.committerHash);
return await this.req({
auth: !!buildId,
path: '/v1/timings',
body: {
buildId,
upstreamBranch,
timings,
defaultMetadata
defaultMeta,
timings
},
bodyDesc: timings.length === 1 ? `${timings.length} timing` : `${timings.length} timings`
});
Expand All @@ -9109,7 +9123,7 @@ class CiStatsReporter {
*/


async metrics(metrics) {
async metrics(metrics, options) {
var _this$config4;

if (!this.hasBuildConfig()) {
Expand All @@ -9127,6 +9141,7 @@ class CiStatsReporter {
path: '/v1/metrics',
body: {
buildId,
defaultMeta: options === null || options === void 0 ? void 0 : options.defaultMeta,
metrics
},
bodyDesc: `metrics: ${metrics.map(({
Expand Down

0 comments on commit 7c087fe

Please sign in to comment.