zipkin-tracer 0.36.2 → 0.37.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 795d59f52ecde287fa4afd8e391285c4bfcd5e60bc8eef827b31c53b14525b4f
4
- data.tar.gz: f21cb46bf09dda5b55e660d7c47a2a70e281a3b21089e033b1ad9828e8802ec8
3
+ metadata.gz: f71b9baf08a3f387fc409c40a8997d29da5a8dd48e9d4fa9609b02c2b4f2ea03
4
+ data.tar.gz: a481c2dc583679f1bbbe4341ac896ad4594a7b7920562344b95ed23f6f266a1f
5
5
  SHA512:
6
- metadata.gz: b63fc26c0ac9cd3c64cd1fcbe3079657ae9fea506213aeed3885cb4a334908aaa18b61086699282c629afb712923e56a4b3ee45e06d3dd4051011b62d528d37b
7
- data.tar.gz: 7b6c764aeee41e2b124c5494a180f31decd6f4f302195f9a3a8f273aff5775cd3fd76a474e97484bc081e54fd522953fbbe7333eb04db3df33f2f5c0fa558592
6
+ metadata.gz: 9bf32473266105e4b8dc1d19d3042937c451962aa8f58deac3f606d04205bc7f0fa8ad856169bd02d216193f22e085d8e7b48a317f99182ec0adf7dedab96509
7
+ data.tar.gz: cc75da694fe165499b0f84c10d795d1e3e8f4c8fac6814531662c103e80330f668c298b7c76f16c8d94385f6550ff6afb489770cacfa6923d79dab826152c5b8
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.37.0
2
+ * Add an `async` option to the HTTP and SQS senders.
3
+ * Allow the `logger` option to be provided.
4
+ * Add the TraceWrapper utility class.
5
+
1
6
  # 0.36.2
2
7
  * Cleanup the gemspec. No code changes.
3
8
 
data/README.md CHANGED
@@ -8,27 +8,34 @@ Rack and Faraday integration middlewares for Zipkin tracing.
8
8
 
9
9
  ### Sending traces on incoming requests
10
10
 
11
- Options can be provided via Rails.config for a Rails 3+ app, or can be passed as a hash argument to the Rack plugin.
11
+ Options can be provided as a hash via `Rails.config.zipkin_tracer` for Rails apps or directly to the Rack middleware:
12
12
 
13
13
  ```ruby
14
14
  require 'zipkin-tracer'
15
- use ZipkinTracer::RackHandler, config # config is optional
15
+ use ZipkinTracer::RackHandler, config
16
16
  ```
17
17
 
18
- where `Rails.config.zipkin_tracer` or `config` is a hash that can contain the following keys:
18
+ ### Configuration options
19
19
 
20
+ #### Common
20
21
  * `:service_name` **REQUIRED** - the name of the service being traced. There are two ways to configure this value. Either write the service name in the config file or set the "DOMAIN" environment variable (e.g. 'test-service.example.com' or 'test-service'). The environment variable takes precedence over the config file value.
21
22
  * `:sample_rate` (default: 0.1) - the ratio of requests to sample, from 0 to 1
22
- * `:json_api_host` - hostname with protocol of a zipkin api instance (e.g. `https://zipkin.example.com`) to use the HTTP sender
23
- * `:zookeeper` - the address of the zookeeper server to use by the Kafka sender
24
- * `:sqs_queue_name` - the name of the Amazon SQS queue to use the SQS sender
25
- * `:sqs_region` - the AWS region for the Amazon SQS queue
26
- * `:log_tracing` - Set to true to log all traces. Only used if traces are not sent to the API or Kafka.
27
- * `:annotate_plugin` - plugin function which receives the Rack env, the response status, headers, and body to record annotations
28
- * `:filter_plugin` - plugin function which receives the Rack env and will skip tracing if it returns false
29
- * `:whitelist_plugin` - plugin function which receives the Rack env and will force sampling if it returns true
30
23
  * `:sampled_as_boolean` - When set to true (default but deprecrated), it uses true/false for the `X-B3-Sampled` header. When set to false uses 1/0 which is preferred.
