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.14.7'.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



89
90
91
# File 'lib/stackify_ruby_apm.rb', line 89

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:



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

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



168
169
170
# File 'lib/stackify_ruby_apm.rb', line 168

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:



97
98
99
# File 'lib/stackify_ruby_apm.rb', line 97

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
# File 'lib/stackify_ruby_apm.rb', line 52

def self.inject_rum_script
  config = StackifyRubyAPM::Config.new
  client_id = config.client_id
  device_id = config.device_id
  client_rundomain = config.client_run_domain
  transaction_id = defined?(StackifyRubyAPM.current_transaction.id) ? StackifyRubyAPM.current_transaction.id : nil
  inject_flag = false

  if client_id && device_id
    inject_flag = true if check_isdomain(client_rundomain)
  end

  return unless inject_flag
  data_request_id = "V2|#{transaction_id}|#{client_id}|#{device_id}"
  @rum_script_injected = true
  "<script src=\"#{config.rum_script_src}\" data-host=\"#{client_rundomain}\" data-requestId=\"#{data_request_id}\"
  data-a=\"#{config.application_name}\" data-e=\"#{config.environment_name}\" data-enableInternalLogging=\"#{config.debug_logging}\" type=\"text/javascript\" async></script>"
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]



152
153
154
# File 'lib/stackify_ruby_apm.rb', line 152

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]



160
161
162
# File 'lib/stackify_ruby_apm.rb', line 160

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



175
176
177
# File 'lib/stackify_ruby_apm.rb', line 175

def self.rum_script_inject
  @rum_script_injected
end

.run_custom_instrumentObject

Run the custom instrument



72
73
74
75
76
# File 'lib/stackify_ruby_apm.rb', line 72

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



84
85
86
# File 'lib/stackify_ruby_apm.rb', line 84

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



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/stackify_ruby_apm.rb', line 123

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



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

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:



110
111
112
113
114
# File 'lib/stackify_ruby_apm.rb', line 110

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