Module: StackifyRubyAPM

Includes:
Log
Defined in:
lib/stackify_apm/util.rb,
lib/stackify_apm/log.rb,
lib/stackify_apm/span.rb,
lib/stackify_ruby_apm.rb,
lib/stackify_apm/agent.rb,
lib/stackify_apm/error.rb,
lib/stackify_apm/spies.rb,
lib/stackify_apm/config.rb,
lib/stackify_apm/worker.rb,
lib/stackify_apm/context.rb,
lib/stackify_apm/railtie.rb,
lib/stackify_apm/version.rb,
lib/stackify_apm/error/log.rb,
lib/stackify_apm/root_info.rb,
lib/stackify_apm/middleware.rb,
lib/stackify_apm/spies/curb.rb,
lib/stackify_apm/spies/tilt.rb,
lib/stackify_apm/spies/yell.rb,
lib/stackify_apm/stacktrace.rb,
lib/stackify_apm/subscriber.rb,
lib/stackify_apm/normalizers.rb,
lib/stackify_apm/serializers.rb,
lib/stackify_apm/spies/log4r.rb,
lib/stackify_apm/spies/mongo.rb,
lib/stackify_apm/spies/redis.rb,
lib/stackify_apm/transaction.rb,
lib/stackify_apm/instrumenter.rb,
lib/stackify_apm/span/context.rb,
lib/stackify_apm/spies/httprb.rb,
lib/stackify_apm/spies/logger.rb,
lib/stackify_apm/spies/sequel.rb,
lib/stackify_apm/trace_logger.rb,
lib/stackify_apm/error_builder.rb,
lib/stackify_apm/spies/faraday.rb,
lib/stackify_apm/spies/logging.rb,
lib/stackify_apm/spies/sidekiq.rb,
lib/stackify_apm/spies/sinatra.rb,
lib/stackify_apm/context/prefix.rb,
lib/stackify_apm/internal_error.rb,
lib/stackify_apm/spies/httparty.rb,
lib/stackify_apm/spies/net_http.rb,
lib/stackify_apm/util/inflector.rb,
lib/stackify_apm/util/inspector.rb,
lib/stackify_apm/util/lru_cache.rb,
lib/stackify_apm/context/request.rb,
lib/stackify_apm/context_builder.rb,
lib/stackify_apm/error/exception.rb,
lib/stackify_apm/spies/curb/easy.rb,
lib/stackify_apm/spies/dynamo_db.rb,
lib/stackify_apm/context/response.rb,
lib/stackify_apm/naively_hashable.rb,
lib/stackify_apm/spies/curb/multi.rb,
lib/stackify_apm/spies/httpclient.rb,
lib/stackify_apm/stacktrace/frame.rb,
lib/stackify_apm/logger/log_device.rb,
lib/stackify_apm/spies/delayed_job.rb,
lib/stackify_apm/serializers/errors.rb,
lib/stackify_apm/spies/sucker_punch.rb,
lib/stackify_apm/stacktrace_builder.rb,
lib/stackify_apm/transport_selector.rb,
lib/stackify_apm/context/request/url.rb,
lib/stackify_apm/instrumenter_helper.rb,
lib/stackify_apm/response_manipulator.rb,
lib/stackify_apm/transport/agent_base.rb,
lib/stackify_apm/transport/log_client.rb,
lib/stackify_apm/spies/action_dispatch.rb,
lib/stackify_apm/spies/stackify_logger.rb,
lib/stackify_apm/context/request/socket.rb,
lib/stackify_apm/util/trace_log_watcher.rb,
lib/stackify_apm/context/request/headers.rb,
lib/stackify_apm/normalizers/action_view.rb,
lib/stackify_apm/serializers/transactions.rb,
lib/stackify_apm/normalizers/action_mailer.rb,
lib/stackify_apm/normalizers/active_record.rb,
lib/stackify_apm/spies/custom_instrumenter.rb,
lib/stackify_apm/logger/logger_high_version.rb,
lib/stackify_apm/logger/logger_lower_version.rb,
lib/stackify_apm/transport/agent_http_client.rb,
lib/stackify_apm/transport/aws_lambda_logging.rb,
lib/stackify_apm/transport/unix_socket_client.rb,
lib/stackify_apm/normalizers/action_controller.rb,
lib/stackify_apm/spies/sinatra_activerecord/mysql_adapter.rb,
lib/stackify_apm/spies/sinatra_activerecord/sqlite_adapter.rb,
lib/stackify_apm/spies/sinatra_activerecord/postgresql_adapter.rb

