Skip to content

Commit

Permalink
Add test for gRPC propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
andremarianiello committed Nov 18, 2018
1 parent 0b2a487 commit df189d8
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
pip3.5 install setuptools
pip3.5 install docker
pip3.5 install docker-compose
pip3.5 install protobuf
pip3.5 install grpcio
sudo mkdir /test-log
sudo chmod a+rwx /test-log
export LOG_DIR=/test-log
Expand Down
22 changes: 14 additions & 8 deletions Dockerfile-test
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ FROM ubuntu:18.04
ARG OPENTRACING_CPP_VERSION=v1.5.0
ARG NGINX_VERSION=1.13.12

COPY . /src

RUN set -x \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
Expand All @@ -25,9 +23,10 @@ RUN set -x \
g++-8 \
### Use gcc-8 (the default gcc has this problem when using with address sanitizer:
### https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84428)
&& update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8 \
&& update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 800 --slave /usr/bin/g++ g++ /usr/bin/g++-8

### Build opentracing-cpp
&& cd / \
RUN cd / \
&& git clone -b $OPENTRACING_CPP_VERSION https://github.com/opentracing/opentracing-cpp.git \
&& cd opentracing-cpp \
&& mkdir .build && cd .build \
Expand All @@ -36,12 +35,16 @@ RUN set -x \
-DCMAKE_SHARED_LINKER_FLAGS="-fno-omit-frame-pointer -fsanitize=address" \
-DCMAKE_EXE_LINKER_FLAGS="-fno-omit-frame-pointer -fsanitize=address" \
-DBUILD_TESTING=OFF .. \
&& make && make install \
&& make && make install

COPY ./opentracing /opentracing

### Build nginx
&& cd /src \
RUN apt-get install -y libssl-dev
RUN cd / \
&& wget -O nginx-release-${NGINX_VERSION}.tar.gz https://github.com/nginx/nginx/archive/release-${NGINX_VERSION}.tar.gz \
&& tar zxf nginx-release-${NGINX_VERSION}.tar.gz \
&& cd /src/nginx-release-${NGINX_VERSION} \
&& cd /nginx-release-${NGINX_VERSION} \
# Temporarily disable leak sanitizer to get around false positives in build
&& export ASAN_OPTIONS=detect_leaks=0 \
&& export CFLAGS="-Wno-error" \
Expand All @@ -50,6 +53,9 @@ RUN set -x \
--with-debug \
--with-cc-opt="-O1 -g -fno-omit-frame-pointer -fsanitize=address" \
--with-ld-opt="-g -fno-omit-frame-pointer -fsanitize=address" \
--add-dynamic-module=/src/opentracing \
# enables grpc
--with-http_v2_module \
--add-dynamic-module=/opentracing \
&& make && make install

CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
5 changes: 4 additions & 1 deletion ci/do_ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ if [[ "$1" == "system.testing" ]]; then
docker build -t nginx-opentracing-test/nginx -f Dockerfile-test .
cd test
docker build -t nginx-opentracing-test/backend -f Dockerfile-backend .
python3 nginx_opentracing_test.py
cd environment/grpc
docker build -t nginx-opentracing-test/grpc-backend .
cd -
PYTHONPATH=environment/grpc python3 nginx_opentracing_test.py
exit 0
elif [[ "$1" == "module.binaries" ]]; then
mkdir -p "${BUILD_DIR}"
Expand Down
13 changes: 13 additions & 0 deletions test/environment/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ services:
- LSAN_OPTIONS=suppressions=/lsan-suppress.txt
expose:
- "8080"
- "8081"
ports:
- "8080:8080"
- "8081:8081"
command:
- /usr/local/nginx/sbin/nginx
- -g
Expand Down Expand Up @@ -55,6 +57,17 @@ services:
ports:
- "9000:9000"

grpc_backend:
image: nginx-opentracing-test/grpc-backend
networks:
testnet:
aliases:
- grpc-backend
expose:
- "50051"
ports:
- "50051:50051"

networks:
testnet: {}

3 changes: 3 additions & 0 deletions test/environment/grpc/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM grpc/python:1.13-onbuild
ENTRYPOINT ["python", "-u"]
CMD ["app.py"]
12 changes: 12 additions & 0 deletions test/environment/grpc/app.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// install grpcio-tools
//
// python3 -m grpc_tools.protoc --proto_path=. --python_out=. --grpc_python_out=. app.proto

syntax = "proto3";

