Skip to content
This repository was archived by the owner on Oct 13, 2021. It is now read-only.

Commit f1e862b

Browse files
committed
[consul] Enable trace analytics
1 parent aa4dadd commit f1e862b

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,6 @@ ENV/
9494
*.swp
9595
# IDEA
9696
.idea/
97+
98+
# VS Code
99+
.vscode/

ddtrace/contrib/consul/patch.py

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from ddtrace.vendor.wrapt import wrap_function_wrapper as _w
44

5+
from ddtrace import config
6+
from ...constants import ANALYTICS_SAMPLE_RATE_KEY
57
from ...ext import consul as consulx
68
from ...pin import Pin
79
from ...utils.wrappers import unwrap as _u
@@ -45,6 +47,9 @@ def trace_func(wrapped, instance, args, kwargs):
4547
resource = name.upper()
4648

4749
with pin.tracer.trace(consulx.CMD, service=pin.service, resource=resource) as span:
50+
rate = config.consul.get_analytics_sample_rate()
51+
if rate is not None:
52+
span.set_tag(ANALYTICS_SAMPLE_RATE_KEY, rate)
4853
span.set_tag(consulx.KEY, path)
4954
span.set_tag(consulx.CMD, resource)
5055
return wrapped(*args, **kwargs)

tests/contrib/consul/test.py

+37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import consul
22
from ddtrace import Pin
3+
from ddtrace.constants import ANALYTICS_SAMPLE_RATE_KEY
34
from ddtrace.ext import consul as consulx
45
from ddtrace.vendor.wrapt import BoundFunctionWrapper
56
from ddtrace.contrib.consul.patch import patch, unpatch
@@ -130,3 +131,39 @@ def test_patch_preserves_functionality(self):
130131
self.c.kv.delete(key)
131132
_, data = self.c.kv.get(key)
132133
assert data is None
134+
135+
def test_analytics_without_rate(self):
136+
with self.override_config('consul', {'analytics_enabled': True}):
137+
key = 'test/kwargs/consul'
138+
value = 'test_value'
139+
140+
self.c.kv.put(key=key, value=value)
141+
142+
spans = self.get_spans()
143+
assert len(spans) == 1
144+
span = spans[0]
145+
assert span.get_metric(ANALYTICS_SAMPLE_RATE_KEY) == 1.0
146+
147+
def test_analytics_with_rate(self):
148+
with self.override_config('consul', {'analytics_enabled': True, 'analytics_sample_rate': 0.5}):
149+
key = 'test/kwargs/consul'
150+
value = 'test_value'
151+
152+
self.c.kv.put(key=key, value=value)
153+
154+
spans = self.get_spans()
155+
assert len(spans) == 1
156+
span = spans[0]
157+
assert span.get_metric(ANALYTICS_SAMPLE_RATE_KEY) == 0.5
158+
159+
def test_analytics_disabled(self):
160+
with self.override_config('consul', {'analytics_enabled': False}):
161+
key = 'test/kwargs/consul'
162+
value = 'test_value'
163+
164+
self.c.kv.put(key=key, value=value)
165+
166+
spans = self.get_spans()
167+
assert len(spans) == 1
168+
span = spans[0]
169+
assert span.get_metric(ANALYTICS_SAMPLE_RATE_KEY) is None

0 commit comments

Comments
 (0)