Module: ElasticAPM

Extended by:
Deprecations
Defined in:
lib/elastic_apm.rb,
lib/elastic_apm/span.rb,
lib/elastic_apm/util.rb,
lib/elastic_apm/agent.rb,
lib/elastic_apm/error.rb,
lib/elastic_apm/spies.rb,
lib/elastic_apm/config.rb,
lib/elastic_apm/context.rb,
lib/elastic_apm/logging.rb,
lib/elastic_apm/metrics.rb,
lib/elastic_apm/railtie.rb,
lib/elastic_apm/version.rb,
lib/elastic_apm/metadata.rb,
lib/elastic_apm/error/log.rb,
lib/elastic_apm/metricset.rb,
lib/elastic_apm/middleware.rb,
lib/elastic_apm/spies/http.rb,
lib/elastic_apm/spies/json.rb,
lib/elastic_apm/spies/rake.rb,
lib/elastic_apm/spies/tilt.rb,
lib/elastic_apm/stacktrace.rb,
lib/elastic_apm/subscriber.rb,
lib/elastic_apm/config/size.rb,
lib/elastic_apm/normalizers.rb,
lib/elastic_apm/opentracing.rb,
lib/elastic_apm/spies/mongo.rb,
lib/elastic_apm/spies/redis.rb,
lib/elastic_apm/transaction.rb,
lib/elastic_apm/context/user.rb,
lib/elastic_apm/deprecations.rb,
lib/elastic_apm/instrumenter.rb,
lib/elastic_apm/span/context.rb,
lib/elastic_apm/span_helpers.rb,
lib/elastic_apm/spies/sequel.rb,
lib/elastic_apm/error_builder.rb,
lib/elastic_apm/spies/faraday.rb,
lib/elastic_apm/spies/sidekiq.rb,
lib/elastic_apm/spies/sinatra.rb,
lib/elastic_apm/trace_context.rb,
lib/elastic_apm/internal_error.rb,
lib/elastic_apm/spies/net_http.rb,
lib/elastic_apm/sql_summarizer.rb,
lib/elastic_apm/transport/base.rb,
lib/elastic_apm/util/inflector.rb,
lib/elastic_apm/util/lru_cache.rb,
lib/elastic_apm/config/duration.rb,
lib/elastic_apm/context/request.rb,
lib/elastic_apm/context_builder.rb,
lib/elastic_apm/error/exception.rb,
lib/elastic_apm/metrics/cpu_mem.rb,
lib/elastic_apm/context/response.rb,
lib/elastic_apm/naively_hashable.rb,
lib/elastic_apm/stacktrace/frame.rb,
lib/elastic_apm/transport/worker.rb,
lib/elastic_apm/spies/delayed_job.rb,
lib/elastic_apm/transport/filters.rb,
lib/elastic_apm/stacktrace_builder.rb,
lib/elastic_apm/context/request/url.rb,
lib/elastic_apm/spies/elasticsearch.rb,
lib/elastic_apm/metadata/system_info.rb,
lib/elastic_apm/transport/connection.rb,
lib/elastic_apm/util/prefixed_logger.rb,
lib/elastic_apm/metadata/process_info.rb,
lib/elastic_apm/metadata/service_info.rb,
lib/elastic_apm/spies/action_dispatch.rb,
lib/elastic_apm/transport/serializers.rb,
lib/elastic_apm/context/request/socket.rb,
lib/elastic_apm/normalizers/action_view.rb,
lib/elastic_apm/normalizers/action_mailer.rb,
lib/elastic_apm/normalizers/active_record.rb,
lib/elastic_apm/normalizers/action_controller.rb,
lib/elastic_apm/transport/filters/secrets_filter.rb,
lib/elastic_apm/metadata/system_info/container_info.rb,
lib/elastic_apm/transport/filters/request_body_filter.rb,
lib/elastic_apm/transport/serializers/span_serializer.rb,
lib/elastic_apm/transport/serializers/error_serializer.rb,
lib/elastic_apm/transport/serializers/context_serializer.rb,
lib/elastic_apm/transport/serializers/metadata_serializer.rb,
lib/elastic_apm/transport/serializers/metricset_serializer.rb,
lib/elastic_apm/transport/serializers/transaction_serializer.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Deprecations, Logging, Metrics, NaivelyHashable, Normalizers, OpenTracing, SpanHelpers, Spies, Transport, Util Classes: Agent, Config, ConfigError, Context, ContextBuilder, Error, ErrorBuilder, ExistingTransactionError, Instrumenter, InternalError, Metadata, Metricset, Middleware, PrefixedLogger, Railtie, Span, SqlSummarizer, Stacktrace, StacktraceBuilder, Subscriber, TraceContext, Transaction

