Skip to content

Commit

Permalink
feat: co2 emission metric
Browse files Browse the repository at this point in the history
  • Loading branch information
albertolerda committed Jan 17, 2024
1 parent b9f94b1 commit 46b9f17
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@slangroom/ethereum": "^1.10.1",
"@slangroom/http": "^1.10.1",
"@slangroom/wallet": "^1.11.0",
"@tgwf/co2": "^0.13.10",
"ajv": "^8.12.0",
"commander": "^11.1.0",
"dotenv": "^16.3.1",
Expand Down
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 22 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,35 @@ const Dir = Directory.getInstance();
const PROM = process.env.PROM == 'true';

const setupProm = async (app: TemplatedApp) => {
const promClient = await import('prom-client');
const promRegister = new promClient.Registry()
promRegister.setDefaultLabels({
const client = await import('prom-client');
const register = new client.Registry()
register.setDefaultLabels({
app: 'ncr'
})
promClient.collectDefaultMetrics({ register: promRegister })
client.collectDefaultMetrics({ register })

const co2lib = await import('@tgwf/co2');
const swd = new co2lib.co2({ model: "swd" })


const co2_emission = new client.Gauge({
name: 'co2_emission',
help: 'Emissions for 1GB',
collect() {
const emissions = swd.perByte(1000000000)
this.set(emissions);
},
});

register.registerMetric(co2_emission);

app.get('/metrics', (res, req) => {
promRegister.metrics().then(metrics =>
register.metrics().then(metrics =>
res
.writeStatus('200 OK')
.writeHeader('Content-Type', promRegister.contentType)
.writeHeader('Content-Type', register.contentType)
.end(metrics));
})
});
}


Expand Down

0 comments on commit 46b9f17

Please sign in to comment.