forked from elastic/apm-agent-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
executable file
·207 lines (165 loc) · 8.01 KB
/
tests.py
File metadata and controls
executable file
·207 lines (165 loc) · 8.01 KB
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
# BSD 3-Clause License
#
# Copyright (c) 2019, Elasticsearch BV
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import pytest # isort:skip
pytest.importorskip("opentelemetry.sdk") # isort:skip
from opentelemetry.trace import Link, SpanContext, SpanKind, TraceFlags
from opentelemetry.trace.propagation import _SPAN_KEY
from opentelemetry.trace.status import Status, StatusCode
import elasticapm.contrib.opentelemetry.context as context
import elasticapm.contrib.opentelemetry.trace as trace
from elasticapm.conf import constants
from elasticapm.contrib.opentelemetry.trace import Tracer
pytestmark = pytest.mark.opentelemetry
@pytest.fixture
def tracer(elasticapm_client) -> Tracer:
yield Tracer("test", elasticapm_client=elasticapm_client)
def test_root_transaction(tracer: Tracer):
with tracer.start_as_current_span("test"):
pass
client = tracer.client
transaction = client.events[constants.TRANSACTION][0]
assert transaction["type"] == "unknown"
assert transaction["name"] == "test"
assert transaction["result"] == "OK"
def test_ot_span(tracer: Tracer):
with tracer.start_as_current_span("test"):
with tracer.start_as_current_span("testspan", kind=SpanKind.CONSUMER):
with tracer.start_as_current_span("testspan2"):
pass
client = tracer.client
transaction = client.events[constants.TRANSACTION][0]
span1 = client.events[constants.SPAN][1]
span2 = client.events[constants.SPAN][0]
assert span1["transaction_id"] == span1["parent_id"] == transaction["id"]
assert span1["name"] == "testspan"
assert span2["transaction_id"] == transaction["id"]
assert span2["parent_id"] == span1["id"]
assert span2["name"] == "testspan2"
def test_ot_span_without_auto_attach(tracer: Tracer):
with tracer.start_as_current_span("test"):
span = tracer.start_span("testspan", kind=SpanKind.CONSUMER)
with tracer.start_as_current_span("testspan2"):
pass
with trace.use_span(span, end_on_exit=True):
with tracer.start_as_current_span("testspan3"):
pass
client = tracer.client
transaction = client.events[constants.TRANSACTION][0]
# The spans come in out of the normal/intuitive order, since "testspan2" ends first.
span1 = client.events[constants.SPAN][2]
span2 = client.events[constants.SPAN][0]
span3 = client.events[constants.SPAN][1]
assert span1["transaction_id"] == span1["parent_id"] == transaction["id"]
assert span1["name"] == "testspan"
assert span2["transaction_id"] == span2["parent_id"] == transaction["id"]
assert span2["name"] == "testspan2"
assert span3["transaction_id"] == transaction["id"]
assert span3["parent_id"] == span1["id"]
assert span3["name"] == "testspan3"
def test_ot_transaction_types(tracer: Tracer):
with tracer.start_as_current_span(
"test_request", attributes={"http.url": "http://localhost"}, kind=SpanKind.SERVER
):
pass
with tracer.start_as_current_span(
"test_messaging", attributes={"messaging.system": "kafka"}, kind=SpanKind.CONSUMER
):
pass
client = tracer.client
transaction1 = client.events[constants.TRANSACTION][0]
transaction2 = client.events[constants.TRANSACTION][1]
assert transaction1["type"] == "request"
assert transaction2["type"] == "messaging"
assert transaction1["otel"]["span_kind"] == "SERVER"
assert transaction1["otel"]["attributes"]["http.url"] == "http://localhost"
def test_ot_exception(tracer: Tracer):
with pytest.raises(Exception):
with tracer.start_as_current_span("test"):
raise Exception()
client = tracer.client
error = client.events[constants.ERROR][0]
assert error["context"]["otel_spankind"] == "INTERNAL"
def test_ot_spancontext(tracer: Tracer):
with tracer.start_as_current_span("test") as ot_span:
span_context = ot_span.get_span_context()
assert isinstance(span_context, SpanContext)
assert span_context.trace_id
assert span_context.trace_flags.sampled == TraceFlags.SAMPLED
def test_span_links(tracer: Tracer):
span_context = SpanContext(
trace_id=int("aabbccddeeff00112233445566778899", 16), span_id=int("0011223344556677", 16), is_remote=False
)
link = Link(span_context)
with tracer.start_as_current_span("testtransaction", links=[link]):
with tracer.start_as_current_span("testspan", links=[link]):
pass
client = tracer.client
transaction = client.events[constants.TRANSACTION][0]
span = client.events[constants.SPAN][0]
assert transaction["links"][0]["trace_id"] == "aabbccddeeff00112233445566778899"
assert transaction["links"][0]["span_id"] == "0011223344556677"
assert span["links"][0]["trace_id"] == "aabbccddeeff00112233445566778899"
assert span["links"][0]["span_id"] == "0011223344556677"
def test_set_status_with_status_object(tracer: Tracer):
"""Test set_status with Status object (original API)"""
with tracer.start_as_current_span("test") as span:
span.set_status(Status(StatusCode.OK))
client = tracer.client
transaction = client.events[constants.TRANSACTION][0]
assert transaction["outcome"] == "success"
def test_set_status_with_status_code(tracer: Tracer):
"""Test set_status with StatusCode enum (new API)"""
with tracer.start_as_current_span("test") as span:
span.set_status(StatusCode.ERROR)
client = tracer.client
transaction = client.events[constants.TRANSACTION][0]
assert transaction["outcome"] == "failure"
def test_set_status_with_status_code_and_description(tracer: Tracer):
"""Test set_status with StatusCode enum and optional description"""
with tracer.start_as_current_span("test") as span:
span.set_status(StatusCode.OK, "Everything is fine")
client = tracer.client
transaction = client.events[constants.TRANSACTION][0]
assert transaction["outcome"] == "success"
def test_set_status_unset(tracer: Tracer):
"""Test set_status with UNSET status code"""
with tracer.start_as_current_span("test") as span:
span.set_status(StatusCode.UNSET)
client = tracer.client
transaction = client.events[constants.TRANSACTION][0]
assert transaction["outcome"] == "unknown"
def test_set_status_on_span(tracer: Tracer):
"""Test set_status on a child span (not transaction)"""
with tracer.start_as_current_span("test"):
with tracer.start_as_current_span("testspan") as span:
span.set_status(StatusCode.ERROR)
client = tracer.client
span_event = client.events[constants.SPAN][0]
assert span_event["outcome"] == "failure"