Constant Summary collapse

VERSION =
'2.3.1'

Class Method Summary collapse

Methods included from Deprecations

deprecate

Class Method Details

.add_filter(key, callback = nil) {|Hash| ... } ⇒ Bool

Provide a filter to transform payloads before sending them off

Parameters:

  • key (Symbol)

    Unique filter key

  • callback (Object, Proc) (defaults to: nil)

    A filter that responds to #call(payload)

Yields:

  • (Hash)

    A filter. Used if provided. Otherwise using callback

Returns:

  • (Bool)

    true



344
345
346
347
348
349
350
# File 'lib/elastic_apm.rb', line 344

def add_filter(key, callback = nil, &block)
  if callback.nil? && !block_given?
    raise ArgumentError, '#add_filter needs either `callback\' or a block'
  end

  agent&.add_filter(key, block || callback)
end

.agentAgent

Returns Currently running [Agent] if any.

Returns:

  • (Agent)

    Currently running [Agent] if any



45
46
47
# File 'lib/elastic_apm.rb', line 45

def 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:



288
289
290
# File 'lib/elastic_apm.rb', line 288

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

.current_spanSpan

Returns the currently active span (if any)

Returns:



61
62
63
# File 'lib/elastic_apm.rb', line 61

def current_span
  agent&.current_span
end

.current_transactionTransaction

Returns the currently active transaction (if any)

Returns:



54
55
56
# File 'lib/elastic_apm.rb', line 54

def current_transaction
  agent&.current_transaction
end

.end_spanSpan

Ends the current span

Returns:



240
241
242
# File 'lib/elastic_apm.rb', line 240

def end_span
  agent&.end_span
end

.end_transaction(result = nil) ⇒ Transaction

Ends the current transaction with result

Parameters:

  • result (String) (defaults to: nil)

    The result of the transaction

Returns:



122
123
124
# File 'lib/elastic_apm.rb', line 122

def end_transaction(result = nil)
  agent&.end_transaction(result)
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]



299
300
301
# File 'lib/elastic_apm.rb', line 299

def report(exception, handled: true)
  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]



307
308
309
# File 'lib/elastic_apm.rb', line 307

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

.running?Boolean

Returns Whether there’s an [Agent] running.

Returns:

  • (Boolean)

    Whether there’s an [Agent] running



40
41
42
# File 'lib/elastic_apm.rb', line 40

def running?
  Agent.running?
end

.set_custom_context(custom) ⇒ Hash

Provide further context for the current transaction

Parameters:

  • custom (Hash)

    A hash with custom information. Can be nested.

Returns:

  • (Hash)

    The current custom context



326
327
328
# File 'lib/elastic_apm.rb', line 326

def set_custom_context(custom)
  agent&.set_custom_context(custom)
end

.set_tag(key, value) ⇒ Object

Set a tag value for the current transaction

Parameters:

  • key (String, Symbol)

    A key

  • value (Object)

    A value (will be converted to string)

Returns:

  • (Object)

    The given value



318
319
320
# File 'lib/elastic_apm.rb', line 318

def set_tag(key, value)
  agent&.set_tag(key, value)
end

.set_user(user) ⇒ Object

Provide a user to the current transaction

Parameters:

  • user (Object)

    An object representing a user

Returns:

  • (Object)

    Given user



334
335
336
# File 'lib/elastic_apm.rb', line 334

def set_user(user)
  agent&.set_user(user)
end

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

Deprecated.

See with_span or start_span

