Skip to content

Commit db1ea70

Browse files
authored
Merge pull request #3 from heywood8/testing
Integration testing with amlen-server on github actions
2 parents dd8868a + ecaddee commit db1ea70

File tree

6 files changed

+89
-41
lines changed

6 files changed

+89
-41
lines changed

.github/workflows/integration.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Integration testing
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
name: integration-test
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
python-version: ["3.8", "3.9", "3.10"]
12+
steps:
13+
- uses: actions/checkout@v3
14+
- name: Set up Python ${{ matrix.python-version }}
15+
uses: actions/setup-python@v3
16+
with:
17+
python-version: ${{ matrix.python-version }}
18+
- name: Install dependencies
19+
run: |
20+
python3 -m pip install --upgrade pip
21+
pip3 install -r requirements.txt
22+
- name: Run exporter
23+
run: |
24+
/usr/bin/docker run -d -p 9089:9089 heywood8/amlen-server:1.0.0.1-20220622.1025_eclipse
25+
/usr/bin/docker ps
26+
sleep 10
27+
python3 src/amlen_exporter.py --once > exporter_result.txt
28+
cat exporter_result.txt
29+
if grep Error exporter_result.txt; then exit 1; fi

.github/workflows/pylint.yml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on: [push]
55
jobs:
66
build:
77
runs-on: ubuntu-latest
8+
name: pylint
89
strategy:
910
matrix:
1011
python-version: ["3.8", "3.9", "3.10"]

.github/workflows/release.yml

+3-8
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Set up Python 3.
2929
uses: actions/setup-python@v4
3030
with:
31-
python-version: '3.9-dev'
31+
python-version: '3.9-dev'
3232

3333
- name: Install requirements.
3434
run: |
@@ -48,7 +48,7 @@ jobs:
4848
run: |
4949
echo $RELEASE_VERSION
5050
echo ${{ env.RELEASE_VERSION }}
51-
51+
5252
- name: Compress action step
5353
uses: a7ul/[email protected]
5454
id: compress
@@ -93,11 +93,6 @@ jobs:
9393
- name: Set env
9494
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
9595

96-
- name: Test
97-
run: |
98-
echo $RELEASE_VERSION
99-
echo ${{ env.RELEASE_VERSION }}
100-
10196
- name: Compress action step
10297
uses: a7ul/[email protected]
10398
id: compress
@@ -116,4 +111,4 @@ jobs:
116111
asset_name: amlen_exporter-${{ env.RELEASE_VERSION }}.centos7-amd64.tar.gz
117112
tag: ${{ github.ref }}
118113
overwrite: true
119-
body: "Release ${{ env.RELEASE_VERSION }}"
114+
body: "Release ${{ env.RELEASE_VERSION }}"

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# amlen-prometheus-exporter
2+
23
Amlen (ex Messagesight) Prometheus exporter
34

