-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
85 lines (76 loc) · 2.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
const logger = require('@dotcom-reliability-kit/logger');
const silence = Boolean(process.env.NEXT_METRICS_SILENCE_WARNINGS);
/**
* @template T
* @param {string} signature
* @param {T} returnValue
* @returns {() => T}
*/
function buildDeprecationLogger (signature, returnValue) {
return () => {
if (!silence) {
const error = new Error(`${signature} no longer does anything. Please remove it from your code`);
logger.warn(error, {
event: 'NEXT_METRICS_DEPRECATION',
message: `Call to ${signature} found`
});
}
return returnValue;
};
}
/**
* @param {function} classFn
* @param {string} methodName
* @param {any} returnValue
*/
function addDeprecatedMethod (classFn, methodName, returnValue = undefined) {
classFn.prototype[methodName] = buildDeprecationLogger(`${classFn.name}#${methodName}()`, returnValue);
}
/**
* @param {function} classFn
* @param {string} propertyName
* @param {any} value
*/
function addDeprecatedProperty (classFn, propertyName, value) {
Object.defineProperty(classFn.prototype, propertyName, {
get: buildDeprecationLogger(`${classFn.name}#${propertyName}[get]`, value),
set: buildDeprecationLogger(`${classFn.name}#${propertyName}[set]`, value)
});
}
// Create a fetcher that matches the old API but doesn't actually
// do anything. We only instrument methods we know are used
class Fetcher {}
addDeprecatedMethod(Fetcher, 'instrument');
// Create a metrics client that matches the old API but doesn't
// actually do anything. We only instrument methods we know are used
class Metrics {}
addDeprecatedProperty(Metrics, 'opts', {});
addDeprecatedProperty(Metrics, 'graphites', []);
addDeprecatedProperty(Metrics, 'aggregators', []);
addDeprecatedProperty(Metrics, 'customCounters', {});
addDeprecatedProperty(Metrics, 'customHistograms', {});
addDeprecatedProperty(Metrics, 'hasValidConfiguration', false);
addDeprecatedProperty(Metrics, 'services', {});
addDeprecatedProperty(Metrics, 'fetch', new Fetcher());
addDeprecatedMethod(Metrics, 'init');
addDeprecatedMethod(Metrics, 'count');
addDeprecatedMethod(Metrics, 'histogram');
addDeprecatedMethod(Metrics, 'flushCustomCounters');
addDeprecatedMethod(Metrics, 'flushCustomHistograms');
addDeprecatedMethod(Metrics, 'flushRate');
addDeprecatedMethod(Metrics, 'setupDefaultAggregators');
addDeprecatedMethod(Metrics, 'setupCustomAggregators');
addDeprecatedMethod(Metrics, 'flush');
addDeprecatedMethod(Metrics, 'instrument');
addDeprecatedMethod(Metrics, 'registerAggregator');
// Create a graphite client that matches the old API but doesn't
// actually do anything. We only instrument methods we know are used
class GraphiteClient {}
addDeprecatedProperty(GraphiteClient, 'destination', { port: 137, host: 'localhost' });
addDeprecatedProperty(GraphiteClient, 'prefix', 'deprecated');
addDeprecatedProperty(GraphiteClient, 'noLog', false);
addDeprecatedMethod(GraphiteClient, 'log');
module.exports = new Metrics();
module.exports.Metrics = Metrics;
module.exports.GraphiteClient = GraphiteClient;