11# -*- coding: utf-8 -*-
2- # Copyright 2019 The Matrix.org Foundation C.I.C.d
2+ # Copyright 2019 The Matrix.org Foundation C.I.C.
33#
44# Licensed under the Apache License, Version 2.0 (the "License");
55# you may not use this file except in compliance with the License.
2424# this move the methods have work very similarly to opentracing's and it should only
2525# be a matter of few regexes to move over to opentracing's access patterns proper.
2626
27+ import contextlib
28+ import logging
29+ import re
30+ from functools import wraps
31+
32+ from twisted .internet import defer
33+
34+ from synapse .config import ConfigError
35+
2736try :
2837 import opentracing
2938except ImportError :
3544 JaegerConfig = None
3645 LogContextScopeManager = None
3746
38- import contextlib
39- import logging
40- import re
41- from functools import wraps
42-
43- from twisted .internet import defer
4447
4548logger = logging .getLogger (__name__ )
4649
@@ -91,7 +94,8 @@ def _only_if_tracing_inner(*args, **kwargs):
9194 return _only_if_tracing_inner
9295
9396
94- # Block everything by default
97+ # A regex which matches the server_names to expose traces for.
98+ # None means 'block everything'.
9599_homeserver_whitelist = None
96100
97101tags = _DumTagNames
@@ -101,31 +105,24 @@ def init_tracer(config):
101105 """Set the whitelists and initialise the JaegerClient tracer
102106
103107 Args:
104- config (Config)
105- The config used by the homeserver. Here it's used to set the service
106- name to the homeserver's.
108+ config (HomeserverConfig): The config used by the homeserver
107109 """
108110 global opentracing
109- if not config .tracer_config . get ( "tracer_enabled" , False ) :
111+ if not config .opentracer_enabled :
110112 # We don't have a tracer
111113 opentracing = None
112114 return
113115
114- if not opentracing :
115- logger .error (
116- "The server has been configure to use opentracing but opentracing is not installed."
117- )
118- raise ModuleNotFoundError ("opentracing" )
119-
120- if not JaegerConfig :
121- logger .error (
122- "The server has been configure to use opentracing but opentracing is not installed."
116+ if not opentracing or not JaegerConfig :
117+ raise ConfigError (
118+ "The server has been configured to use opentracing but opentracing is not "
119+ "installed."
123120 )
124121
125122 # Include the worker name
126123 name = config .worker_name if config .worker_name else "master"
127124
128- set_homeserver_whitelist (config .tracer_config [ "homeserver_whitelist" ] )
125+ set_homeserver_whitelist (config .opentracer_whitelist )
129126 jaeger_config = JaegerConfig (
130127 config = {"sampler" : {"type" : "const" , "param" : 1 }, "logging" : True },
131128 service_name = "{} {}" .format (config .server_name , name ),
@@ -232,7 +229,6 @@ def whitelisted_homeserver(destination):
232229 """Checks if a destination matches the whitelist
233230 Args:
234231 destination (String)"""
235- global _homeserver_whitelist
236232 if _homeserver_whitelist :
237233 return _homeserver_whitelist .match (destination )
238234 return False
0 commit comments