lhc 6.6.0.zipkin.pre.01 → 6.6.0.zipkin.pre.02

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b6394b6bd8236ce4d4e08466c9cd9e66fea9530
4
- data.tar.gz: 7bb72782b609582d68d0dca748cd08f0245cebae
3
+ metadata.gz: 4dbb6e2b6d5d0ccc7f366aab3cd56b140135f36e
4
+ data.tar.gz: c5d5123bc892714980d032602151c94f5edd0cbe
5
5
  SHA512:
6
- metadata.gz: 1b4b679cc13ea4b2434b43f5f1a4272f42584cad6b09c3b488d3c5ac6af997bd277742a2bcb2271e189247ede1a661745c4127cff286cfe82ce25e7b7b656631
7
- data.tar.gz: a03ac4c46ea0ec3eca544d0ee5595cf13ca9188f12d920aef23fd6c41afc7c861efb70f844a477879bb3af4826876a3f6642b6a7f8a51dce9c19b3bd6c0744f9
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
- container = ZipkinTracer::TraceContainer.current
6
- request.headers['X-B3-TraceId'] = container.trace_id.to_s
7
- request.headers['X-B3-ParentSpanId'] = container.parent_id.to_s if container.parent_id
8
- request.headers['X-B3-SpanId'] = container.span_id.to_s
9
- request.headers['X-B3-Sampled'] = container.sampled
10
- request.headers['X-B3-Flags'] = container.flags
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
@@ -1,3 +1,3 @@
1
1
  module LHC
2
- VERSION ||= "6.6.0.zipkin.pre.01"
2
+ VERSION ||= "6.6.0.zipkin.pre.02"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhc
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.6.0.zipkin.pre.01
4
+ version: 6.6.0.zipkin.pre.02
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhc/contributors