-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathros_zipkin_collector.py
executable file
·48 lines (38 loc) · 1.43 KB
/
ros_zipkin_collector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
from __future__ import absolute_import, print_function
import logging
import time
import json
import requests
import rospy
from std_msgs.msg import String
class RosZipkinCollector(object):
def __init__(self):
rospy.init_node('zipkin_collector', log_level=rospy.INFO)
zipkin_port = rospy.get_param("~zipkin_port", 9411)
zipkin_host = rospy.get_param("~zipkin_host", "localhost")
self.zipkin_url = "http://{}:{}/api/v2/spans".format(zipkin_host, zipkin_port)
rospy.loginfo("sending spans to {}".format(self.zipkin_url))
rospy.Subscriber("/spans", String, self.spans_callback)
def spin(self):
rospy.spin()
def spans_callback(self, msg):
rospy.logdebug("got span: {}".format(msg.data))
try:
j = json.loads(msg.data)
except ValueError as e:
rospy.logerr("could not parse span: {}".format(e))
return
try:
r = requests.post(self.zipkin_url, json=j, timeout=0.1)
except (requests.RequestException, requests.exceptions.BaseHTTPError) as e:
rospy.logwarn("Could not post zipkin span: {}".format(e))
return
if r.status_code != 202:
rospy.logwarn("post spans failed: {}".format(r.text))
if __name__ == '__main__':
try:
collector = RosZipkinCollector()
collector.spin()
except rospy.ROSInterruptException:
pass