Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit afac87d

Browse files
committed
update metrics to latest format, specify histogram buckets
1 parent e6e07e0 commit afac87d

File tree

3 files changed

+104
-77
lines changed

3 files changed

+104
-77
lines changed

lib/optimistic-bundler.js

Lines changed: 99 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,6 @@ const Storage = require('./storage');
99
const schemas = require('../lib/schemas');
1010
const { hashContent } = require('./hasher');
1111

12-
const PERSIST_TO_STORAGE_METRIC = {
13-
name: 'asset_server_persist_to_storage_timer',
14-
description: 'Time taken for a persist operation to storage',
15-
};
16-
17-
const RETRIEVE_FROM_STORAGE_METRIC = {
18-
name: 'asset_server_retrieve_from_storage_timer',
19-
description: 'Time taken for a retrieve operation from storage',
20-
};
21-
22-
const EXISTS_IN_STORAGE_METRIC = {
23-
name: 'asset_server_exists_in_storage_timer',
24-
description: 'Time taken for a check for existence operation from storage',
25-
};
26-
2712
module.exports = class OptimisticBundler extends Bundler {
2813
constructor({ bundleInProcess, ...options }) {
2914
super({ bundleInProcess });
@@ -38,6 +23,63 @@ module.exports = class OptimisticBundler extends Bundler {
3823
this.log = abslog(options.logger);
3924
this.options = opts;
4025
this.overrides = {};
26+
27+
this.bundleSizeGaugeMetric = this.metrics.gauge({
28+
name: 'asset_server_bundle_size_gauge',
29+
description: 'Measures the size of bundles created',
30+
});
31+
this.bundlingTimerMetric = this.metrics.histogram({
32+
name: 'asset_server_bundling_timer',
33+
description: 'Time taken to bundle assets',
34+
buckets: [1, 5, 10, 15, 20, 30, 60, 120],
35+
});
36+
this.retrieveFromStorageTimerMetric = this.metrics.histogram({
37+
name: 'asset_server_retrieve_from_storage_timer',
38+
description: 'Time taken for a retrieve operation from storage',
39+
buckets: [1, 5, 10, 15, 20, 30, 60, 120],
40+
});
41+
this.existsInStorageTimerMetric = this.metrics.histogram({
42+
name: 'asset_server_exists_in_storage_timer',
43+
description:
44+
'Time taken for a check for existence operation from storage',
45+
buckets: [1, 5, 10, 15, 20, 30, 60, 120],
46+
});
47+
this.persistToStorageTimerMetric = this.metrics.histogram({
48+
name: 'asset_server_persist_to_storage_timer',
49+
description: 'Time taken for a persist operation to storage',
50+
buckets: [1, 5, 10, 15, 20, 30, 60, 120],
51+
});
52+
this.publishAssetsTimerMetric = this.metrics.histogram({
53+
name: 'asset_server_publish_assets_timer',
54+
description:
55+
'Time taken for a publish assets operation to complete',
56+
buckets: [1, 5, 10, 15, 20, 30, 60, 120],
57+
});
58+
this.saveFeedTimerMetric = this.metrics.histogram({
59+
name: 'asset_server_save_feed_timer',
60+
description: 'Time taken for a feed to be saved to storage',
61+
buckets: [1, 5, 10, 15, 20, 30, 60, 120],
62+
});
63+
this.fallbackBundleSizeGaugeMetric = this.metrics.gauge({
64+
name: 'asset_server_fallback_bundle_size_gauge',
65+
description: 'Measures the size of fallback bundles created',
66+
});
67+
this.saveFallbackBundleTimerMetric = this.metrics.histogram({
68+
name: 'asset_server_save_fallback_bundle_timer',
69+
description:
70+
'Time taken for a fallback bundle to be bundled and saved',
71+
buckets: [1, 5, 10, 15, 20, 30, 60, 120],
72+
});
73+
this.publishInstructionsTimerMetric = this.metrics.histogram({
74+
name: 'asset_server_publish_instructions_timer',
75+
description: 'Time taken for publishInstructions() to complete',
76+
buckets: [1, 5, 10, 15, 20, 30, 60, 120],
77+
});
78+
this.rebundleInstructionsTimerMetric = this.metrics.histogram({
79+
name: 'asset_server_rebundle_instructions_timer',
80+
description: 'Time taken to rebundle all instructions for a tag',
81+
buckets: [1, 5, 10, 15, 20, 30, 60, 120],
82+
});
4183
}
4284

4385
async assetsPublished(tags, hashes, type) {
@@ -55,85 +97,85 @@ module.exports = class OptimisticBundler extends Bundler {
5597
}
5698

5799
async getTags(tags, type) {
58-
const end = this.metrics.timer(RETRIEVE_FROM_STORAGE_METRIC);
100+
const end = this.retrieveFromStorageTimerMetric.timer();
59101

60102
const storageTags = await this.storage.getTags(tags, type);
61103

62-
end({ meta: { method: 'getTags' } });
104+
end({ labels: { method: 'getTags' } });
63105

64106
return storageTags;
65107
}
66108

67109
async getFeed(hash) {
68-
const end = this.metrics.timer(RETRIEVE_FROM_STORAGE_METRIC);
110+
const end = this.retrieveFromStorageTimerMetric.timer();
69111

70112
const feed = await this.storage.getFeed(hash);
71113

72-
end({ meta: { method: 'getFeed' } });
114+
end({ labels: { method: 'getFeed' } });
73115

74116
return feed;
75117
}
76118

77119
async hasFeed(hash) {
78-
const end = this.metrics.timer(EXISTS_IN_STORAGE_METRIC);
120+
const end = this.existsInStorageTimerMetric.timer();
79121

80122
const hasFeed = await this.storage.hasFeed(hash);
81123

82-
end({ meta: { method: 'hasFeed' } });
124+
end({ labels: { method: 'hasFeed' } });
83125

84126
return hasFeed;
85127
}
86128

87129
async hasBundle(hash, type) {
88-
const end = this.metrics.timer(EXISTS_IN_STORAGE_METRIC);
130+
const end = this.existsInStorageTimerMetric.timer();
89131

90132
const hasBundle = await this.storage.hasBundle(hash, type);
91133

92-
end({ meta: { method: 'hasBundle' } });
134+
end({ labels: { method: 'hasBundle' } });
93135

94136
return hasBundle;
95137
}
96138

97139
async setBundle(hash, type, content) {
98-
const end = this.metrics.timer(PERSIST_TO_STORAGE_METRIC);
140+
const end = this.persistToStorageTimerMetric.timer();
99141

100142
await this.storage.setBundle(hash, type, content);
101143

102-
end({ meta: { method: 'setBundle' } });
144+
end({ labels: { method: 'setBundle' } });
103145
}
104146

105147
async getInstructions(tag, type) {
106-
const end = this.metrics.timer(RETRIEVE_FROM_STORAGE_METRIC);
148+
const end = this.retrieveFromStorageTimerMetric.timer();
107149

108150
const instructions = await this.storage.getInstructions(tag, type);
109151

110-
end({ meta: { method: 'getInstructions' } });
152+
end({ labels: { method: 'getInstructions' } });
111153

112154
return instructions;
113155
}
114156

115157
async setInstruction(tag, type, instruction) {
116-
const end = this.metrics.timer(PERSIST_TO_STORAGE_METRIC);
158+
const end = this.persistToStorageTimerMetric.timer();
117159

118160
await this.storage.setInstruction(tag, type, instruction);
119161

120-
end({ meta: { method: 'setInstruction' } });
162+
end({ labels: { method: 'setInstruction' } });
121163
}
122164

123165
async setFeed(feedHash, assetFeed) {
124-
const end = this.metrics.timer(PERSIST_TO_STORAGE_METRIC);
166+
const end = this.persistToStorageTimerMetric.timer();
125167

126168
await this.storage.setFeed(feedHash, assetFeed);
127169

128-
end({ meta: { method: 'setFeed' } });
170+
end({ labels: { method: 'setFeed' } });
129171
}
130172

131173
async setTag(tag, assetType, feedHash) {
132-
const end = this.metrics.timer(PERSIST_TO_STORAGE_METRIC);
174+
const end = this.persistToStorageTimerMetric.timer();
133175

134176
await this.storage.setTag(tag, assetType, feedHash);
135177

136-
end({ meta: { method: 'setTag' } });
178+
end({ labels: { method: 'setTag' } });
137179
}
138180

139181
async bundleExists(tags, hashes, type) {
@@ -164,11 +206,9 @@ module.exports = class OptimisticBundler extends Bundler {
164206
async bundle(tag, tags, hashes, type) {
165207
const feeds = await this.getFeeds(hashes);
166208

167-
const end = this.metrics.timer({
168-
name: 'asset_server_bundling_timer',
169-
description: 'Time taken to bundle assets',
170-
meta: {
171-
sources: tags,
209+
const end = this.bundlingTimerMetric.timer({
210+
labels: {
211+
sources: JSON.stringify(tags),
172212
assetType: type,
173213
publisher: tag,
174214
},
@@ -182,12 +222,9 @@ module.exports = class OptimisticBundler extends Bundler {
182222

183223
end();
184224

185-
this.metrics.metric({
186-
name: 'asset_server_bundle_size_gauge',
187-
description: 'Measures the size of bundles created',
188-
value: Buffer.byteLength(content, 'utf8'),
189-
meta: {
190-
sources: tags,
225+
this.bundleSizeGaugeMetric.set(Buffer.byteLength(content, 'utf8'), {
226+
labels: {
227+
sources: JSON.stringify(tags),
191228
assetType: type,
192229
publisher: tag,
193230
},
@@ -242,10 +279,8 @@ module.exports = class OptimisticBundler extends Bundler {
242279
}
243280

244281
async rebundle(tag, type) {
245-
const end = this.metrics.timer({
246-
name: 'asset_server_rebundle_instructions_timer',
247-
description: 'Time taken to rebundle all instructions for a tag',
248-
meta: {
282+
const end = this.rebundleInstructionsTimerMetric.timer({
283+
labels: {
249284
assetType: type,
250285
publisher: tag,
251286
},
@@ -266,10 +301,8 @@ module.exports = class OptimisticBundler extends Bundler {
266301
`Invalid 'instruction' object given when attempting to publish instructions.`,
267302
);
268303

269-
const end = this.metrics.timer({
270-
name: 'asset_server_publish_instructions_timer',
271-
description: 'Time taken for publishInstructions() to complete',
272-
meta: {
304+
const end = this.publishAssetsTimerMetric.timer({
305+
labels: {
273306
assetType: type,
274307
publisher: tag,
275308
},
@@ -291,11 +324,8 @@ module.exports = class OptimisticBundler extends Bundler {
291324
}
292325

293326
async saveFallbackBundle(tag, hash, type, feed) {
294-
const end = this.metrics.timer({
295-
name: 'asset_server_save_fallback_bundle_timer',
296-
description:
297-
'Time taken for a fallback bundle to be bundled and saved',
298-
meta: {
327+
const end = this.saveFallbackBundleTimerMetric.timer({
328+
labels: {
299329
assetType: type,
300330
publisher: tag,
301331
},
@@ -309,15 +339,15 @@ module.exports = class OptimisticBundler extends Bundler {
309339
const bundle = await super.bundleFeeds([feed], type);
310340
await this.setBundle(hash, type, bundle);
311341

312-
this.metrics.metric({
313-
name: 'asset_server_fallback_bundle_size_gauge',
314-
description: 'Measures the size of fallback bundles created',
315-
value: Buffer.byteLength(bundle, 'utf8'),
316-
meta: {
317-
assetType: type,
318-
publisher: tag,
342+
this.fallbackBundleSizeGaugeMetric.set(
343+
Buffer.byteLength(bundle, 'utf8'),
344+
{
345+
labels: {
346+
assetType: type,
347+
publisher: tag,
348+
},
319349
},
320-
});
350+
);
321351

322352
this.log.info(
323353
`${type} fallback bundle for tag "${tag}" published as "${hash}.${type}"`,
@@ -328,10 +358,8 @@ module.exports = class OptimisticBundler extends Bundler {
328358
}
329359

330360
async saveFeed(tag, hash, type, feed) {
331-
const end = this.metrics.timer({
332-
name: 'asset_server_save_feed_timer',
333-
description: 'Time taken for a feed to be saved to storage',
334-
meta: {
361+
const end = this.saveFeedTimerMetric.timer({
362+
labels: {
335363
assetType: type,
336364
publisher: tag,
337365
},
@@ -358,11 +386,8 @@ module.exports = class OptimisticBundler extends Bundler {
358386
`Invalid 'assets' object given when attempting to publish assets.`,
359387
);
360388

361-
const end = this.metrics.timer({
362-
name: 'asset_server_publish_assets_timer',
363-
description:
364-
'Time taken for a publish assets operation to complete',
365-
meta: {
389+
const end = this.publishAssetsTimerMetric.timer({
390+
labels: {
366391
assetType: type,
367392
publisher: tag,
368393
},

test/main-optimistic.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,6 @@ describe('publishing and bundling js feeds', () => {
432432
'Time taken for a check for existence operation from storage',
433433
);
434434
expect(obj.type).toEqual(5);
435-
expect(obj.meta).toEqual({ method: 'hasFeed' });
435+
expect(obj.meta.buckets).toEqual([1, 5, 10, 15, 20, 30, 60, 120]);
436436
});
437437
});

test/unit/optimistic.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ test('bundle()', async () => {
8080
'Time taken for a retrieve operation from storage',
8181
);
8282
expect(obj.type).toEqual(5);
83-
expect(obj.meta).toEqual({ method: 'getFeed' });
83+
expect(obj.labels).toEqual([{ name: 'method', value: 'getFeed' }]);
84+
expect(obj.meta).toEqual({ buckets: [1, 5, 10, 15, 20, 30, 60, 120] });
8485
});
8586

8687
test('bundleIfNeeded() - produces new bundle', async () => {
@@ -130,7 +131,8 @@ test('bundleIfNeeded() - produces metrics', async () => {
130131
'Time taken for a retrieve operation from storage',
131132
);
132133
expect(obj.type).toEqual(5);
133-
expect(obj.meta).toEqual({ method: 'getTags' });
134+
expect(obj.labels).toEqual([{ name: 'method', value: 'getTags' }]);
135+
expect(obj.meta).toEqual({ buckets: [1, 5, 10, 15, 20, 30, 60, 120] });
134136
});
135137

136138
test('bundleIfNeeded() - does not need to produce bundle', async () => {

0 commit comments

Comments
 (0)