change worker metrics to guage

This commit is contained in:
evan
2025-05-02 08:45:37 -07:00
parent e19e08d542
commit 0dd762e3a8
2 changed files with 61 additions and 75 deletions
+2 -5
View File
@@ -13,7 +13,7 @@ import { Client } from "./Client";
import { GameManager } from "./GameManager";
import { gatekeeper, LimiterType } from "./Gatekeeper";
import { logger } from "./Logger";
import { metrics } from "./WorkerMetrics";
import { initWorkerMetrics } from "./WorkerMetrics";
const config = getServerConfigFromServer();
@@ -34,10 +34,7 @@ export function startWorker() {
const gm = new GameManager(config, log);
if (config.otelEnabled()) {
// Set up periodic metrics updates
setInterval(() => {
metrics.updateGameMetrics(gm);
}, 15000); // Update every 15 seconds
initWorkerMetrics(gm);
}
// Middleware to handle /wX path prefix
+59 -70
View File
@@ -10,14 +10,14 @@ import { getOtelResource } from "./OtelResource";
dotenv.config();
// Get server configuration
const config = getServerConfigFromServer();
export function initWorkerMetrics(gameManager: GameManager): void {
// Get server configuration
const config = getServerConfigFromServer();
// Create resource with worker information
const resource = getOtelResource();
// Create resource with worker information
const resource = getOtelResource();
// Configure headers for basic auth if provided
const getAuthHeaders = () => {
// Configure auth headers
const headers = {};
if (config.otelEnabled()) {
headers["Authorization"] =
@@ -26,78 +26,67 @@ const getAuthHeaders = () => {
"base64",
);
}
return headers;
};
// Create metrics exporter
const metricExporter = new OTLPMetricExporter({
// Dummy endpoint if OTEL is not enabled to avoid parsing errors
url: `${config.otelEndpoint() || "https://dummy_endpoint.com"}/v1/metrics`,
headers: getAuthHeaders(),
});
// Create metrics exporter
const metricExporter = new OTLPMetricExporter({
url: `${config.otelEndpoint()}/v1/metrics`,
headers,
});
// Configure the metric reader
const metricReader = new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 15000, // Export metrics every 15 seconds
});
// Configure the metric reader
const metricReader = new PeriodicExportingMetricReader({
exporter: metricExporter,
exportIntervalMillis: 15000, // Export metrics every 15 seconds
});
// Create a meter provider
const meterProvider = new MeterProvider({
resource,
readers: [metricReader],
});
// Create a meter provider
const meterProvider = new MeterProvider({
resource,
readers: [metricReader],
});
// Get meter for creating metrics
const meter = meterProvider.getMeter("worker-metrics");
// Get meter for creating metrics
const meter = meterProvider.getMeter("worker-metrics");
// Create OpenTelemetry metrics
const activeGamesCounter = meter.createUpDownCounter(
"openfront.active_games.count",
{
description: "Number of active games on this worker",
},
);
// Create observable gauges
const activeGamesGauge = meter.createObservableGauge(
"openfront.active_games.gauge",
{
description: "Number of active games on this worker",
},
);
const connectedClientsCounter = meter.createUpDownCounter(
"openfront.connected_clients.count",
{
description: "Number of connected clients on this worker",
},
);
const connectedClientsGauge = meter.createObservableGauge(
"openfront.connected_clients.gauge",
{
description: "Number of connected clients on this worker",
},
);
const memoryUsageObservable = meter.createObservableGauge(
"openfront.memory_usage.bytes",
{
description: "Current memory usage of the worker process in bytes",
},
);
const memoryUsageGauge = meter.createObservableGauge(
"openfront.memory_usage.bytes",
{
description: "Current memory usage of the worker process in bytes",
},
);
// Register callback for the memory usage observable
memoryUsageObservable.addCallback((result) => {
const memoryUsage = process.memoryUsage();
result.observe(memoryUsage.heapUsed);
});
// Register callback for active games metric
activeGamesGauge.addCallback((result) => {
const count = gameManager.activeGames();
result.observe(count);
});
// Export the metrics for use in the worker
export const metrics = {
// Function to update game-related metrics
updateGameMetrics: (gameManager: GameManager) => {
console.log("Updating game metrics");
// Get the current counts
const currentActiveGames = gameManager.activeGames();
const currentActiveClients = gameManager.activeClients();
// Register callback for connected clients metric
connectedClientsGauge.addCallback((result) => {
const count = gameManager.activeClients();
result.observe(count);
});
// Set the absolute values (createUpDownCounter allows setting absolute values)
activeGamesCounter.add(currentActiveGames);
connectedClientsCounter.add(currentActiveClients);
// Register callback for memory usage metric
memoryUsageGauge.addCallback((result) => {
const memoryUsage = process.memoryUsage();
result.observe(memoryUsage.heapUsed);
});
// Memory metrics are automatically collected by the observable
},
// Expose the meter provider for potential additional metrics
meterProvider,
// Expose the meter for creating additional metrics
meter,
};
console.log("Metrics initialized with GameManager");
}