Module: Vault::Tracing

Defined in:
lib/vault-tools/tracing.rb,
lib/vault-tools/tracing/sidekiq_client.rb,
lib/vault-tools/tracing/sidekiq_server.rb

Defined Under Namespace

Classes: SidekiqClient, SidekiqServer

Constant Summary collapse

ZIPKIN_API_HOST_STAGING =
'https://zipkin-staging.heroku.tools'.freeze

Class Method Summary collapse

Class Method Details

.configHash

Configuration options for the Zipkin RackHandler.

Returns:

  • (Hash)

    config options for Zipkin tracer



49
50
51
52
53
54
55
56
57
# File 'lib/vault-tools/tracing.rb', line 49

def self.config
  {
    service_name: "#{Config.app_name}.herokuapp.com",
    service_port: 443,
    json_api_host: Config[:zipkin_api_host],
    sample_rate: (Config[:zipkin_sample_rate] || 0.1).to_f,
    sampled_as_boolean: false
  }
end

.configureObject

Injects the zipkin middleware into the Web class, add Zipkin middleware to Excon, and adds Zipkin middleware to Sidekiq.

Examples:

Vault::Tracing.configure

Returns:

  • nil



14
15
16
17
18
19
20
21
# File 'lib/vault-tools/tracing.rb', line 14

def self.configure
  return unless Vault::Tracing.enabled?

  Vault::Web.instance_eval { require 'zipkin-tracer' }
  Vault::Web.use ZipkinTracer::RackHandler, config
  setup_excon
  setup_sidekiq
end

.enabled?true

A helper to guard against injecting Zipkin when not desired.

Returns:

  • (true)

    if so



62
63
64
65
66
# File 'lib/vault-tools/tracing.rb', line 62

def self.enabled?
  Config.app_name &&
    Config[:zipkin_enabled] == 'true' &&
    Config[:zipkin_api_host]
end

.setup_sidekiq(sidekiq = Sidekiq) ⇒ Object

Adds our SidekiqClient and SidekiqServer middlware to Sidekiq

Returns:

  • nil



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vault-tools/tracing.rb', line 86

def self.setup_sidekiq(sidekiq = Sidekiq)
  return unless defined?(sidekiq)
  sidekiq.configure_client do |config|
    config.client_middleware do |chain|
      chain.add Vault::Tracing::SidekiqClient
    end
  end

  sidekiq.configure_server do |config|
    config.client_middleware do |chain|
      chain.add Vault::Tracing::SidekiqClient
    end
    config.server_middleware do |chain|
      chain.add Vault::Tracing::SidekiqServer, Vault::Tracing.config
    end
  end
end

.trace_local(name, tracer = ZipkinTracer::TraceClient, **options) {|the_block_passed_in| ... } ⇒ Object

Traces a local component. Useful to track down long running implementation methods within an app, instead of only tracing external calls.

Examples:

Vault::Tracing.trace_local 'cashier_middleware_x', internal: true do
  # some expensive task
end

Parameters:

  • name (String)

    the name of the span to record in Zipkin.

  • tracer (Constant) (defaults to: ZipkinTracer::TraceClient)

    the const that should respond to :local_component_span

  • options (Hash)

    the options to create a message with.

Yields:

  • (the_block_passed_in)


36
37
38
39
40
41
42
43
44
# File 'lib/vault-tools/tracing.rb', line 36

def self.trace_local(name, tracer = ZipkinTracer::TraceClient, **options)
  return yield unless Vault::Tracing.enabled?

  tracer.local_component_span(name) do |span|
    span.name = name
    span.record_tag('options', options.to_s) unless options.empty?
    yield
  end
end