Skip to content

Commit

Permalink
feat(datadog): add option test
Browse files Browse the repository at this point in the history
A regex to match assets against.
  • Loading branch information
brandondoran committed Nov 1, 2018
1 parent 11f22ed commit abd6279
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ const { StatsPlugin, DataDogStatsReporter } = require('stats-webpack-plugin');
module.exports = {
plugins: [
new StatsReporterPlugin({
test:
reporter: new DataDogStatsReporter({
apiKey: process.env.DD_API_KEY,
metricName: 'my-app.assets',
tags: ['app:my-app', 'env:production']
tags: ['app:my-app', 'env:production'],
test: /(js|css)$/
})
})
]
Expand All @@ -31,20 +33,25 @@ module.exports = {
### StatsReporterPlugin

```js
new StatsReporterPlugin((reporter: DataDogStatsReporter));
new StatsReporterPlugin(options: StatsReporterPluginOptions);
```

### Reporters
##### DataDogStatsReporterOptions fields

- `reporter: StatsReporter`: The reporter to use for sending stats.

### Stats Reporters

#### DataDogStatsReporter

```js
new DataDogStatsReporter((options: DataDogStatsReporterOptions));
new DataDogStatsReporter(options: DataDogStatsReporterOptions);
```

##### DataDogStatsReporterOptions fields

- `apiKey: string`: Your DataDog API key
- `gzipSize?: boolean = true`: Report gzipped size if true, uncompressed size if false
- `metricName: string`: The base name for the metric
- `tags?: string[]`: Custom tags for the metric.
- `tags?: string[]`: Custom tags for the metric. A `chunk` tag is always added.
- `test?: RegExp`: Test to match files against. If not set, stats for all emitted assets will be sent.
37 changes: 24 additions & 13 deletions src/reporters/datadog.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import * as gzipSize from 'gzip-size';
import * as path from 'path';
import { StatsReporter } from '../types';

const API_URL = 'https://app.datadoghq.com/api/v1/series';
const METRIC_TYPE_GAUGE = 'gauge';
Expand All @@ -13,21 +14,24 @@ export interface DataDogStatsReporterOptions {
apiKey: string;
gzipSize: boolean;
metricName: string;
tags?: string[]
tags?: string[];
test?: RegExp;
}

export class DataDogStatsReporter {
export class DataDogStatsReporter implements StatsReporter {
private readonly apiKey: string;
private readonly gzipSize: boolean;
private readonly metricName: string;
private readonly tags: string[];
private readonly test?: RegExp;
private readonly url: string;

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

this.validateOptions();
Expand All @@ -36,7 +40,7 @@ export class DataDogStatsReporter {
public async send(stats: any) {
try {
const series = await this.parseStats(stats);
// console.log(require('util').inspect(series, {showHidden: false, depth: null}));
// console.log(require('util').inspect(series, { showHidden: false, depth: null }));
return await axios({
data: { series },
headers,
Expand All @@ -48,20 +52,27 @@ export class DataDogStatsReporter {
}
}

private filterAssets(stats: any) {
const { assets } = stats;
if (!this.test) {
return assets;
}
return assets.filter((asset: any) => this.test!.test(asset.name));
}

private parseStats(stats: any) {
const { assets, outputPath } = stats;
const { outputPath } = stats;
const assets = this.filterAssets(stats);
const now = getTimestamp();
const promises = assets.map(async (asset: any) => {
const size = this.gzipSize === false
? asset.size
: await gzipSize.file(path.join(outputPath, asset.name));
const size =
this.gzipSize === false
? asset.size
: await gzipSize.file(path.join(outputPath, asset.name));
return {
metric: `${this.metricName}.bytes${path.extname(asset.name)}`,
points: [[ now, size ]],
tags: [
...this.tags,
`chunk:${asset.chunkNames[0]}`
],
points: [[now, size]],
tags: [...this.tags, `chunk:${asset.chunkNames[0]}`],
type: METRIC_TYPE_GAUGE
};
});
Expand Down
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface Reporter {
export interface StatsReporter {
send: (stats: any) => Promise<any>;
}

export interface StatsReporterPluginOptions {
reporter: Reporter;
};
reporter: StatsReporter;
}

0 comments on commit abd6279

Please sign in to comment.