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

Commit a9d3050

Browse files
committed
Adds GELF logging
1 parent 62f7b88 commit a9d3050

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

.gitlab-ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
variables:
22
DOCKER_DRIVER: overlay2
33
IMAGE_TAG: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
4+
PORT: 9308
45

56
stages:
67
- lint
@@ -14,7 +15,7 @@ pylint:
1415
stage: lint
1516
script:
1617
- curl -o .pylintrc https://tool.ix.ai/.pylintrc
17-
- pip install prometheus_client pylint requests
18+
- pip install prometheus_client pylint requests pygelf
1819
- pylint --rcfile=.pylintrc src/*.py
1920

2021
build:
@@ -23,7 +24,8 @@ build:
2324
only:
2425
- /^latest$/
2526
- tags
27+
- /(.*)-dev$/
2628
script:
2729
- docker login -u gitlab-ci-token -p ${CI_JOB_TOKEN} ${CI_REGISTRY}
28-
- docker build -f Dockerfile -t ${IMAGE_TAG} .
30+
- docker build -f Dockerfile -t ${IMAGE_TAG} --build-arg PORT="${PORT}" .
2931
- docker push ${IMAGE_TAG}

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
FROM hub.ix.ai/docker/alpine:latest
22
LABEL ai.ix.maintainer="docker@ix.ai"
3+
ARG PORT
34

45
RUN apk add --no-cache py3-requests
56

6-
ENV LOGLEVEL=INFO
7+
ENV LOGLEVEL=INFO PORT=${PORT}
78

89
COPY src/etherscan-exporter.py /
910

10-
EXPOSE 9308
11+
EXPOSE ${PORT}
1112

1213
ENTRYPOINT ["python3", "/etherscan-exporter.py"]

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ docker run --rm -it -p 9308:9308 \
1313
```
1414

1515
## Supported variables
16-
* `API_KEY` (no default) - set this to your Etherscan API key
16+
* `API_KEY` (no default - **mandatory**) - set this to your Etherscan API key
1717
* `URL` (default: https://api.etherscan.io/api) - set this to your Etherscan API secret
1818
* `ADDRESSES` (no default) - a comma separated list of the ETH addresses to export
1919
* `TOKENS` (no default) - a JSON object with the list of tokens to export (see below)
20+
* `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
21+
* `GELF_PORT` (defaults to `12201`) - the port to use for GELF logging
22+
* `PORT` (defaults to `9308`) - the listen port for the exporter
2023
* `LOGLEVEL` (defaults to `INFO`)
2124

2225
## Tokens

src/etherscan-exporter.py

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import json
99
import requests
10+
import pygelf
1011
from prometheus_client import start_http_server
1112
from prometheus_client.core import REGISTRY, GaugeMetricFamily
1213

@@ -19,6 +20,22 @@
1920
)
2021

2122

23+
def configure_logging():
24+
""" Configures the logging """
25+
gelf_enabled: False
26+
27+
if os.environ.get('GELF_HOST'):
28+
GELF = pygelf.GelfUdpHandler(
29+
host=os.environ.get('GELF_HOST'),
30+
port=int(os.environ.get('GELF_PORT', 12201)),
31+
debug=True,
32+
include_extra_fields=True,
33+
)
34+
LOG.addHandler(GELF)
35+
gelf_enabled = True
36+
LOG.info('Initialized logging with GELF enabled: {}'.format(gelf_enabled))
37+
38+
2239
class EtherscanCollector:
2340
""" The EtherscanCollector class """
2441

@@ -32,13 +49,16 @@ def __init__(self):
3249
'addresses': os.environ.get("ADDRESSES"),
3350
'tokens': [],
3451
}
52+
if not self.settings.get('api_key'):
53+
raise ValueError("Missing API_KEY environment variable.")
54+
3555
if os.environ.get('TOKENS'):
3656
self.settings['tokens'] = (json.loads(os.environ.get("TOKENS")))
3757

3858
def get_tokens(self):
3959
""" Gets the tokens from an account """
40-
# Ensure that we don't get blocked
41-
time.sleep(1)
60+
61+
time.sleep(1) # Ensure that we don't get rate limited
4262
for account in self.accounts:
4363
for token in self.settings['tokens']:
4464
request_data = {
@@ -59,7 +79,7 @@ def get_tokens(self):
5979
requests.exceptions.ConnectionError,
6080
requests.exceptions.ReadTimeout,
6181
) as error:
62-
LOG.warning(error)
82+
LOG.exception('Exception caught: {}'.format(error))
6383
req = {}
6484
if req.get('result') and int(req['result']) > 0:
6585
self.tokens.update({
@@ -90,7 +110,7 @@ def get_balances(self):
90110
requests.exceptions.ConnectionError,
91111
requests.exceptions.ReadTimeout,
92112
) as error:
93-
LOG.warning(error)
113+
LOG.exception('Exception caught: {}'.format(error))
94114
req = {}
95115
if req.get('message') == 'OK' and req.get('result'):
96116
for result in req.get('result'):
@@ -140,8 +160,10 @@ def collect(self):
140160

141161

142162
if __name__ == '__main__':
143-
LOG.info("Starting")
163+
configure_logging()
164+
PORT = int(os.environ.get('PORT', 9308))
165+
LOG.info("Starting on port {}".format(PORT))
144166
REGISTRY.register(EtherscanCollector())
145-
start_http_server(9308)
167+
start_http_server(PORT)
146168
while True:
147169
time.sleep(1)

0 commit comments

Comments
 (0)