45
## Installing with Ansible (recommended option)
@@ -27,3 +28,6 @@ sudo systemctl start amlen_exporter
2728
sudo systemctl enable amlen_exporter
2829
```
2930

31+
## Quickstart to run amlen-server (non-persistent)
32+
33+
docker run -p 9089:9089 -d --name imaserver -d heywood8/amlen-server:1.0.0.1-20220622.1025_eclipse

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ anybadge==1.7.0
33
prometheus-client==0.8.0
44
pyinstaller==3.6
55
requests
6-
charset-normalizer==2.1.0
6+
charset-normalizer==2.1.0
7+
argparse==1.4.0

src/amlen_exporter.py

+50-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
''' Amlen exporter for prometheus '''
2-
import json
3-
import sys
4-
import time
5-
import requests
2+
from json import loads
3+
from time import sleep
4+
from argparse import ArgumentParser
5+
from requests import get
66
from prometheus_client import start_http_server, Metric, REGISTRY
77

88
class JsonServerCollector():
@@ -12,9 +12,9 @@ def __init__(self, endpoint):
1212
def collect(self):
1313
''' Collect metrics'''
1414
try:
15-
response = json.loads(requests.get(self._endpoint, timeout=10).content.decode('UTF-8'))
16-
except Exception as ex:
17-
print(f'Cannot make a request to {self._endpoint} : {type(ex).__name__}')
15+
response = loads(get(self._endpoint, timeout=10).content.decode('UTF-8'))
16+
except Exception as unknown_exception:
17+
print(f'Error: Cannot request {self._endpoint} : {type(unknown_exception).__name__}')
1818
return None
1919

2020
metric = Metric('amlen_server_connections',
@@ -79,9 +79,9 @@ def __init__(self, endpoint):
7979
def collect(self):
8080
''' Collect metrics'''
8181
try:
82-
response = json.loads(requests.get(self._endpoint, timeout=10).content.decode('UTF-8'))
83-
except Exception as ex:
84-
print(f'Cannot make a request to {self._endpoint} : {type(ex).__name__}')
82+
response = loads(get(self._endpoint, timeout=10).content.decode('UTF-8'))
83+
except Exception as unknown_exception:
84+
print(f'Error: Cannot request {self._endpoint} : {type(unknown_exception).__name__}')
8585
return None
8686
memory = response['Memory']
8787
metric = Metric('amlen_memory', 'Memory metrics', 'gauge')
@@ -126,10 +126,10 @@ def collect(self):
126126
metric = Metric('amlen_subscription_message',
127127
'Messages in subscriptions', 'gauge')
128128
try:
129-
response = json.loads(requests.get(self._endpoint, timeout=10, params={})
129+
response = loads(get(self._endpoint, timeout=10, params={})
130130
.content.decode('UTF-8'))
131-
except Exception as ex:
132-
print(f'Cannot make a request to {self._endpoint} : {type(ex).__name__}')
131+
except Exception as unknown_exception:
132+
print(f'Error: Cannot request {self._endpoint} : {type(unknown_exception).__name__}')
133133
return None
134134
# Response example: { "Version":"v1", "Subscription":
135135
# [{"SubName":"DemoSubscription","TopicString":"DemoTopic",
@@ -172,8 +172,8 @@ def collect(self):
172172
yield metric
173173
except KeyError:
174174
print('Error collecting Subscription data: No Subscription key')
175-
except Exception as ex:
176-
print(f'Cannot create subscription metrics: {type(ex).__name__}')
175+
except Exception as unknown_exception:
176+
print(f'Error: Cannot create subscription metrics: {type(unknown_exception).__name__}')
177177
return None
178178

179179
class JsonEndpointCollector():
@@ -185,7 +185,7 @@ def collect(self):
185185

186186
try:
187187
params = {'StatType':'ReadMsgs', 'SubType':'History', 'Duration':6}
188-
response = json.loads(requests.get(self._endpoint, timeout=10, params=params)
188+
response = loads(get(self._endpoint, timeout=10, params=params)
189189
.content.decode('UTF-8'))
190190
metric = Metric('amlen_endpoint_message_rate',
191191
'Messages per second', 'gauge')
@@ -195,7 +195,7 @@ def collect(self):
195195
metric.add_sample('amlen_endpoint_message_rate_incoming', {}, msg_rate)
196196
yield metric
197197

198-
response = json.loads(requests.get(self._endpoint, timeout=10).content.decode('UTF-8'))
198+
response = loads(get(self._endpoint, timeout=10).content.decode('UTF-8'))
199199
endpoints = response['Endpoint']
200200

201201
metric = Metric('amlen_endpoint', 'Endpoint counters', 'counter')
@@ -236,9 +236,9 @@ def collect(self):
236236
#yield metric
237237

238238
except KeyError as keyerr:
239-
print(f'Error collecting Endpoint data: No Endpoint key {keyerr}')
240-
except Exception as ex:
241-
print(f'Cannot make a request to {self._endpoint} : {type(ex).__name__}')
239+
print(f'Error: collecting Endpoint data: No Endpoint key {keyerr}')
240+
except Exception as unknown_exception:
241+
print(f'Error: Cannot request {self._endpoint} : {type(unknown_exception).__name__}')
242242

243243

244244
class JsonInfoCollector():
@@ -248,10 +248,10 @@ def __init__(self, endpoint):
248248
def collect(self):
249249
''' Collect metrics'''
250250
try:
251-
response = json.loads(requests.get(self._endpoint, timeout=10)
251+
response = loads(get(self._endpoint, timeout=10)
252252
.content.decode('UTF-8'))
253-
except Exception as ex:
254-
print(f'Cannot make a request to {self._endpoint} : {type(ex).__name__}')
253+
except Exception as unknown_exception:
254+
print(f'Error: Cannot request {self._endpoint} : {type(unknown_exception).__name__}')
255255
return None
256256
metric = Metric('amlen_info', 'Status metrics counters', 'info')
257257
try:
@@ -275,14 +275,32 @@ def collect(self):
275275
yield metric
276276
return None
277277

278-
279278
if __name__ == '__main__':
280279
# Usage: json_exporter.py port endpoint
281-
start_http_server(int(sys.argv[1]))
282-
REGISTRY.register(JsonServerCollector(sys.argv[2]))
283-
REGISTRY.register(JsonMemoryCollector(sys.argv[2]))
284-
REGISTRY.register(JsonEndpointCollector(sys.argv[2]))
285-
REGISTRY.register(JsonSubscriptionCollector(sys.argv[2]))
286-
REGISTRY.register(JsonInfoCollector(sys.argv[2]))
287-
while True:
288-
time.sleep(1)
280+
parser = ArgumentParser(description='Amlen Prometheus exporter')
281+
parser.add_argument('port', type=int, nargs='?',
282+
default=9672,
283+
help='This exporters\' port. Default: 9672')
284+
parser.add_argument('amlen_address', type=str, nargs='?',
285+
default='localhost:9089',
286+
help='Address of Amlen server. Default: localhost:9089')
287+
parser.add_argument('--once', nargs='?', const=True, default=False,
288+
help='Run once instead of running server')
289+
args = parser.parse_args()
290+
start_http_server(args.port)
291+
REGISTRY.register(JsonServerCollector(args.amlen_address))
292+
REGISTRY.register(JsonMemoryCollector(args.amlen_address))
293+
REGISTRY.register(JsonEndpointCollector(args.amlen_address))
294+
REGISTRY.register(JsonSubscriptionCollector(args.amlen_address))
295+
REGISTRY.register(JsonInfoCollector(args.amlen_address))
296+
try:
297+
selfrequest = get(f'http://localhost:{args.port}'
298+
, timeout=10).content.decode('UTF-8')
299+
except Exception as ex:
300+
print(f'Error: Cannot request localhost:{args.port} : {type(ex).__name__}')
301+
302+
if args.once:
303+
print(selfrequest)
304+
305+
while not args.once:
306+
sleep(1)

0 commit comments

Comments
 (0)