31
24
  * `:trace_id_128bit` - When set to true, high 8-bytes will be prepended to trace_id. The upper 4-bytes are epoch seconds and the lower 4-bytes are random. This makes it convertible to Amazon X-Ray trace ID format v1. (See http://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-request-tracing.html)
25
+ * `:async` - By default senders will flush traces asynchronously. Set to `false` to make that process synchronous. Only supported by the HTTP and SQS senders.
26
+ * `:logger` - The default logger for Rails apps is `Rails.logger`, else it is `STDOUT`. Use this option to pass a custom logger.
27
+
28
+ #### Sender specific
29
+ * `:json_api_host` - Hostname with protocol of a zipkin api instance (e.g. `https://zipkin.example.com`) to use the HTTP sender
30
+ * `:zookeeper` - The address of the zookeeper server to use by the Kafka sender
31
+ * `:sqs_queue_name` - The name of the Amazon SQS queue to use the SQS sender
32
+ * `:sqs_region` - The AWS region for the Amazon SQS queue (optional)
33
+ * `:log_tracing` - Set to true to log all traces. Only used if traces are not sent to the API or Kafka.
34
+
35
+ #### Plugins
36
+ * `:annotate_plugin` - Receives the Rack env, the response status, headers, and body to record annotations
37
+ * `:filter_plugin` - Receives the Rack env and will skip tracing if it returns false
38
+ * `:whitelist_plugin` - Receives the Rack env and will force sampling if it returns true
32
39
 
33
40
  ### Sending traces on outgoing requests with Faraday
34
41
 
@@ -210,6 +217,29 @@ For example:
210
217
  lambda { |env| KNOWN_DEVICES.include?(env['HTTP_X_DEVICE_ID']) }
211
218
  ```
212
219
 
220
+ ## Utility classes
221
+
222
+ ### TraceWrapper
223
+
224
+ This class provides a `.wrap_in_custom_span` method which expects a configuration hash, a span name and a block.
225
+ You may also pass a span kind and an Application object using respectively `span_kind:` and `app:` keyword arguments.
226
+
227
+ The block you pass will be executed in the context of a custom span.
228
+ This is useful when your application doesn't use the rack handler but still needs to generate complete traces, for instance background jobs or lambdas calling remote services.
229
+
230
+ The following code will create a trace starting with a span of the (default) `SERVER` kind named "custom span" and then a span of the `CLIENT` kind will be added by the Faraday middleware. Afterwards the configured sender will call `flush!`.
231
+
232
+ ```ruby
233
+ TraceWrapper.wrap_in_custom_span(config, "custom span") do |span|
234
+ conn = Faraday.new(url: remote_service_url) do |builder|
235
+ builder.use ZipkinTracer::FaradayHandler, config[:service_name]
236
+ builder.adapter Faraday.default_adapter
237
+ end
238
+ conn.get("/")
239
+ end
240
+ ```
241
+
242
+
213
243
  ## Development
214
244
 
215
245
  This project uses Rspec. Make sure your PRs contain proper tests.
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "zipkin-tracer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -5,10 +5,9 @@ require 'zipkin-tracer/rack/zipkin-tracer'
5
5
  module ZipkinTracer
6
6
  # Configuration of this gem. It reads the configuration and provides default values
7
7
  class Config
8
- attr_reader :service_name, :sample_rate,
8
+ attr_reader :service_name, :sample_rate, :sampled_as_boolean, :trace_id_128bit, :async, :logger,
9
9
  :json_api_host, :zookeeper, :kafka_producer, :kafka_topic, :sqs_queue_name, :sqs_region, :log_tracing,
10
- :annotate_plugin, :filter_plugin, :whitelist_plugin,
11
- :logger, :sampled_as_boolean, :trace_id_128bit
10
+ :annotate_plugin, :filter_plugin, :whitelist_plugin
12
11
 
13
12
  def initialize(app, config_hash)
14
13
  config = config_hash || Application.config(app)
@@ -25,14 +24,16 @@ module ZipkinTracer
25
24
  @sqs_queue_name = config[:sqs_queue_name]
26
25
  @sqs_region = config[:sqs_region]
27
26
  # Percentage of traces which by default this service traces (as float, 1.0 means 100%)
28
- @sample_rate = config[:sample_rate] || DEFAULTS[:sample_rate]
27
+ @sample_rate = config[:sample_rate] || DEFAULTS[:sample_rate]
29
28
  # A block of code which can be called to do extra annotations of traces
30
29
  @annotate_plugin = config[:annotate_plugin] # call for trace annotation
31
30
  # A block of code which can be called to skip traces. Skip tracing if returns false
32
31
  @filter_plugin = config[:filter_plugin]
33
32
  # A block of code which can be called to force sampling. Forces sampling if returns true
34
33
  @whitelist_plugin = config[:whitelist_plugin]
35
- @logger = Application.logger
34
+ # be strict about checking `false` to ensure misconfigurations don't lead to accidental synchronous configurations
35
+ @async = config[:async] != false
36
+ @logger = config[:logger] || Application.logger
36
37
  # Was the logger in fact setup by the client?
37
38
  @log_tracing = config[:log_tracing]
38
39
  # When set to false, it uses 1/0 in the 'X-B3-Sampled' header, else uses true/false
@@ -0,0 +1,22 @@
1
+ module ZipkinTracer
2
+ class TraceWrapper
3
+ def self.wrap_in_custom_span(config, span_name, span_kind: Trace::Span::Kind::SERVER, app: nil)
4
+ raise ArgumentError, "you must provide a block" unless block_given?
5
+
6
+ zipkin_config = ZipkinTracer::Config.new(app, config).freeze
7
+ tracer = ZipkinTracer::TracerFactory.new.tracer(zipkin_config)
8
+ trace_id = ZipkinTracer::TraceGenerator.new.next_trace_id
9
+
10
+ ZipkinTracer::TraceContainer.with_trace_id(trace_id) do
11
+ if trace_id.sampled?
12
+ tracer.with_new_span(trace_id, span_name) do |span|
13
+ span.kind = span_kind
14
+ yield(span)
15
+ end
16
+ else
17
+ yield(ZipkinTracer::NullSpan.new)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -18,7 +18,12 @@ module ZipkinTracer
18
18
  Trace::ZipkinKafkaSender.new(options)
19
19
  when :sqs
20
20
  require 'zipkin-tracer/zipkin_sqs_sender'
21
- options = { logger: config.logger, queue_name: config.sqs_queue_name , region: config.sqs_region }
21
+ options = {
22
+ async: config.async,
23
+ logger: config.logger,
24
+ queue_name: config.sqs_queue_name,
25
+ region: config.sqs_region
26
+ }
22
27
  Trace::ZipkinSqsSender.new(options)
23
28
  when :logger
24
29
  require 'zipkin-tracer/zipkin_logger_sender'
@@ -1,3 +1,3 @@
1
1
  module ZipkinTracer
2
- VERSION = '0.36.2'.freeze
2
+ VERSION = '0.37.0'.freeze
3
3
  end
@@ -4,7 +4,7 @@ require 'zipkin-tracer/zipkin_sender_base'
4
4
  require 'zipkin-tracer/hostname_resolver'
5
5
 
6
6
  module Trace
7
- class AsyncHttpApiClient
7
+ class HttpApiClient
8
8
  include SuckerPunch::Job
9
9
  SPANS_PATH = '/api/v2/spans'
10
10
 
@@ -32,13 +32,18 @@ module Trace
32
32
  IP_FORMAT = :string
33
33
 
34
34
  def initialize(options)
35
- SuckerPunch.logger = options[:logger]
36
35
  @json_api_host = options[:json_api_host]
36
+ @async = options[:async] != false
37
+ SuckerPunch.logger = options[:logger]
37
38
  super(options)
38
39
  end
39
40
 
40
41
  def flush!
41
- AsyncHttpApiClient.perform_async(@json_api_host, spans.dup)
42
+ if @async
43
+ HttpApiClient.perform_async(@json_api_host, spans.dup)
44
+ else
45
+ HttpApiClient.new.perform(@json_api_host, spans)
46
+ end
42
47
  end
43
48
  end
44
49
  end
@@ -5,7 +5,7 @@ require "zipkin-tracer/zipkin_sender_base"
5
5
  require "zipkin-tracer/hostname_resolver"
6
6
 
7
7
  module Trace
8
- class AsyncSqsClient
8
+ class SqsClient
9
9
  include SuckerPunch::Job
10
10
 
11
11
  def perform(sqs_options, queue_name, spans)
@@ -13,9 +13,10 @@ module Trace
13
13
  ::ZipkinTracer::HostnameResolver.new.spans_with_ips(spans, ZipkinSqsSender::IP_FORMAT).map(&:to_h)
14
14
  sqs = Aws::SQS::Client.new(**sqs_options)
15
15
  queue_url = sqs.get_queue_url(queue_name: queue_name).queue_url
16
- sqs.send_message(queue_url: queue_url, message_body: JSON.generate(spans_with_ips))
16
+ body = JSON.generate(spans_with_ips)
17
+ sqs.send_message(queue_url: queue_url, message_body: body)
17
18
  rescue Aws::SQS::Errors::NonExistentQueue
18
- error_message = "A queue named '#{@queue_name}' does not exist."
19
+ error_message = "The queue '#{queue_name}' does not exist."
19
20
  SuckerPunch.logger.error(error_message)
20
21
  rescue => e
21
22
  SuckerPunch.logger.error(e)
@@ -28,12 +29,17 @@ module Trace
28
29
  def initialize(options)
29
30
  @sqs_options = options[:region] ? { region: options[:region] } : {}
30
31
  @queue_name = options[:queue_name]
32
+ @async = options[:async] != false
31
33
  SuckerPunch.logger = options[:logger]
32
34
  super(options)
33
35
  end
34
36
 
35
37
  def flush!
36
- AsyncSqsClient.perform_async(@sqs_options, @queue_name, spans.dup)
38
+ if @async
39
+ SqsClient.perform_async(@sqs_options, @queue_name, spans.dup)
40
+ else
41
+ SqsClient.new.perform(@sqs_options, @queue_name, spans)
42
+ end
37
43
  end
38
44
  end
39
45
  end
data/lib/zipkin-tracer.rb CHANGED
@@ -4,11 +4,12 @@ require 'zipkin-tracer/sidekiq/middleware'
4
4
  require 'zipkin-tracer/trace_client'
5
5
  require 'zipkin-tracer/trace_container'
6
6
  require 'zipkin-tracer/trace_generator'
7
+ require 'zipkin-tracer/trace_wrapper'
7
8
 
8
9
  begin
9
10
  require 'faraday'
10
11
  require 'zipkin-tracer/faraday/zipkin-tracer'
11
- rescue LoadError #Faraday is not available, we do not load our code.
12
+ rescue LoadError # Faraday is not available, we do not load our code.
12
13
  end
13
14
 
14
15
  begin
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  s.add_development_dependency 'aws-sdk-sqs', '~> 1.0'
32
32
  s.add_development_dependency 'excon', '~> 0.53'
33
33
  s.add_development_dependency 'rspec', '~> 3.8'
34
+ s.add_development_dependency 'rspec-json_expectations', '~> 2.2'
34
35
  s.add_development_dependency 'rack-test', '~> 1.1'
35
36
  s.add_development_dependency 'rake', '~> 10.0'
36
37
  s.add_development_dependency 'timecop', '~> 0.8'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zipkin-tracer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.36.2
4
+ version: 0.37.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franklin Hu
@@ -14,7 +14,7 @@ authors:
14
14
  autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
- date: 2019-04-24 00:00:00.000000000 Z
17
+ date: 2019-05-30 00:00:00.000000000 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: faraday
@@ -100,6 +100,20 @@ dependencies:
100
100
  - - "~>"
101
101
  - !ruby/object:Gem::Version
102
102
  version: '3.8'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rspec-json_expectations
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '2.2'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '2.2'
103
117
  - !ruby/object:Gem::Dependency
104
118
  name: rack-test
105
119
  requirement: !ruby/object:Gem::Requirement
@@ -193,6 +207,8 @@ files:
193
207
  - README.md
194
208
  - RELEASE.md
195
209
  - Rakefile
210
+ - bin/console
211
+ - bin/setup
196
212
  - lib/zipkin-tracer.rb
197
213
  - lib/zipkin-tracer/application.rb
198
214
  - lib/zipkin-tracer/config.rb
@@ -206,6 +222,7 @@ files:
206
222
  - lib/zipkin-tracer/trace_client.rb
207
223
  - lib/zipkin-tracer/trace_container.rb
208
224
  - lib/zipkin-tracer/trace_generator.rb
225
+ - lib/zipkin-tracer/trace_wrapper.rb
209
226
  - lib/zipkin-tracer/tracer_factory.rb
210
227
  - lib/zipkin-tracer/version.rb
211
228
  - lib/zipkin-tracer/zipkin_http_sender.rb