Overview

rubocop:disable Metrics/PerceivedComplexity

Defined Under Namespace

Modules: Log, NaivelyHashable, Normalizers, Serializers, Spies, Util Classes: AWSLoggerClient, Agent, AgentBaseTransport, AgentHTTPClient, Config, Context, ContextBuilder, Error, ErrorBuilder, Instrumenter, InstrumenterHelper, InternalError, LogClient, LogDevice, Middleware, Railtie, ResponseManipulator, RootInfo, Span, StackifyLogger, Stacktrace, StacktraceBuilder, Subscriber, TraceLogger, Transaction, TransportSelector, UnixSocketClient, Worker

Constant Summary collapse

TRANSPORT =
[
    TRACE_LOG = 'default'.freeze,
    UNIX_SOCKET = 'agent_socket'.freeze,
    AGENT_HTTP = 'agent_http'.freeze,
    LOGGING = 'logging'.freeze
].freeze
VERSION =
'1.17.1'.freeze

Constants included from Log

Log::PREFIX

Class Method Summary collapse

Methods included from Log

#debug, #error, #fatal, #info, #log, #warn

Class Method Details

.agentAgent

Returns Currently running [Agent] if any.

Returns:

  • (Agent)

    Currently running [Agent] if any



134
135
136
# File 'lib/stackify_ruby_apm.rb', line 134

def self.agent
  Agent.instance
end

.build_context(rack_env) ⇒ Context

