lhc 6.6.0.zipkin.pre.01 → 6.6.0.zipkin.pre.02
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lhc/interceptors/zipkin_distributed_tracing.rb +63 -7
- data/lib/lhc/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4dbb6e2b6d5d0ccc7f366aab3cd56b140135f36e
|
4
|
+
data.tar.gz: c5d5123bc892714980d032602151c94f5edd0cbe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2b5dae66477ba3410859897ba899e79a69a7285a4211caaa7e3cc641f46a50a4927c5cfb1304c38790145d290e7761058832ceb68c926a3e433424fd783c5ebb
|
7
|
+
data.tar.gz: 73b06b7821eae214634372fbd408227f5ce5882858a8e5484fb307b142f90ef9d18bcc78063b80ad4864fd4ab6f64b1ce36ab7d3275797235e920a8d011479be
|
@@ -1,12 +1,68 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
1
3
|
class LHC::ZipkinDistributedTracing < LHC::Interceptor
|
2
4
|
|
3
5
|
def before_request(request)
|
4
|
-
return unless defined?(ZipkinTracer::TraceContainer)
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
return unless defined?(ZipkinTracer::TraceContainer) && ZipkinTracer::TraceContainer.current && defined?(Trace)
|
7
|
+
trace_id = ZipkinTracer::TraceGenerator.new.next_trace_id
|
8
|
+
ZipkinTracer::TraceContainer.with_trace_id(trace_id) do
|
9
|
+
b3_headers.each do |method, header|
|
10
|
+
request.headers[header] = trace_id.send(method).to_s
|
11
|
+
end
|
12
|
+
trace!(request, trace_id) if ::Trace.tracer && trace_id.sampled?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def after_response(response)
|
17
|
+
if span = response.request.interceptor_environment[:zipkin_span]
|
18
|
+
record_response_tags(span, response)
|
19
|
+
end
|
20
|
+
span.record(Trace::Annotation::CLIENT_RECV, local_endpoint)
|
21
|
+
Trace.tracer.end_span(span)
|
22
|
+
end
|
23
|
+
private
|
24
|
+
|
25
|
+
SERVER_ADDRESS_SPECIAL_VALUE = '1'.freeze
|
26
|
+
|
27
|
+
def b3_headers
|
28
|
+
{
|
29
|
+
trace_id: 'X-B3-TraceId',
|
30
|
+
parent_id: 'X-B3-ParentSpanId',
|
31
|
+
span_id: 'X-B3-SpanId',
|
32
|
+
sampled: 'X-B3-Sampled',
|
33
|
+
flags: 'X-B3-Flags'
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
def trace!(request, trace_id)
|
38
|
+
url_string = request.raw.url
|
39
|
+
url = URI(url_string)
|
40
|
+
service_name = url.host
|
41
|
+
span = Trace.tracer.start_span(trace_id, request.method.to_s.downcase)
|
42
|
+
# annotate with method (GET/POST/etc.) and uri path
|
43
|
+
span.record_tag(Trace::BinaryAnnotation::PATH, url.path, Trace::BinaryAnnotation::Type::STRING, local_endpoint)
|
44
|
+
span.record_tag(Trace::BinaryAnnotation::SERVER_ADDRESS, SERVER_ADDRESS_SPECIAL_VALUE, Trace::BinaryAnnotation::Type::BOOL, remote_endpoint(url, service_name))
|
45
|
+
span.record(Trace::Annotation::CLIENT_SEND, local_endpoint)
|
46
|
+
# store the span in the datum hash so it can be used in the response_call
|
47
|
+
request.interceptor_environment[:zipkin_span] = span
|
48
|
+
rescue ArgumentError, URI::Error => e
|
49
|
+
# Ignore URI errors, don't trace if there is no URI
|
50
|
+
end
|
51
|
+
|
52
|
+
def local_endpoint
|
53
|
+
Trace.default_endpoint # The rack middleware set this up for us.
|
54
|
+
end
|
55
|
+
|
56
|
+
def remote_endpoint(url, service_name)
|
57
|
+
Trace::Endpoint.remote_endpoint(url, service_name, local_endpoint.ip_format) # The endpoint we are calling.
|
58
|
+
end
|
59
|
+
|
60
|
+
def record_response_tags(span, response)
|
61
|
+
status = response.code.to_s
|
62
|
+
span.record_tag(Trace::BinaryAnnotation::STATUS, status, Trace::BinaryAnnotation::Type::STRING, local_endpoint)
|
63
|
+
if !response.success?
|
64
|
+
span.record_tag(Trace::BinaryAnnotation::ERROR, status,
|
65
|
+
Trace::BinaryAnnotation::Type::STRING, local_endpoint)
|
66
|
+
end
|
11
67
|
end
|
12
68
|
end
|
data/lib/lhc/version.rb
CHANGED