Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Commit

Permalink
Restructures the project and cleans up the code
Browse files Browse the repository at this point in the history
Closes #2
Closes #3
  • Loading branch information
tlex committed Feb 8, 2020
1 parent 7ff69b6 commit bbc4b19
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 49 deletions.
6 changes: 4 additions & 2 deletions build.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env sh

echo "Setting VERSION='${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHORT_SHA}' in src/constants.py"
echo "VERSION = '${CI_COMMIT_REF_NAME}-${CI_COMMIT_SHORT_SHA}'" >> src/constants.py
echo "Setting VERSION"
find . -name .git -type d -prune -o -type f -name constants.py -exec sed -i "s/^VERSION.*/VERSION\ =\ \'${CI_COMMIT_REF_NAME:-None}\'/g" {} + -exec grep VERSION {} +
echo "Setting BUILD"
find . -name .git -type d -prune -o -type f -name constants.py -exec sed -i "s/^BUILD.*/BUILD\ =\ \'${CI_COMMIT_SHORT_SHA:-None}\'/g" {} + -exec grep BUILD {} +
Empty file removed src/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions stellar-exporter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

exec python3 -m "stellar-exporter.stellar-exporter" "$@"
11 changes: 11 additions & 0 deletions stellar-exporter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" initializes alertmanager_notifier """

import os
from .lib import log as logging

log = logging.setup_logger(
name=__package__,
level=os.environ.get('LOGLEVEL', 'INFO'),
)
2 changes: 2 additions & 0 deletions stellar-exporter/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
1 change: 1 addition & 0 deletions src/constants.py → stellar-exporter/lib/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
""" Constants declarations """

VERSION = None
BUILD = None
22 changes: 22 additions & 0 deletions stellar-exporter/lib/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Global logging configuration """

import logging


def setup_logger(name=__package__, level='INFO'):
""" sets up the logger """
logging.basicConfig(handlers=[logging.NullHandler()])
formatter = logging.Formatter(
fmt='%(asctime)s.%(msecs)03d %(levelname)s [%(module)s.%(funcName)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
)
logger = logging.getLogger(name)
logger.setLevel(level)

handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addHandler(handler)

return logger
File renamed without changes.
79 changes: 34 additions & 45 deletions src/stellar-exporter.py → stellar-exporter/stellar-exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,31 @@
import logging
import time
import os
import sys
import pygelf
from stellar_sdk.server import Server
from prometheus_client import start_http_server
from prometheus_client.core import REGISTRY, GaugeMetricFamily
import constants
from .lib import constants

LOG = logging.getLogger(__name__)
logging.basicConfig(
stream=sys.stdout,
level=os.environ.get("LOGLEVEL", "INFO"),
format='%(asctime)s.%(msecs)03d %(levelname)s {%(module)s} [%(funcName)s] %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)

FILENAME = os.path.splitext(sys.modules['__main__'].__file__)[0]


def configure_logging():
""" Configures the logging """
gelf_enabled = False

if os.environ.get('GELF_HOST'):
GELF = pygelf.GelfUdpHandler(
host=os.environ.get('GELF_HOST'),
port=int(os.environ.get('GELF_PORT', 12201)),
debug=True,
include_extra_fields=True,
_ix_id=FILENAME
)
LOG.addHandler(GELF)
gelf_enabled = True
LOG.info('Initialized logging with GELF enabled: {}'.format(gelf_enabled))
log = logging.getLogger(__package__)
version = f'{constants.VERSION}-{constants.BUILD}'


class StellarCollector:
""" The StellarCollector class """
accounts = {}
settings = {}

def __init__(self):
server = os.environ.get('HORIZON_URL') if os.environ.get('HORIZON_URL') else 'https://horizon.stellar.org/'
def __init__(self, **kwargs):
self.settings = {
'accounts': os.environ.get("ACCOUNTS", '').split(','),
'server': Server(horizon_url=server),
'accounts': [],
'server': Server(horizon_url=kwargs['horizon_url']),
}
if kwargs.get('accounts'):
self.settings['accounts'] = kwargs['accounts'].split(',')

def get_accounts(self):
""" Connects to the Stellar network and retrieves the account information """
log.info('Retrieving accounts')
for account in self.settings['accounts']:
balances = self.settings['server'].accounts().account_id(account).call().get('balances')
if isinstance(balances, list):
Expand All @@ -63,14 +39,14 @@ def get_accounts(self):
else:
currency = balance.get('asset_type')
self.accounts.update({
'{}-{}'.format(account, currency): {
f'{account}-{currency}': {
'account': account,
'currency': currency,
'balance': float(balance.get('balance'))
}
})

LOG.debug('Found the following accounts: {}'.format(self.accounts))
log.debug(f'Found the following accounts: {self.accounts}')

def describe(self):
""" Just a needed method, so that collect() isn't called at startup """
Expand All @@ -82,15 +58,14 @@ def collect(self):
'account_balance': GaugeMetricFamily(
'account_balance',
'Account Balance',
labels=['source_currency', 'currency', 'account', 'type']
labels=['currency', 'account', 'type']
),
}
self.get_accounts()
for a in self.accounts:
metrics['account_balance'].add_metric(
value=self.accounts[a]['balance'],
labels=[
self.accounts[a]['currency'],
self.accounts[a]['currency'],
self.accounts[a]['account'],
'stellar',
Expand All @@ -100,14 +75,28 @@ def collect(self):
yield metric


def _test(collector):
for metric in collector.collect():
log.info(f"{metric}")


if __name__ == '__main__':
configure_logging()
PORT = int(os.environ.get('PORT', 9188))
# pylint: disable=no-member
LOG.info("Starting {} {} on port {}".format(FILENAME, constants.VERSION, PORT))
REGISTRY.register(StellarCollector())
TEST = os.environ.get('TEST', False)
if not TEST:
start_http_server(PORT)
port = int(os.environ.get('PORT', 9188))
settings = {
'horizon_url': os.environ.get('HORIZON_URL', 'https://horizon.stellar.org/'),
'accounts': os.environ.get("ACCOUNTS", []),
}
log.warning(f"Starting {__package__} {version} on port {port}")

if os.environ.get('TEST', False):
_test(StellarCollector(
horizon_url='https://horizon.stellar.org/',
accounts='GA5XIGA5C7QTPTWXQHY6MCJRMTRZDOSHR6EFIBNDQTCQHG262N4GGKTM' # kraken
',GD6RMKTCHQGEOGYWIKSY5G7QWXPZOAEZIKPKEVZUAXOQCZRVBRRFGLJM' # NydroEnergy
',GCNSGHUCG5VMGLT5RIYYZSO7VQULQKAJ62QA33DBC5PPBSO57LFWVV6P' # InterstellarExchange
))
else:
REGISTRY.register(StellarCollector(**settings))
start_http_server(port)
while True:
time.sleep(1)
5 changes: 3 additions & 2 deletions test.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/sh
#!/usr/bin/env sh

TEST=true python3 src/${CI_PROJECT_NAME}.py
export TEST=true
./stellar-exporter.sh

0 comments on commit bbc4b19

Please sign in to comment.