Skip to content

Commit

Permalink
feat: first version of exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
terinjokes committed Aug 31, 2020
1 parent c25f581 commit 044bc2a
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2020 Terin Stock <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# OpenRCT2 Prometheus Exporter

An OpenRCT2 plugin that exports Prometheus metrics for a (limited but growing) set of park metrics.

## Installation

This plugin requires the in-development [networking API](https://github.com/OpenRCT2/OpenRCT2/pull/12712). It has been tested against
commit 66fb9f556e1ff6b7e52776083303d17b98721ebf.

Download [index.js](./index.js) from GitHub and save the file into your OpenRCT2 plugin folder. This location can be found
according to the [OpenRCT2 scripting](https://github.com/OpenRCT2/OpenRCT2/blob/e9803fc4b51d80fdeabf5fdb57469ff6fc753db3/distribution/scripting.md#scripts-for-openrct2) documentation.

## Usage

The plugin runs in the background, listening for scripts at the address "localhost:9751", which is currently hardcoded into the file, but may be configurable soon.
99 changes: 99 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
(function () {
'use strict';

var port = 9751;

function Gauge(name, labels) {
this.name = name;
this.val = 0;
this.labels = labels;
}

Gauge.prototype.set = function(val) {
this.val = val;
};

Gauge.prototype.inc = function(val) {
this.val += val;
};

Gauge.prototype.toString = function() {
if (this.labels.length%2 != 0) {
console.log('error: invalid label length');
}
var labels = ""
var i = 0;
while (i < this.labels.length) {
labels += this.labels[i] + '="' + this.labels[i+1] + '"';

if (i+2 < this.labels.length) {
labels += ',';
}

i += 2;
}

return this.name + '{' + labels + '} '+ this.val;
};

function httpServer() {
var server = network.createListener();
server.on('connection', function(conn) {
conn.on('data', function() {
var park_cash = new Gauge("park_cash", ["park", park.name]);
park_cash.set(park.cash/10);
var park_rating = new Gauge("park_rating", ["park", park.name]);
park_rating.set(park.rating);
var park_bankLoan = new Gauge("park_bankLoan", ["park", park.name]);
park_bankLoan.set(park.bankLoan);

conn.write('HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n');
conn.write('# TYPE park_cash gauge\n' + park_cash + '\n');
conn.write('# TYPE park_rating gauge\n' + park_rating + '\n');
conn.write('# TYPE park_bank_loan gauge\n' + park_bankLoan + '\n');
conn.write('# TYPE park_max_bank_loan counter\npark_max_bank_loan{park="' + park.name + '"} ' + park.maxBankLoan/10 + '\n');

var guest_count = new Gauge("guest_count", ["park", park.name]);
var staff_count = new Gauge("staff_count", ["park", park.name]);
var peep_count = new Gauge("peep_count", ["park", park.name]);
var duck_count = new Gauge("duck_count", ["park", park.name]);

var peeps = map.getAllEntities("peep");
peep_count.set(peeps.length);

var ducks = map.getAllEntities("duck");
duck_count.set(ducks.length);

peeps.forEach(function(peep) {
if (peep.peepType === 'guest' && peep.getFlag("hasPaidForParkEntry")) {
guest_count.inc(1);
} else if (peep.peepType === 'staff') {
staff_count.inc(1);
}
});

conn.write('# TYPE peep_count gauge\n' + peep_count + '\n');
conn.write('# TYPE guest_count gauge\n' + guest_count + '\n');
conn.write('# TYPE staff_count gauge\n' + staff_count + '\n');
conn.write('# TYPE duck_count gauge\n' + duck_count + '\n');
conn.end();
});
});
server.listen(port);
console.log('listening on port ' + port + '...');
}

function main() {
httpServer();
}

registerPlugin({
name: 'openrct2-prometheus-exporter',
version: '0.0.0',
licence: 'MIT',
authors: ['Terin Stock'],
type: 'local',
main: main
});

}());

0 comments on commit 044bc2a

Please sign in to comment.