[med-svn] [biomaj3-zipkin] 01/02: New upstream version 0.2.2
Olivier Sallou
osallou at debian.org
Thu Aug 17 14:43:24 UTC 2017
This is an automated email from the git hooks/post-receive script.
osallou pushed a commit to branch master
in repository biomaj3-zipkin.
commit de03b61b858b38bfcca1b26d5fe79a52cdfc3962
Author: Olivier Sallou <osallou at debian.org>
Date: Thu Aug 17 07:52:07 2017 +0000
New upstream version 0.2.2
---
.gitignore | 4 ++
CHANGES.txt | 6 +++
MANIFEST.in | 1 +
README.md | 3 ++
biomaj_zipkin/__init__.py | 0
biomaj_zipkin/zipkin.py | 121 ++++++++++++++++++++++++++++++++++++++++++++++
setup.cfg | 2 +
setup.py | 56 +++++++++++++++++++++
test.py | 28 +++++++++++
9 files changed, 221 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0f89f7d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+*.egg-info
+**/*.pyc
+build
+dist
diff --git a/CHANGES.txt b/CHANGES.txt
new file mode 100644
index 0000000..cf3c28d
--- /dev/null
+++ b/CHANGES.txt
@@ -0,0 +1,6 @@
+0.2.2:
+ add missing README
+0.2.1:
+ Fix span getter
+0.2.0:
+ First creation
diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..376b77a
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1 @@
+include *.txt *.md
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..eaafe46
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# About
+
+This library adds zipkin integration to BioMAJ to track micro services communications and follow workflow progress and timing.
diff --git a/biomaj_zipkin/__init__.py b/biomaj_zipkin/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/biomaj_zipkin/zipkin.py b/biomaj_zipkin/zipkin.py
new file mode 100644
index 0000000..5608f0c
--- /dev/null
+++ b/biomaj_zipkin/zipkin.py
@@ -0,0 +1,121 @@
+import requests
+import logging
+import time
+from datetime import datetime
+import socket
+
+from py_zipkin.util import generate_random_64bit_string
+
+class Zipkin(object):
+
+ @classmethod
+ def set_config(cls, cfg):
+ Zipkin.cfg = cfg
+
+ def __init__(self, service_name, span_name, trace_id=None, parent_id=None, is_sampled=False):
+ if trace_id:
+ self.trace_id = trace_id
+ else:
+ self.trace_id = generate_random_64bit_string()
+ self.span_id = generate_random_64bit_string()
+ self.parent_id = parent_id
+ self.is_sampled = is_sampled
+ self.flags = 0
+ self.annotations = []
+ self.binaryAnnotations = []
+ self.service_name = service_name
+ self.span_name = span_name
+ start_time = datetime.now()
+ self.timestamp = time.mktime(start_time.timetuple()) * 1000 * 1000 # micro seconds
+ self.annotations.append({
+ 'endpoint': {
+ 'serviceName': self.service_name
+ },
+ 'timestamp': self.timestamp,
+ 'value': 'sr'
+ })
+
+ def get_trace_id(self):
+ return self.trace_id
+
+ def get_span_id(self):
+ return self.span_id
+
+ def add_binary_annotation(self, key, value):
+ if not key or not value:
+ return
+ self.binaryAnnotations.append({
+ 'key': key,
+ 'value': value
+ })
+
+ def add_send_annotation(self, endpoint, key=None, value=None):
+ cur_time = datetime.now()
+ timestamp = time.mktime(cur_time.timetuple()) * 1000 * 1000 # micro seconds
+ self.annotations.append({
+ 'endpoint': {
+ 'serviceName': endpoint
+ },
+ 'timestamp': timestamp,
+ 'value': 'cs'
+ })
+ if key and value:
+ self.binaryAnnotations.append({
+ 'endpoint': {
+ 'serviceName': endpoint
+ },
+ 'key': key,
+ 'value': value
+ })
+
+ def add_receive_annotation(self, endpoint, key=None, value=None):
+ cur_time = datetime.now()
+ timestamp = time.mktime(cur_time.timetuple()) * 1000 * 1000 # micro seconds
+ self.annotations.append({
+ 'endpoint': {
+ 'serviceName': endpoint
+ },
+ 'timestamp': timestamp,
+ 'value': 'cr'
+ })
+ if key and value:
+ self.binaryAnnotations.append({
+ 'endpoint': {
+ 'serviceName': endpoint
+ },
+ 'key': key,
+ 'value': value
+ })
+
+ def trace(self):
+ if 'zipkin' not in Zipkin.cfg or 'url' not in Zipkin.cfg['zipkin'] or not Zipkin.cfg['zipkin']['url']:
+ logging.warn('Zipkin not configured, skipping...')
+ return
+ end_time = datetime.now()
+ end_timestamp = time.mktime(end_time.timetuple()) * 1000 * 1000 # micro seconds
+
+ span = {
+ 'traceId': self.trace_id,
+ 'name': self.span_name,
+ 'parentId': self.parent_id,
+ 'id': self.span_id,
+ 'timestamp': self.timestamp,
+ 'duration': end_timestamp - self.timestamp,
+ 'debug': self.is_sampled,
+ 'annotations': self.annotations,
+ 'binaryAnnotations': self.binaryAnnotations
+ }
+ self.annotations.append({
+ 'endpoint': {
+ 'serviceName': self.service_name
+ },
+ 'timestamp': end_timestamp,
+ 'value': 'ss'
+ })
+
+ r = requests.post(Zipkin.cfg['zipkin']['url'] + '/api/v1/spans', json=[span])
+ if not r.status_code == 202:
+ logging.error('Failed to send span')
+ logging.debug(r.status_code)
+ return
+ logging.debug(r.text)
diff --git a/setup.cfg b/setup.cfg
new file mode 100644
index 0000000..3c6e79c
--- /dev/null
+++ b/setup.cfg
@@ -0,0 +1,2 @@
+[bdist_wheel]
+universal=1
diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..d945ce2
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,56 @@
+try:
+ from setuptools import setup, find_packages
+except ImportError:
+ from distutils.core import setup
+
+from distutils.command.install import install
+import os
+
+
+here = os.path.abspath(os.path.dirname(__file__))
+with open(os.path.join(here, 'README.md')) as f:
+ README = f.read()
+with open(os.path.join(here, 'CHANGES.txt')) as f:
+ CHANGES = f.read()
+
+
+config = {
+ 'description': 'BioMAJ zipkin log tracing library',
+ 'long_description': README + '\n\n' + CHANGES,
+ 'author': 'Olivier Sallou',
+ 'url': 'http://biomaj.genouest.org',
+ 'download_url': 'http://biomaj.genouest.org',
+ 'author_email': 'olivier.sallou at irisa.fr',
+ 'version': '0.2.2',
+ 'classifiers': [
+ # How mature is this project? Common values are
+ # 3 - Alpha
+ # 4 - Beta
+ # 5 - Production/Stable
+ 'Development Status :: 3 - Alpha',
+ 'Environment :: Console',
+ 'Natural Language :: English',
+ 'Operating System :: POSIX :: Linux',
+ # Indicate who your project is intended for
+ 'Intended Audience :: Science/Research',
+ 'Topic :: Scientific/Engineering :: Bio-Informatics',
+ # Pick your license as you wish (should match "license" above)
+ 'License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)',
+ # Specify the Python versions you support here. In particular, ensure
+ # that you indicate whether you support Python 2, Python 3 or both.
+ 'Programming Language :: Python :: 3',
+ 'Programming Language :: Python :: 3.4'
+ ],
+ 'install_requires': [
+ 'py-zipkin',
+ 'requests'
+ ],
+ 'tests_require': ['nose'],
+ 'test_suite': 'nose.collector',
+ 'packages': find_packages(),
+ 'include_package_data': True,
+ 'scripts': [],
+ 'name': 'biomaj_zipkin'
+}
+
+setup(**config)
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..7b1f579
--- /dev/null
+++ b/test.py
@@ -0,0 +1,28 @@
+import os
+import logging
+import time
+
+from biomaj_zipkin.zipkin import Zipkin
+
+logging.basicConfig(level=logging.DEBUG)
+
+
+cfg = {
+ 'zipkin': {
+ 'url': 'http://localhost:9411'
+ }
+}
+
+Zipkin.set_config(cfg)
+
+span = Zipkin('test-biomaj', 'maintrace')
+span.add_binary_annotation('received something', 'blabla')
+
+subspan = Zipkin('sub1-biomaj', 'sub1', trace_id= span.get_trace_id(), parent_id=span.get_span_id())
+time.sleep(2)
+subspan.trace()
+subspan2 = Zipkin('sub2-biomaj', 'sub2', trace_id= span.get_trace_id(), parent_id=span.get_span_id())
+time.sleep(2)
+subspan2.trace()
+time.sleep(1)
+span.trace()
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/biomaj3-zipkin.git
More information about the debian-med-commit
mailing list