service App {
rpc CheckTraceHeader(Empty) returns (Empty);
}

message Empty {
}
32 changes: 32 additions & 0 deletions test/environment/grpc/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from concurrent import futures
import sys
import signal
import time
import grpc
import app_pb2 as app_messages
import app_pb2_grpc as app_service

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

class AppService(app_service.AppServicer):
def CheckTraceHeader(self, request, context):
metadata = dict(context.invocation_metadata())
if 'x-ot-span-context' not in metadata:
context.set_code(grpc.StatusCode.INTERNAL)
context.set_details("Metadatum x-ot-span-context not found")
return app_messages.Empty()

def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
app_service.add_AppServicer_to_server(AppService(), server)
server.add_insecure_port('0.0.0.0:50051')
server.start()
signal.signal(signal.SIGTERM, signal.getsignal(signal.SIGINT))
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)

if __name__ == '__main__':
serve()
86 changes: 86 additions & 0 deletions test/environment/grpc/app_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions test/environment/grpc/app_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
import grpc

import app_pb2 as app__pb2


class AppStub(object):
# missing associated documentation comment in .proto file
pass

def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.CheckTraceHeader = channel.unary_unary(
'/App/CheckTraceHeader',
request_serializer=app__pb2.Empty.SerializeToString,
response_deserializer=app__pb2.Empty.FromString,
)


class AppServicer(object):
# missing associated documentation comment in .proto file
pass

def CheckTraceHeader(self, request, context):
# missing associated documentation comment in .proto file
pass
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
context.set_details('Method not implemented!')
raise NotImplementedError('Method not implemented!')


def add_AppServicer_to_server(servicer, server):
rpc_method_handlers = {
'CheckTraceHeader': grpc.unary_unary_rpc_method_handler(
servicer.CheckTraceHeader,
request_deserializer=app__pb2.Empty.FromString,
response_serializer=app__pb2.Empty.SerializeToString,
),
}
generic_handler = grpc.method_handlers_generic_handler(
'App', rpc_method_handlers)
server.add_generic_rpc_handlers((generic_handler,))
1 change: 1 addition & 0 deletions test/environment/grpc/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#grpcio-tools
6 changes: 6 additions & 0 deletions test/environment/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ http {
server {
error_log logs/debug.log debug;
listen 8080;
listen 8081 http2;
server_name localhost;

location = / {
Expand Down Expand Up @@ -54,5 +55,10 @@ http {
fastcgi_param SCRIPT_FILENAME /var/www/app.php;
fastcgi_pass php_fpm:9000;
}

location = /App/CheckTraceHeader {
opentracing_grpc_propagate_context;
grpc_pass grpc://grpc_backend:50051;
}
}
}
19 changes: 18 additions & 1 deletion test/nginx_opentracing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import docker
import json
import http.client
import grpc
import app_pb2 as app_messages
import app_pb2_grpc as app_service

class NginxOpenTracingTest(unittest.TestCase):
def setUp(self):
Expand All @@ -26,7 +29,7 @@ def setUp(self):
stderr=subprocess.PIPE)
self.client = docker.from_env()
timeout = time.time() + 60
while len(self.client.containers.list()) != 3:
while len(self.client.containers.list()) != 4:
if time.time() > timeout:
raise TimeoutError()
time.sleep(0.001)
Expand All @@ -36,6 +39,11 @@ def setUp(self):
time.sleep(2)

self.conn = http.client.HTTPConnection('localhost', 8080, timeout=5)
self.grpcConn = grpc.insecure_channel('localhost:8081')
try:
grpc.channel_ready_future(self.grpcConn).result(timeout=10)
except grpc.FutureTimeoutError:
sys.exit('Error connecting to server')
self.running = True

def _logEnvironment(self):
Expand Down Expand Up @@ -68,6 +76,7 @@ def tearDown(self):
os.chdir(self.testdir)
self.client.close()
self.conn.close()
self.grpcConn.close()

def _stopDocker(self):
if not self.running:
Expand Down Expand Up @@ -138,5 +147,13 @@ def testFastcgiPropagation(self):

self.assertEqual(len(self.nginx_traces), 2)

def testGrpcPropagation(self):
app = app_service.AppStub(self.grpcConn)
app.CheckTraceHeader(app_messages.Empty())

self._stopEnvironment()

self.assertEqual(len(self.nginx_traces), 2)

if __name__ == '__main__':
unittest.main(verbosity=2)

0 comments on commit df189d8

Please sign in to comment.