Skip to content

Commit

Permalink
feat(datadog): add tags option
Browse files Browse the repository at this point in the history
  • Loading branch information
brandondoran committed Oct 31, 2018
1 parent 390cbc1 commit d8d494b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion examples/react/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ module.exports = {
new StatsReporterPlugin({
reporter: new DataDogStatsReporter({
apiKey: process.env.DATADOG_API_KEY,
metricName: "test-app.assets"
metricName: "test-app.assets",
tags: ["env:production", "framework:react"]
})
})
]
Expand Down
13 changes: 11 additions & 2 deletions src/reporters/datadog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ const getTimestamp = () => parseInt(`${new Date().getTime() / 1000}`, 10);
export interface DataDogStatsReporterOptions {
apiKey: string;
metricName: string;
tags?: string[]
}

export class DataDogStatsReporter {
private readonly apiKey: string;
private readonly metricName: string;
private readonly tags: string[];
private readonly url: string;

constructor(options: DataDogStatsReporterOptions) {
this.apiKey = options.apiKey;
this.metricName = options.metricName;
this.tags = options.tags || [];
this.url = `${API_URL}?api_key=${this.apiKey}`;

this.validateOptions();
Expand All @@ -48,9 +51,12 @@ export class DataDogStatsReporter {
const promises = assets.map(async (asset: any) => {
const size = await gzipSize.file(path.join(outputPath, asset.name));
return {
metric: `${this.metricName}.bytes.${path.extname(asset.name)}`,
metric: `${this.metricName}.bytes${path.extname(asset.name)}`,
points: [[ now, size ]],
tags: [`chunk:${asset.chunkNames[0]}`],
tags: [
...this.tags,
`chunk:${asset.chunkNames[0]}`
],
type: METRIC_TYPE_GAUGE
};
});
Expand All @@ -65,6 +71,9 @@ export class DataDogStatsReporter {
if (!this.metricName) {
errors.push('metricName is required.');
}
if (!Array.isArray(this.tags)) {
errors.push('tags must be an array of strings.');
}
if (errors.length !== 0) {
throw new Error(`DataDogStatsReporter: ${errors.join(' ')}`);
}
Expand Down

0 comments on commit d8d494b

Please sign in to comment.