Skip to content

Commit

Permalink
Add gc stats onto statsd
Browse files Browse the repository at this point in the history
[changelog:added]
  • Loading branch information
cdupuis committed Nov 15, 2018
1 parent f008f35 commit 7639432
Show file tree
Hide file tree
Showing 4 changed files with 603 additions and 90 deletions.
2 changes: 1 addition & 1 deletion lib/internal/util/memory.ts
Expand Up @@ -8,7 +8,7 @@ let DataDirectory = `${appRoot.path}/heap`;

/**
* Initialise memory monitoring. This is will set the data directory for heap dumps and
* print the head usage to the connsole every 60 seconds.
* print the head usage to the console every 60 seconds.
* @param {string} dataDirectory
*/
export function initMemoryMonitoring(dataDirectory: string = `${appRoot.path}/heap`, log: boolean = true) {
Expand Down
65 changes: 48 additions & 17 deletions lib/util/statsd.ts
Expand Up @@ -29,6 +29,16 @@ import {
} from "../spi/message/MessageClient";
import { logger } from "./logger";

const GcTypes = {
0: "unknown",
1: "scavenge",
2: "mark_sweep_compact",
3: "scavenge_and_mark_sweep_compact",
4: "incremental_marking",
8: "weak_phantom",
15: "all",
};

export class StatsdAutomationEventListener extends AutomationEventListenerSupport {

private statsd: StatsD;
Expand All @@ -40,6 +50,7 @@ export class StatsdAutomationEventListener extends AutomationEventListenerSuppor
super();
this.registrationName = `${this.configuration.name}/${this.configuration.version}`;
this.initStatsd();
this.initGc();
}

public registrationSuccessful(handler: RequestProcessor) {
Expand Down Expand Up @@ -118,7 +129,6 @@ export class StatsdAutomationEventListener extends AutomationEventListenerSuppor
const tags = [
`atomist_operation:${payload.name}`,
`atomist_operation_type:command`,
...this.teamDetail(ctx),
];
this.increment("counter.operation.failure", tags);
this.timing("timer.operation", ctx, tags);
Expand All @@ -139,7 +149,6 @@ export class StatsdAutomationEventListener extends AutomationEventListenerSuppor
const tags = [
`atomist_operation:${payload.extensions.operationName}`,
`atomist_operation_type:event`,
...this.teamDetail(ctx),
];
this.increment("counter.operation.failure", tags);
this.timing("timer.operation", ctx, tags);
Expand Down Expand Up @@ -167,7 +176,6 @@ export class StatsdAutomationEventListener extends AutomationEventListenerSuppor
});
this.increment("counter.message", [
`atomist_message_type:${type}`,
...this.teamDetail(ctx),
]);
return Promise.resolve();
}
Expand Down Expand Up @@ -238,20 +246,6 @@ export class StatsdAutomationEventListener extends AutomationEventListenerSuppor
(this.configuration.statsd as any).__instance = this.statsd;
}

private teamDetail(ctx: HandlerContext): string[] {
if (ctx && (ctx as any as AutomationContextAware).context) {
const context = (ctx as any as AutomationContextAware).context;
const safeTeamName = context.workspaceName ?
context.workspaceName.trim().replace(/ /g, "_").replace(/\W/g, "") : undefined;
return [
`atomist_team_id:${context.workspaceId}`,
`atomist_team_name:${safeTeamName}`,
];
} else {
return [];
}
}

private submitHeapStats() {
const heap = process.memoryUsage();
this.statsd.gauge("heap.rss", heap.rss, 1, [], this.callback);
Expand All @@ -269,4 +263,41 @@ export class StatsdAutomationEventListener extends AutomationEventListenerSuppor
this.callback);
}
}

private initGc() {
try {
const gc = require("gc-stats");
gc().on('stats', stats => {
const gcType = GcTypes[stats.gctype];

const tags = [
`atomist_gc_type:${gcType}`,
];

this.statsd.increment(
"gc",
1,
1,
tags,
this.callback);
this.statsd.timing(
"gc.duration",
stats.pause / 1e9 * 1000,
1,
tags,
this.callback);

if (stats.diff.usedHeapSize < 0) {
this.statsd.gauge(
"gc.head.reclaimed",
stats.diff.usedHeapSize * -1,
1,
tags,
this.callback);
}
});
} catch (err) {
// Ignore missing gc-stats package
}
}
}

0 comments on commit 7639432

Please sign in to comment.