-
Notifications
You must be signed in to change notification settings - Fork 651
Expand file tree
/
Copy pathheapSpacesSizeAndUsedCollector.ts
More file actions
117 lines (102 loc) · 3.41 KB
/
heapSpacesSizeAndUsedCollector.ts
File metadata and controls
117 lines (102 loc) · 3.41 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as v8 from 'node:v8';
import type { HeapSpaceInfo } from 'v8';
import { Meter } from '@opentelemetry/api';
import { BaseCollector } from './baseCollector';
import {
ATTR_V8JS_HEAP_SPACE_NAME,
METRIC_V8JS_MEMORY_HEAP_LIMIT,
METRIC_V8JS_MEMORY_HEAP_MAX,
METRIC_V8JS_MEMORY_HEAP_USED,
METRIC_V8JS_MEMORY_HEAP_SPACE_AVAILABLE_SIZE,
METRIC_V8JS_MEMORY_HEAP_SPACE_PHYSICAL_SIZE,
} from '../semconv';
export class HeapSpacesSizeAndUsedCollector extends BaseCollector {
updateMetricInstruments(meter: Meter): void {
const heapLimit = meter.createObservableGauge(
METRIC_V8JS_MEMORY_HEAP_LIMIT,
{
description: 'Total heap memory size pre-allocated.',
unit: 'By',
}
);
const heapSpaceUsed = meter.createObservableGauge(
METRIC_V8JS_MEMORY_HEAP_USED,
{
description: 'Heap Memory size allocated.',
unit: 'By',
}
);
const heapSpaceAvailable = meter.createObservableGauge(
METRIC_V8JS_MEMORY_HEAP_SPACE_AVAILABLE_SIZE,
{
description: 'Heap space available size.',
unit: 'By',
}
);
const heapSpacePhysical = meter.createObservableGauge(
METRIC_V8JS_MEMORY_HEAP_SPACE_PHYSICAL_SIZE,
{
description: 'Committed size of a heap space.',
unit: 'By',
}
);
const heapMax = meter.createObservableGauge(METRIC_V8JS_MEMORY_HEAP_MAX, {
description:
'Maximum heap size allowed by the V8 engine, as set by --max-old-space-size or V8 defaults.',
unit: 'By',
});
meter.addBatchObservableCallback(
observableResult => {
if (!this._config.enabled) return;
const heapStats = v8.getHeapStatistics();
observableResult.observe(heapMax, heapStats.heap_size_limit);
const data = this.scrape();
if (data === undefined) return;
for (const space of data) {
const spaceName = space.space_name;
observableResult.observe(heapLimit, space.space_size, {
[ATTR_V8JS_HEAP_SPACE_NAME]: spaceName,
});
observableResult.observe(heapSpaceUsed, space.space_used_size, {
[ATTR_V8JS_HEAP_SPACE_NAME]: spaceName,
});
observableResult.observe(
heapSpaceAvailable,
space.space_available_size,
{
[ATTR_V8JS_HEAP_SPACE_NAME]: spaceName,
}
);
observableResult.observe(
heapSpacePhysical,
space.physical_space_size,
{
[ATTR_V8JS_HEAP_SPACE_NAME]: spaceName,
}
);
}
},
[heapMax, heapLimit, heapSpaceUsed, heapSpaceAvailable, heapSpacePhysical]
);
}
internalEnable(): void {}
internalDisable(): void {}
private scrape(): HeapSpaceInfo[] {
return v8.getHeapSpaceStatistics();
}
}