Commit 1ae82b68 authored by Alex Thomae's avatar Alex Thomae

Adds GELF logger

parent 6144927f
variables:
DOCKER_DRIVER: overlay2
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
PORT: 9308
stages:
- lint
......@@ -14,7 +15,7 @@ pylint:
stage: lint
script:
- curl -o .pylintrc https://tool.ix.ai/.pylintrc
- pip install prometheus_client pylint requests
- pip install prometheus_client pylint requests pygelf
- pylint --rcfile=.pylintrc src/*.py
build:
......@@ -23,7 +24,8 @@ build:
only:
- /^latest$/
- tags
- /(.*)-dev$/
script:
- docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
- docker build -f Dockerfile -t ${IMAGE_TAG} .
- docker build -f Dockerfile -t ${IMAGE_TAG} --build-arg PORT="${PORT}" .
- docker push ${IMAGE_TAG}
FROM hub.ix.ai/docker/alpine:latest
LABEL ai.ix.maintainer="docker@ix.ai"
ARG PORT=9308
ENV LOGLEVEL=INFO URL=https://data.ripple.com
RUN pip3 install --no-cache-dir requests
RUN pip3 install requests
ENV LOGLEVEL=INFO URL=https://data.ripple.com PORT=${PORT}
COPY src/ripple-exporter.py /
EXPOSE 9308
EXPOSE ${PORT}
ENTRYPOINT ["python3", "/ripple-exporter.py"]
......@@ -4,9 +4,10 @@
## Usage
```
docker run --rm -it -p 9308:9308 \
docker run --rm -it -p 9999:9999 \
-e LOGLEVEL=DEBUG \
-e URL="https://data.ripple.com"
-e URL="https://data.ripple.com" \
-e PORT=9999 \
--name ripple-exporter \
hub.ix.ai/docker/ripple-exporter:latest
```
......@@ -14,4 +15,7 @@ docker run --rm -it -p 9308:9308 \
## Supported variables
* `ADDRESSES` (no default) - comma separated list of the addresses monitor the balances
* `URL` (default: `https://data.ripple.com`) - the Ripple URL
* `GELF_HOST` (no default) - 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` (defaults to `12201`) - the port to use for GELF logging
* `PORT` (defaults to `9308`) - the listen port for the exporter
* `LOGLEVEL` (defaults to `INFO`)
......@@ -6,6 +6,7 @@ import time
import os
import sys
import requests
import pygelf
from prometheus_client import start_http_server
from prometheus_client.core import REGISTRY, GaugeMetricFamily
......@@ -18,6 +19,22 @@ logging.basicConfig(
)
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
)
LOG.addHandler(GELF)
gelf_enabled = True
LOG.info('Initialized logging with GELF enabled: {}'.format(gelf_enabled))
class RippleCollector:
""" The RippleCollector class """
accounts = {}
......@@ -40,15 +57,8 @@ class RippleCollector:
try:
r = requests.get(url).json()
LOG.debug('Response: {}'.format(r))
except (
requests.exceptions.ConnectionError,
requests.exceptions.ReadTimeout
) as e:
LOG.warning("Can't connect to {}. The error received follows.".format(
self.settings['url']
))
LOG.warning(e)
except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout) as error:
LOG.exception('Exception caught: {}'.format(error))
if r.get('result') == 'success' and r.get('balances'):
for balance in r.get('balances'):
LOG.debug('Registering balance {balance} for the currency {currency} - account {account}'.format(
......@@ -98,8 +108,10 @@ class RippleCollector:
if __name__ == '__main__':
LOG.info("Starting")
configure_logging()
PORT = int(os.environ.get('PORT', 9308))
LOG.info("Starting on port {}".format(PORT))
REGISTRY.register(RippleCollector())
start_http_server(9308)
start_http_server(PORT)
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