Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Native support for Datadog metrics #505

Merged
merged 3 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 83 additions & 4 deletions packages/core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"async-retry": "^1.3.1",
"bottleneck": "^2.19.5",
"cli-table": "^0.3.6",
"datadog-metrics": "^0.9.3",
"fs-extra": "^9.1.0",
"glob": "^7.1.6",
"hot-shots": "^8.3.0",
Expand All @@ -35,6 +36,7 @@
"xml2js": "^0.4.23"
},
"devDependencies": {
"@types/datadog-metrics": "^0.6.1",
"@types/async-retry": "^1.4.2",
"@types/jest": "^26.0.19",
"jest": "^26.6.3",
Expand Down
142 changes: 106 additions & 36 deletions packages/core/src/utils/SFPStatsSender.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import StatsDClient, { ClientOptions, StatsD } from "hot-shots";
import * as fs from "fs-extra";
import { EOL } from "os";
import { BufferedMetricsLogger } from "datadog-metrics";

export default class SFPStatsSender {
private static client: StatsD;
private static metricsLogger;
private static nativeDataDogMetricsLogger: BufferedMetricsLogger;

static initialize(
port: string,
host: string,
protocol: string
) {
static initialize(port: string, host: string, protocol: string) {
let options: ClientOptions = {
host: host,
port: port == null ? 8125 : Number(port),
Expand All @@ -20,73 +18,145 @@ export default class SFPStatsSender {
SFPStatsSender.client = new StatsDClient(options);
}

static initializeLogBasedMetrics()
{
try
{
fs.mkdirpSync(".sfpowerscripts/logs");
SFPStatsSender.metricsLogger = `.sfpowerscripts/logs/metrics.log`;
}
catch(error)
{
console.log("Unable to initiate Log based metrics",error);
static initializeLogBasedMetrics() {
try {
fs.mkdirpSync(".sfpowerscripts/logs");
SFPStatsSender.metricsLogger = `.sfpowerscripts/logs/metrics.log`;
} catch (error) {
console.log("Unable to initiate Log based metrics", error);
}
}

static initializeNativeDataDogMetrics(apiHost: string, apiKey: string) {
try {
SFPStatsSender.nativeDataDogMetricsLogger = new BufferedMetricsLogger({
apiHost: apiHost,
apiKey: apiKey,
prefix: "sfpowerscripts.",
flushIntervalSeconds: 0,
});
} catch (error) {
console.log("Unable to intialize native datadog logger", error);
}
}

static logElapsedTime(metric: string, elapsedMilliSeconds: number, tags?: { [key: string]: string } | string[]) {


static logElapsedTime(
metric: string,
elapsedMilliSeconds: number,
tags?: { [key: string]: string } | string[]
) {
if (SFPStatsSender.client != null)
SFPStatsSender.client.timing(metric, elapsedMilliSeconds, tags);

//Native Datadog integration
if (SFPStatsSender.nativeDataDogMetricsLogger != null) {
SFPStatsSender.sendDataDogGaugeMetric(metric, elapsedMilliSeconds, tags);
}

let metrics = {
metric: `sfpowerscripts.${metric}`,
type: `timers`,
value: elapsedMilliSeconds,
timestamp:Date.now(),
tags: tags
}
SFPStatsSender.logMetrics(metrics,SFPStatsSender.metricsLogger);
timestamp: Date.now(),
tags: tags,
};
SFPStatsSender.logMetrics(metrics, SFPStatsSender.metricsLogger);
}

static logGauge(metric: string, value: number, tags?: { [key: string]: string } | string[]) {


static logGauge(
metric: string,
value: number,
tags?: { [key: string]: string } | string[]
) {
if (SFPStatsSender.client != null)
SFPStatsSender.client.gauge(metric, value, tags);

//Native Datadog integration
if (SFPStatsSender.nativeDataDogMetricsLogger != null) {
SFPStatsSender.sendDataDogGaugeMetric(metric, value, tags);
}

let metrics = {
metric: `sfpowerscripts.${metric}`,
type: `guage`,
value: value,
timestamp:Date.now(),
tags: tags
}
SFPStatsSender.logMetrics(metrics,SFPStatsSender.metricsLogger);
timestamp: Date.now(),
tags: tags,
};
SFPStatsSender.logMetrics(metrics, SFPStatsSender.metricsLogger);
}


static logCount(metric: string, tags?: { [key: string]: string } | string[]) {

if (SFPStatsSender.client != null)
SFPStatsSender.client.increment(metric, tags)
SFPStatsSender.client.increment(metric, tags);

//Native Datadog integration
if (SFPStatsSender.nativeDataDogMetricsLogger != null) {
SFPStatsSender.sendDataDogCountMetric(metric, tags);
}

let metrics = {
metric: `sfpowerscripts.${metric}`,
type: `count`,
timestamp:Date.now(),
tags: tags
timestamp: Date.now(),
tags: tags,
};
SFPStatsSender.logMetrics(metrics, SFPStatsSender.metricsLogger);
}

private static sendDataDogGaugeMetric(
metric: string,
value: number,
tags: string[] | { [key: string]: string }
) {
try {
let transformedTags = SFPStatsSender.transformTagsToStringArray(tags);
SFPStatsSender.nativeDataDogMetricsLogger.gauge(
metric,
value,
transformedTags
);
SFPStatsSender.nativeDataDogMetricsLogger.flush();
} catch (error) {
console.log("Unable to transmit metrics for metric", metric);
}
SFPStatsSender.logMetrics(metrics,SFPStatsSender.metricsLogger);
}


private static sendDataDogCountMetric(
metric: string,
tags: string[] | { [key: string]: string }
) {
try {
let transformedTags = SFPStatsSender.transformTagsToStringArray(tags);
SFPStatsSender.nativeDataDogMetricsLogger.increment(
metric,
1,
transformedTags
);
SFPStatsSender.nativeDataDogMetricsLogger.flush();
} catch (error) {
console.log("Unable to transmit metrics for metric", metric);
}
}

static logMetrics(key: any, logger?:any) {
static logMetrics(key: any, logger?: any) {
if (logger) {
fs.appendFileSync(logger, `${JSON.stringify(key)}${EOL}`, 'utf8')
fs.appendFileSync(logger, `${JSON.stringify(key)}${EOL}`, "utf8");
}
}

private static transformTagsToStringArray(
tags: { [key: string]: string } | string[]
): string[] {
if (tags != null && !Array.isArray(tags)) {
let transformedTagArray: string[] = new Array();
for (const [key, value] of Object.entries(tags)) {
transformedTagArray.push(`${key}:${value}`);
}
return transformedTagArray;
}
return tags as string[];
}
}
5 changes: 5 additions & 0 deletions packages/sfpowerscripts-cli/src/SfpowerscriptsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ export default abstract class SfpowerscriptsCommand extends SfdxCommand {
{
SFPStatsSender.initialize(process.env.SFPOWERSCRIPTS_STATSD_PORT,process.env.SFPOWERSCRIPTS_STATSD_HOST,process.env.SFPOWERSCRIPTS_STATSD_PROTOCOL);
}
if(process.env.SFPOWERSCRIPTS_DATADOG)
{
SFPStatsSender.initializeNativeDataDogMetrics(process.env.SFPOWERSCRIPTS_DATADOG_HOST,process.env.SFPOWERSCRIPTS_DATADOG_API_KEY);
}

SFPStatsSender.initializeLogBasedMetrics();
}
}