From 3d5ca5bce544423db225ab3edecb56240fcd4aae Mon Sep 17 00:00:00 2001 From: Alex Thomae Date: Sun, 5 Jan 2020 14:53:19 +0000 Subject: [PATCH] * sets the default port to `9188`, as exposed in the Dockerfile * sets fixed versions for `prometheus_client`, `stellar_base`, `stellar-sdk` and `toml` * adds support for `HORIZON_URL` * updates README.md --- .gitlab-ci.yml | 2 ++ Dockerfile | 8 +++----- README.md | 16 +++++++++------- src/requirements.txt | 8 ++++---- src/stellar-exporter.py | 38 ++++++++++++++++++++------------------ 5 files changed, 38 insertions(+), 34 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2e3ed7a..eb1eb25 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,6 +7,8 @@ include: - remote: https://gitlab.com/ix.ai/ci-templates/raw/master/python-project.yml test: + extends: + - .tags-template stage: test image: python:latest variables: diff --git a/Dockerfile b/Dockerfile index 741ec5c..fd6e945 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,12 +6,10 @@ WORKDIR /app COPY src/ /app RUN apk --no-cache upgrade && \ - apk add --no-cache python3-dev gcc musl-dev libffi-dev make && \ + apk add --no-cache python3 python3-dev gcc musl-dev libffi-dev make && \ pip3 install --no-cache-dir -r requirements.txt && \ - apk del --no-cache --purge gcc musl-dev libffi-dev make + apk del --no-cache --purge python3-dev gcc musl-dev libffi-dev make -COPY src/stellar-exporter.py / - -EXPOSE 9308 +EXPOSE 9188 ENTRYPOINT ["python3", "/app/stellar-exporter.py"] diff --git a/README.md b/README.md index 67ea8fb..38c64eb 100644 --- a/README.md +++ b/README.md @@ -12,19 +12,21 @@ A [Prometheus](https://prometheus.io) exporter for [Stellar](https://www.stellar docker run --rm -it -p 9308:9308 \ -e LOGLEVEL=DEBUG \ -e GELF_HOST=graylog \ + -e ACCOUNTS='AAAAA,BBBBB' \ --name stellar-exporter \ registry.gitlab.com/ix.ai/stellar-exporter:latest ``` ## Supported variables -| **Variable** | **Default** | **Mandatory** | **Description** | -|:-------------|:-----------:|:-------------:|:-----------------------------------------------------------------------------------------------------------------------| -| `ACCOUNTS` | - | **YES** | comma separated list of the accounts monitor the balances | -| `LOGLEVEL` | `INFO` | **NO** | [Logging Level](https://docs.python.org/3/library/logging.html#levels) | -| `GELF_HOST` | - | **NO** | if set, the exporter will also log to this [GELF](https://docs.graylog.org/en/3.0/pages/gelf.html) capable host on UDP | -| `GELF_PORT` | `12201` | **NO** | Ignored, if `GELF_HOST` is unset. The UDP port for GELF logging | -| `PORT` | `9308` | **NO** | The port for prometheus metrics | +| **Variable** | **Default** | **Mandatory** | **Description** | +|:--------------|:------------------------------:|:-------------:|:-----------------------------------------------------------------------------------------------------------------------| +| `ACCOUNTS` | - | **YES** | comma separated list of the accounts monitor the balances | +| `HORIZON_URL` | `https://horizon.stellar.org/` | **NO** | The URL of the horizon server. For the Test network you can use `https://horizon-testnet.stellar.org/` | +| `LOGLEVEL` | `INFO` | **NO** | [Logging Level](https://docs.python.org/3/library/logging.html#levels) | +| `GELF_HOST` | - | **NO** | if set, the exporter will also log to this [GELF](https://docs.graylog.org/en/3.0/pages/gelf.html) capable host on UDP | +| `GELF_PORT` | `12201` | **NO** | Ignored, if `GELF_HOST` is unset. The UDP port for GELF logging | +| `PORT` | `9188` | **NO** | The port for prometheus metrics | diff --git a/src/requirements.txt b/src/requirements.txt index 031c085..09c56d2 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -1,6 +1,6 @@ pbkdf2 -prometheus_client +prometheus_client==0.7.1 pygelf -stellar-base -stellar-sdk -toml +stellar-base==1.1.2.0 +stellar-sdk==2.1.0 +toml==0.10.0 diff --git a/src/stellar-exporter.py b/src/stellar-exporter.py index c9a7f6a..2d2aded 100644 --- a/src/stellar-exporter.py +++ b/src/stellar-exporter.py @@ -5,7 +5,7 @@ import os import sys import pygelf -from stellar_base.address import Address +from stellar_sdk.server import Server from prometheus_client import start_http_server from prometheus_client.core import REGISTRY, GaugeMetricFamily @@ -41,29 +41,31 @@ class StellarCollector: settings = {} def __init__(self): + server = os.environ.get('HORIZON_URL') if os.environ.get('HORIZON_URL') else 'https://horizon.stellar.org/' self.settings = { 'accounts': os.environ.get("ACCOUNTS", '').split(','), + 'server': Server(horizon_url=server), } def get_accounts(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')) - } - }) + balances = self.settings['server'].accounts().account_id(account).call().get('balances') + if isinstance(balances, list): + for balance in 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)) @@ -97,7 +99,7 @@ def collect(self): if __name__ == '__main__': configure_logging() - PORT = int(os.environ.get('PORT', 9308)) + PORT = int(os.environ.get('PORT', 9188)) LOG.info("Starting on port {}".format(PORT)) REGISTRY.register(StellarCollector()) TEST = os.environ.get('TEST', False)