Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 782bd79

Browse files
committed
Clean up opentracing configuration options (#5712)
2 parents adb0431 + 82345bc commit 782bd79

7 files changed

Lines changed: 96 additions & 63 deletions

File tree

changelog.d/5544.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add support for opentracing.
2+

changelog.d/5544.misc

Lines changed: 0 additions & 1 deletion
This file was deleted.

changelog.d/5712.feature

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add support for opentracing.
2+

docs/sample_config.yaml

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,17 +1611,34 @@ password_config:
16111611

16121612

16131613
## Opentracing ##
1614-
# These settings enable opentracing which implements distributed tracing
1615-
# This allows you to observe the causal chain of events across servers
1616-
# including requests, key lookups etc. across any server running
1617-
# synapse or any other other services which supports opentracing.
1618-
# (specifically those implemented with jaeger)
1619-
1620-
#opentracing:
1621-
# # Enable / disable tracer
1622-
# tracer_enabled: false
1623-
# # The list of homeservers we wish to expose our current traces to.
1624-
# # The list is a list of regexes which are matched against the
1625-
# # servername of the homeserver
1626-
# homeserver_whitelist:
1627-
# - ".*"
1614+
1615+
# These settings enable opentracing, which implements distributed tracing.
1616+
# This allows you to observe the causal chains of events across servers
1617+
# including requests, key lookups etc., across any server running
1618+
# synapse or any other other services which supports opentracing
1619+
# (specifically those implemented with Jaeger).
1620+
#
1621+
opentracing:
1622+
# tracing is disabled by default. Uncomment the following line to enable it.
1623+
#
1624+
#enabled: true
1625+
1626+
# The list of homeservers we wish to send and receive span contexts and span baggage.
1627+
#
1628+
# Though it's mostly safe to send and receive span contexts to and from
1629+
# untrusted users since span contexts are usually opaque ids it can lead to
1630+
# two problems, namely:
1631+
# - If the span context is marked as sampled by the sending homeserver the receiver will
1632+
# sample it. Therefore two homeservers with wildly disparaging sampling policies
1633+
# could incur higher sampling counts than intended.
1634+
# - Span baggage can be arbitrary data. For safety this has been disabled in synapse
1635+
# but that doesn't prevent another server sending you baggage which will be logged
1636+
# to opentracing logs.
1637+
#
1638+
# This a list of regexes which are matched against the server_name of the
1639+
# homeserver.
1640+
#
1641+
# By defult, it is empty, so no servers are matched.
1642+
#
1643+
#homeserver_whitelist:
1644+
# - ".*"

synapse/config/tracer.py

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,52 @@
1818

1919
class TracerConfig(Config):
2020
def read_config(self, config, **kwargs):
21-
self.tracer_config = config.get("opentracing")
21+
opentracing_config = config.get("opentracing")
22+
if opentracing_config is None:
23+
opentracing_config = {}
2224

23-
self.tracer_config = config.get("opentracing", {"tracer_enabled": False})
25+
self.opentracer_enabled = opentracing_config.get("enabled", False)
26+
if not self.opentracer_enabled:
27+
return
2428

25-
if self.tracer_config.get("tracer_enabled", False):
26-
# The tracer is enabled so sanitize the config
27-
# If no whitelists are given
28-
self.tracer_config.setdefault("homeserver_whitelist", [])
29+
# The tracer is enabled so sanitize the config
2930

30-
if not isinstance(self.tracer_config.get("homeserver_whitelist"), list):
31-
raise ConfigError("Tracer homesererver_whitelist config is malformed")
31+
self.opentracer_whitelist = opentracing_config.get("homeserver_whitelist", [])
32+
if not isinstance(self.opentracer_whitelist, list):
33+
raise ConfigError("Tracer homeserver_whitelist config is malformed")
3234

3335
def generate_config_section(cls, **kwargs):
3436
return """\
3537
## Opentracing ##
36-
# These settings enable opentracing which implements distributed tracing
37-
# This allows you to observe the causal chain of events across servers
38-
# including requests, key lookups etc. across any server running
39-
# synapse or any other other services which supports opentracing.
40-
# (specifically those implemented with jaeger)
41-
42-
#opentracing:
43-
# # Enable / disable tracer
44-
# tracer_enabled: false
45-
# # The list of homeservers we wish to expose our current traces to.
46-
# # The list is a list of regexes which are matched against the
47-
# # servername of the homeserver
48-
# homeserver_whitelist:
49-
# - ".*"
38+
39+
# These settings enable opentracing, which implements distributed tracing.
40+
# This allows you to observe the causal chains of events across servers
41+
# including requests, key lookups etc., across any server running
42+
# synapse or any other other services which supports opentracing
43+
# (specifically those implemented with Jaeger).
44+
#
45+
opentracing:
46+
# tracing is disabled by default. Uncomment the following line to enable it.
47+
#
48+
#enabled: true
49+
50+
# The list of homeservers we wish to send and receive span contexts and span baggage.
51+
#
52+
# Though it's mostly safe to send and receive span contexts to and from
53+
# untrusted users since span contexts are usually opaque ids it can lead to
54+
# two problems, namely:
55+
# - If the span context is marked as sampled by the sending homeserver the receiver will
56+
# sample it. Therefore two homeservers with wildly disparaging sampling policies
57+
# could incur higher sampling counts than intended.
58+
# - Span baggage can be arbitrary data. For safety this has been disabled in synapse
59+
# but that doesn't prevent another server sending you baggage which will be logged
60+
# to opentracing logs.
61+
#
62+
# This a list of regexes which are matched against the server_name of the
63+
# homeserver.
64+
#
65+
# By defult, it is empty, so no servers are matched.
66+
#
67+
#homeserver_whitelist:
68+
# - ".*"
5069
"""

synapse/logging/opentracing.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -24,6 +24,15 @@
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+
2736
try:
2837
import opentracing
2938
except ImportError:
@@ -35,12 +44,6 @@
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

4548
logger = 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

97101
tags = _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

synapse/logging/scopecontextmanager.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ class LogContextScopeManager(ScopeManager):
3434
"""
3535

3636
def __init__(self, config):
37-
# Set the whitelists
38-
logger.info(config.tracer_config)
39-
self._homeserver_whitelist = config.tracer_config["homeserver_whitelist"]
37+
pass
4038

4139
@property
4240
def active(self):

0 commit comments

Comments
 (0)