Build a [Context] from a Rack ‘env`. The context may include information about the request, response, current user and more

Parameters:

  • rack_env (Rack::Env)

    A Rack env

Returns:



186
187
188
# File 'lib/stackify_ruby_apm.rb', line 186

def self.build_context(rack_env)
  agent && agent.build_context(rack_env)
end

.check_isdomain(url) ⇒ Object

Check if the URL is a domain

Parameters:

  • message (String)

    The message

Returns:

  • TRUE/FALASE



213
214
215
# File 'lib/stackify_ruby_apm.rb', line 213

def self.check_isdomain(url)
  url =~ %r{^(http|https):\/\/|[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,6}(:[0-9]{1,5})?(\/.*)?$}
end

.current_transactionTransaction

Returns the currently active transaction (if any)

Returns:



142
143
144
# File 'lib/stackify_ruby_apm.rb', line 142

def self.current_transaction
  agent && agent.current_transaction
end

.inject_rum_scriptObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/stackify_ruby_apm.rb', line 52

def self.inject_rum_script
  transaction = defined?(StackifyRubyAPM.current_transaction) ? StackifyRubyAPM.current_transaction : nil
  return '' unless transaction

  transaction_id = defined?(transaction.id) ? transaction.id : nil
  inject_flag = false

  config = nil
  if defined?(agent.config)
    config = agent.config
  end

  return '' unless config

  if transaction_id
    inject_flag = true
  end

  return '' unless inject_flag

  return '' if config.rum_script_url.to_s.empty? || config.rum_key.to_s.empty?

  rum_settings = {
    "ID" => transaction_id
  }

  if defined?(config.environment_name)
    environment_name = 'Development'
    if !config.environment_name.to_s.empty?
      environment_name = config.environment_name
    end

    rum_settings["Env"] = Base64.strict_encode64(environment_name.encode('utf-8'))
  end

  application_name = config.application_name
  return '' if application_name.to_s.empty?

  rum_settings["Name"] = Base64.strict_encode64(application_name.strip.encode('utf-8'))

  if defined?(transaction.name) && !transaction.name.to_s.empty?
    reporting_url = Util.maskReportingUrl(transaction.name)

    if defined?(transaction.context.request.method) && !transaction.context.request.nil? && !transaction.context.request.method.nil?
      reporting_url = "#{transaction.context.request.method}-#{reporting_url}"
    end

    rum_settings["Trans"] = Base64.strict_encode64(reporting_url.encode('utf-8'))
  end

  @rum_script_injected = true

  if defined?(transaction.context)
    transaction.context.rum = true
  end

  rum_content = "<script type=\"text/javascript\">(window.StackifySettings || (window.StackifySettings = #{rum_settings.to_json}))</script><script src=\"#{config.rum_script_url}\" data-key=\"#{config.rum_key}\" async></script>"
  if rum_content.respond_to?(:html_safe)
    rum_content.html_safe
  else
    rum_content
  end
end

.report(exception, handled: true) ⇒ Error

Report and exception to APM

Parameters:

  • exception (Exception)

    The exception

  • handled (Boolean) (defaults to: true)

    Whether the exception was rescued

Returns:

  • (Error)

    The generated [Error]



197
198
199
# File 'lib/stackify_ruby_apm.rb', line 197

def self.report(exception, handled: true)
  agent && agent.report(exception, handled: handled)
end

.report_message(message, **attrs) ⇒ Error

Report a custom string error message to APM

Parameters:

  • message (String)

    The message

Returns:

  • (Error)

    The generated [Error]



205
206
207
# File 'lib/stackify_ruby_apm.rb', line 205

def self.report_message(message, **attrs)
  agent && agent.report_message(message, backtrace: caller, **attrs)
end

.rum_script_injectObject

Check if the RUM Script is Injected

Returns:

  • TRUE/FALASE



220
221
222
# File 'lib/stackify_ruby_apm.rb', line 220

def self.rum_script_inject
  @rum_script_injected
end

.run_custom_instrumentObject

Run the custom instrument



117
118
119
120
121
# File 'lib/stackify_ruby_apm.rb', line 117

def self.run_custom_instrument
  return unless running?
  StackifyRubyAPM::InstrumenterHelper.custom_instrumented_reset
  StackifyRubyAPM::Spies.run_custom_instrumentation
end

.running?Boolean

Returns Whether there’s an [Agent] running.

Returns:

  • (Boolean)

    Whether there’s an [Agent] running



129
130
131
# File 'lib/stackify_ruby_apm.rb', line 129

def self.running?
  Agent.running?
end

.span(name, type = nil, context: nil, include_stacktrace: true) {|Span| ... } ⇒ Span

Starts a new span under the current transaction

Parameters:

  • name (String)

    A description of the span, eq ‘SELECT FROM “users”`

  • type (String) (defaults to: nil)

    The kind of span, eq ‘db.mysql2.query`

  • context (Span::Context) (defaults to: nil)

    Context information about the span

Yields:

  • (Span)

    Optional block encapsulating span

Returns:

  • (Span)

    Unless block given



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/stackify_ruby_apm.rb', line 168

def self.span(name, type = nil, context: nil, include_stacktrace: true,
              &block)
  return (block_given? ? yield : nil) unless agent

  agent.span(
    name,
    type,
    context: context,
    backtrace: include_stacktrace ? caller : nil,
    &block
  )
end

.start(config = {}) ⇒ Agent

Starts the StackifyRubyAPM Agent

Parameters:

  • config (Config) (defaults to: {})

    An instance of Config

Returns:

  • (Agent)

    The resulting [Agent]



48
49
50
# File 'lib/stackify_ruby_apm.rb', line 48

def self.start(config = {})
  Agent.start config
end

.stopObject

Stops the StackifyRubyAPM Agent



124
125
126
# File 'lib/stackify_ruby_apm.rb', line 124

def self.stop
  Agent.stop
end

.transaction(name = nil, type = nil, context: nil) {|Transaction| ... } ⇒ Transaction

Start a new transaction or return the currently running

‘ExamplesController#index` `db.mysql2.query`

Parameters:

  • name (String) (defaults to: nil)

    A description of the transaction, eg

  • type (String) (defaults to: nil)

    The kind of the transaction, eg ‘app.request.get` or

  • context (Context) (defaults to: nil)

    An optional [Context]

Yields:

  • (Transaction)

    Optional block encapsulating transaction

Returns:



155
156
157
158
159
# File 'lib/stackify_ruby_apm.rb', line 155

def self.transaction(name = nil, type = nil, context: nil, &block)
  return (block_given? ? yield : nil) unless agent

  agent.transaction(name, type, context: context, &block)
end