blob: 15443371b34e23acadbcd93d6667b3de1414edd8 [file] [log] [blame]
Ilya Shakhat1291bb42017-11-29 18:08:16 +01001# Licensed under the Apache License, Version 2.0 (the "License"); you may
2# not use this file except in compliance with the License. You may obtain
3# a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10# License for the specific language governing permissions and limitations
11# under the License.
12
13import base64
14import hashlib
15import hmac
16import json
17
18from oslo_utils import encodeutils
19from oslo_utils import uuidutils
20
21_profiler = {}
22
23
24def enable(profiler_key, trace_id=None):
25 """Enable global profiler instance
26
27 :param profiler_key: the secret key used to enable profiling in services
28 :param trace_id: unique id of the trace, if empty the id is generated
29 automatically
30 """
31 _profiler['key'] = profiler_key
32 _profiler['uuid'] = trace_id or uuidutils.generate_uuid()
33
34
35def disable():
36 """Disable global profiler instance"""
37 _profiler.clear()
38
39
40def serialize_as_http_headers():
41 """Serialize profiler state as HTTP headers
42
43 This function corresponds to the one from osprofiler library.
44 :return: dictionary with 2 keys `X-Trace-Info` and `X-Trace-HMAC`.
45 """
46 p = _profiler
47 if not p: # profiler is not enabled
48 return {}
49
50 info = {'base_id': p['uuid'], 'parent_id': p['uuid']}
51 trace_info = base64.urlsafe_b64encode(
52 encodeutils.to_utf8(json.dumps(info)))
53 trace_hmac = _sign(trace_info, p['key'])
54
55 return {
56 'X-Trace-Info': trace_info,
57 'X-Trace-HMAC': trace_hmac,
58 }
59
60
61def _sign(trace_info, key):
62 h = hmac.new(encodeutils.to_utf8(key), digestmod=hashlib.sha1)
63 h.update(trace_info)
64 return h.hexdigest()