-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
129 lines (108 loc) · 4.52 KB
/
app.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
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
118
119
120
121
122
123
124
125
126
127
128
129
'use strict';
const {DiagConsoleLogger, DiagLogLevel, ValueType, diag} = require('@opentelemetry/api');
const {OTLPMetricExporter} = require('@opentelemetry/exporter-metrics-otlp-proto');
const {
ExponentialHistogramAggregation, MeterProvider, PeriodicExportingMetricReader, View,
} = require('@opentelemetry/sdk-metrics');
const {Resource} = require('@opentelemetry/resources');
const {
SemanticResourceAttributes,
} = require('@opentelemetry/semantic-conventions');
const os = require('os');
const {v4: uuidv4} = require('uuid');
const metricPrefix = process.env.METRIC_PREFIX
const workflowIDCount = parseInt(process.env.WORKFLOW_ID_COUNT);
const customerIDCount = parseInt(process.env.CUSTOMER_ID_COUNT);
const otlpEndpoint = process.env.OTLP_ENDPOINT;
const serviceName = process.env.SERVICE_NAME;
const otelMetricExporterFrequency = parseInt(process.env.OTLP_METRIC_EXPORTER_FREQUENCY);
const otelMeterName = process.env.OTLP_METER_NAME;
const environment = process.env.ENVIRONMENT;
// Global variables
// Toggle this number below to explode cardinality.
const workflowIDs = Array.from({length: workflowIDCount}, () => uuidv4());
const customerIDs = Array.from({length: customerIDCount}, () => uuidv4());
// Optional and only needed to see the internal diagnostic logging (during development)
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
const metricExporter = new OTLPMetricExporter({
url: otlpEndpoint
});
// Create an instance of the metric provider
const meterProvider = new MeterProvider({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: serviceName,
})
});
meterProvider.addMetricReader(new PeriodicExportingMetricReader({
exporter: metricExporter, // exporter: new ConsoleMetricExporter(),
exportIntervalMillis: otelMetricExporterFrequency,
}));
const meter = meterProvider.getMeter(otelMeterName);
// Create Observable Gauges
const memoryUsageGauge = meter.createObservableGauge(`${metricPrefix}_memory_usage`, {
description: 'Tracks the memory usage of the application', valueType: ValueType.DOUBLE
});
const concurrencyGauge = meter.createObservableGauge(`${metricPrefix}_concurrency`, {
description: 'Current concurrency level', valueType: ValueType.INT
});
const cpuUsageGauge = meter.createObservableGauge(`${metricPrefix}_cpu_usage`, {
description: 'CPU usage percentage', valueType: ValueType.DOUBLE
});
const runTimeGauge = meter.createObservableGauge(`${metricPrefix}_run_time`, {
description: 'Run time in seconds', valueType: ValueType.INT
});
// Base Label Sets
const attributes = {pid: process.pid, environment: environment};
// Callbacks for Observable Gauges
concurrencyGauge.addCallback((observableResult) => {
for (let i = 0; i < workflowIDs.length; i++) {
const workflow_id = workflowIDs[i];
for (let j = 0; j < customerIDs.length; j++) {
const customer_id = customerIDs[j];
let concurrency = Math.floor(Math.random() * 10) + 1;
observableResult.observe(concurrency, {
...attributes, 'workflow_id': workflow_id, 'customer_id': customer_id
});
}
}
});
cpuUsageGauge.addCallback((observableResult) => {
for (let i = 0; i < workflowIDs.length; i++) {
const workflow_id = workflowIDs[i];
for (let j = 0; j < customerIDs.length; j++) {
const customer_id = customerIDs[j];
let cpuUsage = os.loadavg()[0]; // Load average for 1 minute; adjust as needed
observableResult.observe(cpuUsage, {
...attributes, 'workflow_id': workflow_id, 'customer_id': customer_id
});
}
}
});
runTimeGauge.addCallback((observableResult) => {
for (let i = 0; i < workflowIDs.length; i++) {
const workflow_id = workflowIDs[i];
for (let j = 0; j < customerIDs.length; j++) {
const customer_id = customerIDs[j];
let runTime = Math.floor(Math.random() * 60);
observableResult.observe(runTime, {
...attributes, 'workflow_id': workflow_id, 'customer_id': customer_id
});
}
}
});
memoryUsageGauge.addCallback((observableResult) => {
for (let i = 0; i < workflowIDs.length; i++) {
const workflow_id = workflowIDs[i];
for (let j = 0; j < customerIDs.length; j++) {
const customer_id = customerIDs[j];
let usedMemory = process.memoryUsage().heapUsed / 1024 / 1024; // Convert bytes to megabytes
observableResult.observe(usedMemory, {
...attributes, 'workflow_id': workflow_id, 'customer_id': customer_id, 'unit': 'MB'
});
}
}
});
// Periodically update global variables if needed
setInterval(() => {
console.log("Job Executed")
}, 15000); // Adjust as needed