Skip to content

Instantly share code, notes, and snippets.

@koss822
Created June 14, 2023 17:24
Show Gist options
  • Save koss822/547a17f7e819b7e7a7fac19727b676ea to your computer and use it in GitHub Desktop.
Save koss822/547a17f7e819b7e7a7fac19727b676ea to your computer and use it in GitHub Desktop.
const fs = require('fs');
// Read the text file
const fileContent = fs.readFileSync('exchange_rates.txt', 'utf-8');
// Split the content by new lines to get individual rows
const rows = fileContent.split('\n');
// Initialize an empty array to store the currency data
const currencyData = [];
// Extract currency and exchange rate from each row
for (let i = 1; i < rows.length; i++) {
const row = rows[i].split('|');
const currency = row[1];
const exchangeRate = parseFloat(row[4].replace(',', '.'));
// Add currency and exchange rate as an object to the array
currencyData.push({ currency, exchangeRate });
}
// Create a Prometheus exporter endpoint
const express = require('express');
const app = express();
const port = 8080;
app.get('/metrics', (req, res) => {
// Generate Prometheus metrics format
let metrics = '';
currencyData.forEach(data => {
metrics += `exchange_rate{currency="${data.currency}"} ${data.exchangeRate}\n`;
});
res.setHeader('Content-Type', 'text/plain');
res.send(metrics);
});
// Start the server
app.listen(port, () => {
console.log(`Prometheus exporter is running on port ${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment