summaryrefslogtreecommitdiff
path: root/tests/unit/test_tracing.py
blob: 00ef85e360227a56719a9b25f71295ec36b6f498 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# Copyright 2022 Acme Gating, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import time

from tests.base import iterate_timeout, ZuulTestCase

import zuul.lib.tracing as tracing
from opentelemetry import trace


def attributes_to_dict(attrlist):
    ret = {}
    for attr in attrlist:
        ret[attr.key] = attr.value.string_value
    return ret


class TestTracing(ZuulTestCase):
    config_file = 'zuul-tracing.conf'
    tenant_config_file = "config/single-tenant/main.yaml"

    def _waitForSpans(self, *span_names, timeout=60,):
        for _ in iterate_timeout(timeout, "requests to arrive"):
            test_requests = [
                r for r in self.otlp.requests
                if r.resource_spans[0].scope_spans[0].spans[0].name
                in span_names
            ]
            if len(test_requests) == len(span_names):
                return test_requests

    def test_tracing_api(self):
        tracer = trace.get_tracer("zuul")

        # We have a lot of timestamps stored as floats, so make sure
        # our root span is a ZuulSpan that can handle that input.
        span_info = tracing.startSavedSpan('parent-trace',
                                           start_time=time.time(),
                                           attributes={'startattr': 'bar'},
                                           include_attributes=True)

        # Simulate a reconstructed root span
        span = tracing.restoreSpan(span_info)

        # Within the root span, use the more typical OpenTelemetry
        # context manager api.
        with trace.use_span(span):
            with tracer.start_span('child1-trace') as child1_span:
                link = trace.Link(child1_span.context,
                                  attributes={'relationship': 'prev'})

        # Make sure that we can manually start and stop a child span,
        # and that it is a ZuulSpan as well.
        with trace.use_span(span):
            child = tracer.start_span('child2-trace', start_time=time.time(),
                                      links=[link])
            child.end(end_time=time.time())

        # Make sure that we can start a child span from a span
        # context and not a full span:
        span_context = tracing.getSpanContext(span)
        with tracing.startSpanInContext(span_context, 'child3-trace') as child:
            child.end(end_time=time.time())

        # End our root span manually.
        tracing.endSavedSpan(span_info, end_time=time.time(),
                             attributes={'endattr': 'baz'})

        test_requests = self._waitForSpans(
            "parent-trace", "child1-trace", "child2-trace", "child3-trace")

        req1 = test_requests[0]
        self.log.debug("Received:\n%s", req1)
        attrs = attributes_to_dict(req1.resource_spans[0].resource.attributes)
        self.assertEqual({"service.name": "zuultest"}, attrs)
        self.assertEqual("zuul",
                         req1.resource_spans[0].scope_spans[0].scope.name)
        span1 = req1.resource_spans[0].scope_spans[0].spans[0]
        self.assertEqual("child1-trace", span1.name)

        req2 = test_requests[1]
        self.log.debug("Received:\n%s", req2)
        span2 = req2.resource_spans[0].scope_spans[0].spans[0]
        self.assertEqual("child2-trace", span2.name)
        self.assertEqual(span2.links[0].span_id, span1.span_id)
        attrs = attributes_to_dict(span2.links[0].attributes)
        self.assertEqual({"relationship": "prev"}, attrs)

        req3 = test_requests[2]
        self.log.debug("Received:\n%s", req3)
        span3 = req3.resource_spans[0].scope_spans[0].spans[0]
        self.assertEqual("child3-trace", span3.name)

        req4 = test_requests[3]
        self.log.debug("Received:\n%s", req4)
        span4 = req4.resource_spans[0].scope_spans[0].spans[0]
        self.assertEqual("parent-trace", span4.name)
        attrs = attributes_to_dict(span4.attributes)
        self.assertEqual({"startattr": "bar",
                          "endattr": "baz"}, attrs)

        self.assertEqual(span1.trace_id, span4.trace_id)
        self.assertEqual(span2.trace_id, span4.trace_id)
        self.assertEqual(span3.trace_id, span4.trace_id)

    def test_tracing_api_null(self):
        tracer = trace.get_tracer("zuul")

        # Test that restoring spans and span contexts works with
        # null values.

        span_info = None
        # Simulate a reconstructed root span from a null value
        span = tracing.restoreSpan(span_info)

        # Within the root span, use the more typical OpenTelemetry
        # context manager api.
        with trace.use_span(span):
            with tracer.start_span('child1-trace') as child1_span:
                link = trace.Link(child1_span.context,
                                  attributes={'relationship': 'prev'})

        # Make sure that we can manually start and stop a child span,
        # and that it is a ZuulSpan as well.
        with trace.use_span(span):
            child = tracer.start_span('child2-trace', start_time=time.time(),
                                      links=[link])
            child.end(end_time=time.time())

        # Make sure that we can start a child span from a null span
        # context:
        span_context = None
        with tracing.startSpanInContext(span_context, 'child3-trace') as child:
            child.end(end_time=time.time())

        # End our root span manually.
        span.end(end_time=time.time())

        test_requests = self._waitForSpans(
            "child1-trace", "child2-trace", "child3-trace")

        req1 = test_requests[0]
        self.log.debug("Received:\n%s", req1)
        attrs = attributes_to_dict(req1.resource_spans[0].resource.attributes)
        self.assertEqual({"service.name": "zuultest"}, attrs)
        self.assertEqual("zuul",
                         req1.resource_spans[0].scope_spans[0].scope.name)
        span1 = req1.resource_spans[0].scope_spans[0].spans[0]
        self.assertEqual("child1-trace", span1.name)

        req2 = test_requests[1]
        self.log.debug("Received:\n%s", req2)
        span2 = req2.resource_spans[0].scope_spans[0].spans[0]
        self.assertEqual("child2-trace", span2.name)
        self.assertEqual(span2.links[0].span_id, span1.span_id)
        attrs = attributes_to_dict(span2.links[0].attributes)
        self.assertEqual({"relationship": "prev"}, attrs)

        req3 = test_requests[2]
        self.log.debug("Received:\n%s", req3)
        span3 = req3.resource_spans[0].scope_spans[0].spans[0]
        self.assertEqual("child3-trace", span3.name)

        self.assertNotEqual(span1.trace_id, span2.trace_id)
        self.assertNotEqual(span2.trace_id, span3.trace_id)
        self.assertNotEqual(span1.trace_id, span3.trace_id)

    def test_tracing(self):
        A = self.fake_gerrit.addFakeChange('org/project', 'master', 'A')
        A.addApproval('Code-Review', 2)
        self.fake_gerrit.addEvent(A.addApproval('Approved', 1))
        self.waitUntilSettled()

        for _ in iterate_timeout(60, "request to arrive"):
            if len(self.otlp.requests) >= 2:
                break

        buildset = self.getSpan('BuildSet')
        self.log.debug("Received:\n%s", buildset)
        item = self.getSpan('QueueItem')
        self.log.debug("Received:\n%s", item)
        merge_job = self.getSpan('Merge')
        self.log.debug("Received:\n%s", merge_job)
        build = self.getSpan('Build')
        self.log.debug("Received:\n%s", build)
        job = self.getSpan('JobExecution')
        self.log.debug("Received:\n%s", job)
        self.assertEqual(item.trace_id, buildset.trace_id)
        self.assertEqual(item.trace_id, build.trace_id)
        self.assertNotEqual(item.span_id, job.span_id)
        self.assertTrue(buildset.start_time_unix_nano >=
                        item.start_time_unix_nano)
        self.assertTrue(buildset.end_time_unix_nano <=
                        item.end_time_unix_nano)
        self.assertTrue(merge_job.start_time_unix_nano >=
                        buildset.start_time_unix_nano)
        self.assertTrue(merge_job.end_time_unix_nano <=
                        buildset.end_time_unix_nano)
        item_attrs = attributes_to_dict(item.attributes)
        self.assertTrue(item_attrs['ref_number'] == "1")
        self.assertTrue(item_attrs['ref_patchset'] == "1")
        self.assertTrue('zuul_event_id' in item_attrs)

    def getSpan(self, name):
        for req in self.otlp.requests:
            span = req.resource_spans[0].scope_spans[0].spans[0]
            if span.name == name:
                return span