rubocop:disable Metrics/MethodLength Start a new span

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



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/elastic_apm.rb', line 180

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

  if block_given?
    with_span(
      name,
      type,
      context: context,
      include_stacktrace: include_stacktrace,
      &block
    )
  else
    start_span(
      name,
      type,
      context: context,
      include_stacktrace: include_stacktrace
    )
  end
end

.start(config = {}) ⇒ Agent

Starts the ElasticAPM Agent

Parameters:

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

    An instance of Config

Returns:

  • (Agent)

    The resulting [Agent]



30
31
32
# File 'lib/elastic_apm.rb', line 30

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

.start_span(name, type = nil, context: nil, include_stacktrace: true, trace_context: nil) ⇒ Span

Start a new span

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

  • include_stacktrace (Boolean) (defaults to: true)

    Whether or not to capture a stacktrace

Returns:



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/elastic_apm.rb', line 217

def start_span(
  name,
  type = nil,
  context: nil,
  include_stacktrace: true,
  trace_context: nil
)
  agent&.start_span(
    name,
    type,
    context: context,
    trace_context: trace_context
  ).tap do |span|
    break unless span && include_stacktrace
    break unless agent.config.span_frames_min_duration?

    span.original_backtrace ||= caller
  end
end

.start_transaction(name = nil, type = nil, context: nil, trace_context: nil, traceparent: nil) ⇒ Transaction

rubocop:disable Metrics/MethodLength Start a new transaction

‘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]

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/elastic_apm.rb', line 96

def start_transaction(
  name = nil,
  type = nil,
  context: nil,
  trace_context: nil,
  traceparent: nil
)
  if traceparent
    trace_context ||= traceparent
    warn "[ElasticAPM] [DEPRECATED] `start_transaction' with" \
      "`traceparent:' has been renamed. Use `trace_context:' instead."
  end

  agent&.start_transaction(
    name,
    type,
    context: context,
    trace_context: trace_context
  )
end

.stopObject

Stops the ElasticAPM Agent



35
36
37
# File 'lib/elastic_apm.rb', line 35

def stop
  Agent.stop
end

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

Deprecated.

See with_transaction or start_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:



75
76
77
78
79
80
81
82
83
# File 'lib/elastic_apm.rb', line 75

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

  if block_given?
    with_transaction(name, type, context: context, &block)
  else
    start_transaction(name, type, context: context)
  end
end

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

rubocop:disable Metrics/MethodLength Wrap a block in a Span, ending it after the block

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

  • include_stacktrace (Boolean) (defaults to: true)

    Whether or not to capture a stacktrace

Yields:

Returns:

  • Result of block



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/elastic_apm.rb', line 253

def with_span(
  name,
  type = nil,
  context: nil,
  include_stacktrace: true,
  trace_context: nil
)
  unless block_given?
    raise ArgumentError,
      'expected a block. Do you want `start_span\' instead?'
  end

  return yield nil unless agent

  begin
    span =
      start_span(
        name,
        type,
        context: context,
        include_stacktrace: include_stacktrace,
        trace_context: trace_context
      )
    yield span
  ensure
    end_span
  end
end

.with_transaction(name = nil, type = nil, context: nil, trace_context: nil, traceparent: nil) {|Transaction| ... } ⇒ Object

rubocop:disable Metrics/MethodLength Wrap a block in a Transaction, ending it after the block

‘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:

Returns:

  • result of block



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/elastic_apm.rb', line 136

def with_transaction(
  name = nil,
  type = nil,
  context: nil,
  trace_context: nil,
  traceparent: nil
)
  unless block_given?
    raise ArgumentError,
      'expected a block. Do you want `start_transaction\' instead?'
  end

  if traceparent
    trace_context ||= traceparent
    warn "[ElasticAPM] [DEPRECATED] `with_transaction' with " \
      "`traceparent:' has been renamed. Use `trace_context:' instead."
  end

  return yield(nil) unless agent

  begin
    transaction =
      start_transaction(
        name,
        type,
        context: context,
        trace_context: trace_context
      )
    yield transaction
  ensure
    end_transaction
  end
end