Skip to content

Commit aba6bc8

Browse files
authored
New grpcio Instrumentation (#170)
* Initial RPC test app and pkg dependencies * Propagator Improvements: - New binary propagator for upcoming grpc instrumentation - Fixed handling/processing of level header * Register new Binary propagator * Updated GRPC application for the test suite * Initial GRPC instrumentation and tests * Update tests to follow InstanaSpanContext changes * Update MariaDB to fix py2.7 build issue with Mysql-python * Updated GRPC app with various streaming rpc call support * GRPC instrumentation: streaming call suport * Streaming GRPC tests * GRPC Async/Futures support with tests * Improved exception logging * Test cases for error reporting * Attempt to work-around build bug * Limit grpcio tests to python >3.5
1 parent 74ef6eb commit aba6bc8

20 files changed

+1517
-32
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ version: 2
66
jobs:
77
python27:
88
docker:
9-
- image: circleci/python:2.7.16
9+
- image: circleci/python:2.7.15
1010

1111
# Specify service dependencies here if necessary
1212
# CircleCI maintains a library of pre-built images
1313
# documented at https://circleci.com/docs/2.0/circleci-images/
1414
- image: circleci/postgres:9.6.5-alpine-ram
15-
- image: circleci/mariadb:10-ram
15+
- image: circleci/mariadb:10.1-ram
1616
- image: circleci/redis:5.0.4
1717
- image: rabbitmq:3.5.4
1818

instana/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def boot_agent():
6969
from .instrumentation import mysqlclient
7070

7171
from .instrumentation import flask
72+
from .instrumentation import grpcio
7273
from .instrumentation.tornado import client
7374
from .instrumentation.tornado import server
7475
from .instrumentation import logging

instana/binary_propagator.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from __future__ import absolute_import
2+
3+
import opentracing as ot
4+
5+
from .log import logger
6+
from .util import header_to_id
7+
from .span_context import InstanaSpanContext
8+
9+
10+
class BinaryPropagator():
11+
"""
12+
A Propagator for TEXT_MAP.
13+
"""
14+
HEADER_KEY_T = b'x-instana-t'
15+
HEADER_KEY_S = b'x-instana-s'
16+
HEADER_KEY_L = b'x-instana-l'
17+
18+
def inject(self, span_context, carrier):
19+
try:
20+
trace_id = str.encode(span_context.trace_id)
21+
span_id = str.encode(span_context.span_id)
22+
level = str.encode("1")
23+
24+
if type(carrier) is dict or hasattr(carrier, "__dict__"):
25+
carrier[self.HEADER_KEY_T] = trace_id
26+
carrier[self.HEADER_KEY_S] = span_id
27+
carrier[self.HEADER_KEY_L] = level
28+
elif type(carrier) is list:
29+
carrier.append((self.HEADER_KEY_T, trace_id))
30+
carrier.append((self.HEADER_KEY_S, span_id))
31+
carrier.append((self.HEADER_KEY_L, level))
32+
elif type(carrier) is tuple:
33+
carrier = carrier.__add__(((self.HEADER_KEY_T, trace_id),))
34+
carrier = carrier.__add__(((self.HEADER_KEY_S, span_id),))
35+
carrier = carrier.__add__(((self.HEADER_KEY_L, level),))
36+
elif hasattr(carrier, '__setitem__'):
37+
carrier.__setitem__(self.HEADER_KEY_T, trace_id)
38+
carrier.__setitem__(self.HEADER_KEY_S, span_id)
39+
carrier.__setitem__(self.HEADER_KEY_L, level)
40+
else:
41+
raise Exception("Unsupported carrier type", type(carrier))
42+
43+
return carrier
44+
except Exception:
45+
logger.debug("inject error:", exc_info=True)
46+
47+
def extract(self, carrier): # noqa
48+
trace_id = None
49+
span_id = None
50+
level = None
51+
52+
try:
53+
if type(carrier) is dict or hasattr(carrier, "__getitem__"):
54+
dc = carrier
55+
elif hasattr(carrier, "__dict__"):
56+
dc = carrier.__dict__
57+
elif type(carrier) is list:
58+
dc = dict(carrier)
59+
else:
60+
raise ot.SpanContextCorruptedException()
61+
62+
for key, value in dc.items():
63+
if type(key) is str:
64+
key = str.encode(key)
65+
66+
if self.HEADER_KEY_T == key:
67+
trace_id = header_to_id(value)
68+
elif self.HEADER_KEY_S == key:
69+
span_id = header_to_id(value)
70+
elif self.HEADER_KEY_L == key:
71+
level = value
72+
73+
ctx = None
74+
if trace_id is not None and span_id is not None:
75+
ctx = InstanaSpanContext(span_id=span_id,
76+
trace_id=trace_id,
77+
level=level,
78+
baggage={},
79+
sampled=True)
80+
return ctx
81+
82+
except Exception:
83+
logger.debug("extract error:", exc_info=True)

instana/http_propagator.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import
22

33
import opentracing as ot
4-
from basictracer.context import SpanContext
4+
from .span_context import InstanaSpanContext
55

66
from .log import logger
77
from .util import header_to_id
@@ -62,6 +62,7 @@ def inject(self, span_context, carrier):
6262
def extract(self, carrier): # noqa
6363
trace_id = None
6464
span_id = None
65+
level = 1
6566

6667
try:
6768
if type(carrier) is dict or hasattr(carrier, "__getitem__"):
@@ -82,18 +83,23 @@ def extract(self, carrier): # noqa
8283
trace_id = header_to_id(dc[key])
8384
elif self.LC_HEADER_KEY_S == lc_key:
8485
span_id = header_to_id(dc[key])
86+
elif self.LC_HEADER_KEY_L == lc_key:
87+
level = dc[key]
8588

8689
elif self.ALT_LC_HEADER_KEY_T == lc_key:
8790
trace_id = header_to_id(dc[key])
8891
elif self.ALT_LC_HEADER_KEY_S == lc_key:
8992
span_id = header_to_id(dc[key])
93+
elif self.ALT_LC_HEADER_KEY_L == lc_key:
94+
level = dc[key]
9095

9196
ctx = None
9297
if trace_id is not None and span_id is not None:
93-
ctx = SpanContext(span_id=span_id,
94-
trace_id=trace_id,
95-
baggage={},
96-
sampled=True)
98+
ctx = InstanaSpanContext(span_id=span_id,
99+
trace_id=trace_id,
100+
level=level,
101+
baggage={},
102+
sampled=True)
97103
return ctx
98104

99105
except Exception:

0 commit comments

Comments
 (0)