diff options
author | Tovin Seven <vinhnt@vn.fujitsu.com> | 2017-07-04 11:09:06 +0700 |
---|---|---|
committer | Tovin Seven <vinhnt@vn.fujitsu.com> | 2018-05-29 06:03:46 +0000 |
commit | 513a7c88a76f5b805de60a0a1b25b9b41e10a6d4 (patch) | |
tree | a97c6c1a3aad0a6c427932ac101eee0aee833044 /osprofiler/tests | |
parent | e1ba98e13a598f2df7d3f18376680e372adc309d (diff) | |
download | osprofiler-513a7c88a76f5b805de60a0a1b25b9b41e10a6d4.tar.gz |
OSprofiler with Jaeger Tracing as backend
In this patch, I use Uber Jaeger as a tracer.
Trace/span from OSprofiler will be sent to Jaeger.
We can use Jaeger UI to view trace.
A sample image of trace in Jaeger:
https://tovin07.github.io/opentracing/jaeger-openstack-image-list.png
Reference:
- OpenTracing: http://opentracing.io/
- Uber Jager: https://www.jaegertracing.io/docs/
Depends-On: Iaac3ac3853867a67f7e386e762e0522076235daf
Change-Id: I8f75af487f50d5e67a1321a4c951d2a0d85733a1
Implements: blueprint opentracing-compatible
Diffstat (limited to 'osprofiler/tests')
-rw-r--r-- | osprofiler/tests/unit/drivers/test_jaeger.py | 78 | ||||
-rw-r--r-- | osprofiler/tests/unit/test_profiler.py | 12 | ||||
-rw-r--r-- | osprofiler/tests/unit/test_utils.py | 24 |
3 files changed, 113 insertions, 1 deletions
diff --git a/osprofiler/tests/unit/drivers/test_jaeger.py b/osprofiler/tests/unit/drivers/test_jaeger.py new file mode 100644 index 0000000..661d75b --- /dev/null +++ b/osprofiler/tests/unit/drivers/test_jaeger.py @@ -0,0 +1,78 @@ +# Copyright 2018 Fujitsu Ltd. +# All Rights Reserved. +# +# 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 mock + +from osprofiler.drivers import jaeger +from osprofiler.tests import test + + +class JaegerTestCase(test.TestCase): + + def setUp(self): + super(JaegerTestCase, self).setUp() + self.payload_start = { + "name": "api-start", + "base_id": "4e3e0ec6-2938-40b1-8504-09eb1d4b0dee", + "trace_id": "1c089ea8-28fe-4f3d-8c00-f6daa2bc32f1", + "parent_id": "e2715537-3d1c-4f0c-b3af-87355dc5fc5b", + "timestamp": "2018-05-03T04:31:51.781381", + "info": { + "host": "test" + } + } + + self.payload_stop = { + "name": "api-stop", + "base_id": "4e3e0ec6-2938-40b1-8504-09eb1d4b0dee", + "trace_id": "1c089ea8-28fe-4f3d-8c00-f6daa2bc32f1", + "parent_id": "e2715537-3d1c-4f0c-b3af-87355dc5fc5b", + "timestamp": "2018-05-03T04:31:51.781381", + "info": { + "host": "test", + "function": { + "result": 1 + } + } + } + + self.driver = jaeger.Jaeger("jaeger://127.0.0.1:6831", + project="nova", service="api") + + @mock.patch("osprofiler._utils.shorten_id") + def test_notify_start(self, mock_shorten_id): + self.driver.notify(self.payload_start) + calls = [ + mock.call(self.payload_start["base_id"]), + mock.call(self.payload_start["parent_id"]), + mock.call(self.payload_start["trace_id"]) + ] + mock_shorten_id.assert_has_calls(calls, any_order=True) + + @mock.patch("jaeger_client.span.Span") + @mock.patch("time.time") + def test_notify_stop(self, mock_time, mock_span): + fake_time = 1525416065.5958152 + mock_time.return_value = fake_time + + span = mock_span() + self.driver.spans.append(mock_span()) + + self.driver.notify(self.payload_stop) + + mock_time.assert_called_once() + mock_time.reset_mock() + + span.finish.assert_called_once_with(finish_time=fake_time) diff --git a/osprofiler/tests/unit/test_profiler.py b/osprofiler/tests/unit/test_profiler.py index 943760a..96f0654 100644 --- a/osprofiler/tests/unit/test_profiler.py +++ b/osprofiler/tests/unit/test_profiler.py @@ -62,6 +62,13 @@ class ProfilerGlobMethodsTestCase(test.TestCase): class ProfilerTestCase(test.TestCase): + def test_profiler_get_shorten_id(self): + uuid_id = "4e3e0ec6-2938-40b1-8504-09eb1d4b0dee" + prof = profiler._Profiler("secret", base_id="1", parent_id="2") + result = prof.get_shorten_id(uuid_id) + expected = "850409eb1d4b0dee" + self.assertEqual(expected, result) + def test_profiler_get_base_id(self): prof = profiler._Profiler("secret", base_id="1", parent_id="2") self.assertEqual(prof.get_base_id(), "1") @@ -167,7 +174,10 @@ class WithTraceTestCase(test.TestCase): self.assertRaises(ValueError, foo) mock_start.assert_called_once_with("foo", info=None) - mock_stop.assert_called_once_with(info={"etype": "ValueError"}) + mock_stop.assert_called_once_with(info={ + "etype": "ValueError", + "message": "bar" + }) @profiler.trace("function", info={"info": "some_info"}) diff --git a/osprofiler/tests/unit/test_utils.py b/osprofiler/tests/unit/test_utils.py index 19aff30..ffb7b09 100644 --- a/osprofiler/tests/unit/test_utils.py +++ b/osprofiler/tests/unit/test_utils.py @@ -16,6 +16,7 @@ import base64 import hashlib import hmac +import uuid import mock @@ -111,6 +112,29 @@ class UtilsTestCase(test.TestCase): self.assertIsNone(utils.signed_unpack(data, hmac_data, hmac)) + def test_shorten_id_with_valid_uuid(self): + valid_id = "4e3e0ec6-2938-40b1-8504-09eb1d4b0dee" + + uuid_obj = uuid.UUID(valid_id) + + with mock.patch("uuid.UUID") as mock_uuid: + mock_uuid.return_value = uuid_obj + + result = utils.shorten_id(valid_id) + expected = 9584796812364680686 + + self.assertEqual(expected, result) + + @mock.patch("oslo_utils.uuidutils.generate_uuid") + def test_shorten_id_with_invalid_uuid(self, mock_gen_uuid): + invalid_id = "invalid" + mock_gen_uuid.return_value = "1c089ea8-28fe-4f3d-8c00-f6daa2bc32f1" + + result = utils.shorten_id(invalid_id) + expected = 10088334584203457265 + + self.assertEqual(expected, result) + def test_itersubclasses(self): class A(object): |