Class: Hypertrace::RubyAgent

Inherits:
Object
  • Object
show all
Includes:
Logging, Singleton
Defined in:
lib/hypertrace/ruby_agent.rb

Overview

We can’t name the class ‘Hypertrace::Agent’ because the built proto definitions create a module Hypertrace::Agent :( RubyAgent is repetitve, but want to remain somewhat consistent compared to python/node

Constant Summary collapse

SUPPORTED_INSTRUMENTATIONS =
[
  'OpenTelemetry::Instrumentation::ActionPack',
  'OpenTelemetry::Instrumentation::ActionView',
  'OpenTelemetry::Instrumentation::ActiveRecord',
  'OpenTelemetry::Instrumentation::ActiveSupport',
  'OpenTelemetry::Instrumentation::Faraday',
  'OpenTelemetry::Instrumentation::Mongo',
  'OpenTelemetry::Instrumentation::Mysql2',
  'OpenTelemetry::Instrumentation::PG',
  'OpenTelemetry::Instrumentation::Rack',
  'OpenTelemetry::Instrumentation::Sinatra',
  'OpenTelemetry::Instrumentation::Net::HTTP',
  'OpenTelemetry::Instrumentation::HTTP',
  'OpenTelemetry::Instrumentation::RestClient'
].freeze
SUPPORTED_INSTRUMENTATIONS_ADDITIONAL_PATCH =
{
  'OpenTelemetry::Instrumentation::Faraday' => ['./instrumentation/faraday_patch'],
  'OpenTelemetry::Instrumentation::Rack' => ['./instrumentation/rack', './instrumentation/rack_env_getter'],
  'OpenTelemetry::Instrumentation::Net::HTTP' => ['./instrumentation/net_http_patch'],
  'OpenTelemetry::Instrumentation::HTTP' => ['./instrumentation/http_patch'],
  'OpenTelemetry::Instrumentation::RestClient' => ['./instrumentation/rest_client_patch'],
}

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log

Constructor Details

#initialize(version = Hypertrace::VERSION) ⇒ RubyAgent

Returns a new instance of RubyAgent.



71
72
73
74
75
76
77
# File 'lib/hypertrace/ruby_agent.rb', line 71

def initialize(version = Hypertrace::VERSION)
  log.info { "Initializing Hypertrace" }
  @config = Hypertrace::Config::Config.new
  @version = version
  log.info { "Hypertrace version: #{Hypertrace::VERSION}" }
  log.info { "Ruby version: #{RUBY_VERSION}" }
end

Class Method Details

.configObject



67
68
69
# File 'lib/hypertrace/ruby_agent.rb', line 67

def self.config
  self.instance.config
end

.instrument!Object



59
60
61
# File 'lib/hypertrace/ruby_agent.rb', line 59

def self.instrument!
  self.instance.instrument!
end

.instrument_as_additional!Object



63
64
65
# File 'lib/hypertrace/ruby_agent.rb', line 63

def self.instrument_as_additional!
  self.instance.instrument_as_additional!
end

Instance Method Details

#configObject



79
80
81
# File 'lib/hypertrace/ruby_agent.rb', line 79

def config
  @config.config
end

#configure_instrumentation(span_processor, resource) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/hypertrace/ruby_agent.rb', line 119

def configure_instrumentation span_processor, resource
  OpenTelemetry::SDK.configure do |c|
    c.add_span_processor span_processor if span_processor
    c.resource = resource if resource

    SUPPORTED_INSTRUMENTATIONS.each do |instrumentation_string|
      c.use instrumentation_string
      additional_patches = SUPPORTED_INSTRUMENTATIONS_ADDITIONAL_PATCH[instrumentation_string]
      if additional_patches
        additional_patches.each do |patch_file|
          apply_custom_patch patch_file
        end
      end
    end
  end
end

#create_span_processor(exporter) ⇒ Object



105
106
107
108
# File 'lib/hypertrace/ruby_agent.rb', line 105

def create_span_processor exporter
  return OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(EXPORTER) if ENV['HT_CI_TEST'] != nil
  return OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(exporter)
end

#initalize_tracerObject



110
111
112
113
114
115
116
117
# File 'lib/hypertrace/ruby_agent.rb', line 110

def initalize_tracer
  resource = OpenTelemetry::SDK::Resources::Resource.create(create_resource_attributes)
  exporter = create_exporter
  span_processor = create_span_processor(exporter)
  # TODO: Extra resource Attributes From Config
  configure_instrumentation span_processor, resource
  configure_propagators
end

#instrument!Object



83
84
85
# File 'lib/hypertrace/ruby_agent.rb', line 83

def instrument!
  initalize_tracer
end

#instrument_as_additional!Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hypertrace/ruby_agent.rb', line 87

def instrument_as_additional!
  resource = OpenTelemetry::SDK::Resources::Resource.create(create_resource_attributes)
  exporter = create_resource_customized_exporter resource
  span_processor = create_span_processor exporter

  existing_processors = OpenTelemetry.tracer_provider.instance_variable_get(:"@span_processors")
  if existing_processors.nil? || existing_processors&.empty?
    log.error("No existing tracer_provider found, continuing without Hypertrace instrumentation")
    return
  end
  existing_processors << span_processor
  OpenTelemetry.tracer_provider.instance_variable_set(:'@span_processors', existing_processors)
  # We don't pass a span_processor or resource because we have to add it to the already configured tracer provider
  # The resource will be added at the export phase
  configure_instrumentation nil, nil
  configure_propagators
end