-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from p2p-org/astar_network_onborading
- Loading branch information
Showing
24 changed files
with
699 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
WS_ENDPOINT="wss://astar-rpc.dwellir.com" | ||
WS_ENDPOINTS="wss://astar-rpc.dwellir.com" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
version: '3.4' | ||
|
||
services: | ||
astar_exporter: | ||
build: | ||
context: ./exporters/astar | ||
environment: | ||
- "LISTEN=0.0.0.0" | ||
- "PORT=9150" | ||
- "CHAIN=astar" | ||
env_file: | ||
- ./astar.env | ||
networks: | ||
- exporters | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
WS_ENDPOINT="wss://ws.calamari.systems/" | ||
WS_ENDPOINTS="wss://ws.calamari.systems/" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
version: '3.4' | ||
|
||
services: | ||
calamari_exporter: | ||
build: | ||
context: ./exporters/manta | ||
environment: | ||
- "LISTEN=0.0.0.0" | ||
- "PORT=9150" | ||
- "CHAIN=calamari" | ||
env_file: | ||
- ./manta.env | ||
networks: | ||
- exporters | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
FROM alpine/flake8:latest as linter | ||
WORKDIR /apps/ | ||
COPY . /apps/ | ||
## ingore E501 line too long (XX > 79 characters) | ||
RUN flake8 --ignore="E501" *.py | ||
|
||
FROM --platform=linux/amd64 python:3.11-slim-buster | ||
|
||
WORKDIR / | ||
|
||
RUN apt-get update && apt-get install -y gcc g++ | ||
COPY requirements.txt requirements.txt | ||
RUN pip3 install -r requirements.txt --no-cache-dir | ||
RUN groupadd -r exporter && useradd -r -g exporter exporter | ||
|
||
COPY --from=linter /apps/exporter.py app.py | ||
COPY --from=linter /apps/functions.py functions.py | ||
|
||
USER exporter | ||
CMD ["python3", "app.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#!/usr/bin/env python3 | ||
import os | ||
import threading | ||
import time | ||
import logging | ||
from collections import deque | ||
from functions import SUBSTRATE_INTERFACE | ||
from flask import Flask, make_response | ||
|
||
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %I:%M:%S') | ||
app = Flask(__name__) | ||
|
||
|
||
@app.route("/metrics") | ||
def metrics(): | ||
metrics = q_metrics[0].copy() | ||
|
||
out = "" | ||
|
||
try: | ||
out += '# HELP astar_currentSession Current session\n' | ||
out += '# TYPE astar_currentSession counter\n' | ||
|
||
out += 'astar_currentSession{chain="%s"} %s\n' % (chain, metrics['common']['current_session']) | ||
except KeyError: | ||
pass | ||
|
||
try: | ||
out += '# HELP astar_activeCollators Active collators\n' | ||
out += '# TYPE astar_activeCollators counter\n' | ||
|
||
for i in metrics['collators'].keys(): | ||
out += 'astar_activeCollators{chain="%s", account="%s"} 1\n' % (chain, i) | ||
|
||
except KeyError: | ||
pass | ||
|
||
try: | ||
out += '# HELP astar_sessionBlocks Session blocks\n' | ||
out += '# TYPE astar_sessionBlocks counter\n' | ||
|
||
out += 'astar_sessionBlocks{chain="%s"} %s\n' % (chain, metrics['common']['session_blocks']) | ||
except KeyError: | ||
pass | ||
|
||
try: | ||
out += '# HELP astar_blockAuthorship Blocks authored\n' | ||
out += '# TYPE astar_blockAuthorship counter\n' | ||
|
||
for k, v in metrics['collators'].items(): | ||
out += 'astar_blockAuthorship{chain="%s", account="%s"} %s\n' % (chain, k, v['authored_blocks_count']) | ||
except KeyError: | ||
pass | ||
|
||
response = make_response(out, 200) | ||
response.mimetype = "text/plain" | ||
|
||
return response | ||
|
||
|
||
def main(): | ||
block = 0 | ||
session = 0 | ||
|
||
while True: | ||
try: | ||
current_session = int(substrate_interface.request('Session', 'CurrentIndex').value) | ||
|
||
if session != current_session: | ||
active_collators = substrate_interface.request('Session', 'Validators').value | ||
result = {'collators': {}, 'common': {}} | ||
|
||
for addr in active_collators: | ||
result['collators'][addr] = {'is_active': 0, 'authored_blocks_count': 0} | ||
|
||
result['common']['current_session'] = current_session | ||
result['common']['session_blocks'] = 0 | ||
|
||
logging.info('New session ' + str(current_session) + ' has just begun') | ||
|
||
last_block = substrate_interface.request('System', 'Number').value | ||
|
||
if last_block != block: | ||
logging.info('Processing block ' + str(last_block)) | ||
|
||
for addr, params in result['collators'].items(): | ||
authored_block = substrate_interface.request('CollatorSelection', 'LastAuthoredBlock', [addr]).value | ||
|
||
if 'last_authored_block' not in params.keys(): | ||
params['last_authored_block'] = authored_block | ||
|
||
continue | ||
|
||
if authored_block == last_block: | ||
params['authored_blocks_count'] += 1 | ||
params['last_authored_block'] = authored_block | ||
|
||
logging.info('Collator ' + str(addr) + ' has just constructed block ' + str(authored_block)) | ||
|
||
result['common']['session_blocks'] += 1 | ||
|
||
q_metrics.clear() | ||
q_metrics.append(result) | ||
|
||
session = current_session | ||
block = last_block | ||
|
||
except Exception as e: | ||
logging.critical('The main thread been stucked with error "' + str(e) + '"') | ||
time.sleep(10) | ||
|
||
continue | ||
|
||
time.sleep(3) | ||
|
||
|
||
if __name__ == '__main__': | ||
endpoint_listen = os.environ['LISTEN'] | ||
endpoint_port = os.environ['PORT'] | ||
ws_endpoint = os.environ['WS_ENDPOINT'] | ||
chain = os.environ['CHAIN'] | ||
|
||
substrate_interface = SUBSTRATE_INTERFACE(ws_endpoint) | ||
|
||
q_metrics = deque([]) | ||
|
||
worker = threading.Thread(target=main) | ||
worker.daemon = True | ||
worker.start() | ||
|
||
app.run(host="0.0.0.0", port=int(endpoint_port)) |
Oops, something went wrong.