Permalink
Browse files

Add gc stats onto statsd

[changelog:added]
  • Loading branch information...
cdupuis committed Nov 15, 2018
1 parent f008f35 commit 7639432a321e8add351f24539b8a5c0b6434d9d7
Showing with 603 additions and 90 deletions.
  1. +1 −1 lib/internal/util/memory.ts
  2. +48 −17 lib/util/statsd.ts
  3. +553 −72 package-lock.json
  4. +1 −0 package.json
@@ -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) {
@@ -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;
@@ -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) {
@@ -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);
@@ -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);
@@ -167,7 +176,6 @@ export class StatsdAutomationEventListener extends AutomationEventListenerSuppor
});
this.increment("counter.message", [
`atomist_message_type:${type}`,
...this.teamDetail(ctx),
]);
return Promise.resolve();
}
@@ -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);
@@ -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
}
}
}
Oops, something went wrong.

0 comments on commit 7639432

Please sign in to comment.