1# Copyright The OpenTracing Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from __future__ import absolute_import 16import mock 17import time 18import types 19from opentracing import child_of 20from opentracing import Format 21from opentracing import Tracer 22from opentracing import logs 23from opentracing import tags 24 25 26def test_span(): 27 tracer = Tracer() 28 parent = tracer.start_span('parent') 29 child = tracer.start_span('test', references=child_of(parent.context)) 30 assert parent == child 31 child.log_kv({'event': 'cache_hit', 'size.bytes': 42}) 32 child.log_kv({'event': 'cache_miss'}, time.time()) 33 child.log_event('cache_hit', ['arg1', 'arg2']) 34 35 with mock.patch.object(parent, 'finish') as finish: 36 with mock.patch.object(parent, 'log_event') as log_event: 37 with mock.patch.object(parent, 'log_kv') as log_kv: 38 with mock.patch.object(parent, 'set_tag') as set_tag: 39 try: 40 with parent: 41 raise ValueError() 42 except ValueError: 43 pass 44 assert finish.call_count == 1 45 assert log_event.call_count == 0 46 assert log_kv.call_count == 1 47 assert set_tag.call_count == 1 48 49 with mock.patch.object(parent, 'finish') as finish: 50 with mock.patch.object(parent, 'log_event') as log_kv: 51 with parent: 52 pass 53 assert finish.call_count == 1 54 assert log_kv.call_count == 0 55 56 parent.set_tag('x', 'y').set_tag('z', 1) # test chaining 57 parent.set_tag(tags.PEER_SERVICE, 'test-service') 58 parent.set_tag(tags.PEER_HOST_IPV4, 127 << 24 + 1) 59 parent.set_tag(tags.PEER_HOST_IPV6, '::') 60 parent.set_tag(tags.PEER_HOSTNAME, 'uber.com') 61 parent.set_tag(tags.PEER_PORT, 123) 62 parent.finish() 63 64 65def test_span_error_report(): 66 tracer = Tracer() 67 span = tracer.start_span('foo') 68 error_message = 'unexpected_situation' 69 70 with mock.patch.object(span, 'log_kv') as log_kv: 71 with mock.patch.object(span, 'set_tag') as set_tag: 72 try: 73 with span: 74 raise ValueError(error_message) 75 except ValueError: 76 pass 77 78 assert set_tag.call_count == 1 79 assert set_tag.call_args[0] == (tags.ERROR, True) 80 81 assert log_kv.call_count == 1 82 log_kv_args = log_kv.call_args[0][0] 83 assert log_kv_args.get(logs.EVENT, None) is tags.ERROR 84 assert log_kv_args.get(logs.MESSAGE, None) is error_message 85 assert log_kv_args.get(logs.ERROR_KIND, None) is ValueError 86 assert isinstance(log_kv_args.get(logs.ERROR_OBJECT, None), 87 ValueError) 88 assert isinstance(log_kv_args.get(logs.STACK, None), 89 types.TracebackType) 90 91 92def test_inject(): 93 tracer = Tracer() 94 span = tracer.start_span() 95 96 bin_carrier = bytearray() 97 tracer.inject( 98 span_context=span.context, 99 format=Format.BINARY, 100 carrier=bin_carrier) 101 assert bin_carrier == bytearray() 102 103 text_carrier = {} 104 tracer.inject( 105 span_context=span.context, 106 format=Format.TEXT_MAP, 107 carrier=text_carrier) 108 assert text_carrier == {} 109 110 111def test_extract(): 112 tracer = Tracer() 113 noop_span = tracer._noop_span 114 115 bin_carrier = bytearray() 116 span_ctx = tracer.extract(Format.BINARY, carrier=bin_carrier) 117 assert noop_span.context == span_ctx 118 119 text_carrier = {} 120 span_ctx = tracer.extract(Format.TEXT_MAP, carrier=text_carrier) 121 assert noop_span.context == span_ctx 122