forked from open-telemetry/opentelemetry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pymongo_integration.py
More file actions
187 lines (160 loc) · 6.49 KB
/
test_pymongo_integration.py
File metadata and controls
187 lines (160 loc) · 6.49 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
# Copyright 2019, OpenTelemetry Authors
#
# 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 unittest
from unittest import mock
from opentelemetry import trace as trace_api
from opentelemetry.ext.pymongo import CommandTracer, trace_integration
from opentelemetry.util import time_ns
class TestPymongoIntegration(unittest.TestCase):
def test_trace_integration(self):
mock_register = mock.Mock()
patch = mock.patch(
"pymongo.monitoring.register", side_effect=mock_register
)
mock_tracer = MockTracer()
with patch:
trace_integration(mock_tracer)
self.assertTrue(mock_register.called)
def test_started(self):
command_attrs = {
"filter": "filter",
"sort": "sort",
"limit": "limit",
"pipeline": "pipeline",
"command_name": "find",
}
mock_tracer = MockTracer()
command_tracer = CommandTracer(mock_tracer)
mock_event = MockEvent(
command_attrs, ("test.com", "1234"), "test_request_id"
)
command_tracer.started(event=mock_event)
# pylint: disable=protected-access
span = command_tracer._get_span(mock_event)
self.assertIs(span.kind, trace_api.SpanKind.CLIENT)
self.assertEqual(span.name, "mongodb.command_name.find")
self.assertEqual(span.attributes["component"], "mongodb")
self.assertEqual(span.attributes["db.type"], "mongodb")
self.assertEqual(span.attributes["db.instance"], "database_name")
self.assertEqual(span.attributes["db.statement"], "command_name find")
self.assertEqual(span.attributes["net.peer.name"], "test.com")
self.assertEqual(span.attributes["net.peer.port"], "1234")
self.assertEqual(
span.attributes["db.mongo.operation_id"], "operation_id"
)
self.assertEqual(
span.attributes["db.mongo.request_id"], "test_request_id"
)
self.assertEqual(span.attributes["db.mongo.filter"], "filter")
self.assertEqual(span.attributes["db.mongo.sort"], "sort")
self.assertEqual(span.attributes["db.mongo.limit"], "limit")
self.assertEqual(span.attributes["db.mongo.pipeline"], "pipeline")
def test_succeeded(self):
mock_tracer = MockTracer()
mock_event = MockEvent({})
command_tracer = CommandTracer(mock_tracer)
command_tracer.started(event=mock_event)
# pylint: disable=protected-access
span = command_tracer._get_span(mock_event)
command_tracer.succeeded(event=mock_event)
self.assertEqual(
span.attributes["db.mongo.duration_micros"], "duration_micros"
)
self.assertIs(
span.status.canonical_code, trace_api.status.StatusCanonicalCode.OK
)
self.assertEqual(span.status.description, "reply")
self.assertIsNotNone(span.end_time)
def test_failed(self):
mock_tracer = MockTracer()
mock_event = MockEvent({})
command_tracer = CommandTracer(mock_tracer)
command_tracer.started(event=mock_event)
# pylint: disable=protected-access
span = command_tracer._get_span(mock_event)
command_tracer.failed(event=mock_event)
self.assertEqual(
span.attributes["db.mongo.duration_micros"], "duration_micros"
)
self.assertIs(
span.status.canonical_code,
trace_api.status.StatusCanonicalCode.UNKNOWN,
)
self.assertEqual(span.status.description, "failure")
self.assertIsNotNone(span.end_time)
def test_multiple_commands(self):
mock_tracer = MockTracer()
first_mock_event = MockEvent({}, ("firstUrl", "123"), "first")
second_mock_event = MockEvent({}, ("secondUrl", "456"), "second")
command_tracer = CommandTracer(mock_tracer)
command_tracer.started(event=first_mock_event)
# pylint: disable=protected-access
first_span = command_tracer._get_span(first_mock_event)
command_tracer.started(event=second_mock_event)
# pylint: disable=protected-access
second_span = command_tracer._get_span(second_mock_event)
command_tracer.succeeded(event=first_mock_event)
command_tracer.failed(event=second_mock_event)
self.assertEqual(first_span.attributes["db.mongo.request_id"], "first")
self.assertIs(
first_span.status.canonical_code,
trace_api.status.StatusCanonicalCode.OK,
)
self.assertEqual(
second_span.attributes["db.mongo.request_id"], "second"
)
self.assertIs(
second_span.status.canonical_code,
trace_api.status.StatusCanonicalCode.UNKNOWN,
)
class MockCommand:
def __init__(self, command_attrs):
self.command_attrs = command_attrs
def get(self, key, default=""):
return self.command_attrs.get(key, default)
class MockEvent:
def __init__(self, command_attrs, connection_id=None, request_id=""):
self.command = MockCommand(command_attrs)
self.connection_id = connection_id
self.request_id = request_id
def __getattr__(self, item):
return item
class MockSpan:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
return False
def __init__(self):
self.status = None
self.name = ""
self.kind = trace_api.SpanKind.INTERNAL
self.attributes = None
self.end_time = None
def set_attribute(self, key, value):
self.attributes[key] = value
def set_status(self, status):
self.status = status
def end(self, end_time=None):
self.end_time = end_time if end_time is not None else time_ns()
class MockTracer:
def __init__(self):
self.end_span = mock.Mock()
# pylint: disable=no-self-use
def start_span(self, name, kind):
span = MockSpan()
span.attributes = {}
span.status = None
span.name = name
span.kind = kind
return span