Commit 3c10268b authored by Alex Thomae's avatar Alex Thomae

Merge branch 'master' into 'latest'

Initial import

See merge request docker/stellar-exporter!1
parents 9341aae2 2465887e
variables:
DOCKER_DRIVER: overlay2
stages:
- lint
- build
services:
- docker:dind
pylint:
image: python:latest
stage: lint
script:
- pip install prometheus_client pylint stellar-base
- pylint --rcfile=.pylintrc *.py
build:
image: docker:latest
stage: build
except:
- master
before_script:
- export IMAGE_TAG=${CI_REGISTRY_IMAGE}:${CI_COMMIT_REF_NAME}
script:
- docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
- docker build -f Dockerfile -t ${IMAGE_TAG} .
- docker push ${IMAGE_TAG}
This diff is collapsed.
FROM hub.ix.ai/docker/alpine:latest
LABEL ai.ix.maintainer="docker@ix.ai"
ENV LOGLEVEL=INFO
RUN pip3 install stellar-base
COPY stellar-exporter.py /
EXPOSE 9308
ENTRYPOINT ["python3", "/stellar-exporter.py"]
# stellar-exporter
A [Prometheus](https://prometheus.io) exporter for [Stellar](https://www.stellar.org/) written in python
\ No newline at end of file
A [Prometheus](https://prometheus.io) exporter for [Stellar](https://www.stellar.org/) written in python
## Usage
```
docker run --rm -it -p 9308:9308 \
-e LOGLEVEL=DEBUG \
--name stellar-exporter \
hub.ix.ai/docker/stellar-exporter:latest
```
## Supported variables
* `ACCOUNTS` (no default) - comma separated list of the accounts monitor the balances
* `LOGLEVEL` (defaults to `INFO`)
#!/usr/bin/env python3
""" A Prometheus exporter for Stellar """
import logging
import time
import os
import sys
from stellar_base.address import Address
from prometheus_client import start_http_server
from prometheus_client.core import REGISTRY, GaugeMetricFamily
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'
)
class StellarCollector:
""" The StellarCollector class """
accounts = {}
settings = {}
def __init__(self):
self.settings = {
'accounts': os.environ.get("ACCOUNTS", '').split(','),
}
def getAccounts(self):
""" Connects to the Stellar network and retrieves the account information """
for account in self.settings['accounts']:
a = Address(address=account, network='public')
a.get()
for balance in a.balances:
if balance.get('asset_code'):
currency = balance.get('asset_code')
elif balance.get('asset_type') == 'native':
currency = 'XLM'
else:
currency = balance.get('asset_type')
self.accounts.update({
'{}-{}'.format(account, currency): {
'account': account,
'currency': currency,
'balance': float(balance.get('balance'))
}
})
LOG.debug('Found the following accounts: {}'.format(self.accounts))
def collect(self):
""" The actual metrics collector """
metrics = {
'account_balance': GaugeMetricFamily(
'account_balance',
'Account Balance',
labels=['source_currency', 'currency', 'account', 'type']
),
}
self.getAccounts()
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',
]
)
for metric in metrics.values():
yield metric
if __name__ == '__main__':
LOG.info("Starting")
REGISTRY.register(StellarCollector())
start_http_server(9308)
while True:
time.sleep(1)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment